Skip to content

Commit d07761f

Browse files
authored
Allow --composite false or --composite null on the command line (#36997)
* Add tests for specifying composite as command line option * Allow passing --composite false on commandline * Add test to verify tsc --composite false from command line * Handle "undefined" as option value to be set to undefined for that option * Support "null" as option to be converted to undefined which is normally end result from our config file as well * Support null as option for any tsconfig only option as well, and dont support undefined * Fix public api test case * Validates objects instead of stringify result * Add composite true to base source
1 parent 05c9ec3 commit d07761f

9 files changed

+384
-48
lines changed

src/compiler/commandLineParser.ts

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,11 +1164,13 @@ namespace ts {
11641164
}
11651165
}
11661166

1167-
interface OptionsBase {
1167+
/*@internal*/
1168+
export interface OptionsBase {
11681169
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
11691170
}
11701171

1171-
interface ParseCommandLineWorkerDiagnostics extends DidYouMeanOptionsDiagnostics {
1172+
/*@internal*/
1173+
export interface ParseCommandLineWorkerDiagnostics extends DidYouMeanOptionsDiagnostics {
11721174
getOptionsNameMap: () => OptionsNameMap;
11731175
optionTypeMismatchDiagnostic: DiagnosticMessage;
11741176
}
@@ -1189,7 +1191,8 @@ namespace ts {
11891191
createDiagnostics(diagnostics.unknownOptionDiagnostic, unknownOptionErrorText || unknownOption);
11901192
}
11911193

1192-
function parseCommandLineWorker(
1194+
/*@internal*/
1195+
export function parseCommandLineWorker(
11931196
diagnostics: ParseCommandLineWorkerDiagnostics,
11941197
commandLine: readonly string[],
11951198
readFile?: (path: string) => string | undefined) {
@@ -1279,50 +1282,75 @@ namespace ts {
12791282
errors: Diagnostic[]
12801283
) {
12811284
if (opt.isTSConfigOnly) {
1282-
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_can_only_be_specified_in_tsconfig_json_file, opt.name));
1285+
const optValue = args[i];
1286+
if (optValue === "null") {
1287+
options[opt.name] = undefined;
1288+
i++;
1289+
}
1290+
else if (opt.type === "boolean") {
1291+
if (optValue === "false") {
1292+
options[opt.name] = false;
1293+
i++;
1294+
}
1295+
else {
1296+
if (optValue === "true") i++;
1297+
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_can_only_be_specified_in_tsconfig_json_file_or_set_to_false_or_null_on_command_line, opt.name));
1298+
}
1299+
}
1300+
else {
1301+
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_can_only_be_specified_in_tsconfig_json_file_or_set_to_null_on_command_line, opt.name));
1302+
if (optValue && !startsWith(optValue, "-")) i++;
1303+
}
12831304
}
12841305
else {
12851306
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
12861307
if (!args[i] && opt.type !== "boolean") {
12871308
errors.push(createCompilerDiagnostic(diagnostics.optionTypeMismatchDiagnostic, opt.name, getCompilerOptionValueTypeString(opt)));
12881309
}
12891310

1290-
switch (opt.type) {
1291-
case "number":
1292-
options[opt.name] = parseInt(args[i]);
1293-
i++;
1294-
break;
1295-
case "boolean":
1296-
// boolean flag has optional value true, false, others
1297-
const optValue = args[i];
1298-
options[opt.name] = optValue !== "false";
1299-
// consume next argument as boolean flag value
1300-
if (optValue === "false" || optValue === "true") {
1311+
if (args[i] !== "null") {
1312+
switch (opt.type) {
1313+
case "number":
1314+
options[opt.name] = parseInt(args[i]);
13011315
i++;
1302-
}
1303-
break;
1304-
case "string":
1305-
options[opt.name] = args[i] || "";
1306-
i++;
1307-
break;
1308-
case "list":
1309-
const result = parseListTypeOption(opt, args[i], errors);
1310-
options[opt.name] = result || [];
1311-
if (result) {
1316+
break;
1317+
case "boolean":
1318+
// boolean flag has optional value true, false, others
1319+
const optValue = args[i];
1320+
options[opt.name] = optValue !== "false";
1321+
// consume next argument as boolean flag value
1322+
if (optValue === "false" || optValue === "true") {
1323+
i++;
1324+
}
1325+
break;
1326+
case "string":
1327+
options[opt.name] = args[i] || "";
13121328
i++;
1313-
}
1314-
break;
1315-
// If not a primitive, the possible types are specified in what is effectively a map of options.
1316-
default:
1317-
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i], errors);
1318-
i++;
1319-
break;
1329+
break;
1330+
case "list":
1331+
const result = parseListTypeOption(opt, args[i], errors);
1332+
options[opt.name] = result || [];
1333+
if (result) {
1334+
i++;
1335+
}
1336+
break;
1337+
// If not a primitive, the possible types are specified in what is effectively a map of options.
1338+
default:
1339+
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i], errors);
1340+
i++;
1341+
break;
1342+
}
1343+
}
1344+
else {
1345+
options[opt.name] = undefined;
1346+
i++;
13201347
}
13211348
}
13221349
return i;
13231350
}
13241351

1325-
const compilerOptionsDidYouMeanDiagnostics: ParseCommandLineWorkerDiagnostics = {
1352+
/*@internal*/
1353+
export const compilerOptionsDidYouMeanDiagnostics: ParseCommandLineWorkerDiagnostics = {
13261354
getOptionsNameMap,
13271355
optionDeclarations,
13281356
unknownOptionDiagnostic: Diagnostics.Unknown_compiler_option_0,
@@ -2170,7 +2198,7 @@ namespace ts {
21702198
}
21712199

21722200
function convertToOptionValueWithAbsolutePaths(option: CommandLineOption | undefined, value: CompilerOptionsValue, toAbsolutePath: (path: string) => string) {
2173-
if (option) {
2201+
if (option && !isNullOrUndefined(value)) {
21742202
if (option.type === "list") {
21752203
const values = value as readonly (string | number)[];
21762204
if (option.element.isFilePath && values.length) {

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3642,7 +3642,7 @@
36423642
"category": "Message",
36433643
"code": 6061
36443644
},
3645-
"Option '{0}' can only be specified in 'tsconfig.json' file.": {
3645+
"Option '{0}' can only be specified in 'tsconfig.json' file or set to 'null' on command line.": {
36463646
"category": "Error",
36473647
"code": 6064
36483648
},
@@ -4296,6 +4296,10 @@
42964296
"category": "Error",
42974297
"code": 6229
42984298
},
4299+
"Option '{0}' can only be specified in 'tsconfig.json' file or set to 'false' or 'null' on command line.": {
4300+
"category": "Error",
4301+
"code": 6230
4302+
},
42994303

43004304
"Projects to reference": {
43014305
"category": "Message",

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
"unittests/tsbuild/transitiveReferences.ts",
127127
"unittests/tsbuild/watchEnvironment.ts",
128128
"unittests/tsbuild/watchMode.ts",
129+
"unittests/tsc/composite.ts",
129130
"unittests/tsc/declarationEmit.ts",
130131
"unittests/tsc/incremental.ts",
131132
"unittests/tsc/listFilesOnly.ts",

0 commit comments

Comments
 (0)