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

Add support for raising if you set a tsconfig entry of target/module with the right setting in the root #44964

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2852,6 +2852,7 @@ namespace ts {
let typeAcquisition: TypeAcquisition | undefined, typingOptionstypeAcquisition: TypeAcquisition | undefined;
let watchOptions: WatchOptions | undefined;
let extendedConfigPath: string | undefined;
const rootCompilerOptions: PropertyName[] = [];
orta marked this conversation as resolved.
Show resolved Hide resolved

const optionsIterator: JsonConversionNotifier = {
onSetValidOptionKeyValueInParent(parentOption: string, option: CommandLineOption, value: CompilerOptionsValue) {
Expand Down Expand Up @@ -2890,22 +2891,12 @@ namespace ts {
return;
}
},
onSetUnknownOptionKeyValueInRoot(key: string, keyNode: PropertyName, value: CompilerOptionsValue, _valueNode: Expression) {
onSetUnknownOptionKeyValueInRoot(key: string, keyNode: PropertyName, _value: CompilerOptionsValue, _valueNode: Expression) {
if (key === "excludes") {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, keyNode, Diagnostics.Unknown_option_excludes_Did_you_mean_exclude));
}
if (key === "target") {
const possibleValidEntries = arrayFrom(targetOptionDeclaration.type.keys());
if (contains(possibleValidEntries, value)) {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, keyNode, Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_tsconfig_json, "target"));
}
}
if (key === "module") {
const moduleOption = commandOptionsWithoutBuild.find(opt => opt.name === "module")!;
const possibleValidEntries = arrayFrom((moduleOption.type as Map<string>).keys());
if (contains(possibleValidEntries, value)) {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, keyNode, Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_tsconfig_json, "module"));
}
if (find(commandOptionsWithoutBuild, (opt) => opt.name === key)) {
rootCompilerOptions.push(keyNode);
orta marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
Expand All @@ -2926,6 +2917,11 @@ namespace ts {
}
}

// eslint-disable-next-line no-in-operator
if (rootCompilerOptions.length && json && !("compilerOptions" in json)) {
orta marked this conversation as resolved.
Show resolved Hide resolved
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, rootCompilerOptions[0], Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_config_json_file, getTextOfPropertyName(rootCompilerOptions[0]) as string));
}

return { raw: json, options, watchOptions, typeAcquisition, extendedConfigPath };
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4918,7 +4918,7 @@
"category": "Message",
"code": 6257
},
"'{0}' should be set inside the 'compilerOptions' object of the tsconfig.json": {
"'{0}' should be set inside the 'compilerOptions' object of the config json file": {
"category": "Error",
"code": 6258
},
Expand Down
22 changes: 6 additions & 16 deletions src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,28 +663,24 @@ namespace ts {
});
});


it("raises an error if you've set module to a correct string in the root", () => {
it("raises an error if you've set a compiler flag in the root without including 'compilerOptions'", () => {
assertCompilerOptionsWithJsonText(`{
"module": "esnext",
"compilerOptions": {
"target": "esnext"
}
}`, "tsconfig.json", {
compilerOptions: {
target: ScriptTarget.ESNext
},
errors: [{
...Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_tsconfig_json,
messageText: "'module' should be set inside the 'compilerOptions' object of the tsconfig.json.",
...Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_config_json_file,
messageText: "'module' should be set inside the 'compilerOptions' object of the config json file.",
file: undefined,
start: 0,
length: 0
}]
});
});

it("raises an error if you've set target to a correct string in the root", () => {
it("does not raise an error if you've set a compiler flag in the root when you have included 'compilerOptions'", () => {
assertCompilerOptionsWithJsonText(`{
"target": "esnext",
"compilerOptions": {
Expand All @@ -694,17 +690,11 @@ namespace ts {
compilerOptions: {
module: ModuleKind.ESNext
},
errors: [{
...Diagnostics._0_should_be_set_inside_the_compilerOptions_object_of_the_tsconfig_json,
messageText: "'target' should be set inside the 'compilerOptions' object of the tsconfig.json.",
file: undefined,
start: 0,
length: 0
}]
errors: []
});
});

it("Don't crash when root expression is not objecty at all", () => {
it("Don't crash when root expression is not object at all", () => {
assertCompilerOptionsWithJsonText(`42`, "tsconfig.json", {
compilerOptions: {},
errors: [{
Expand Down