Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2890,10 +2890,23 @@ 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"));
}
}
}
};
const json = convertConfigFileToObject(sourceFile, errors, /*reportOptionsErrors*/ true, optionsIterator);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4918,6 +4918,10 @@
"category": "Message",
"code": 6257
},
"'{0}' should be set inside the 'compilerOptions' object of the tsconfig.json": {
"category": "Error",
"code": 6258
},

"Projects to reference": {
"category": "Message",
Expand Down
41 changes: 41 additions & 0 deletions src/testRunner/unittests/config/convertCompilerOptionsFromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,47 @@ namespace ts {
});
});


it("raises an error if you've set module to a correct string in the root", () => {
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.",
file: undefined,
start: 0,
length: 0
}]
});
});

it("raises an error if you've set target to a correct string in the root", () => {
assertCompilerOptionsWithJsonText(`{
"target": "esnext",
"compilerOptions": {
"module": "esnext"
}
}`, "tsconfig.json", {
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
}]
});
});

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