Skip to content

Commit 2976374

Browse files
committed
resolve ts imports
1 parent e9791ff commit 2976374

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

src/analyze/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ function addEventToCollection(allEvents, event, baseDir) {
4444
}
4545

4646
/**
47-
* Processes non-TypeScript files
47+
* Processes all files that are not TypeScript files
4848
* @param {Array<string>} files - Array of file paths
4949
* @param {Object} allEvents - Collection to add events to
5050
* @param {string} baseDir - Base directory for relative paths
5151
* @param {Array} customFunctionSignatures - Custom function signatures to detect
5252
*/
53-
async function processOtherFiles(files, allEvents, baseDir, customFunctionSignatures) {
53+
async function processFiles(files, allEvents, baseDir, customFunctionSignatures) {
5454
for (const file of files) {
5555
let events = [];
5656

@@ -97,15 +97,15 @@ async function analyzeDirectory(dirPath, customFunctions) {
9797
}
9898
}
9999

100+
// First process non-TypeScript files
101+
await processFiles(otherFiles, allEvents, dirPath, customFunctionSignatures);
102+
100103
// Process TypeScript files with optimized batch processing
101104
if (tsFiles.length > 0) {
102105
const tsEvents = analyzeTsFiles(tsFiles, customFunctionSignatures);
103106
tsEvents.forEach(event => addEventToCollection(allEvents, event, dirPath));
104107
}
105108

106-
// Process remaining file types
107-
await processOtherFiles(otherFiles, allEvents, dirPath, customFunctionSignatures);
108-
109109
return allEvents;
110110
}
111111

src/analyze/typescript/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const path = require('path');
1616
function createStandaloneProgram(filePath) {
1717
const compilerOptions = {
1818
...DEFAULT_COMPILER_OPTIONS,
19-
noResolve: true, // Don't try to resolve imports - just analyze the file content
19+
// We intentionally allow module resolution here so that imported constants
20+
// (e.g. event name strings defined in a sibling file) can be followed by the
21+
// TypeScript compiler.
2022
isolatedModules: true
2123
};
2224

@@ -76,22 +78,28 @@ function tryStandaloneAnalysis(filePath, customFunctionSignatures) {
7678
* @returns {Object} TypeScript program
7779
*/
7880
function getCachedTsProgram(filePath, programCache) {
79-
// Find the nearest tsconfig.json
81+
// Locate nearest tsconfig.json (may be undefined)
8082
const searchPath = path.dirname(filePath);
8183
const configPath = ts.findConfigFile(searchPath, ts.sys.fileExists, 'tsconfig.json');
82-
83-
// Use config path as cache key, or 'standalone' if no config found
84-
const cacheKey = configPath || 'standalone';
85-
86-
// Return cached program if available
87-
if (programCache.has(cacheKey)) {
84+
85+
// We only cache when a tsconfig.json exists because the resulting program
86+
// represents an entire project. If no config is present we build a
87+
// stand-alone program that should not be reused for other files – otherwise
88+
// later files would be missing from the program (which is precisely what
89+
// caused the regression we are fixing).
90+
const shouldCache = Boolean(configPath);
91+
const cacheKey = configPath; // undefined when shouldCache is false
92+
93+
if (shouldCache && programCache.has(cacheKey)) {
8894
return programCache.get(cacheKey);
8995
}
90-
91-
// Create new program and cache it
96+
9297
const program = getProgram(filePath, null);
93-
programCache.set(cacheKey, program);
94-
98+
99+
if (shouldCache) {
100+
programCache.set(cacheKey, program);
101+
}
102+
95103
return program;
96104
}
97105

tests/cli.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ function compareYAMLFiles(actualPath, expectedPath) {
5656
assert.ok(actual.source);
5757
assert.ok(actual.source.repository);
5858

59+
// Helper to sort implementations deterministically
60+
const sortImpls = (impls = []) =>
61+
impls.slice().sort((a, b) => {
62+
if (a.path !== b.path) return a.path.localeCompare(b.path);
63+
if (a.line !== b.line) return a.line - b.line;
64+
if ((a.destination || '') !== (b.destination || '')) return (a.destination || '').localeCompare(b.destination || '');
65+
return (a.function || '').localeCompare(b.function || '');
66+
});
67+
68+
// Normalise events so that order of implementations does not matter
69+
const normaliseEvent = (evt) => {
70+
if (!evt) return evt;
71+
return {
72+
...evt,
73+
implementations: sortImpls(evt.implementations)
74+
};
75+
};
76+
5977
// Compare events using deep equality (order-insensitive)
6078
const diff = {};
6179
for (const eventName in expected.events) {
@@ -64,14 +82,17 @@ function compareYAMLFiles(actualPath, expectedPath) {
6482
continue;
6583
}
6684

67-
const actualEvent = actual.events[eventName];
68-
const expectedEvent = expected.events[eventName];
85+
const actualEvent = normaliseEvent(actual.events[eventName]);
86+
const expectedEvent = normaliseEvent(expected.events[eventName]);
6987

7088
if (!_.isEqual(actualEvent, expectedEvent)) {
7189
diff[eventName] = {
7290
properties: {
7391
missing: Object.keys(expectedEvent.properties || {}).filter(p => !actualEvent.properties?.[p]),
74-
unexpected: Object.keys(actualEvent.properties || {}).filter(p => !expectedEvent.properties?.[p])
92+
unexpected: Object.keys(actualEvent.properties || {}).filter(p => !expectedEvent.properties?.[p]),
93+
changed: Object.keys(expectedEvent.properties || {}).filter(p =>
94+
actualEvent.properties?.[p] && !_.isEqual(actualEvent.properties[p], expectedEvent.properties[p])
95+
)
7596
},
7697
implementations: {
7798
missing: (expectedEvent.implementations || []).filter(impl =>

tests/fixtures/tracking-schema-all.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ events:
989989
destination: segment
990990
properties:
991991
documentId:
992-
type: any
992+
type: number
993993
documentType:
994994
type: string
995995
cart_viewed:

0 commit comments

Comments
 (0)