Skip to content

Commit 157b8e7

Browse files
author
Kanchalai Tanglertsampan
committed
Add a new unittest for command line parsing for --lib
1 parent c7df777 commit 157b8e7

5 files changed

Lines changed: 182 additions & 17 deletions

File tree

Jakefile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ var harnessSources = harnessCoreSources.concat([
150150
"reuseProgramStructure.ts",
151151
"cachingInServerLSHost.ts",
152152
"moduleResolution.ts",
153-
"tsconfigParsing.ts"
153+
"tsconfigParsing.ts",
154+
"commandLineParsing.ts"
154155
].map(function (f) {
155156
return path.join(unittestsDirectory, f);
156157
})).concat([

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ namespace ts {
518518
}
519519

520520
function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string) {
521-
const map = <Map<number>>opt.type;
521+
const map = opt.type;
522522
const key = (value || "").toLowerCase();
523523
if (hasProperty(map, key)) {
524524
return map[key];
@@ -529,12 +529,15 @@ namespace ts {
529529
}
530530
}
531531

532-
function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): number[] | string[] {
533-
const values = (value || "").split(",");
532+
function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): (number | string)[] {
533+
const values = (value || "").split(",").filter(v => { return v != undefined; });
534534
switch (opt.element.type) {
535-
case "number": return ts.map(values, parseInt);
536-
case "string": return ts.map(values, v => v || "");
537-
default: return ts.map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v));
535+
case "number":
536+
return ts.map(values, parseInt);
537+
case "string":
538+
return ts.map(values, v => v || "");
539+
default:
540+
return ts.map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v)).filter(v => { return v != undefined; });
538541
}
539542
}
540543
}

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,7 @@ namespace ts {
24372437
allowSyntheticDefaultImports?: boolean;
24382438
allowJs?: boolean;
24392439
noImplicitUseStrict?: boolean;
2440+
lib?: string[];
24402441
/* @internal */ stripInternal?: boolean;
24412442

24422443
// Skip checking lib.d.ts to help speed up tests.
@@ -2446,7 +2447,7 @@ namespace ts {
24462447

24472448
list?: string[];
24482449

2449-
[option: string]: string | number | boolean | TsConfigOnlyOptions | string[] | number[];
2450+
[option: string]: string | number | boolean | TsConfigOnlyOptions | (string | number)[];
24502451
}
24512452

24522453
export interface TypingOptions {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/// <reference path="..\..\..\src\harness\harness.ts" />
2+
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
3+
4+
namespace ts {
5+
describe('parseCommandLine', () => {
6+
7+
function assertParseResult(commandLine: string[], expectedParsedCommandLine: ts.ParsedCommandLine) {
8+
const parsed = ts.parseCommandLine(commandLine);
9+
const parsedCompilerOptions = JSON.stringify(parsed.options);
10+
const expectedCompilerOptions = JSON.stringify(expectedParsedCommandLine.options);
11+
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
12+
13+
const parsedErrors = parsed.errors;
14+
const expectedErrors = expectedParsedCommandLine.errors;
15+
assert.isTrue(parsedErrors.length === expectedErrors.length, `Expected error: ${JSON.stringify(expectedErrors)}. Actual error: ${JSON.stringify(parsedErrors)}.`);
16+
for (let i = 0; i < parsedErrors.length; ++i) {
17+
const parsedError = parsedErrors[i];
18+
const expectedError = expectedErrors[i];
19+
assert.equal(parsedError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(parsedError.code)}.`);
20+
assert.equal(parsedError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(parsedError.category)}.`);
21+
}
22+
23+
const parsedFileNames = parsed.fileNames;
24+
const expectedFileNames = expectedParsedCommandLine.fileNames;
25+
assert.isTrue(parsedFileNames.length === expectedFileNames.length, `Expected fileNames: [${JSON.stringify(expectedFileNames)}]. Actual fileNames: [${JSON.stringify(parsedFileNames)}].`);
26+
for (let i = 0; i < parsedFileNames.length; ++i) {
27+
const parsedFileName = parsedFileNames[i];
28+
const expectedFileName = expectedFileNames[i];
29+
assert.equal(parsedFileName, expectedFileName, `Expected filename: ${JSON.stringify(expectedFileName)}. Actual fileName: ${JSON.stringify(parsedFileName)}.`);
30+
}
31+
}
32+
33+
it("Parse single option of library flag ", () => {
34+
// --lib es6 0.ts
35+
assertParseResult(["--lib", "es6", "0.ts"],
36+
{
37+
errors: [],
38+
fileNames: ["0.ts"],
39+
options: {
40+
lib: ["lib.es6.d.ts"]
41+
}
42+
});
43+
});
44+
45+
it("Parse multiple options of library flags ", () => {
46+
// --lib es5,es6.symbol.wellknown 0.ts
47+
assertParseResult(["--lib", "es5,es6.symbol.wellknown", "0.ts"],
48+
{
49+
errors: [],
50+
fileNames: ["0.ts"],
51+
options: {
52+
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"]
53+
}
54+
});
55+
});
56+
57+
it("Parse unavailable options of library flags ", () => {
58+
// --lib es5,es7 0.ts
59+
assertParseResult(["--lib", "es5,es8", "0.ts"],
60+
{
61+
errors: [{
62+
messageText: "",
63+
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
64+
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
65+
66+
file: undefined,
67+
start: undefined,
68+
length: undefined,
69+
}],
70+
fileNames: ["0.ts"],
71+
options: {
72+
lib: ["lib.es5.d.ts"]
73+
}
74+
});
75+
});
76+
77+
it("Parse incorrect form of library flags ", () => {
78+
// --lib es5, es7 0.ts
79+
assertParseResult(["--lib", "es5,", "es7", "0.ts"],
80+
{
81+
errors: [{
82+
messageText: "",
83+
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
84+
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
85+
86+
file: undefined,
87+
start: undefined,
88+
length: undefined,
89+
}],
90+
fileNames: ["es7", "0.ts"],
91+
options: {
92+
lib: ["lib.es5.d.ts"]
93+
}
94+
});
95+
});
96+
97+
it("Parse multiple compiler flags with input files at the end", () => {
98+
// --lib es5,es6.symbol.wellknown --target es5 0.ts
99+
assertParseResult(["--lib", "es5,es6.symbol.wellknown", "--target", "es5", "0.ts"],
100+
{
101+
errors: [],
102+
fileNames: ["0.ts"],
103+
options: {
104+
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"],
105+
target: ts.ScriptTarget.ES5,
106+
}
107+
});
108+
});
109+
110+
it("Parse multiple compiler flags with input files in the middle", () => {
111+
// --module commonjs --target es5 0.ts --lib es5,es6.symbol.wellknown
112+
assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--lib", "es5,es6.symbol.wellknown"],
113+
{
114+
errors: [],
115+
fileNames: ["0.ts"],
116+
options: {
117+
module: ts.ModuleKind.CommonJS,
118+
target: ts.ScriptTarget.ES5,
119+
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"],
120+
}
121+
});
122+
});
123+
124+
it("Parse incorrect form of multiple compiler flags with input files in the middle", () => {
125+
// --module commonjs --target es5 0.ts --lib es5, es6.symbol.wellknown
126+
assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--lib", "es5,", "es6.symbol.wellknown"],
127+
{
128+
errors: [{
129+
messageText: "",
130+
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
131+
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
132+
133+
file: undefined,
134+
start: undefined,
135+
length: undefined,
136+
}],
137+
fileNames: ["0.ts", "es6.symbol.wellknown"],
138+
options: {
139+
module: ts.ModuleKind.CommonJS,
140+
target: ts.ScriptTarget.ES5,
141+
lib: ["lib.es5.d.ts"],
142+
}
143+
});
144+
});
145+
146+
it("Parse multiple library compiler flags ", () => {
147+
// --module commonjs --target es5 --lib es5 0.ts --library es6.array,es6.symbol.wellknown
148+
assertParseResult(["--module", "commonjs", "--target", "es5", "--lib", "es5", "0.ts", "--lib", "es6.array,es6.symbol.wellknown"],
149+
{
150+
errors: [],
151+
fileNames: ["0.ts"],
152+
options: {
153+
module: ts.ModuleKind.CommonJS,
154+
target: ts.ScriptTarget.ES5,
155+
lib: ["lib.es6.array.d.ts", "lib.es6.symbol.wellknown.d.ts"],
156+
}
157+
});
158+
});
159+
});
160+
}

tests/cases/unittests/tsconfigParsing.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ namespace ts {
8787
assertParseResult(
8888
`{
8989
"compilerOptions": {
90-
"library": "es5"
90+
"lib": "es5"
9191
}
92-
}`, {
93-
config: { compilerOptions: { library: "es5" } }
94-
});
95-
92+
}`, {
93+
config: { compilerOptions: { lib: "es5" } }
94+
});
95+
9696
assertParseResult(
9797
`{
9898
"compilerOptions": {
99-
"library": "es5,es6"
99+
"lib": "es5,es6"
100100
}
101-
}`, {
102-
config: { compilerOptions: { library: "es5,es6" } }
103-
});
101+
}`, {
102+
config: { compilerOptions: { lib: "es5,es6" } }
103+
});
104104
});
105105
});
106106
}

0 commit comments

Comments
 (0)