-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-file.ts
More file actions
43 lines (38 loc) · 1.44 KB
/
Copy pathsource-file.ts
File metadata and controls
43 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* source-file.ts — read a path and parse it into a TypeScript AST.
*
* Wraps readFileSync + ts.createSourceFile. No type-checker, no transformers —
* just the parse step Sentinel needs before walking the tree.
*
* Error handling: this module throws on read failure (ENOENT, EACCES, etc.).
* That is intentional for script/learning callers (e.g. scripts/parse-ast.ts)
* which fail fast with a clear exit code. The production scan pipeline uses
* readFileContent() + Result and never calls parseSourceFile.
*/
import { readFileSync } from 'node:fs'
import * as ts from 'typescript'
/**
* Read a .ts/.tsx/.js file from disk and return its parsed SourceFile AST.
*
* @param filePath Absolute or relative path (used for ScriptKind and error messages).
*/
export function parseSourceFile(filePath: string): ts.SourceFile {
const content = readFileSync(filePath, 'utf8')
return ts.createSourceFile(
filePath,
content,
ts.ScriptTarget.Latest,
/* setParentNodes */ true,
detectScriptKind(filePath),
)
}
function detectScriptKind(filePath: string): ts.ScriptKind {
if (filePath.endsWith('.tsx') || filePath.endsWith('.jsx')) return ts.ScriptKind.TSX
if (filePath.endsWith('.ts') || filePath.endsWith('.cts') || filePath.endsWith('.mts')) {
return ts.ScriptKind.TS
}
if (filePath.endsWith('.js') || filePath.endsWith('.cjs') || filePath.endsWith('.mjs')) {
return ts.ScriptKind.JS
}
return ts.ScriptKind.Unknown
}