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

This commit is contained in:
Sheetal Nandi 2023-03-20 13:26:04 -07:00 committed by GitHub
parent 79a414bb5e
commit 8814f6da48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 11 deletions

View File

@ -3170,6 +3170,7 @@ function getExtendsConfigPathOrArray(
basePath: string,
configFileName: string | undefined,
errors: Diagnostic[],
propertyAssignment?: PropertyAssignment,
valueExpression?: Expression,
sourceFile?: JsonSourceFile,
) {
@ -3200,12 +3201,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;
}
@ -3248,7 +3249,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;
@ -3275,7 +3276,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") {
@ -3458,18 +3459,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);
@ -3530,10 +3536,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);
}
/**

View File

@ -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",

View File

@ -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",

View File

@ -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.

View File

@ -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}}
   ~~~~~~

File diff suppressed because one or more lines are too long