@flisk/analyze-tracking 0.7.1 → 0.7.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.
Files changed (71) hide show
  1. package/README.md +35 -61
  2. package/bin/cli.js +1 -1
  3. package/package.json +18 -3
  4. package/src/analyze/go/astTraversal.js +121 -0
  5. package/src/analyze/go/constants.js +20 -0
  6. package/src/analyze/go/eventDeduplicator.js +47 -0
  7. package/src/analyze/go/eventExtractor.js +156 -0
  8. package/src/analyze/go/goAstParser/constants.js +39 -0
  9. package/src/analyze/go/goAstParser/expressionParser.js +281 -0
  10. package/src/analyze/go/goAstParser/index.js +52 -0
  11. package/src/analyze/go/goAstParser/statementParser.js +387 -0
  12. package/src/analyze/go/goAstParser/tokenizer.js +196 -0
  13. package/src/analyze/go/goAstParser/typeParser.js +202 -0
  14. package/src/analyze/go/goAstParser/utils.js +99 -0
  15. package/src/analyze/go/index.js +55 -0
  16. package/src/analyze/go/propertyExtractor.js +670 -0
  17. package/src/analyze/go/trackingDetector.js +71 -0
  18. package/src/analyze/go/trackingExtractor.js +54 -0
  19. package/src/analyze/go/typeContext.js +88 -0
  20. package/src/analyze/go/utils.js +215 -0
  21. package/src/analyze/index.js +11 -7
  22. package/src/analyze/javascript/constants.js +115 -0
  23. package/src/analyze/javascript/detectors/analytics-source.js +119 -0
  24. package/src/analyze/javascript/detectors/index.js +10 -0
  25. package/src/analyze/javascript/extractors/event-extractor.js +179 -0
  26. package/src/analyze/javascript/extractors/index.js +13 -0
  27. package/src/analyze/javascript/extractors/property-extractor.js +172 -0
  28. package/src/analyze/javascript/index.js +38 -0
  29. package/src/analyze/javascript/parser.js +126 -0
  30. package/src/analyze/javascript/utils/function-finder.js +123 -0
  31. package/src/analyze/python/index.js +111 -0
  32. package/src/analyze/python/pythonTrackingAnalyzer.py +814 -0
  33. package/src/analyze/ruby/detectors.js +46 -0
  34. package/src/analyze/ruby/extractors.js +258 -0
  35. package/src/analyze/ruby/index.js +51 -0
  36. package/src/analyze/ruby/traversal.js +123 -0
  37. package/src/analyze/ruby/types.js +30 -0
  38. package/src/analyze/ruby/visitor.js +66 -0
  39. package/src/analyze/typescript/constants.js +109 -0
  40. package/src/analyze/typescript/detectors/analytics-source.js +120 -0
  41. package/src/analyze/typescript/detectors/index.js +10 -0
  42. package/src/analyze/typescript/extractors/event-extractor.js +269 -0
  43. package/src/analyze/typescript/extractors/index.js +14 -0
  44. package/src/analyze/typescript/extractors/property-extractor.js +395 -0
  45. package/src/analyze/typescript/index.js +48 -0
  46. package/src/analyze/typescript/parser.js +131 -0
  47. package/src/analyze/typescript/utils/function-finder.js +114 -0
  48. package/src/analyze/typescript/utils/type-resolver.js +193 -0
  49. package/src/generateDescriptions/index.js +81 -0
  50. package/src/generateDescriptions/llmUtils.js +33 -0
  51. package/src/generateDescriptions/promptUtils.js +62 -0
  52. package/src/generateDescriptions/schemaUtils.js +61 -0
  53. package/src/index.js +7 -2
  54. package/src/{fileProcessor.js → utils/fileProcessor.js} +5 -0
  55. package/src/{repoDetails.js → utils/repoDetails.js} +5 -0
  56. package/src/{yamlGenerator.js → utils/yamlGenerator.js} +5 -0
  57. package/.github/workflows/npm-publish.yml +0 -33
  58. package/.github/workflows/pr-check.yml +0 -17
  59. package/jest.config.js +0 -7
  60. package/src/analyze/analyzeGoFile.js +0 -1164
  61. package/src/analyze/analyzeJsFile.js +0 -72
  62. package/src/analyze/analyzePythonFile.js +0 -41
  63. package/src/analyze/analyzeRubyFile.js +0 -409
  64. package/src/analyze/analyzeTsFile.js +0 -69
  65. package/src/analyze/go2json.js +0 -1069
  66. package/src/analyze/helpers.js +0 -217
  67. package/src/analyze/pythonTrackingAnalyzer.py +0 -439
  68. package/src/generateDescriptions.js +0 -196
  69. package/tests/detectSource.test.js +0 -20
  70. package/tests/extractProperties.test.js +0 -109
  71. package/tests/findWrappingFunction.test.js +0 -30
@@ -1,196 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const { z } = require('zod');
4
- const { PromptTemplate } = require('@langchain/core/prompts');
5
-
6
- function createPrompt(eventName, properties, implementations, codebaseDir) {
7
- let prompt = `Event Name: "${eventName}"\n\n`;
8
- prompt += `Properties:\n`;
9
-
10
- function appendPropertiesToPrompt(properties, indent = '') {
11
- for (const propName in properties) {
12
- const prop = properties[propName];
13
- prompt += `${indent}- "${propName}" (type: ${prop.type})\n`;
14
- if (prop.properties) {
15
- prompt += `${indent} Sub-properties:\n`;
16
- appendPropertiesToPrompt(prop.properties, indent + ' ');
17
- }
18
- }
19
- }
20
-
21
- appendPropertiesToPrompt(properties);
22
-
23
- // Add implementations with code snippets
24
- prompt += `\nImplementations:\n`;
25
- for (const impl of implementations) {
26
- const codeSnippet = getCodeSnippet(path.join(codebaseDir, impl.path), impl.line);
27
- prompt += `- Path: "${impl.path}", Line: ${impl.line}, Function: "${impl.function}", Destination: "${impl.destination}"\n`;
28
- prompt += `Code Snippet:\n`;
29
- prompt += '```\n';
30
- prompt += codeSnippet + '\n';
31
- prompt += '```\n';
32
- }
33
-
34
- return prompt;
35
- }
36
-
37
- function getCodeSnippet(filePath, lineNumber, contextLines = 5) {
38
- // Extract a code snippet from the file around the specified line
39
- try {
40
- const fileContent = fs.readFileSync(filePath, 'utf8');
41
- const lines = fileContent.split('\n');
42
- const startLine = Math.max(0, lineNumber - contextLines - 1);
43
- const endLine = Math.min(lines.length, lineNumber + contextLines);
44
-
45
- const snippetLines = lines.slice(startLine, endLine);
46
- return snippetLines.join('\n');
47
- } catch (e) {
48
- console.error(`Failed to read file ${filePath}:`, e);
49
- return '';
50
- }
51
- }
52
-
53
- function createEventDescriptionSchema(properties) {
54
- function buildPropertySchema(prop) {
55
- if (prop.properties) {
56
- const subPropertiesSchema = {};
57
- for (const subPropName in prop.properties) {
58
- subPropertiesSchema[subPropName] = buildPropertySchema(prop.properties[subPropName]);
59
- }
60
- return z.object({
61
- description: z
62
- .string()
63
- .describe('A maximum of 10 words describing the property and what it means'),
64
- properties: z.object(subPropertiesSchema),
65
- });
66
- } else {
67
- return z.object({
68
- description: z
69
- .string()
70
- .describe('A maximum of 10 words describing the property and what it means'),
71
- });
72
- }
73
- }
74
-
75
- // Define the schema for properties
76
- const propertiesSchema = {};
77
- for (const propName in properties) {
78
- propertiesSchema[propName] = buildPropertySchema(properties[propName]);
79
- }
80
-
81
- // Define the schema for implementations
82
- const implementationsSchema = z.array(
83
- z.object({
84
- description: z
85
- .string()
86
- .describe('A maximum of 10 words describing how this event is triggered without using the word "triggered"'),
87
- path: z.string(),
88
- line: z.number(),
89
- })
90
- );
91
-
92
- // Construct the full schema
93
- const eventDescriptionSchema = z.object({
94
- eventDescription: z
95
- .string()
96
- .describe('A maximum of 10 words describing the event and what it tracks without using the word "tracks"'),
97
- properties: z.object(propertiesSchema),
98
- implementations: implementationsSchema,
99
- });
100
-
101
- return eventDescriptionSchema;
102
- }
103
-
104
- async function sendPromptToLLM(prompt, schema, model) {
105
- try {
106
- const promptTemplate = new PromptTemplate({
107
- template: `You are an expert at structured data extraction. Generate detailed descriptions for the following analytics event, its properties, and implementations.\n{input}`,
108
- inputVariables: ['input'],
109
- });
110
-
111
- const formattedPrompt = await promptTemplate.format({
112
- input: prompt,
113
- });
114
-
115
- const structuredModel = model.withStructuredOutput(schema);
116
- const response = await structuredModel.invoke(formattedPrompt);
117
-
118
- return {
119
- descriptions: response,
120
- };
121
- } catch (error) {
122
- console.error('Error during LLM response parsing:', error);
123
- return null;
124
- }
125
- }
126
-
127
- async function generateEventDescription(eventName, event, codebaseDir, model) {
128
- const properties = event.properties || {};
129
- const implementations = event.implementations || [];
130
-
131
- // Create prompt for the LLM
132
- const prompt = createPrompt(eventName, properties, implementations, codebaseDir);
133
-
134
- // Define the output schema using Zod
135
- const eventDescriptionSchema = createEventDescriptionSchema(properties);
136
-
137
- // Send prompt to the LLM and get the structured response
138
- const { descriptions } = await sendPromptToLLM(prompt, eventDescriptionSchema, model);
139
-
140
- return { eventName, descriptions };
141
- }
142
-
143
- async function generateDescriptions(events, codebaseDir, model) {
144
- const eventPromises = Object.entries(events).map(([eventName, event]) =>
145
- generateEventDescription(eventName, event, codebaseDir, model)
146
- );
147
-
148
- console.log(`Running ${eventPromises.length} prompts in parallel...`);
149
-
150
- const results = await Promise.all(eventPromises);
151
-
152
- // Process results and update the events object
153
- results.forEach(({ eventName, descriptions }) => {
154
- if (descriptions) {
155
- const event = events[eventName];
156
- event.description = descriptions.eventDescription;
157
-
158
- // Update property descriptions recursively
159
- function updatePropertyDescriptions(eventProperties, descriptionProperties) {
160
- for (const propName in descriptionProperties) {
161
- if (eventProperties[propName]) {
162
- eventProperties[propName].description = descriptionProperties[propName].description;
163
- if (eventProperties[propName].properties && descriptionProperties[propName].properties) {
164
- updatePropertyDescriptions(
165
- eventProperties[propName].properties,
166
- descriptionProperties[propName].properties
167
- );
168
- }
169
- }
170
- }
171
- }
172
-
173
- updatePropertyDescriptions(event.properties, descriptions.properties);
174
-
175
- // Update implementations with descriptions
176
- for (let i = 0; i < descriptions.implementations.length; i++) {
177
- if (event.implementations[i]) {
178
- if (
179
- event.implementations[i].path === descriptions.implementations[i].path &&
180
- event.implementations[i].line === descriptions.implementations[i].line
181
- ) {
182
- event.implementations[i].description = descriptions.implementations[i].description;
183
- } else {
184
- console.error(`Returned implementation description does not match path or line for event: ${eventName}`);
185
- }
186
- }
187
- }
188
- } else {
189
- console.error(`Failed to get description for event: ${eventName}`);
190
- }
191
- });
192
-
193
- return events;
194
- }
195
-
196
- module.exports = { generateDescriptions };
@@ -1,20 +0,0 @@
1
- const {
2
- detectSourceJs,
3
- } = require('../src/analyze/helpers');
4
-
5
- describe('detectSourceJs', () => {
6
- it('should detect Google Analytics', () => {
7
- const node = { callee: { type: 'Identifier', name: 'gtag' } };
8
- expect(detectSourceJs(node)).toBe('googleanalytics');
9
- });
10
-
11
- it('should detect Segment', () => {
12
- const node = { callee: { type: 'MemberExpression', object: { name: 'analytics' }, property: { name: 'track' } } };
13
- expect(detectSourceJs(node)).toBe('segment');
14
- });
15
-
16
- it('should return unknown for unrecognized source', () => {
17
- const node = { callee: { type: 'Identifier', name: 'unknownLib' } };
18
- expect(detectSourceJs(node)).toBe('unknown');
19
- });
20
- });
@@ -1,109 +0,0 @@
1
- const ts = require('typescript');
2
- const {
3
- extractJsProperties,
4
- extractTsProperties,
5
- } = require('../src/analyze/helpers');
6
-
7
- describe('extractJsProperties', () => {
8
- it('should extract simple properties', () => {
9
- const node = {
10
- properties: [
11
- { key: { name: 'userId' }, value: { value: '12345', type: 'Literal' } },
12
- { key: { name: 'plan' }, value: { value: 'Free', type: 'Literal' } },
13
- ],
14
- };
15
- const properties = extractJsProperties(node);
16
- expect(properties).toEqual({
17
- userId: { type: 'string' },
18
- plan: { type: 'string' },
19
- });
20
- });
21
-
22
- it('should handle nested object properties', () => {
23
- const node = {
24
- properties: [
25
- {
26
- key: { name: 'address' },
27
- value: {
28
- type: 'ObjectExpression',
29
- properties: [
30
- { key: { name: 'city' }, value: { value: 'San Francisco', type: 'Literal' } },
31
- { key: { name: 'state' }, value: { value: 'CA', type: 'Literal' } },
32
- ],
33
- },
34
- },
35
- ],
36
- };
37
- const properties = extractJsProperties(node);
38
- expect(properties).toEqual({
39
- address: {
40
- type: 'object',
41
- properties: {
42
- city: { type: 'string' },
43
- state: { type: 'string' },
44
- },
45
- },
46
- });
47
- });
48
-
49
- it('should handle properties with undefined type', () => {
50
- const node = {
51
- properties: [{ key: { name: 'undefinedProp' }, value: { value: undefined, type: 'Literal' } }],
52
- };
53
- const properties = extractJsProperties(node);
54
- expect(properties).toEqual({
55
- undefinedProp: { type: 'any' },
56
- });
57
- });
58
- });
59
-
60
- describe('extractTsProperties', () => {
61
- it('should extract properties from TypeScript object', () => {
62
- const node = {
63
- properties: [
64
- { name: { text: 'userId' }, initializer: { text: '12345', type: 'Literal' } },
65
- { name: { text: 'plan' }, initializer: { text: 'Free', type: 'Literal' } },
66
- ],
67
- };
68
- const checker = {
69
- getTypeAtLocation: jest.fn().mockReturnValue({}),
70
- typeToString: jest.fn().mockReturnValue('string'),
71
- };
72
- const properties = extractTsProperties(checker, node);
73
- expect(properties).toEqual({
74
- userId: { type: 'string' },
75
- plan: { type: 'string' },
76
- });
77
- });
78
-
79
- it('should handle nested object properties in TypeScript', () => {
80
- const node = {
81
- properties: [
82
- {
83
- name: { text: 'address' },
84
- initializer: {
85
- kind: ts.SyntaxKind.ObjectLiteralExpression,
86
- properties: [
87
- { name: { text: 'city' }, initializer: { text: 'San Francisco', type: 'Literal' } },
88
- { name: { text: 'state' }, initializer: { text: 'CA', type: 'Literal' } },
89
- ],
90
- },
91
- },
92
- ],
93
- };
94
- const checker = {
95
- getTypeAtLocation: jest.fn().mockReturnValue({}),
96
- typeToString: jest.fn().mockReturnValue('string'),
97
- };
98
- const properties = extractTsProperties(checker, node);
99
- expect(properties).toEqual({
100
- address: {
101
- type: 'object',
102
- properties: {
103
- city: { type: 'string' },
104
- state: { type: 'string' },
105
- },
106
- },
107
- });
108
- });
109
- });
@@ -1,30 +0,0 @@
1
- const ts = require('typescript');
2
- const {
3
- findWrappingFunctionJs,
4
- } = require('../src/analyze/helpers');
5
-
6
- describe('findWrappingFunctionJs', () => {
7
- it('should return function name for arrow function assigned to variable', () => {
8
- const node = { type: 'ArrowFunctionExpression' };
9
- const ancestors = [
10
- { type: 'Program' },
11
- { type: 'VariableDeclarator', init: node, id: { name: 'checkout' } },
12
- ];
13
- expect(findWrappingFunctionJs(node, ancestors)).toBe('checkout');
14
- });
15
-
16
- it('should return function name for function expression assigned to variable', () => {
17
- const node = { type: 'FunctionExpression' };
18
- const ancestors = [
19
- { type: 'Program' },
20
- { type: 'VariableDeclarator', init: node, id: { name: 'myFunc' } },
21
- ];
22
- expect(findWrappingFunctionJs(node, ancestors)).toBe('myFunc');
23
- });
24
-
25
- it('should return "global" if no wrapping function is found', () => {
26
- const node = {};
27
- const ancestors = [{ type: 'Program' }];
28
- expect(findWrappingFunctionJs(node, ancestors)).toBe('global');
29
- });
30
- });