@flisk/analyze-tracking 0.8.1 → 0.8.3

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.3",
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
@@ -3,6 +3,10 @@
3
3
  * @module analyze/ruby/traversal
4
4
  */
5
5
 
6
+
7
+ // Prevent infinite recursion in AST traversal
8
+ const MAX_RECURSION_DEPTH = 20;
9
+
6
10
  /**
7
11
  * Finds the wrapping function for a given node
8
12
  * @param {Object} node - The current AST node
@@ -33,8 +37,9 @@ async function findWrappingFunction(node, ancestors) {
33
37
  * @param {Object} node - The current AST node
34
38
  * @param {Function} nodeVisitor - Function to call for each node
35
39
  * @param {Array} ancestors - The ancestor nodes stack
40
+ * @param {number} depth - Current recursion depth to prevent infinite loops
36
41
  */
37
- async function traverseNode(node, nodeVisitor, ancestors = []) {
42
+ async function traverseNode(node, nodeVisitor, ancestors = [], depth = 0) {
38
43
  const {
39
44
  ProgramNode,
40
45
  StatementsNode,
@@ -46,11 +51,23 @@ async function traverseNode(node, nodeVisitor, ancestors = []) {
46
51
  AssocNode,
47
52
  ClassNode,
48
53
  ModuleNode,
49
- CallNode
54
+ CallNode,
55
+ CaseNode,
56
+ WhenNode
50
57
  } = await import('@ruby/prism');
51
58
 
52
59
  if (!node) return;
53
60
 
61
+ // Prevent infinite recursion with depth limit
62
+ if (depth > MAX_RECURSION_DEPTH) {
63
+ return;
64
+ }
65
+
66
+ // Check for circular references - if this node is already in ancestors, skip it
67
+ if (ancestors.includes(node)) {
68
+ return;
69
+ }
70
+
54
71
  ancestors.push(node);
55
72
 
56
73
  // Call the visitor for this node
@@ -60,45 +77,102 @@ async function traverseNode(node, nodeVisitor, ancestors = []) {
60
77
 
61
78
  // Visit all child nodes based on node type
62
79
  if (node instanceof ProgramNode) {
63
- await traverseNode(node.statements, nodeVisitor, ancestors);
80
+ await traverseNode(node.statements, nodeVisitor, ancestors, depth + 1);
64
81
  } else if (node instanceof StatementsNode) {
65
82
  for (const child of node.body) {
66
- await traverseNode(child, nodeVisitor, ancestors);
83
+ await traverseNode(child, nodeVisitor, ancestors, depth + 1);
67
84
  }
68
85
  } else if (node instanceof ClassNode) {
69
86
  if (node.body) {
70
- await traverseNode(node.body, nodeVisitor, ancestors);
87
+ await traverseNode(node.body, nodeVisitor, ancestors, depth + 1);
71
88
  }
72
89
  } else if (node instanceof ModuleNode) {
73
90
  if (node.body) {
74
- await traverseNode(node.body, nodeVisitor, ancestors);
91
+ await traverseNode(node.body, nodeVisitor, ancestors, depth + 1);
75
92
  }
76
93
  } else if (node instanceof DefNode) {
77
94
  if (node.body) {
78
- await traverseNode(node.body, nodeVisitor, ancestors);
95
+ await traverseNode(node.body, nodeVisitor, ancestors, depth + 1);
79
96
  }
80
97
  } else if (node instanceof IfNode) {
81
98
  if (node.statements) {
82
- await traverseNode(node.statements, nodeVisitor, ancestors);
99
+ await traverseNode(node.statements, nodeVisitor, ancestors, depth + 1);
83
100
  }
84
101
  if (node.subsequent) {
85
- await traverseNode(node.subsequent, nodeVisitor, ancestors);
102
+ await traverseNode(node.subsequent, nodeVisitor, ancestors, depth + 1);
86
103
  }
87
104
  } else if (node instanceof BlockNode) {
88
105
  if (node.body) {
89
- await traverseNode(node.body, nodeVisitor, ancestors);
106
+ await traverseNode(node.body, nodeVisitor, ancestors, depth + 1);
90
107
  }
91
108
  } else if (node instanceof ArgumentsNode) {
92
- for (const arg of node.arguments) {
93
- await traverseNode(arg, nodeVisitor, ancestors);
109
+ const argsList = node.arguments || [];
110
+ for (const arg of argsList) {
111
+ await traverseNode(arg, nodeVisitor, ancestors, depth + 1);
94
112
  }
95
113
  } else if (node instanceof HashNode) {
96
114
  for (const element of node.elements) {
97
- await traverseNode(element, nodeVisitor, ancestors);
115
+ await traverseNode(element, nodeVisitor, ancestors, depth + 1);
98
116
  }
99
117
  } else if (node instanceof AssocNode) {
100
- await traverseNode(node.key, nodeVisitor, ancestors);
101
- await traverseNode(node.value, nodeVisitor, ancestors);
118
+ await traverseNode(node.key, nodeVisitor, ancestors, depth + 1);
119
+ await traverseNode(node.value, nodeVisitor, ancestors, depth + 1);
120
+ } else if (node instanceof CaseNode) {
121
+ // Traverse through each 'when' clause and the optional else clause
122
+ const whenClauses = node.whens || node.conditions || node.when_bodies || [];
123
+ for (const when of whenClauses) {
124
+ await traverseNode(when, nodeVisitor, ancestors, depth + 1);
125
+ }
126
+ if (node.else_) {
127
+ await traverseNode(node.else_, nodeVisitor, ancestors, depth + 1);
128
+ } else if (node.elseBody) {
129
+ await traverseNode(node.elseBody, nodeVisitor, ancestors, depth + 1);
130
+ }
131
+ } else if (node instanceof WhenNode) {
132
+ // Handle a single when clause: traverse its condition(s) and body
133
+ if (Array.isArray(node.conditions)) {
134
+ for (const cond of node.conditions) {
135
+ await traverseNode(cond, nodeVisitor, ancestors, depth + 1);
136
+ }
137
+ } else if (node.conditions) {
138
+ await traverseNode(node.conditions, nodeVisitor, ancestors, depth + 1);
139
+ }
140
+ if (node.statements) {
141
+ await traverseNode(node.statements, nodeVisitor, ancestors, depth + 1);
142
+ }
143
+ if (node.next) {
144
+ await traverseNode(node.next, nodeVisitor, ancestors, depth + 1);
145
+ }
146
+ } else {
147
+ // Generic fallback: iterate over enumerable properties to find nested nodes
148
+ for (const key of Object.keys(node)) {
149
+ const val = node[key];
150
+ if (!val) continue;
151
+
152
+ const visitChild = async (child) => {
153
+ if (child && typeof child === 'object') {
154
+ // More restrictive check: ensure it's actually a Prism AST node
155
+ // Check for specific Prism node indicators and avoid circular references
156
+ if (
157
+ child.location &&
158
+ child.constructor &&
159
+ child.constructor.name &&
160
+ child.constructor.name.endsWith('Node') &&
161
+ !ancestors.includes(child)
162
+ ) {
163
+ await traverseNode(child, nodeVisitor, ancestors, depth + 1);
164
+ }
165
+ }
166
+ };
167
+
168
+ if (Array.isArray(val)) {
169
+ for (const c of val) {
170
+ await visitChild(c);
171
+ }
172
+ } else {
173
+ await visitChild(val);
174
+ }
175
+ }
102
176
  }
103
177
 
104
178
  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);