Skip to content

Commit 0245c2d

Browse files
committed
Only copy non error values in array when converting the json
Fixes microsoft#27432
1 parent 21148b3 commit 0245c2d

File tree

2 files changed

+125
-10
lines changed

2 files changed

+125
-10
lines changed

src/compiler/commandLineParser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,12 @@ namespace ts {
15291529
elements: NodeArray<Expression>,
15301530
elementOption: CommandLineOption | undefined
15311531
): any[] | void {
1532-
return (returnValue ? elements.map : elements.forEach).call(elements, (element: Expression) => convertPropertyValueToJson(element, elementOption));
1532+
if (!returnValue) {
1533+
return elements.forEach(element => convertPropertyValueToJson(element, elementOption));
1534+
}
1535+
1536+
// Filter out invalid values
1537+
return filter(elements.map(element => convertPropertyValueToJson(element, elementOption)), v => v !== undefined);
15331538
}
15341539

15351540
function convertPropertyValueToJson(valueExpression: Expression, option: CommandLineOption | undefined): any {

src/testRunner/unittests/convertCompilerOptionsFromJson.ts

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
namespace ts {
22
describe("convertCompilerOptionsFromJson", () => {
3+
const formatDiagnosticHost: FormatDiagnosticsHost = {
4+
getCurrentDirectory: () => "/apath/",
5+
getCanonicalFileName: createGetCanonicalFileName(/*useCaseSensitiveFileNames*/ true),
6+
getNewLine: () => "\n"
7+
};
38
function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
49
assertCompilerOptionsWithJson(json, configFileName, expectedResult);
510
assertCompilerOptionsWithJsonNode(json, configFileName, expectedResult);
@@ -24,9 +29,11 @@ namespace ts {
2429
}
2530

2631
function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
27-
const fileText = JSON.stringify(json);
32+
assertCompilerOptionsWithJsonText(JSON.stringify(json), configFileName, expectedResult);
33+
}
34+
35+
function assertCompilerOptionsWithJsonText(fileText: string, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: (Diagnostic | string)[] }) {
2836
const result = parseJsonText(configFileName, fileText);
29-
assert(!result.parseDiagnostics.length);
3037
assert(!!result.endOfFileToken);
3138
const host: ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: "/apath/" }));
3239
const { options: actualCompilerOptions, errors: actualParseErrors } = parseJsonSourceFileConfigFileContent(result, host, "/apath/", /*existingOptions*/ undefined, configFileName);
@@ -37,20 +44,29 @@ namespace ts {
3744
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
3845
assert.equal(actualCompilerOptions.configFile, result);
3946

40-
const actualErrors = filter(actualParseErrors, error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code);
47+
const actualErrors = (result.parseDiagnostics as Diagnostic[]).concat(actualParseErrors.filter(error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code));
4148
const expectedErrors = expectedResult.errors;
42-
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
49+
assert.isTrue(expectedErrors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedErrors.map(e => isString(e) ? e : getDiagnosticString(e)), undefined, " ")}. Actual error: ${JSON.stringify(actualErrors.map(getDiagnosticString), undefined, " ")}.`);
4350
for (let i = 0; i < actualErrors.length; i++) {
4451
const actualError = actualErrors[i];
4552
const expectedError = expectedErrors[i];
46-
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
47-
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
48-
assert(actualError.file);
49-
assert(actualError.start);
50-
assert(actualError.length);
53+
if (isString(expectedError)) {
54+
assert.equal(getDiagnosticString(actualError), expectedError);
55+
}
56+
else {
57+
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
58+
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
59+
assert(actualError.file);
60+
assert(actualError.start);
61+
assert(actualError.length);
62+
}
5163
}
5264
}
5365

66+
function getDiagnosticString(diagnostic: Diagnostic) {
67+
return formatDiagnostic(diagnostic, formatDiagnosticHost);
68+
}
69+
5470
// tsconfig.json tests
5571
it("Convert correctly format tsconfig.json to compiler-options ", () => {
5672
assertCompilerOptions(
@@ -539,5 +555,99 @@ namespace ts {
539555
}
540556
);
541557
});
558+
559+
it("Convert tsconfig options when there are multiple invalid strings", () => {
560+
assertCompilerOptionsWithJsonText(`{
561+
"compilerOptions": {
562+
"target": "<%- options.useTsWithBabel ? 'esnext' : 'es5' %>",
563+
"module": "esnext",
564+
<%_ if (options.classComponent) { _%>
565+
"experimentalDecorators": true,
566+
<%_ } _%>
567+
"sourceMap": true,
568+
"types": [
569+
"webpack-env"<% if (hasMocha || hasJest) { %>,<% } %>
570+
<%_ if (hasMocha) { _%>
571+
"mocha",
572+
"chai"
573+
<%_ } else if (hasJest) { _%>
574+
"jest"
575+
<%_ } _%>
576+
]
577+
}
578+
}
579+
`, "tsconfig.json",
580+
{
581+
compilerOptions: {
582+
target: undefined,
583+
module: ModuleKind.ESNext,
584+
types: []
585+
},
586+
errors: [
587+
"tsconfig.json(5,5): error TS1136: Property assignment expected.\n",
588+
"tsconfig.json(5,6): error TS1136: Property assignment expected.\n",
589+
"tsconfig.json(5,9): error TS1005: ':' expected.\n",
590+
"tsconfig.json(5,20): error TS1005: ',' expected.\n",
591+
"tsconfig.json(5,41): error TS1109: Expression expected.\n",
592+
"tsconfig.json(6,29): error TS1005: ';' expected.\n",
593+
"tsconfig.json(7,5): error TS1109: Expression expected.\n",
594+
"tsconfig.json(7,6): error TS1109: Expression expected.\n",
595+
"tsconfig.json(7,11): error TS1005: ',' expected.\n",
596+
"tsconfig.json(7,12): error TS1005: ':' expected.\n",
597+
"tsconfig.json(7,13): error TS1109: Expression expected.\n",
598+
"tsconfig.json(8,16): error TS1005: ',' expected.\n",
599+
"tsconfig.json(8,22): error TS1005: ':' expected.\n",
600+
"tsconfig.json(10,21): error TS1109: Expression expected.\n",
601+
"tsconfig.json(10,23): error TS1109: Expression expected.\n",
602+
"tsconfig.json(10,36): error TS1005: ',' expected.\n",
603+
"tsconfig.json(10,50): error TS1109: Expression expected.\n",
604+
"tsconfig.json(10,51): error TS1109: Expression expected.\n",
605+
"tsconfig.json(10,52): error TS1109: Expression expected.\n",
606+
"tsconfig.json(10,53): error TS1109: Expression expected.\n",
607+
"tsconfig.json(10,54): error TS1109: Expression expected.\n",
608+
"tsconfig.json(10,56): error TS1109: Expression expected.\n",
609+
"tsconfig.json(10,58): error TS1005: ',' expected.\n",
610+
"tsconfig.json(10,59): error TS1136: Property assignment expected.\n",
611+
"tsconfig.json(11,7): error TS1136: Property assignment expected.\n",
612+
"tsconfig.json(11,8): error TS1136: Property assignment expected.\n",
613+
"tsconfig.json(11,11): error TS1005: ':' expected.\n",
614+
"tsconfig.json(11,29): error TS1109: Expression expected.\n",
615+
"tsconfig.json(14,8): error TS1109: Expression expected.\n",
616+
"tsconfig.json(14,13): error TS1005: ',' expected.\n",
617+
"tsconfig.json(14,18): error TS1005: ':' expected.\n",
618+
"tsconfig.json(14,35): error TS1109: Expression expected.\n",
619+
"tsconfig.json(16,8): error TS1109: Expression expected.\n",
620+
"tsconfig.json(16,13): error TS1005: ',' expected.\n",
621+
"tsconfig.json(16,14): error TS1005: ':' expected.\n",
622+
"tsconfig.json(16,15): error TS1109: Expression expected.\n",
623+
"tsconfig.json(17,5): error TS1109: Expression expected.\n",
624+
"tsconfig.json(3,15): error TS6046: Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'esnext'.\n",
625+
"tsconfig.json(5,7): error TS1327: String literal with double quotes expected.\n",
626+
"tsconfig.json(5,7): error TS5023: Unknown compiler option '_'.\n",
627+
"tsconfig.json(5,8): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
628+
"tsconfig.json(5,9): error TS1136: Property assignment expected.\n",
629+
"tsconfig.json(7,11): error TS1327: String literal with double quotes expected.\n",
630+
"tsconfig.json(7,11): error TS5023: Unknown compiler option '_'.\n",
631+
"tsconfig.json(7,12): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
632+
"tsconfig.json(8,18): error TS1327: String literal with double quotes expected.\n",
633+
"tsconfig.json(8,18): error TS5023: Unknown compiler option 'true'.\n",
634+
"tsconfig.json(8,22): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
635+
"tsconfig.json(10,7): error TS5024: Compiler option 'types' requires a value of type string.\n",
636+
"tsconfig.json(10,23): error TS1136: Property assignment expected.\n",
637+
"tsconfig.json(11,9): error TS1327: String literal with double quotes expected.\n",
638+
"tsconfig.json(11,9): error TS5023: Unknown compiler option '_'.\n",
639+
"tsconfig.json(11,10): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
640+
"tsconfig.json(11,11): error TS1136: Property assignment expected.\n",
641+
"tsconfig.json(14,13): error TS1327: String literal with double quotes expected.\n",
642+
"tsconfig.json(14,13): error TS5023: Unknown compiler option 'else'.\n",
643+
"tsconfig.json(14,17): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
644+
"tsconfig.json(14,18): error TS1136: Property assignment expected.\n",
645+
"tsconfig.json(16,13): error TS1327: String literal with double quotes expected.\n",
646+
"tsconfig.json(16,13): error TS5023: Unknown compiler option '_'.\n",
647+
"tsconfig.json(16,14): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n"
648+
]
649+
}
650+
);
651+
});
542652
});
543653
}

0 commit comments

Comments
 (0)