Skip to content

Commit 0e80617

Browse files
committed
handle multiple typescript programs
1 parent e1002a4 commit 0e80617

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/analyze/index.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
const path = require('path');
7-
const ts = require('typescript');
7+
88
const { parseCustomFunctionSignature } = require('./utils/customFunctionParser');
99
const { getAllFiles } = require('../utils/fileProcessor');
1010
const { analyzeJsFile } = require('./javascript');
@@ -19,29 +19,6 @@ async function analyzeDirectory(dirPath, customFunctions) {
1919
const customFunctionSignatures = (customFunctions && customFunctions?.length > 0) ? customFunctions.map(parseCustomFunctionSignature) : null;
2020

2121
const files = getAllFiles(dirPath);
22-
const tsFiles = files.filter(file => /\.(tsx?)$/.test(file));
23-
24-
// Attempt to reuse project tsconfig.json compiler options for proper module resolution (e.g., path aliases)
25-
let tsCompilerOptions = {
26-
target: ts.ScriptTarget.ESNext,
27-
module: ts.ModuleKind.CommonJS,
28-
jsx: ts.JsxEmit.Preserve,
29-
allowJs: true,
30-
noEmit: true,
31-
};
32-
33-
const tsConfigPath = ts.findConfigFile(dirPath, ts.sys.fileExists, 'tsconfig.json');
34-
if (tsConfigPath) {
35-
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
36-
if (!readResult.error && readResult.config) {
37-
const parsedConfig = ts.parseJsonConfigFileContent(readResult.config, ts.sys, path.dirname(tsConfigPath));
38-
if (!parsedConfig.errors || parsedConfig.errors.length === 0) {
39-
tsCompilerOptions = { ...tsCompilerOptions, ...parsedConfig.options };
40-
}
41-
}
42-
}
43-
44-
const tsProgram = ts.createProgram(tsFiles, tsCompilerOptions);
4522

4623
for (const file of files) {
4724
let events = [];
@@ -55,7 +32,8 @@ async function analyzeDirectory(dirPath, customFunctions) {
5532
if (isJsFile) {
5633
events = analyzeJsFile(file, customFunctionSignatures);
5734
} else if (isTsFile) {
58-
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);
5937
} else if (isPythonFile) {
6038
events = await analyzePythonFile(file, customFunctionSignatures);
6139
} else if (isRubyFile) {

tests/fixtures/tracking-schema-all.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,12 @@ events:
11351135
function: handleView
11361136
destination: custom
11371137
properties: {}
1138+
ViewedPage:
1139+
implementations:
1140+
- path: typescript-alias/app/components/main.ts
1141+
line: 10
1142+
function: global
1143+
destination: mixpanel
1144+
properties:
1145+
foo:
1146+
type: string

0 commit comments

Comments
 (0)