@@ -7,6 +7,7 @@ const ts = require('typescript');
77const { detectAnalyticsSource } = require ( './detectors' ) ;
88const { extractEventData, processEventData } = require ( './extractors' ) ;
99const { findWrappingFunction } = require ( './utils/function-finder' ) ;
10+ const path = require ( 'path' ) ;
1011
1112/**
1213 * Error thrown when TypeScript program cannot be created
@@ -44,16 +45,37 @@ function getProgram(filePath, existingProgram) {
4445 }
4546
4647 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 = {
4953 target : ts . ScriptTarget . Latest ,
5054 module : ts . ModuleKind . CommonJS ,
5155 allowJs : true ,
5256 checkJs : false ,
53- noEmit : true
57+ noEmit : true ,
58+ jsx : ts . JsxEmit . Preserve
5459 } ;
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+ }
5577
56- const program = ts . createProgram ( [ filePath ] , options ) ;
78+ const program = ts . createProgram ( rootNames , compilerOptions ) ;
5779 return program ;
5880 } catch ( error ) {
5981 throw new ProgramError ( filePath , error ) ;
0 commit comments