Skip to content

Commit a928586

Browse files
authoredJun 16, 2021
fix(typescript 🐛): dont try to be smarter than tsc (#754)
1 parent dd857c5 commit a928586

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed
 

‎packages/typescript/src/typescript.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import * as ts from 'typescript';
55

66
const NEW_LINE = '\n';
77

8-
// When making a project with only a subset of the files in the original tsconfig,
9-
// 6307 errors will appear when importing other files, but they're only a problem because the tsconfig has been edited
10-
const CODE_FILE_NOT_INCLUDED = 6307;
11-
128
type TypeScriptReadConfigResult = {
139
config: {
1410
compilerOptions: ts.CompilerOptions;
@@ -81,6 +77,11 @@ export function typescript(configFilePath: string, extraCompilerOptions: ts.Comp
8177
});
8278
}
8379

80+
// TypeScript throws a 6307 error when it need to access type information from a file
81+
// that wasn't included by the tsconfig. This happens whenever we run the compiler on
82+
// a subset of files, so we need to filter out those errors!
83+
const CODE_FILE_NOT_INCLUDED = 6307;
84+
8485
/** @internal Definitely not stable! Please don't use! */
8586
export function typescriptΔ(configFilePath: string, extraCompilerOptions: ts.CompilerOptions = {}): BettererFileTest {
8687
if (!configFilePath) {
@@ -106,8 +107,11 @@ export function typescriptΔ(configFilePath: string, extraCompilerOptions: ts.Co
106107
...extraCompilerOptions
107108
};
108109
config.compilerOptions = fullCompilerOptions;
109-
config.files = filePaths;
110-
delete config.include;
110+
111+
if (!config.compilerOptions.incremental) {
112+
config.files = filePaths;
113+
delete config.include;
114+
}
111115

112116
const compilerHost = ts.createCompilerHost(fullCompilerOptions);
113117
const configHost: ts.ParseConfigHost = {
@@ -139,8 +143,13 @@ export function typescriptΔ(configFilePath: string, extraCompilerOptions: ts.Co
139143
]);
140144

141145
allDiagnostics
142-
.filter((d): d is ts.DiagnosticWithLocation => d.file !== undefined && d.start != null && d.length != null)
143-
.filter(({ file, code }) => filePaths.includes(file.fileName) && code !== CODE_FILE_NOT_INCLUDED)
146+
.filter((diagnostic): diagnostic is ts.DiagnosticWithLocation => {
147+
const { file, start, length } = diagnostic;
148+
return file != null && start != null && length != null;
149+
})
150+
.filter(({ file, code }) => {
151+
return filePaths.includes(file.fileName) && code !== CODE_FILE_NOT_INCLUDED;
152+
})
144153
.forEach(({ start, length, file: source, messageText }) => {
145154
const file = fileTestResult.addFile(source.fileName, source.getFullText());
146155
const message = ts.flattenDiagnosticMessageText(messageText, NEW_LINE);

‎test/betterer-typescript.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default {
183183
"resolveJsonModule": true,
184184
"strict": false
185185
},
186-
"include": [".betterer.ts"]
186+
"include": ["./src/**/*.ts", ".betterer.ts"]
187187
}
188188
`,
189189
'./src/foo.ts': `

0 commit comments

Comments
 (0)