Skip to content

Refresh semantic diagnostics when compiler options affecting semantic diagnostics generation changes #26200

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

Merged
merged 4 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ namespace ts {
}
state.changedFilesSet = createMap<true>();
const useOldState = BuilderState.canReuseOldState(state.referencedMap, oldState);
const canCopySemanticDiagnostics = useOldState && oldState!.semanticDiagnosticsPerFile && !!state.semanticDiagnosticsPerFile;
const canCopySemanticDiagnostics = useOldState && oldState!.semanticDiagnosticsPerFile && !!state.semanticDiagnosticsPerFile &&
!compilerOptionsAffectSemanticDiagnostics(compilerOptions, oldState!.program.getCompilerOptions());
if (useOldState) {
// Verify the sanity of old state
if (!oldState!.currentChangedFilePath) {
Expand Down
20 changes: 19 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,41 +319,47 @@ namespace ts {
{
name: "noImplicitAny",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type
},
{
name: "strictNullChecks",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_null_checks
},
{
name: "strictFunctionTypes",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_checking_of_function_types
},
{
name: "strictPropertyInitialization",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_checking_of_property_initialization_in_classes
},
{
name: "noImplicitThis",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Raise_error_on_this_expressions_with_an_implied_any_type,
},
{
name: "alwaysStrict",
type: "boolean",
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file
Expand All @@ -363,27 +369,31 @@ namespace ts {
{
name: "noUnusedLocals",
type: "boolean",
affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Report_errors_on_unused_locals,
},
{
name: "noUnusedParameters",
type: "boolean",
affectsSemanticDiagnostics: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What determines whether affectsSemanticDiagnostics is true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flags is true if changing value of the option, affects the semantic diagnostics reported(more/less) even if there is no change in file content. ( in turn whether to we need to throw away semantic diagnostics from cache(in --w mode) ) Eg. --noUnusedLocals can report more errors when turned on so it will have the flag true

Copy link
Member

@sandersn sandersn Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems easy to forget to set this when adding a new flag. Would making it required help? It would be annoying then, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I don't think we want this required.

showInSimplifiedHelpView: true,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Report_errors_on_unused_parameters,
},
{
name: "noImplicitReturns",
type: "boolean",
affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Report_error_when_not_all_code_paths_in_function_return_a_value
},
{
name: "noFallthroughCasesInSwitch",
type: "boolean",
affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Report_errors_for_fallthrough_cases_in_switch_statement
Expand Down Expand Up @@ -455,12 +465,14 @@ namespace ts {
{
name: "allowSyntheticDefaultImports",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
},
{
name: "esModuleInterop",
type: "boolean",
affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.Enables_emit_interoperability_between_CommonJS_and_ES_Modules_via_creation_of_namespace_objects_for_all_imports_Implies_allowSyntheticDefaultImports
Expand Down Expand Up @@ -640,6 +652,7 @@ namespace ts {
{
name: "noImplicitUseStrict",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output
},
Expand Down Expand Up @@ -678,24 +691,28 @@ namespace ts {
{
name: "allowUnusedLabels",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Do_not_report_errors_on_unused_labels
},
{
name: "allowUnreachableCode",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Do_not_report_errors_on_unreachable_code
},
{
name: "suppressExcessPropertyErrors",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Suppress_excess_property_checks_for_object_literals,
},
{
name: "suppressImplicitAnyIndexErrors",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures,
},
Expand All @@ -714,6 +731,7 @@ namespace ts {
{
name: "noStrictGenericChecks",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Disable_strict_checking_of_generic_signatures_in_function_types,
},
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4540,6 +4540,8 @@ namespace ts {
isCommandLineOnly?: boolean;
showInSimplifiedHelpView?: boolean;
category?: DiagnosticMessage;
strictFlag?: true; // true if the option is one of the flag under strict
affectsSemanticDiagnostics?: true; // true if option affects semantic diagnostics
}

/* @internal */
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6967,6 +6967,15 @@ namespace ts {
return compilerOptions[flag] === undefined ? !!compilerOptions.strict : !!compilerOptions[flag];
}

export function compilerOptionsAffectSemanticDiagnostics(newOptions: CompilerOptions, oldOptions: CompilerOptions) {
if (oldOptions === newOptions) {
return false;
}

return optionDeclarations.some(option => (!!option.strictFlag && getStrictOptionValue(newOptions, option.name as StrictOptionName) !== getStrictOptionValue(oldOptions, option.name as StrictOptionName)) ||
(!!option.affectsSemanticDiagnostics && !newOptions[option.name] !== !oldOptions[option.name]));
}

export function hasZeroOrOneAsteriskCharacter(str: string): boolean {
let seenAsterisk = false;
for (let i = 0; i < str.length; i++) {
Expand Down
44 changes: 43 additions & 1 deletion src/testRunner/unittests/tscWatchMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,6 @@ namespace ts.tscWatch {
}

it("without outDir or outFile is specified", () => {
debugger;
verifyWithOptions({ module: ModuleKind.AMD }, ["file1.js", "src/file2.js"]);
});

Expand Down Expand Up @@ -1313,6 +1312,49 @@ export class B
// File a need not be rewritten
assert.equal(host.getModifiedTime(`${currentDirectory}/a.js`), modifiedTimeOfAJs);
});

it("updates errors when strictNullChecks changes", () => {
const currentDirectory = "/user/username/projects/myproject";
const aFile: File = {
path: `${currentDirectory}/a.ts`,
content: `declare function foo(): null | { hello: any };
foo().hello`
};
const compilerOptions: CompilerOptions = {
};
const config: File = {
path: `${currentDirectory}/tsconfig.json`,
content: JSON.stringify({ compilerOptions })
};
const files = [aFile, config, libFile];
const host = createWatchedSystem(files, { currentDirectory });
const watch = createWatchOfConfigFile("tsconfig.json", host);
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
checkOutputErrorsInitial(host, emptyArray);
const modifiedTimeOfAJs = host.getModifiedTime(`${currentDirectory}/a.js`);
compilerOptions.strictNullChecks = true;
host.writeFile(config.path, JSON.stringify({ compilerOptions }));
host.runQueuedTimeoutCallbacks();
const expectedStrictNullErrors = [
getDiagnosticOfFileFromProgram(watch(), aFile.path, aFile.content.lastIndexOf("foo()"), 5, Diagnostics.Object_is_possibly_null)
];
checkOutputErrorsIncremental(host, expectedStrictNullErrors);
// File a need not be rewritten
assert.equal(host.getModifiedTime(`${currentDirectory}/a.js`), modifiedTimeOfAJs);
compilerOptions.strict = true;
delete (compilerOptions.strictNullChecks);
host.writeFile(config.path, JSON.stringify({ compilerOptions }));
host.runQueuedTimeoutCallbacks();
checkOutputErrorsIncremental(host, expectedStrictNullErrors);
// File a need not be rewritten
assert.equal(host.getModifiedTime(`${currentDirectory}/a.js`), modifiedTimeOfAJs);
delete (compilerOptions.strict);
host.writeFile(config.path, JSON.stringify({ compilerOptions }));
host.runQueuedTimeoutCallbacks();
checkOutputErrorsIncremental(host, emptyArray);
// File a need not be rewritten
assert.equal(host.getModifiedTime(`${currentDirectory}/a.js`), modifiedTimeOfAJs);
});
});

describe("tsc-watch emit with outFile or out setting", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9333,7 +9333,7 @@ export function Test2() {
textSpan: protocolTextSpanFromSubstring(userTs.content, "fnA", { index: 1 }),
definitions: [protocolFileSpanFromSubstring(aTs, "fnA")],
});
checkNumberOfProjects(session.getProjectService(), { configuredProjects: 1 }); debugger;
checkNumberOfProjects(session.getProjectService(), { configuredProjects: 1 });
verifyUserTsConfigProject(session);

// Navigate to the definition
Expand Down