Skip to content

Commit 8814f6d

Browse files
authored
Report error if commanline only option is specified in tsconfig (#53397)
1 parent 79a414b commit 8814f6d

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

src/compiler/commandLineParser.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,6 +3170,7 @@ function getExtendsConfigPathOrArray(
31703170
basePath: string,
31713171
configFileName: string | undefined,
31723172
errors: Diagnostic[],
3173+
propertyAssignment?: PropertyAssignment,
31733174
valueExpression?: Expression,
31743175
sourceFile?: JsonSourceFile,
31753176
) {
@@ -3200,12 +3201,12 @@ function getExtendsConfigPathOrArray(
32003201
));
32013202
}
32023203
else {
3203-
convertJsonOption(extendsOptionDeclaration.element, value, basePath, errors, (valueExpression as ArrayLiteralExpression | undefined)?.elements[index], sourceFile);
3204+
convertJsonOption(extendsOptionDeclaration.element, value, basePath, errors, propertyAssignment, (valueExpression as ArrayLiteralExpression | undefined)?.elements[index], sourceFile);
32043205
}
32053206
}
32063207
}
32073208
else {
3208-
convertJsonOption(extendsOptionDeclaration, value, basePath, errors, valueExpression, sourceFile);
3209+
convertJsonOption(extendsOptionDeclaration, value, basePath, errors, propertyAssignment, valueExpression, sourceFile);
32093210
}
32103211
return extendedConfigPath;
32113212
}
@@ -3248,7 +3249,7 @@ function parseOwnConfigOfJsonSourceFile(
32483249
option: CommandLineOption | undefined,
32493250
) {
32503251
// Ensure value is verified except for extends which is handled in its own way for error reporting
3251-
if (option && option !== extendsOptionDeclaration) value = convertJsonOption(option, value, basePath, errors, propertyAssignment.initializer, sourceFile);
3252+
if (option && option !== extendsOptionDeclaration) value = convertJsonOption(option, value, basePath, errors, propertyAssignment, propertyAssignment.initializer, sourceFile);
32523253
if (parentOption?.name) {
32533254
if (option) {
32543255
let currentOption;
@@ -3275,7 +3276,7 @@ function parseOwnConfigOfJsonSourceFile(
32753276
}
32763277
else if (parentOption === rootOptions) {
32773278
if (option === extendsOptionDeclaration) {
3278-
extendedConfigPath = getExtendsConfigPathOrArray(value, host, basePath, configFileName, errors, propertyAssignment.initializer, sourceFile);
3279+
extendedConfigPath = getExtendsConfigPathOrArray(value, host, basePath, configFileName, errors, propertyAssignment, propertyAssignment.initializer, sourceFile);
32793280
}
32803281
else if (!option) {
32813282
if (keyText === "excludes") {
@@ -3458,18 +3459,23 @@ export function convertJsonOption(
34583459
value: any,
34593460
basePath: string,
34603461
errors: Diagnostic[],
3462+
propertyAssignment?: PropertyAssignment,
34613463
valueExpression?: Expression,
34623464
sourceFile?: TsConfigSourceFile,
34633465
): CompilerOptionsValue {
3466+
if (opt.isCommandLineOnly) {
3467+
errors.push(createDiagnosticForNodeInSourceFileOrCompilerDiagnostic(sourceFile, propertyAssignment?.name, Diagnostics.Option_0_can_only_be_specified_on_command_line, opt.name));
3468+
return undefined;
3469+
}
34643470
if (isCompilerOptionsValue(opt, value)) {
34653471
const optType = opt.type;
34663472
if ((optType === "list") && isArray(value)) {
3467-
return convertJsonOptionOfListType(opt, value, basePath, errors, valueExpression as ArrayLiteralExpression | undefined, sourceFile);
3473+
return convertJsonOptionOfListType(opt, value, basePath, errors, propertyAssignment, valueExpression as ArrayLiteralExpression | undefined, sourceFile);
34683474
}
34693475
else if (optType === "listOrElement") {
34703476
return isArray(value) ?
3471-
convertJsonOptionOfListType(opt, value, basePath, errors, valueExpression as ArrayLiteralExpression | undefined, sourceFile) :
3472-
convertJsonOption(opt.element, value, basePath, errors, valueExpression, sourceFile);
3477+
convertJsonOptionOfListType(opt, value, basePath, errors, propertyAssignment, valueExpression as ArrayLiteralExpression | undefined, sourceFile) :
3478+
convertJsonOption(opt.element, value, basePath, errors, propertyAssignment, valueExpression, sourceFile);
34733479
}
34743480
else if (!isString(opt.type)) {
34753481
return convertJsonOptionOfCustomType(opt as CommandLineOptionOfCustomType, value as string, errors, valueExpression, sourceFile);
@@ -3530,10 +3536,11 @@ function convertJsonOptionOfListType(
35303536
values: readonly any[],
35313537
basePath: string,
35323538
errors: Diagnostic[],
3539+
propertyAssignment: PropertyAssignment | undefined,
35333540
valueExpression: ArrayLiteralExpression | undefined,
35343541
sourceFile: TsConfigSourceFile | undefined,
35353542
): any[] {
3536-
return filter(map(values, (v, index) => convertJsonOption(option.element, v, basePath, errors, valueExpression?.elements[index], sourceFile)), v => option.listPreserveFalsyValues ? true : !!v);
3543+
return filter(map(values, (v, index) => convertJsonOption(option.element, v, basePath, errors, propertyAssignment, valueExpression?.elements[index], sourceFile)), v => option.listPreserveFalsyValues ? true : !!v);
35373544
}
35383545

35393546
/**

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5270,6 +5270,10 @@
52705270
"category": "Message",
52715271
"code": 6265
52725272
},
5273+
"Option '{0}' can only be specified on command line.": {
5274+
"category": "Error",
5275+
"code": 6266
5276+
},
52735277

52745278
"Directory '{0}' has no containing package.json scope. Imports will not resolve.": {
52755279
"category": "Message",

src/testRunner/unittests/config/tsconfigParsing.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@ describe("unittests:: config:: tsconfigParsing:: parseConfigFileTextToJson", ()
342342
allFileList: ["/apath/a.ts"],
343343
}]);
344344

345+
baselinedParsed("generates errors when commandline option is in tsconfig", () => [{
346+
jsonText: JSON.stringify({
347+
compilerOptions: {
348+
help: true,
349+
}
350+
}),
351+
configFileName: "/apath/tsconfig.json",
352+
basePath: "tests/cases/unittests",
353+
allFileList: ["/apath/a.ts"],
354+
}]);
355+
345356
function baselineWildcards(subScenario: string, scenario: () => { configFileName: string, jsonText: string, basePath: string }[]) {
346357
baselineParseConfig({
347358
scenario: "tsconfigParsing",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Fs::
2+
//// [/apath/a.ts]
3+
4+
5+
//// [/apath/tsconfig.json]
6+
{"compilerOptions":{"help":true}}
7+
8+
9+
configFileName:: /apath/tsconfig.json
10+
FileNames::
11+
/apath/a.ts
12+
Errors::
13+
error TS6266: Option 'help' can only be specified on command line.
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Fs::
2+
//// [/apath/a.ts]
3+
4+
5+
//// [/apath/tsconfig.json]
6+
{"compilerOptions":{"help":true}}
7+
8+
9+
configFileName:: /apath/tsconfig.json
10+
FileNames::
11+
/apath/a.ts
12+
Errors::
13+
/apath/tsconfig.json:1:21 - error TS6266: Option 'help' can only be specified on command line.
14+
15+
1 {"compilerOptions":{"help":true}}
16+
   ~~~~~~
17+

0 commit comments

Comments
 (0)