@flisk/analyze-tracking 0.8.0 → 0.8.1

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.0",
3
+ "version": "0.8.1",
4
4
  "description": "Analyzes tracking code in a project and generates data schemas",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -28,29 +28,31 @@ const EXTRACTION_STRATEGIES = {
28
28
  * Extracts event information from a CallExpression node
29
29
  * @param {Object} node - AST CallExpression node
30
30
  * @param {string} source - Analytics provider source
31
+ * @param {Object} constantMap - Collected constant map
31
32
  * @param {Object} customConfig - Parsed custom function configuration
32
33
  * @returns {EventData} Extracted event data
33
34
  */
34
- function extractEventData(node, source, customConfig) {
35
+ function extractEventData(node, source, constantMap = {}, customConfig) {
35
36
  const strategy = EXTRACTION_STRATEGIES[source] || EXTRACTION_STRATEGIES.default;
36
37
  if (source === 'custom') {
37
- return strategy(node, customConfig);
38
+ return strategy(node, constantMap, customConfig);
38
39
  }
39
- return strategy(node);
40
+ return strategy(node, constantMap);
40
41
  }
41
42
 
42
43
  /**
43
44
  * Extracts Google Analytics event data
44
45
  * @param {Object} node - CallExpression node
46
+ * @param {Object} constantMap - Collected constant map
45
47
  * @returns {EventData}
46
48
  */
47
- function extractGoogleAnalyticsEvent(node) {
49
+ function extractGoogleAnalyticsEvent(node, constantMap) {
48
50
  if (!node.arguments || node.arguments.length < 3) {
49
51
  return { eventName: null, propertiesNode: null };
50
52
  }
51
53
 
52
54
  // gtag('event', 'event_name', { properties })
53
- const eventName = getStringValue(node.arguments[1]);
55
+ const eventName = getStringValue(node.arguments[1], constantMap);
54
56
  const propertiesNode = node.arguments[2];
55
57
 
56
58
  return { eventName, propertiesNode };
@@ -59,9 +61,10 @@ function extractGoogleAnalyticsEvent(node) {
59
61
  /**
60
62
  * Extracts Snowplow event data
61
63
  * @param {Object} node - CallExpression node
64
+ * @param {Object} constantMap - Collected constant map
62
65
  * @returns {EventData}
63
66
  */
64
- function extractSnowplowEvent(node) {
67
+ function extractSnowplowEvent(node, constantMap) {
65
68
  if (!node.arguments || node.arguments.length === 0) {
66
69
  return { eventName: null, propertiesNode: null };
67
70
  }
@@ -75,7 +78,7 @@ function extractSnowplowEvent(node) {
75
78
 
76
79
  if (structEventArg.type === NODE_TYPES.OBJECT_EXPRESSION) {
77
80
  const actionProperty = findPropertyByKey(structEventArg, 'action');
78
- const eventName = actionProperty ? getStringValue(actionProperty.value) : null;
81
+ const eventName = actionProperty ? getStringValue(actionProperty.value, constantMap) : null;
79
82
 
80
83
  return { eventName, propertiesNode: structEventArg };
81
84
  }
@@ -87,15 +90,16 @@ function extractSnowplowEvent(node) {
87
90
  /**
88
91
  * Extracts mParticle event data
89
92
  * @param {Object} node - CallExpression node
93
+ * @param {Object} constantMap - Collected constant map
90
94
  * @returns {EventData}
91
95
  */
92
- function extractMparticleEvent(node) {
96
+ function extractMparticleEvent(node, constantMap) {
93
97
  if (!node.arguments || node.arguments.length < 3) {
94
98
  return { eventName: null, propertiesNode: null };
95
99
  }
96
100
 
97
101
  // mParticle.logEvent('event_name', mParticle.EventType.Navigation, { properties })
98
- const eventName = getStringValue(node.arguments[0]);
102
+ const eventName = getStringValue(node.arguments[0], constantMap);
99
103
  const propertiesNode = node.arguments[2];
100
104
 
101
105
  return { eventName, propertiesNode };
@@ -104,15 +108,16 @@ function extractMparticleEvent(node) {
104
108
  /**
105
109
  * Default event extraction for standard providers
106
110
  * @param {Object} node - CallExpression node
111
+ * @param {Object} constantMap - Collected constant map
107
112
  * @returns {EventData}
108
113
  */
109
- function extractDefaultEvent(node) {
114
+ function extractDefaultEvent(node, constantMap) {
110
115
  if (!node.arguments || node.arguments.length < 2) {
111
116
  return { eventName: null, propertiesNode: null };
112
117
  }
113
118
 
114
119
  // provider.track('event_name', { properties })
115
- const eventName = getStringValue(node.arguments[0]);
120
+ const eventName = getStringValue(node.arguments[0], constantMap);
116
121
  const propertiesNode = node.arguments[1];
117
122
 
118
123
  return { eventName, propertiesNode };
@@ -121,16 +126,17 @@ function extractDefaultEvent(node) {
121
126
  /**
122
127
  * Extracts Custom function event data according to signature
123
128
  * @param {Object} node - CallExpression node
129
+ * @param {Object} constantMap - Collected constant map
124
130
  * @param {Object} customConfig - Parsed custom function configuration
125
131
  * @returns {EventData & {extraArgs:Object}} event data plus extra args map
126
132
  */
127
- function extractCustomEvent(node, customConfig) {
133
+ function extractCustomEvent(node, constantMap, customConfig) {
128
134
  const args = node.arguments || [];
129
135
 
130
136
  const eventArg = args[customConfig?.eventIndex ?? 0];
131
137
  const propertiesArg = args[customConfig?.propertiesIndex ?? 1];
132
138
 
133
- const eventName = getStringValue(eventArg);
139
+ const eventName = getStringValue(eventArg, constantMap);
134
140
 
135
141
  const extraArgs = {};
136
142
  if (customConfig && customConfig.extraParams) {
@@ -197,13 +203,17 @@ function processEventData(eventData, source, filePath, line, functionName, custo
197
203
  /**
198
204
  * Gets string value from an AST node
199
205
  * @param {Object} node - AST node
206
+ * @param {Object} constantMap - Collected constant map
200
207
  * @returns {string|null} String value or null
201
208
  */
202
- function getStringValue(node) {
209
+ function getStringValue(node, constantMap = {}) {
203
210
  if (!node) return null;
204
211
  if (node.type === NODE_TYPES.LITERAL && typeof node.value === 'string') {
205
212
  return node.value;
206
213
  }
214
+ if (node.type === NODE_TYPES.MEMBER_EXPRESSION) {
215
+ return resolveMemberExpressionToString(node, constantMap);
216
+ }
207
217
  return null;
208
218
  }
209
219
 
@@ -240,6 +250,26 @@ function inferNodeValueType(node) {
240
250
  }
241
251
  }
242
252
 
253
+ // Helper to resolve MemberExpression (CONST.KEY) to string using collected constant map
254
+ function resolveMemberExpressionToString(node, constantMap) {
255
+ if (!node || node.type !== NODE_TYPES.MEMBER_EXPRESSION) return null;
256
+ if (node.computed) return null; // Only support dot notation
257
+
258
+ const object = node.object;
259
+ const property = node.property;
260
+
261
+ if (object.type !== NODE_TYPES.IDENTIFIER) return null;
262
+ if (property.type !== NODE_TYPES.IDENTIFIER) return null;
263
+
264
+ const objName = object.name;
265
+ const propName = property.name;
266
+
267
+ if (constantMap && constantMap[objName] && typeof constantMap[objName][propName] === 'string') {
268
+ return constantMap[objName][propName];
269
+ }
270
+ return null;
271
+ }
272
+
243
273
  module.exports = {
244
274
  extractEventData,
245
275
  processEventData
@@ -120,6 +120,57 @@ function nodeMatchesCustomFunction(node, fnName) {
120
120
  return false;
121
121
  }
122
122
 
123
+ // -----------------------------------------------------------------------------
124
+ // Utility – collect constants defined as plain objects or Object.freeze({...})
125
+ // -----------------------------------------------------------------------------
126
+ function collectConstantStringMap(ast) {
127
+ const map = {};
128
+
129
+ walk.simple(ast, {
130
+ VariableDeclaration(node) {
131
+ // Only consider const declarations
132
+ if (node.kind !== 'const') return;
133
+ node.declarations.forEach(decl => {
134
+ if (decl.id.type !== NODE_TYPES.IDENTIFIER || !decl.init) return;
135
+ const name = decl.id.name;
136
+ let objLiteral = null;
137
+
138
+ if (decl.init.type === NODE_TYPES.OBJECT_EXPRESSION) {
139
+ objLiteral = decl.init;
140
+ } else if (decl.init.type === NODE_TYPES.CALL_EXPRESSION) {
141
+ // Check for Object.freeze({...})
142
+ const callee = decl.init.callee;
143
+ if (
144
+ callee &&
145
+ callee.type === NODE_TYPES.MEMBER_EXPRESSION &&
146
+ callee.object.type === NODE_TYPES.IDENTIFIER &&
147
+ callee.object.name === 'Object' &&
148
+ callee.property.type === NODE_TYPES.IDENTIFIER &&
149
+ callee.property.name === 'freeze' &&
150
+ decl.init.arguments.length > 0 &&
151
+ decl.init.arguments[0].type === NODE_TYPES.OBJECT_EXPRESSION
152
+ ) {
153
+ objLiteral = decl.init.arguments[0];
154
+ }
155
+ }
156
+
157
+ if (objLiteral) {
158
+ map[name] = {};
159
+ objLiteral.properties.forEach(prop => {
160
+ if (!prop.key || !prop.value) return;
161
+ const keyName = prop.key.name || prop.key.value;
162
+ if (prop.value.type === NODE_TYPES.LITERAL && typeof prop.value.value === 'string') {
163
+ map[name][keyName] = prop.value.value;
164
+ }
165
+ });
166
+ }
167
+ });
168
+ }
169
+ });
170
+
171
+ return map;
172
+ }
173
+
123
174
  /**
124
175
  * Walk the AST once and find tracking events for built-in providers plus any number of custom
125
176
  * function configurations. This avoids the previous O(n * customConfigs) behaviour.
@@ -132,6 +183,9 @@ function nodeMatchesCustomFunction(node, fnName) {
132
183
  function findTrackingEvents(ast, filePath, customConfigs = []) {
133
184
  const events = [];
134
185
 
186
+ // Collect constant mappings once per file
187
+ const constantMap = collectConstantStringMap(ast);
188
+
135
189
  walk.ancestor(ast, {
136
190
  [NODE_TYPES.CALL_EXPRESSION]: (node, ancestors) => {
137
191
  try {
@@ -148,12 +202,10 @@ function findTrackingEvents(ast, filePath, customConfigs = []) {
148
202
  }
149
203
 
150
204
  if (matchedCustomConfig) {
151
- // Force source to 'custom' and use matched config
152
- const event = extractTrackingEvent(node, ancestors, filePath, matchedCustomConfig);
205
+ const event = extractTrackingEvent(node, ancestors, filePath, constantMap, matchedCustomConfig);
153
206
  if (event) events.push(event);
154
207
  } else {
155
- // Let built-in detector figure out source (pass undefined customFunction)
156
- const event = extractTrackingEvent(node, ancestors, filePath, null);
208
+ const event = extractTrackingEvent(node, ancestors, filePath, constantMap, null);
157
209
  if (event) events.push(event);
158
210
  }
159
211
  } catch (error) {
@@ -170,24 +222,18 @@ function findTrackingEvents(ast, filePath, customConfigs = []) {
170
222
  * @param {Object} node - CallExpression node
171
223
  * @param {Array<Object>} ancestors - Ancestor nodes
172
224
  * @param {string} filePath - File path
225
+ * @param {Object} constantMap - Constant string map
173
226
  * @param {Object} [customConfig] - Custom function configuration object
174
227
  * @returns {Object|null} Extracted event or null
175
228
  */
176
- function extractTrackingEvent(node, ancestors, filePath, customConfig) {
177
- // Detect the analytics source
229
+ function extractTrackingEvent(node, ancestors, filePath, constantMap, customConfig) {
178
230
  const source = detectAnalyticsSource(node, customConfig?.functionName);
179
231
  if (source === 'unknown') {
180
232
  return null;
181
233
  }
182
-
183
- // Extract event data based on the source
184
- const eventData = extractEventData(node, source, customConfig);
185
-
186
- // Get location and context information
234
+ const eventData = extractEventData(node, source, constantMap, customConfig);
187
235
  const line = node.loc.start.line;
188
236
  const functionName = findWrappingFunction(node, ancestors);
189
-
190
- // Process the event data into final format
191
237
  return processEventData(eventData, source, filePath, line, functionName, customConfig);
192
238
  }
193
239
 
@@ -296,27 +296,75 @@ function resolvePropertyAccessToString(node, checker, sourceFile) {
296
296
  try {
297
297
  // Get the symbol for the property access
298
298
  const symbol = checker.getSymbolAtLocation(node);
299
- if (!symbol || !symbol.valueDeclaration) {
300
- return null;
301
- }
302
-
303
- // Check if it's a property assignment with a string initializer
304
- if (ts.isPropertyAssignment(symbol.valueDeclaration) &&
305
- symbol.valueDeclaration.initializer &&
306
- ts.isStringLiteral(symbol.valueDeclaration.initializer)) {
307
- return symbol.valueDeclaration.initializer.text;
299
+ if (symbol && symbol.valueDeclaration) {
300
+ // Check if it's a property assignment with a string initializer
301
+ if (ts.isPropertyAssignment(symbol.valueDeclaration) &&
302
+ symbol.valueDeclaration.initializer &&
303
+ ts.isStringLiteral(symbol.valueDeclaration.initializer)) {
304
+ return symbol.valueDeclaration.initializer.text;
305
+ }
306
+
307
+ // Check if it's a variable declaration property (string literal type)
308
+ if (ts.isPropertySignature(symbol.valueDeclaration) ||
309
+ ts.isMethodSignature(symbol.valueDeclaration)) {
310
+ const type = checker.getTypeAtLocation(node);
311
+ if (type && type.isStringLiteral && type.isStringLiteral()) {
312
+ return type.value;
313
+ }
314
+ }
308
315
  }
309
-
310
- // Check if it's a variable declaration property
311
- if (ts.isPropertySignature(symbol.valueDeclaration) ||
312
- ts.isMethodSignature(symbol.valueDeclaration)) {
313
- // Try to get the type and see if it's a string literal type
314
- const type = checker.getTypeAtLocation(node);
315
- if (type.isStringLiteral && type.isStringLiteral()) {
316
- return type.value;
316
+
317
+ // ---------------------------------------------------------------------
318
+ // Fallback – manually resolve patterns like:
319
+ // const CONST = { KEY: 'value' };
320
+ // const CONST = Object.freeze({ KEY: 'value' });
321
+ // And later used as CONST.KEY
322
+ // ---------------------------------------------------------------------
323
+ if (ts.isIdentifier(node.expression)) {
324
+ const objIdentifier = node.expression;
325
+ const initializer = resolveIdentifierToInitializer(checker, objIdentifier, sourceFile);
326
+ if (initializer) {
327
+ let objectLiteral = null;
328
+
329
+ // Handle direct object literal initializers
330
+ if (ts.isObjectLiteralExpression(initializer)) {
331
+ objectLiteral = initializer;
332
+ }
333
+ // Handle Object.freeze({ ... }) pattern
334
+ else if (ts.isCallExpression(initializer)) {
335
+ const callee = initializer.expression;
336
+ if (
337
+ ts.isPropertyAccessExpression(callee) &&
338
+ ts.isIdentifier(callee.expression) &&
339
+ callee.expression.escapedText === 'Object' &&
340
+ callee.name.escapedText === 'freeze' &&
341
+ initializer.arguments.length > 0 &&
342
+ ts.isObjectLiteralExpression(initializer.arguments[0])
343
+ ) {
344
+ objectLiteral = initializer.arguments[0];
345
+ }
346
+ }
347
+
348
+ if (objectLiteral) {
349
+ const propNode = findPropertyByKey(objectLiteral, node.name.escapedText || node.name.text);
350
+ if (propNode && propNode.initializer && ts.isStringLiteral(propNode.initializer)) {
351
+ return propNode.initializer.text;
352
+ }
353
+ }
317
354
  }
318
355
  }
319
-
356
+
357
+ // Final fallback – use type information at location (works for imported Object.freeze constants)
358
+ try {
359
+ const t = checker.getTypeAtLocation(node);
360
+ if (t && t.isStringLiteral && typeof t.isStringLiteral === 'function' && t.isStringLiteral()) {
361
+ return t.value;
362
+ }
363
+ if (t && t.flags && (t.flags & ts.TypeFlags.StringLiteral)) {
364
+ return t.value;
365
+ }
366
+ } catch (_) {/* ignore */}
367
+
320
368
  return null;
321
369
  } catch (error) {
322
370
  return null;