Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set of extra ignored diagnostics for REPL mode #1396

Merged
merged 9 commits into from Jul 21, 2021
7 changes: 2 additions & 5 deletions src/bin.ts
Expand Up @@ -197,11 +197,6 @@ export function main(
let evalStuff: VirtualFileState | undefined;
let replStuff: VirtualFileState | undefined;
let stdinStuff: VirtualFileState | undefined;
// let evalService: ReplService | undefined;
// let replState: EvalState | undefined;
// let replService: ReplService | undefined;
// let stdinState: EvalState | undefined;
// let stdinService: ReplService | undefined;
let evalAwarePartialHost: EvalAwarePartialHost | undefined = undefined;
if (executeEval) {
const state = new EvalState(join(cwd, EVAL_FILENAME));
Expand All @@ -210,6 +205,7 @@ export function main(
repl: createRepl({
state,
composeWithEvalAwarePartialHost: evalAwarePartialHost,
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl: false,
}),
};
({ evalAwarePartialHost } = evalStuff.repl);
Expand All @@ -225,6 +221,7 @@ export function main(
repl: createRepl({
state,
composeWithEvalAwarePartialHost: evalAwarePartialHost,
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl: false,
}),
};
({ evalAwarePartialHost } = stdinStuff.repl);
Expand Down
54 changes: 42 additions & 12 deletions src/index.ts
Expand Up @@ -410,6 +410,8 @@ export interface Service {
configFilePath: string | undefined;
/** @internal */
moduleTypeClassifier: ModuleTypeClassifier;
/** @internal */
addDiagnosticFilter(filter: DiagnosticFilter): void;
}

/**
Expand All @@ -419,6 +421,16 @@ export interface Service {
*/
export type Register = Service;

/** @internal */
export interface DiagnosticFilter {
/** if true, filter applies to all files */
appliesToAllFiles: boolean;
/** Filter applies onto to these filenames. Only used if appliesToAllFiles is false */
filenamesAbsolute: string[];
/** these diagnostic codes are ignored */
diagnosticsIgnored: number[];
}

/** @internal */
export function getExtensions(config: _ts.ParsedCommandLine) {
const tsExtensions = ['.ts'];
Expand Down Expand Up @@ -516,16 +528,22 @@ export function create(rawOptions: CreateOptions = {}): Service {
const transpileOnly =
options.transpileOnly === true && options.typeCheck !== true;
const transformers = options.transformers || undefined;
const ignoreDiagnostics = [
6059, // "'rootDir' is expected to contain all source files."
18002, // "The 'files' list in config file is empty."
18003, // "No inputs were found in config file."
...(options.ignoreDiagnostics || []),
].map(Number);
const diagnosticFilters: Array<DiagnosticFilter> = [
{
appliesToAllFiles: true,
filenamesAbsolute: [],
diagnosticsIgnored: [
6059, // "'rootDir' is expected to contain all source files."
18002, // "The 'files' list in config file is empty."
18003, // "No inputs were found in config file."
...(options.ignoreDiagnostics || []),
].map(Number),
},
];

const configDiagnosticList = filterDiagnostics(
config.errors,
ignoreDiagnostics
diagnosticFilters
);
const outputCache = new Map<
string,
Expand Down Expand Up @@ -804,7 +822,7 @@ export function create(rawOptions: CreateOptions = {}): Service {

const diagnosticList = filterDiagnostics(
diagnostics,
ignoreDiagnostics
diagnosticFilters
);
if (diagnosticList.length) reportTSError(diagnosticList);

Expand Down Expand Up @@ -963,7 +981,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
const diagnosticList = filterDiagnostics(
diagnostics,
ignoreDiagnostics
diagnosticFilters
);
if (diagnosticList.length) reportTSError(diagnosticList);

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

const diagnosticList = filterDiagnostics(
result.diagnostics || [],
ignoreDiagnostics
diagnosticFilters
);
if (diagnosticList.length) reportTSError(diagnosticList);

Expand Down Expand Up @@ -1141,6 +1159,10 @@ export function create(rawOptions: CreateOptions = {}): Service {
return true;
};

function addDiagnosticFilter(filter: DiagnosticFilter) {
diagnosticFilters.push(filter);
}

return {
ts,
config,
Expand All @@ -1151,6 +1173,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
options,
configFilePath,
moduleTypeClassifier,
addDiagnosticFilter,
};
}

Expand Down Expand Up @@ -1306,9 +1329,16 @@ function updateSourceMap(sourceMapText: string, fileName: string) {
*/
function filterDiagnostics(
diagnostics: readonly _ts.Diagnostic[],
ignore: number[]
filters: DiagnosticFilter[]
) {
return diagnostics.filter((x) => ignore.indexOf(x.code) === -1);
return diagnostics.filter((d) =>
filters.every(
(f) =>
(!f.appliesToAllFiles &&
f.filenamesAbsolute.indexOf(d.file?.fileName!) === -1) ||
f.diagnosticsIgnored.indexOf(d.code) === -1
)
);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/repl.ts
Expand Up @@ -59,6 +59,11 @@ export interface CreateReplOptions {
stderr?: NodeJS.WritableStream;
/** @internal */
composeWithEvalAwarePartialHost?: EvalAwarePartialHost;
/**
* @internal
* Ignore diagnostics that are annoying when interactively entering input line-by-line.
*/
ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl?: boolean;
}

/**
Expand Down Expand Up @@ -86,6 +91,7 @@ export function createRepl(options: CreateReplOptions = {}) {
stdout === process.stdout && stderr === process.stderr
? console
: new Console(stdout, stderr);
const { ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl = true } = options;

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

function setService(_service: Service) {
service = _service;
if (ignoreDiagnosticsThatAreAnnoyingInInteractiveRepl) {
service.addDiagnosticFilter({
appliesToAllFiles: false,
filenamesAbsolute: [state.path],
diagnosticsIgnored: [
2393, // Duplicate function implementation: https://github.com/TypeStrong/ts-node/issues/729
6133, // <identifier> is declared but its value is never read. https://github.com/TypeStrong/ts-node/issues/850
7027, // Unreachable code detected. https://github.com/TypeStrong/ts-node/issues/469
],
});
}
}

function evalCode(code: string) {
Expand Down