Skip to content

Report error if commanline only option is specified in tsconfig #53397

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 1 commit into from
Mar 20, 2023
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
23 changes: 15 additions & 8 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,7 @@ function getExtendsConfigPathOrArray(
basePath: string,
configFileName: string | undefined,
errors: Diagnostic[],
propertyAssignment?: PropertyAssignment,
valueExpression?: Expression,
sourceFile?: JsonSourceFile,
) {
Expand Down Expand Up @@ -3199,12 +3200,12 @@ function getExtendsConfigPathOrArray(
));
}
else {
convertJsonOption(extendsOptionDeclaration.element, value, basePath, errors, (valueExpression as ArrayLiteralExpression | undefined)?.elements[index], sourceFile);
convertJsonOption(extendsOptionDeclaration.element, value, basePath, errors, propertyAssignment, (valueExpression as ArrayLiteralExpression | undefined)?.elements[index], sourceFile);
}
}
}
else {
convertJsonOption(extendsOptionDeclaration, value, basePath, errors, valueExpression, sourceFile);
convertJsonOption(extendsOptionDeclaration, value, basePath, errors, propertyAssignment, valueExpression, sourceFile);
}
return extendedConfigPath;
}
Expand Down Expand Up @@ -3247,7 +3248,7 @@ function parseOwnConfigOfJsonSourceFile(
option: CommandLineOption | undefined,
) {
// Ensure value is verified except for extends which is handled in its own way for error reporting
if (option && option !== extendsOptionDeclaration) value = convertJsonOption(option, value, basePath, errors, propertyAssignment.initializer, sourceFile);
if (option && option !== extendsOptionDeclaration) value = convertJsonOption(option, value, basePath, errors, propertyAssignment, propertyAssignment.initializer, sourceFile);
if (parentOption?.name) {
if (option) {
let currentOption;
Expand All @@ -3274,7 +3275,7 @@ function parseOwnConfigOfJsonSourceFile(
}
else if (parentOption === rootOptions) {
if (option === extendsOptionDeclaration) {
extendedConfigPath = getExtendsConfigPathOrArray(value, host, basePath, configFileName, errors, propertyAssignment.initializer, sourceFile);
extendedConfigPath = getExtendsConfigPathOrArray(value, host, basePath, configFileName, errors, propertyAssignment, propertyAssignment.initializer, sourceFile);
}
else if (!option) {
if (keyText === "excludes") {
Expand Down Expand Up @@ -3457,18 +3458,23 @@ export function convertJsonOption(
value: any,
basePath: string,
errors: Diagnostic[],
propertyAssignment?: PropertyAssignment,
valueExpression?: Expression,
sourceFile?: TsConfigSourceFile,
): CompilerOptionsValue {
if (opt.isCommandLineOnly) {
errors.push(createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, propertyAssignment?.name, Diagnostics.Option_0_can_only_be_specified_on_command_line, opt.name));
return undefined;
}
if (isCompilerOptionsValue(opt, value)) {
const optType = opt.type;
if ((optType === "list") && isArray(value)) {
return convertJsonOptionOfListType(opt, value, basePath, errors, valueExpression as ArrayLiteralExpression | undefined, sourceFile);
return convertJsonOptionOfListType(opt, value, basePath, errors, propertyAssignment, valueExpression as ArrayLiteralExpression | undefined, sourceFile);
}
else if (optType === "listOrElement") {
return isArray(value) ?
convertJsonOptionOfListType(opt, value, basePath, errors, valueExpression as ArrayLiteralExpression | undefined, sourceFile) :
convertJsonOption(opt.element, value, basePath, errors, valueExpression, sourceFile);
convertJsonOptionOfListType(opt, value, basePath, errors, propertyAssignment, valueExpression as ArrayLiteralExpression | undefined, sourceFile) :
convertJsonOption(opt.element, value, basePath, errors, propertyAssignment, valueExpression, sourceFile);
}
else if (!isString(opt.type)) {
return convertJsonOptionOfCustomType(opt as CommandLineOptionOfCustomType, value as string, errors, valueExpression, sourceFile);
Expand Down Expand Up @@ -3529,10 +3535,11 @@ function convertJsonOptionOfListType(
values: readonly any[],
basePath: string,
errors: Diagnostic[],
propertyAssignment: PropertyAssignment | undefined,
valueExpression: ArrayLiteralExpression | undefined,
sourceFile: TsConfigSourceFile | undefined,
): any[] {
return filter(map(values, (v, index) => convertJsonOption(option.element, v, basePath, errors, valueExpression?.elements[index], sourceFile)), v => option.listPreserveFalsyValues ? true : !!v);
return filter(map(values, (v, index) => convertJsonOption(option.element, v, basePath, errors, propertyAssignment, valueExpression?.elements[index], sourceFile)), v => option.listPreserveFalsyValues ? true : !!v);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5270,6 +5270,10 @@
"category": "Message",
"code": 6265
},
"Option '{0}' can only be specified on command line.": {
"category": "Error",
"code": 6266
},

"Directory '{0}' has no containing package.json scope. Imports will not resolve.": {
"category": "Message",
Expand Down
11 changes: 11 additions & 0 deletions src/testRunner/unittests/config/tsconfigParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ describe("unittests:: config:: tsconfigParsing:: parseConfigFileTextToJson", ()
allFileList: ["/apath/a.ts"],
}]);

baselinedParsed("generates errors when commandline option is in tsconfig", () => [{
jsonText: JSON.stringify({
compilerOptions: {
help: true,
}
}),
configFileName: "/apath/tsconfig.json",
basePath: "tests/cases/unittests",
allFileList: ["/apath/a.ts"],
}]);

function baselineWildcards(subScenario: string, scenario: () => { configFileName: string, jsonText: string, basePath: string }[]) {
baselineParseConfig({
scenario: "tsconfigParsing",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Fs::
//// [/apath/a.ts]


//// [/apath/tsconfig.json]
{"compilerOptions":{"help":true}}


configFileName:: /apath/tsconfig.json
FileNames::
/apath/a.ts
Errors::
error TS6266: Option 'help' can only be specified on command line.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Fs::
//// [/apath/a.ts]


//// [/apath/tsconfig.json]
{"compilerOptions":{"help":true}}


configFileName:: /apath/tsconfig.json
FileNames::
/apath/a.ts
Errors::
/apath/tsconfig.json:1:21 - error TS6266: Option 'help' can only be specified on command line.

1 {"compilerOptions":{"help":true}}
   ~~~~~~

Loading