Skip to content

Commit e29755c

Browse files
author
roman.vasilev
committed
fix: Restored getSource file fallback implementation
1 parent 562bf9b commit e29755c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/get-source-file.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as ts from 'typescript';
2+
3+
export function getSourceFile(program: ts.Program, fileName: string, sourceText?: string): ts.SourceFile {
4+
if (program !== undefined) {
5+
const sourceFile = program.getSourceFile(fileName);
6+
if (sourceFile) {
7+
return sourceFile;
8+
}
9+
}
10+
if (sourceText === undefined) {
11+
throw new Error(`Invalid source file: ${fileName}`);
12+
}
13+
return ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.ES5, true);
14+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ts from 'typescript';
22
import { createProgram } from './create-program';
3+
import { getSourceFile } from './get-source-file';
34

45
type createServiceOptions = {
56
configFile: string;
@@ -18,7 +19,7 @@ export function createService({ compilerOptions, configFile }: createServiceOpti
1819
program = ts.createProgram(rootFileNames, program.getCompilerOptions(), host, program);
1920
sourceFile = program.getSourceFile(fileName);
2021
}
21-
return sourceFile;
22+
return sourceFile || getSourceFile(program, fileName, sourceText);
2223
},
2324
getDiagnostics: (fileName: string, sourceText?: string) => {
2425
const sourceFile = api.getSourceFile(fileName, sourceText);

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
"declaration": false,
88
"outDir": "dist",
99
"removeComments": false,
10-
"strict": true,
10+
"noImplicitReturns": true,
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"noUnusedParameters": true,
14+
"noUnusedLocals": true,
15+
"skipLibCheck": true,
16+
"skipDefaultLibCheck": true,
1117
"sourceMap": true,
1218
"lib": [
1319
"esnext"

0 commit comments

Comments
 (0)