@flisk/analyze-tracking 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flisk/analyze-tracking",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Analyzes tracking code in a project and generates data schemas",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  const path = require('path');
7
- const ts = require('typescript');
7
+
8
8
  const { parseCustomFunctionSignature } = require('./utils/customFunctionParser');
9
9
  const { getAllFiles } = require('../utils/fileProcessor');
10
10
  const { analyzeJsFile } = require('./javascript');
@@ -19,11 +19,6 @@ async function analyzeDirectory(dirPath, customFunctions) {
19
19
  const customFunctionSignatures = (customFunctions && customFunctions?.length > 0) ? customFunctions.map(parseCustomFunctionSignature) : null;
20
20
 
21
21
  const files = getAllFiles(dirPath);
22
- const tsFiles = files.filter(file => /\.(tsx?)$/.test(file));
23
- const tsProgram = ts.createProgram(tsFiles, {
24
- target: ts.ScriptTarget.ESNext,
25
- module: ts.ModuleKind.CommonJS,
26
- });
27
22
 
28
23
  for (const file of files) {
29
24
  let events = [];
@@ -37,7 +32,8 @@ async function analyzeDirectory(dirPath, customFunctions) {
37
32
  if (isJsFile) {
38
33
  events = analyzeJsFile(file, customFunctionSignatures);
39
34
  } else if (isTsFile) {
40
- events = analyzeTsFile(file, tsProgram, customFunctionSignatures);
35
+ // Pass null program so analyzeTsFile will create a per-file program using the file's nearest tsconfig.json
36
+ events = analyzeTsFile(file, null, customFunctionSignatures);
41
37
  } else if (isPythonFile) {
42
38
  events = await analyzePythonFile(file, customFunctionSignatures);
43
39
  } else if (isRubyFile) {
@@ -4,15 +4,17 @@
4
4
  */
5
5
 
6
6
  const { getValueType } = require('./types');
7
+ const prismPromise = import('@ruby/prism');
7
8
 
8
9
  /**
9
10
  * Extracts the event name from a tracking call based on the source
10
11
  * @param {Object} node - The AST CallNode
11
12
  * @param {string} source - The detected analytics source
12
13
  * @param {Object} customConfig - Custom configuration for custom functions
14
+ * @param {Object} constantMap - Map of constants to resolve constant paths
13
15
  * @returns {string|null} - The extracted event name or null
14
16
  */
15
- function extractEventName(node, source, customConfig = null) {
17
+ async function extractEventName(node, source, customConfig = null, constantMap = {}) {
16
18
  if (source === 'segment' || source === 'rudderstack') {
17
19
  // Both Segment and Rudderstack use the same format
18
20
  const params = node.arguments_?.arguments_?.[0]?.elements;
@@ -62,8 +64,33 @@ function extractEventName(node, source, customConfig = null) {
62
64
  }
63
65
 
64
66
  const eventArg = args[customConfig.eventIndex];
65
- if (eventArg?.unescaped?.value) {
66
- return eventArg.unescaped.value;
67
+ if (eventArg) {
68
+ // String literal
69
+ if (eventArg.unescaped?.value) {
70
+ return eventArg.unescaped.value;
71
+ }
72
+
73
+ // Constant references
74
+ const { ConstantReadNode, ConstantPathNode } = await prismPromise;
75
+ const buildConstPath = (n) => {
76
+ if (!n) return '';
77
+ if (n instanceof ConstantReadNode) return n.name;
78
+ if (n instanceof ConstantPathNode) {
79
+ const parent = buildConstPath(n.parent);
80
+ return parent ? `${parent}::${n.name}` : n.name;
81
+ }
82
+ return '';
83
+ };
84
+
85
+ if (eventArg instanceof ConstantReadNode) {
86
+ const name = eventArg.name;
87
+ // Try to resolve with current constant map, else return name
88
+ return constantMap[name] || name;
89
+ }
90
+ if (eventArg instanceof ConstantPathNode) {
91
+ const pathStr = buildConstPath(eventArg);
92
+ return constantMap[pathStr] || pathStr;
93
+ }
67
94
  }
68
95
  return null;
69
96
  }
@@ -79,7 +106,7 @@ function extractEventName(node, source, customConfig = null) {
79
106
  * @returns {Object|null} - The extracted properties or null
80
107
  */
81
108
  async function extractProperties(node, source, customConfig = null) {
82
- const { HashNode, ArrayNode } = await import('@ruby/prism');
109
+ const { HashNode, ArrayNode } = await prismPromise;
83
110
 
84
111
  if (source === 'segment' || source === 'rudderstack') {
85
112
  // Both Segment and Rudderstack use the same format
@@ -235,7 +262,7 @@ async function extractProperties(node, source, customConfig = null) {
235
262
  * @returns {Object} - The extracted properties
236
263
  */
237
264
  async function extractHashProperties(hashNode) {
238
- const { AssocNode, HashNode, ArrayNode } = await import('@ruby/prism');
265
+ const { AssocNode, HashNode, ArrayNode } = await prismPromise;
239
266
  const properties = {};
240
267
 
241
268
  for (const element of hashNode.elements) {
@@ -278,7 +305,7 @@ async function extractHashProperties(hashNode) {
278
305
  * @returns {Object} - Type information for array items
279
306
  */
280
307
  async function extractArrayItemProperties(arrayNode) {
281
- const { HashNode } = await import('@ruby/prism');
308
+ const { HashNode } = await prismPromise;
282
309
 
283
310
  if (arrayNode.elements.length === 0) {
284
311
  return { type: 'any' };
@@ -4,11 +4,162 @@
4
4
  */
5
5
 
6
6
  const fs = require('fs');
7
+ const path = require('path');
7
8
  const TrackingVisitor = require('./visitor');
8
9
 
9
10
  // Lazy-loaded parse function from Ruby Prism
10
11
  let parse = null;
11
12
 
13
+ /**
14
+ * Extracts the string literal value from an AST node, ignoring a trailing `.freeze` call.
15
+ * Supports:
16
+ * - StringNode
17
+ * - CallNode with receiver StringNode and method `freeze`
18
+ *
19
+ * @param {import('@ruby/prism').PrismNode} node
20
+ * @returns {string|null}
21
+ */
22
+ async function extractStringLiteral(node) {
23
+ if (!node) return null;
24
+
25
+ const {
26
+ StringNode,
27
+ CallNode
28
+ } = await import('@ruby/prism');
29
+
30
+ if (node instanceof StringNode) {
31
+ return node.unescaped?.value ?? null;
32
+ }
33
+
34
+ // Handle "_Section".freeze pattern
35
+ if (node instanceof CallNode && node.name === 'freeze' && node.receiver) {
36
+ return extractStringLiteral(node.receiver);
37
+ }
38
+
39
+ return null;
40
+ }
41
+
42
+ /**
43
+ * Recursively traverses an AST to collect constant assignments and build a map
44
+ * of fully-qualified constant names (e.g. "TelemetryHelper::FINISHED_SECTION")
45
+ * to their string literal values.
46
+ *
47
+ * @param {import('@ruby/prism').PrismNode} node - current AST node
48
+ * @param {string[]} namespaceStack - stack of module/class names
49
+ * @param {Object} constantMap - accumulator map of constant path -> string value
50
+ */
51
+ async function collectConstants(node, namespaceStack, constantMap) {
52
+ if (!node) return;
53
+
54
+ const {
55
+ ModuleNode,
56
+ ClassNode,
57
+ StatementsNode,
58
+ ConstantWriteNode,
59
+ ConstantPathWriteNode,
60
+ ConstantPathNode
61
+ } = await import('@ruby/prism');
62
+
63
+ // Helper to build constant path from ConstantPathNode
64
+ const buildConstPath = (pathNode) => {
65
+ if (!pathNode) return '';
66
+ if (pathNode.type === 'ConstantReadNode') {
67
+ return pathNode.name;
68
+ }
69
+ if (pathNode.type === 'ConstantPathNode') {
70
+ const parent = buildConstPath(pathNode.parent);
71
+ return parent ? `${parent}::${pathNode.name}` : pathNode.name;
72
+ }
73
+ return '';
74
+ };
75
+
76
+ // Process constant assignments
77
+ if (node instanceof ConstantWriteNode) {
78
+ const fullName = [...namespaceStack, node.name].join('::');
79
+ const literal = await extractStringLiteral(node.value);
80
+ if (literal !== null) {
81
+ constantMap[fullName] = literal;
82
+ }
83
+ } else if (node instanceof ConstantPathWriteNode) {
84
+ const fullName = buildConstPath(node.target);
85
+ const literal = await extractStringLiteral(node.value);
86
+ if (fullName && literal !== null) {
87
+ constantMap[fullName] = literal;
88
+ }
89
+ }
90
+
91
+ // Recurse into children depending on node type
92
+ if (node instanceof ModuleNode || node instanceof ClassNode) {
93
+ // Enter namespace
94
+ const name = node.constantPath?.name || node.name; // ModuleNode has constantPath
95
+ const childNamespaceStack = name ? [...namespaceStack, name] : namespaceStack;
96
+
97
+ if (node.body) {
98
+ await collectConstants(node.body, childNamespaceStack, constantMap);
99
+ }
100
+ return;
101
+ }
102
+
103
+ // Generic traversal for other nodes
104
+ if (node instanceof StatementsNode) {
105
+ for (const child of node.body) {
106
+ await collectConstants(child, namespaceStack, constantMap);
107
+ }
108
+ return;
109
+ }
110
+
111
+ // Fallback: iterate over enumerable properties to find nested nodes
112
+ for (const key of Object.keys(node)) {
113
+ const val = node[key];
114
+ if (!val) continue;
115
+
116
+ const traverseChild = async (child) => {
117
+ if (child && typeof child === 'object' && (child.location || child.constructor?.name?.endsWith('Node'))) {
118
+ await collectConstants(child, namespaceStack, constantMap);
119
+ }
120
+ };
121
+
122
+ if (Array.isArray(val)) {
123
+ for (const c of val) {
124
+ await traverseChild(c);
125
+ }
126
+ } else {
127
+ await traverseChild(val);
128
+ }
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Builds a map of constant names to their literal string values for all .rb
134
+ * files in the given directory. This is a best-effort resolver intended for
135
+ * test fixtures and small projects and is not a fully-fledged Ruby constant
136
+ * resolver.
137
+ *
138
+ * @param {string} directory
139
+ * @returns {Promise<Object<string,string>>}
140
+ */
141
+ async function buildConstantMapForDirectory(directory) {
142
+ const constantMap = {};
143
+
144
+ if (!fs.existsSync(directory)) return constantMap;
145
+
146
+ const files = fs.readdirSync(directory).filter(f => f.endsWith('.rb'));
147
+
148
+ for (const file of files) {
149
+ const fullPath = path.join(directory, file);
150
+ try {
151
+ const content = fs.readFileSync(fullPath, 'utf8');
152
+ const ast = await parse(content);
153
+ await collectConstants(ast.value, [], constantMap);
154
+ } catch (err) {
155
+ // Ignore parse errors for unrelated files
156
+ continue;
157
+ }
158
+ }
159
+
160
+ return constantMap;
161
+ }
162
+
12
163
  /**
13
164
  * Analyzes a Ruby file for analytics tracking calls
14
165
  * @param {string} filePath - Path to the Ruby file to analyze
@@ -27,6 +178,10 @@ async function analyzeRubyFile(filePath, customFunctionSignatures = null) {
27
178
  // Read the file content
28
179
  const code = fs.readFileSync(filePath, 'utf8');
29
180
 
181
+ // Build constant map for current directory (sibling .rb files)
182
+ const currentDir = path.dirname(filePath);
183
+ const constantMap = await buildConstantMapForDirectory(currentDir);
184
+
30
185
  // Parse the Ruby code into an AST once
31
186
  let ast;
32
187
  try {
@@ -36,8 +191,8 @@ async function analyzeRubyFile(filePath, customFunctionSignatures = null) {
36
191
  return [];
37
192
  }
38
193
 
39
- // Single visitor pass covering all custom configs
40
- const visitor = new TrackingVisitor(code, filePath, customFunctionSignatures || []);
194
+ // Single visitor pass covering all custom configs, with constant map for resolution
195
+ const visitor = new TrackingVisitor(code, filePath, customFunctionSignatures || [], constantMap);
41
196
  const events = await visitor.analyze(ast);
42
197
 
43
198
  // Deduplicate events
@@ -46,7 +46,9 @@ async function traverseNode(node, nodeVisitor, ancestors = []) {
46
46
  AssocNode,
47
47
  ClassNode,
48
48
  ModuleNode,
49
- CallNode
49
+ CallNode,
50
+ CaseNode,
51
+ WhenNode
50
52
  } = await import('@ruby/prism');
51
53
 
52
54
  if (!node) return;
@@ -89,7 +91,8 @@ async function traverseNode(node, nodeVisitor, ancestors = []) {
89
91
  await traverseNode(node.body, nodeVisitor, ancestors);
90
92
  }
91
93
  } else if (node instanceof ArgumentsNode) {
92
- for (const arg of node.arguments) {
94
+ const argsList = node.arguments || [];
95
+ for (const arg of argsList) {
93
96
  await traverseNode(arg, nodeVisitor, ancestors);
94
97
  }
95
98
  } else if (node instanceof HashNode) {
@@ -99,6 +102,55 @@ async function traverseNode(node, nodeVisitor, ancestors = []) {
99
102
  } else if (node instanceof AssocNode) {
100
103
  await traverseNode(node.key, nodeVisitor, ancestors);
101
104
  await traverseNode(node.value, nodeVisitor, ancestors);
105
+ } else if (node instanceof CaseNode) {
106
+ // Traverse through each 'when' clause and the optional else clause
107
+ const whenClauses = node.whens || node.conditions || node.when_bodies || [];
108
+ for (const when of whenClauses) {
109
+ await traverseNode(when, nodeVisitor, ancestors);
110
+ }
111
+ if (node.else_) {
112
+ await traverseNode(node.else_, nodeVisitor, ancestors);
113
+ } else if (node.elseBody) {
114
+ await traverseNode(node.elseBody, nodeVisitor, ancestors);
115
+ }
116
+ } else if (node instanceof WhenNode) {
117
+ // Handle a single when clause: traverse its condition(s) and body
118
+ if (Array.isArray(node.conditions)) {
119
+ for (const cond of node.conditions) {
120
+ await traverseNode(cond, nodeVisitor, ancestors);
121
+ }
122
+ } else if (node.conditions) {
123
+ await traverseNode(node.conditions, nodeVisitor, ancestors);
124
+ }
125
+ if (node.statements) {
126
+ await traverseNode(node.statements, nodeVisitor, ancestors);
127
+ }
128
+ if (node.next) {
129
+ await traverseNode(node.next, nodeVisitor, ancestors);
130
+ }
131
+ } else {
132
+ // Generic fallback: iterate over enumerable properties to find nested nodes
133
+ for (const key of Object.keys(node)) {
134
+ const val = node[key];
135
+ if (!val) continue;
136
+
137
+ const visitChild = async (child) => {
138
+ if (child && typeof child === 'object') {
139
+ // crude check: Prism nodes have a `location` field
140
+ if (child.location || child.type || child.constructor?.name?.endsWith('Node')) {
141
+ await traverseNode(child, nodeVisitor, ancestors);
142
+ }
143
+ }
144
+ };
145
+
146
+ if (Array.isArray(val)) {
147
+ for (const c of val) {
148
+ await visitChild(c);
149
+ }
150
+ } else {
151
+ await visitChild(val);
152
+ }
153
+ }
102
154
  }
103
155
 
104
156
  ancestors.pop();
@@ -8,10 +8,11 @@ const { extractEventName, extractProperties } = require('./extractors');
8
8
  const { findWrappingFunction, traverseNode, getLineNumber } = require('./traversal');
9
9
 
10
10
  class TrackingVisitor {
11
- constructor(code, filePath, customConfigs = []) {
11
+ constructor(code, filePath, customConfigs = [], constantMap = {}) {
12
12
  this.code = code;
13
13
  this.filePath = filePath;
14
14
  this.customConfigs = Array.isArray(customConfigs) ? customConfigs : [];
15
+ this.constantMap = constantMap || {};
15
16
  this.events = [];
16
17
  }
17
18
 
@@ -42,7 +43,7 @@ class TrackingVisitor {
42
43
 
43
44
  if (!source) return;
44
45
 
45
- const eventName = extractEventName(node, source, matchedConfig);
46
+ const eventName = await extractEventName(node, source, matchedConfig, this.constantMap);
46
47
  if (!eventName) return;
47
48
 
48
49
  const line = getLineNumber(this.code, node.location);
@@ -7,6 +7,7 @@ const ts = require('typescript');
7
7
  const { detectAnalyticsSource } = require('./detectors');
8
8
  const { extractEventData, processEventData } = require('./extractors');
9
9
  const { findWrappingFunction } = require('./utils/function-finder');
10
+ const path = require('path');
10
11
 
11
12
  /**
12
13
  * Error thrown when TypeScript program cannot be created
@@ -44,16 +45,37 @@ function getProgram(filePath, existingProgram) {
44
45
  }
45
46
 
46
47
  try {
47
- // Create a minimal program for single file analysis
48
- const options = {
48
+ // Try to locate a tsconfig.json nearest to the file to inherit compiler options (important for path aliases)
49
+ const searchPath = path.dirname(filePath);
50
+ const configPath = ts.findConfigFile(searchPath, ts.sys.fileExists, 'tsconfig.json');
51
+
52
+ let compilerOptions = {
49
53
  target: ts.ScriptTarget.Latest,
50
54
  module: ts.ModuleKind.CommonJS,
51
55
  allowJs: true,
52
56
  checkJs: false,
53
- noEmit: true
57
+ noEmit: true,
58
+ jsx: ts.JsxEmit.Preserve
54
59
  };
60
+ let rootNames = [filePath];
61
+
62
+ if (configPath) {
63
+ // Read and parse the tsconfig.json
64
+ const readResult = ts.readConfigFile(configPath, ts.sys.readFile);
65
+ if (!readResult.error && readResult.config) {
66
+ const parseResult = ts.parseJsonConfigFileContent(
67
+ readResult.config,
68
+ ts.sys,
69
+ path.dirname(configPath)
70
+ );
71
+ if (!parseResult.errors || parseResult.errors.length === 0) {
72
+ compilerOptions = { ...compilerOptions, ...parseResult.options };
73
+ rootNames = parseResult.fileNames.length > 0 ? parseResult.fileNames : rootNames;
74
+ }
75
+ }
76
+ }
55
77
 
56
- const program = ts.createProgram([filePath], options);
78
+ const program = ts.createProgram(rootNames, compilerOptions);
57
79
  return program;
58
80
  } catch (error) {
59
81
  throw new ProgramError(filePath, error);