mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Add a new unittest for command line parsing for --lib
This commit is contained in:
parent
c7df7770cd
commit
157b8e7456
@ -150,7 +150,8 @@ var harnessSources = harnessCoreSources.concat([
|
||||
"reuseProgramStructure.ts",
|
||||
"cachingInServerLSHost.ts",
|
||||
"moduleResolution.ts",
|
||||
"tsconfigParsing.ts"
|
||||
"tsconfigParsing.ts",
|
||||
"commandLineParsing.ts"
|
||||
].map(function (f) {
|
||||
return path.join(unittestsDirectory, f);
|
||||
})).concat([
|
||||
|
||||
@ -518,7 +518,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string) {
|
||||
const map = <Map<number>>opt.type;
|
||||
const map = opt.type;
|
||||
const key = (value || "").toLowerCase();
|
||||
if (hasProperty(map, key)) {
|
||||
return map[key];
|
||||
@ -529,12 +529,15 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): number[] | string[] {
|
||||
const values = (value || "").split(",");
|
||||
function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): (number | string)[] {
|
||||
const values = (value || "").split(",").filter(v => { return v != undefined; });
|
||||
switch (opt.element.type) {
|
||||
case "number": return ts.map(values, parseInt);
|
||||
case "string": return ts.map(values, v => v || "");
|
||||
default: return ts.map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v));
|
||||
case "number":
|
||||
return ts.map(values, parseInt);
|
||||
case "string":
|
||||
return ts.map(values, v => v || "");
|
||||
default:
|
||||
return ts.map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v)).filter(v => { return v != undefined; });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2437,6 +2437,7 @@ namespace ts {
|
||||
allowSyntheticDefaultImports?: boolean;
|
||||
allowJs?: boolean;
|
||||
noImplicitUseStrict?: boolean;
|
||||
lib?: string[];
|
||||
/* @internal */ stripInternal?: boolean;
|
||||
|
||||
// Skip checking lib.d.ts to help speed up tests.
|
||||
@ -2446,7 +2447,7 @@ namespace ts {
|
||||
|
||||
list?: string[];
|
||||
|
||||
[option: string]: string | number | boolean | TsConfigOnlyOptions | string[] | number[];
|
||||
[option: string]: string | number | boolean | TsConfigOnlyOptions | (string | number)[];
|
||||
}
|
||||
|
||||
export interface TypingOptions {
|
||||
|
||||
160
tests/cases/unittests/commandLineParsing.ts
Normal file
160
tests/cases/unittests/commandLineParsing.ts
Normal file
@ -0,0 +1,160 @@
|
||||
/// <reference path="..\..\..\src\harness\harness.ts" />
|
||||
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
|
||||
|
||||
namespace ts {
|
||||
describe('parseCommandLine', () => {
|
||||
|
||||
function assertParseResult(commandLine: string[], expectedParsedCommandLine: ts.ParsedCommandLine) {
|
||||
const parsed = ts.parseCommandLine(commandLine);
|
||||
const parsedCompilerOptions = JSON.stringify(parsed.options);
|
||||
const expectedCompilerOptions = JSON.stringify(expectedParsedCommandLine.options);
|
||||
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
|
||||
|
||||
const parsedErrors = parsed.errors;
|
||||
const expectedErrors = expectedParsedCommandLine.errors;
|
||||
assert.isTrue(parsedErrors.length === expectedErrors.length, `Expected error: ${JSON.stringify(expectedErrors)}. Actual error: ${JSON.stringify(parsedErrors)}.`);
|
||||
for (let i = 0; i < parsedErrors.length; ++i) {
|
||||
const parsedError = parsedErrors[i];
|
||||
const expectedError = expectedErrors[i];
|
||||
assert.equal(parsedError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(parsedError.code)}.`);
|
||||
assert.equal(parsedError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(parsedError.category)}.`);
|
||||
}
|
||||
|
||||
const parsedFileNames = parsed.fileNames;
|
||||
const expectedFileNames = expectedParsedCommandLine.fileNames;
|
||||
assert.isTrue(parsedFileNames.length === expectedFileNames.length, `Expected fileNames: [${JSON.stringify(expectedFileNames)}]. Actual fileNames: [${JSON.stringify(parsedFileNames)}].`);
|
||||
for (let i = 0; i < parsedFileNames.length; ++i) {
|
||||
const parsedFileName = parsedFileNames[i];
|
||||
const expectedFileName = expectedFileNames[i];
|
||||
assert.equal(parsedFileName, expectedFileName, `Expected filename: ${JSON.stringify(expectedFileName)}. Actual fileName: ${JSON.stringify(parsedFileName)}.`);
|
||||
}
|
||||
}
|
||||
|
||||
it("Parse single option of library flag ", () => {
|
||||
// --lib es6 0.ts
|
||||
assertParseResult(["--lib", "es6", "0.ts"],
|
||||
{
|
||||
errors: [],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
lib: ["lib.es6.d.ts"]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse multiple options of library flags ", () => {
|
||||
// --lib es5,es6.symbol.wellknown 0.ts
|
||||
assertParseResult(["--lib", "es5,es6.symbol.wellknown", "0.ts"],
|
||||
{
|
||||
errors: [],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse unavailable options of library flags ", () => {
|
||||
// --lib es5,es7 0.ts
|
||||
assertParseResult(["--lib", "es5,es8", "0.ts"],
|
||||
{
|
||||
errors: [{
|
||||
messageText: "",
|
||||
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
|
||||
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
|
||||
|
||||
file: undefined,
|
||||
start: undefined,
|
||||
length: undefined,
|
||||
}],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
lib: ["lib.es5.d.ts"]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse incorrect form of library flags ", () => {
|
||||
// --lib es5, es7 0.ts
|
||||
assertParseResult(["--lib", "es5,", "es7", "0.ts"],
|
||||
{
|
||||
errors: [{
|
||||
messageText: "",
|
||||
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
|
||||
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
|
||||
|
||||
file: undefined,
|
||||
start: undefined,
|
||||
length: undefined,
|
||||
}],
|
||||
fileNames: ["es7", "0.ts"],
|
||||
options: {
|
||||
lib: ["lib.es5.d.ts"]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse multiple compiler flags with input files at the end", () => {
|
||||
// --lib es5,es6.symbol.wellknown --target es5 0.ts
|
||||
assertParseResult(["--lib", "es5,es6.symbol.wellknown", "--target", "es5", "0.ts"],
|
||||
{
|
||||
errors: [],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"],
|
||||
target: ts.ScriptTarget.ES5,
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse multiple compiler flags with input files in the middle", () => {
|
||||
// --module commonjs --target es5 0.ts --lib es5,es6.symbol.wellknown
|
||||
assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--lib", "es5,es6.symbol.wellknown"],
|
||||
{
|
||||
errors: [],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES5,
|
||||
lib: ["lib.es5.d.ts", "lib.es6.symbol.wellknown.d.ts"],
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse incorrect form of multiple compiler flags with input files in the middle", () => {
|
||||
// --module commonjs --target es5 0.ts --lib es5, es6.symbol.wellknown
|
||||
assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--lib", "es5,", "es6.symbol.wellknown"],
|
||||
{
|
||||
errors: [{
|
||||
messageText: "",
|
||||
category: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.category,
|
||||
code: ts.Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
|
||||
|
||||
file: undefined,
|
||||
start: undefined,
|
||||
length: undefined,
|
||||
}],
|
||||
fileNames: ["0.ts", "es6.symbol.wellknown"],
|
||||
options: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES5,
|
||||
lib: ["lib.es5.d.ts"],
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("Parse multiple library compiler flags ", () => {
|
||||
// --module commonjs --target es5 --lib es5 0.ts --library es6.array,es6.symbol.wellknown
|
||||
assertParseResult(["--module", "commonjs", "--target", "es5", "--lib", "es5", "0.ts", "--lib", "es6.array,es6.symbol.wellknown"],
|
||||
{
|
||||
errors: [],
|
||||
fileNames: ["0.ts"],
|
||||
options: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES5,
|
||||
lib: ["lib.es6.array.d.ts", "lib.es6.symbol.wellknown.d.ts"],
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -87,20 +87,20 @@ namespace ts {
|
||||
assertParseResult(
|
||||
`{
|
||||
"compilerOptions": {
|
||||
"library": "es5"
|
||||
"lib": "es5"
|
||||
}
|
||||
}`, {
|
||||
config: { compilerOptions: { library: "es5" } }
|
||||
});
|
||||
|
||||
}`, {
|
||||
config: { compilerOptions: { lib: "es5" } }
|
||||
});
|
||||
|
||||
assertParseResult(
|
||||
`{
|
||||
"compilerOptions": {
|
||||
"library": "es5,es6"
|
||||
"lib": "es5,es6"
|
||||
}
|
||||
}`, {
|
||||
config: { compilerOptions: { library: "es5,es6" } }
|
||||
});
|
||||
}`, {
|
||||
config: { compilerOptions: { lib: "es5,es6" } }
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user