Skip to content

Commit 08585b9

Browse files
authored
Set of extra ignored diagnostics for REPL mode (#1396)
* empty commit * Add mechanism to ignore diagnostics in certain files; use it to ignore annoying diagnostics in the REPL * misc cleanup of commented-out code * Apply the new diagnostic filtering only to interactive REPL; not to [stdin] nor [eval] * Fix missing addDiagnosticFilter implementation; lint format * WIP * Finish; add tests * revert unnecessary formatting changes
1 parent 6681fcd commit 08585b9

File tree

7 files changed

+293
-96
lines changed

7 files changed

+293
-96
lines changed

src/bin.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,6 @@ export function main(
197197
let evalStuff: VirtualFileState | undefined;
198198
let replStuff: VirtualFileState | undefined;
199199
let stdinStuff: VirtualFileState | undefined;
200-
// let evalService: ReplService | undefined;
201-
// let replState: EvalState | undefined;
202-
// let replService: ReplService | undefined;
203-
// let stdinState: EvalState | undefined;
204-
// let stdinService: ReplService | undefined;
205200
let evalAwarePartialHost: EvalAwarePartialHost | undefined = undefined;
206201
if (executeEval) {
207202
const state = new EvalState(join(cwd, EVAL_FILENAME));
@@ -210,6 +205,7 @@ export function main(
210205
repl: createRepl({
211206
state,
212207
composeWithEvalAwarePartialHost: evalAwarePartialHost,
208+
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl: false,
213209
}),
214210
};
215211
({ evalAwarePartialHost } = evalStuff.repl);
@@ -225,6 +221,7 @@ export function main(
225221
repl: createRepl({
226222
state,
227223
composeWithEvalAwarePartialHost: evalAwarePartialHost,
224+
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl: false,
228225
}),
229226
};
230227
({ evalAwarePartialHost } = stdinStuff.repl);

src/index.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ export interface Service {
410410
configFilePath: string | undefined;
411411
/** @internal */
412412
moduleTypeClassifier: ModuleTypeClassifier;
413+
/** @internal */
414+
addDiagnosticFilter(filter: DiagnosticFilter): void;
413415
}
414416

415417
/**
@@ -419,6 +421,16 @@ export interface Service {
419421
*/
420422
export type Register = Service;
421423

424+
/** @internal */
425+
export interface DiagnosticFilter {
426+
/** if true, filter applies to all files */
427+
appliesToAllFiles: boolean;
428+
/** Filter applies onto to these filenames. Only used if appliesToAllFiles is false */
429+
filenamesAbsolute: string[];
430+
/** these diagnostic codes are ignored */
431+
diagnosticsIgnored: number[];
432+
}
433+
422434
/** @internal */
423435
export function getExtensions(config: _ts.ParsedCommandLine) {
424436
const tsExtensions = ['.ts'];
@@ -516,16 +528,22 @@ export function create(rawOptions: CreateOptions = {}): Service {
516528
const transpileOnly =
517529
options.transpileOnly === true && options.typeCheck !== true;
518530
const transformers = options.transformers || undefined;
519-
const ignoreDiagnostics = [
520-
6059, // "'rootDir' is expected to contain all source files."
521-
18002, // "The 'files' list in config file is empty."
522-
18003, // "No inputs were found in config file."
523-
...(options.ignoreDiagnostics || []),
524-
].map(Number);
531+
const diagnosticFilters: Array<DiagnosticFilter> = [
532+
{
533+
appliesToAllFiles: true,
534+
filenamesAbsolute: [],
535+
diagnosticsIgnored: [
536+
6059, // "'rootDir' is expected to contain all source files."
537+
18002, // "The 'files' list in config file is empty."
538+
18003, // "No inputs were found in config file."
539+
...(options.ignoreDiagnostics || []),
540+
].map(Number),
541+
},
542+
];
525543

526544
const configDiagnosticList = filterDiagnostics(
527545
config.errors,
528-
ignoreDiagnostics
546+
diagnosticFilters
529547
);
530548
const outputCache = new Map<
531549
string,
@@ -804,7 +822,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
804822

805823
const diagnosticList = filterDiagnostics(
806824
diagnostics,
807-
ignoreDiagnostics
825+
diagnosticFilters
808826
);
809827
if (diagnosticList.length) reportTSError(diagnosticList);
810828

@@ -963,7 +981,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
963981
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
964982
const diagnosticList = filterDiagnostics(
965983
diagnostics,
966-
ignoreDiagnostics
984+
diagnosticFilters
967985
);
968986
if (diagnosticList.length) reportTSError(diagnosticList);
969987

@@ -1079,7 +1097,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
10791097

10801098
const diagnosticList = filterDiagnostics(
10811099
result.diagnostics || [],
1082-
ignoreDiagnostics
1100+
diagnosticFilters
10831101
);
10841102
if (diagnosticList.length) reportTSError(diagnosticList);
10851103

@@ -1141,6 +1159,10 @@ export function create(rawOptions: CreateOptions = {}): Service {
11411159
return true;
11421160
};
11431161

1162+
function addDiagnosticFilter(filter: DiagnosticFilter) {
1163+
diagnosticFilters.push(filter);
1164+
}
1165+
11441166
return {
11451167
ts,
11461168
config,
@@ -1151,6 +1173,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
11511173
options,
11521174
configFilePath,
11531175
moduleTypeClassifier,
1176+
addDiagnosticFilter,
11541177
};
11551178
}
11561179

@@ -1306,9 +1329,16 @@ function updateSourceMap(sourceMapText: string, fileName: string) {
13061329
*/
13071330
function filterDiagnostics(
13081331
diagnostics: readonly _ts.Diagnostic[],
1309-
ignore: number[]
1332+
filters: DiagnosticFilter[]
13101333
) {
1311-
return diagnostics.filter((x) => ignore.indexOf(x.code) === -1);
1334+
return diagnostics.filter((d) =>
1335+
filters.every(
1336+
(f) =>
1337+
(!f.appliesToAllFiles &&
1338+
f.filenamesAbsolute.indexOf(d.file?.fileName!) === -1) ||
1339+
f.diagnosticsIgnored.indexOf(d.code) === -1
1340+
)
1341+
);
13121342
}
13131343

13141344
/**

src/repl.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export interface CreateReplOptions {
5959
stderr?: NodeJS.WritableStream;
6060
/** @internal */
6161
composeWithEvalAwarePartialHost?: EvalAwarePartialHost;
62+
/**
63+
* @internal
64+
* Ignore diagnostics that are annoying when interactively entering input line-by-line.
65+
*/
66+
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl?: boolean;
6267
}
6368

6469
/**
@@ -86,6 +91,7 @@ export function createRepl(options: CreateReplOptions = {}) {
8691
stdout === process.stdout && stderr === process.stderr
8792
? console
8893
: new Console(stdout, stderr);
94+
const { ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl = true } = options;
8995

9096
const replService: ReplService = {
9197
state: options.state ?? new EvalState(join(process.cwd(), EVAL_FILENAME)),
@@ -103,6 +109,17 @@ export function createRepl(options: CreateReplOptions = {}) {
103109

104110
function setService(_service: Service) {
105111
service = _service;
112+
if (ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl) {
113+
service.addDiagnosticFilter({
114+
appliesToAllFiles: false,
115+
filenamesAbsolute: [state.path],
116+
diagnosticsIgnored: [
117+
2393, // Duplicate function implementation: https://github.com/TypeStrong/ts-node/issues/729
118+
6133, // <identifier> is declared but its value is never read. https://github.com/TypeStrong/ts-node/issues/850
119+
7027, // Unreachable code detected. https://github.com/TypeStrong/ts-node/issues/469
120+
],
121+
});
122+
}
106123
}
107124

108125
function evalCode(code: string) {

0 commit comments

Comments
 (0)