diff --git a/Jakefile.js b/Jakefile.js index 2ffdfc37807..a6570c3c710 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -150,7 +150,10 @@ var harnessSources = harnessCoreSources.concat([ "reuseProgramStructure.ts", "cachingInServerLSHost.ts", "moduleResolution.ts", - "tsconfigParsing.ts" + "tsconfigParsing.ts", + "commandLineParsing.ts", + "convertCompilerOptionsFromJson.ts", + "convertTypingOptionsFromJson.ts" ].map(function (f) { return path.join(unittestsDirectory, f); })).concat([ diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index cf55f030c33..2f10a8103c4 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -58,12 +58,11 @@ namespace ts { }, paramType: Diagnostics.KIND, description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react, - error: Diagnostics.Argument_for_jsx_must_be_preserve_or_react }, { name: "reactNamespace", type: "string", - description: Diagnostics.Specifies_the_object_invoked_for_createElement_and_spread_when_targeting_react_JSX_emit + description: Diagnostics.Specify_the_object_invoked_for_createElement_and_spread_when_targeting_react_JSX_emit }, { name: "listFiles", @@ -77,7 +76,7 @@ namespace ts { name: "mapRoot", type: "string", isFilePath: true, - description: Diagnostics.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations, + description: Diagnostics.Specify_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations, paramType: Diagnostics.LOCATION, }, { @@ -94,7 +93,6 @@ namespace ts { }, description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015, paramType: Diagnostics.KIND, - error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_umd_es2015_or_none }, { name: "newLine", @@ -102,9 +100,8 @@ namespace ts { "crlf": NewLineKind.CarriageReturnLineFeed, "lf": NewLineKind.LineFeed }, - description: Diagnostics.Specifies_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix, + description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix, paramType: Diagnostics.NEWLINE, - error: Diagnostics.Argument_for_newLine_option_must_be_CRLF_or_LF }, { name: "noEmit", @@ -186,8 +183,8 @@ namespace ts { name: "rootDir", type: "string", isFilePath: true, - description: Diagnostics.Specifies_the_root_directory_of_input_files_Use_to_control_the_output_directory_structure_with_outDir, paramType: Diagnostics.LOCATION, + description: Diagnostics.Specify_the_root_directory_of_input_files_Use_to_control_the_output_directory_structure_with_outDir, }, { name: "isolatedModules", @@ -202,7 +199,7 @@ namespace ts { name: "sourceRoot", type: "string", isFilePath: true, - description: Diagnostics.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations, + description: Diagnostics.Specify_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations, paramType: Diagnostics.LOCATION, }, { @@ -231,9 +228,8 @@ namespace ts { "es6": ScriptTarget.ES6, "es2015": ScriptTarget.ES2015, }, - description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015_experimental, + description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015, paramType: Diagnostics.VERSION, - error: Diagnostics.Argument_for_target_option_must_be_ES3_ES5_or_ES2015 }, { name: "version", @@ -264,8 +260,7 @@ namespace ts { "node": ModuleResolutionKind.NodeJs, "classic": ModuleResolutionKind.Classic, }, - description: Diagnostics.Specifies_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6, - error: Diagnostics.Argument_for_moduleResolution_option_must_be_node_or_classic, + description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6, }, { name: "allowUnusedLabels", @@ -309,9 +304,13 @@ namespace ts { // this option can only be specified in tsconfig.json // use type = object to copy the value as-is name: "rootDirs", - type: "object", + type: "list", isTSConfigOnly: true, - isFilePath: true + element: { + name: "rootDirs", + type: "string", + isFilePath: true + } }, { name: "traceModuleResolution", @@ -335,6 +334,30 @@ namespace ts { } ]; + /* @internal */ + export let typingOptionDeclarations: CommandLineOption[] = [ + { + name: "enableAutoDiscovery", + type: "boolean", + }, + { + name: "include", + type: "list", + element: { + name: "include", + type: "string" + } + }, + { + name: "exclude", + type: "list", + element: { + name: "exclude", + type: "string" + } + } + ]; + /* @internal */ export interface OptionNameMap { optionNameMap: Map; @@ -361,6 +384,16 @@ namespace ts { return optionNameMapCache; } + /* @internal */ + export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType): Diagnostic { + const namesOfType: string[] = []; + forEachKey(opt.type, key => { + namesOfType.push(` '${key}'`); + }); + + return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType); + } + export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine { const options: CompilerOptions = {}; const fileNames: string[] = []; @@ -414,17 +447,15 @@ namespace ts { options[opt.name] = args[i] || ""; i++; break; + case "list": + options[opt.name] = parseListTypeOption(opt, args[i]); + i++; + break; // If not a primitive, the possible types are specified in what is effectively a map of options. default: - let map = >opt.type; - let key = (args[i] || "").toLowerCase(); + options[opt.name] = parseCustomTypeOption(opt, args[i]); i++; - if (hasProperty(map, key)) { - options[opt.name] = map[key]; - } - else { - errors.push(createCompilerDiagnostic((opt).error)); - } + break; } } } @@ -435,6 +466,29 @@ namespace ts { else { fileNames.push(s); } + + function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string) { + const key = (value || "").trim().toLowerCase(); + const map = opt.type; + if (hasProperty(map, key)) { + return map[key]; + } + else { + errors.push(createCompilerDiagnosticForInvalidCustomType(opt)); + } + } + + function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): (string | number)[] { + const values = (value || "").trim().split(","); + switch (opt.element.type) { + case "number": + return ts.map(values, parseInt); + case "string": + return ts.map(values, v => v || ""); + default: + return filter(map(values, v => parseCustomTypeOption(opt.element, v)), v => !!v); + } + } } } @@ -502,7 +556,6 @@ namespace ts { } } - /** * Remove the comments from a json like text. * Comments can be single line comments (starting with # or //) or multiline comments using / * * / @@ -536,21 +589,24 @@ namespace ts { * file to. e.g. outDir */ export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string): ParsedCommandLine { - const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath, configFileName); + const errors: Diagnostic[] = []; + const compilerOptions: CompilerOptions = convertCompilerOptionsFromJson(optionDeclarations, json["compilerOptions"], basePath, errors, configFileName); + const options = extend(existingOptions, compilerOptions); + const typingOptions: TypingOptions = convertTypingOptionsFromJson(typingOptionDeclarations, json["typingOptions"], basePath, errors, configFileName); - const options = extend(existingOptions, optionsFromJsonConfigFile); + const fileNames = getFileNames(errors); return { options, - fileNames: getFileNames(), - typingOptions: getTypingOptions(), + fileNames, + typingOptions, errors }; - function getFileNames(): string[] { + function getFileNames(errors: Diagnostic[]): string[] { let fileNames: string[] = []; if (hasProperty(json, "files")) { - if (json["files"] instanceof Array) { + if (isArray(json["files"])) { fileNames = map(json["files"], s => combinePaths(basePath, s)); } else { @@ -561,7 +617,7 @@ namespace ts { const filesSeen: Map = {}; let exclude: string[] = []; - if (json["exclude"] instanceof Array) { + if (isArray(json["exclude"])) { exclude = json["exclude"]; } else { @@ -608,47 +664,33 @@ namespace ts { } return fileNames; } - - function getTypingOptions(): TypingOptions { - const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json" - ? { enableAutoDiscovery: true, include: [], exclude: [] } - : { enableAutoDiscovery: false, include: [], exclude: [] }; - const jsonTypingOptions = json["typingOptions"]; - if (jsonTypingOptions) { - for (const id in jsonTypingOptions) { - if (id === "enableAutoDiscovery") { - if (typeof jsonTypingOptions[id] === "boolean") { - options.enableAutoDiscovery = jsonTypingOptions[id]; - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id)); - } - } - else if (id === "include") { - options.include = convertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); - } - else if (id === "exclude") { - options.exclude = convertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id)); - } - } - } - return options; - } } - export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } { - const options: CompilerOptions = {}; - const errors: Diagnostic[] = []; + /* @internal */ + export function convertCompilerOptionsFromJson(optionsDeclarations: CommandLineOption[], jsonOptions: any, + basePath: string, errors: Diagnostic[], configFileName?: string): CompilerOptions { - if (configFileName && getBaseFileName(configFileName) === "jsconfig.json") { - options.allowJs = true; - } + const options: CompilerOptions = getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true } : {}; + convertOptionsFromJson(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors); + return options; + } + + /* @internal */ + export function convertTypingOptionsFromJson(optionsDeclarations: CommandLineOption[], jsonOptions: any, + basePath: string, errors: Diagnostic[], configFileName?: string): TypingOptions { + + const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json" + ? { enableAutoDiscovery: true, include: [], exclude: [] } + : { enableAutoDiscovery: false, include: [], exclude: [] }; + convertOptionsFromJson(typingOptionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_typing_option_0, errors); + return options; + } + + function convertOptionsFromJson(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string, + defaultOptions: T, diagnosticMessage: DiagnosticMessage, errors: Diagnostic[]) { if (!jsonOptions) { - return { options, errors }; + return ; } const optionNameMap = arrayToMap(optionDeclarations, opt => opt.name); @@ -656,69 +698,50 @@ namespace ts { for (const id in jsonOptions) { if (hasProperty(optionNameMap, id)) { const opt = optionNameMap[id]; - const optType = opt.type; - let value = jsonOptions[id]; - const expectedType = typeof optType === "string" ? optType : "string"; - if (typeof value === expectedType) { - if (typeof optType !== "string") { - const key = value.toLowerCase(); - if (hasProperty(optType, key)) { - value = optType[key]; - } - else { - errors.push(createCompilerDiagnostic((opt).error)); - value = 0; - } - } - if (opt.isFilePath) { - switch (typeof value) { - case "string": - value = normalizePath(combinePaths(basePath, value)); - break; - case "object": - // "object" options with 'isFilePath' = true expected to be string arrays - value = convertJsonOptionToStringArray(opt.name, value, errors, (element) => normalizePath(combinePaths(basePath, element))); - break; - } - if (value === "") { - value = "."; - } - } - options[opt.name] = value; - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType)); - } + defaultOptions[opt.name] = convertJsonOption(opt, jsonOptions[id], basePath, errors); } else { - errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id)); + errors.push(createCompilerDiagnostic(diagnosticMessage, id)); } } - - return { options, errors }; } - function convertJsonOptionToStringArray(optionName: string, optionJson: any, errors: Diagnostic[], func?: (element: string) => string): string[] { - const items: string[] = []; - let invalidOptionType = false; - if (!isArray(optionJson)) { - invalidOptionType = true; + function convertJsonOption(opt: CommandLineOption, value: any, basePath: string, errors: Diagnostic[]): CompilerOptionsValue { + const optType = opt.type; + const expectedType = typeof optType === "string" ? optType : "string"; + if (optType === "list" && isArray(value)) { + return convertJsonOptionOfListType(opt, value, basePath, errors); + } + else if (typeof value === expectedType) { + if (typeof optType !== "string") { + return convertJsonOptionOfCustomType(opt, value, errors); + } + else { + if (opt.isFilePath) { + value = normalizePath(combinePaths(basePath, value)); + if (value === "") { + value = "."; + } + } + } + return value; } else { - for (const element of optionJson) { - if (typeof element === "string") { - const item = func ? func(element) : element; - items.push(item); - } - else { - invalidOptionType = true; - break; - } - } + errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, opt.name, expectedType)); } - if (invalidOptionType) { - errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, optionName)); + } + + function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) { + const key = value.toLowerCase(); + if (hasProperty(opt.type, key)) { + return opt.type[key]; } - return items; + else { + errors.push(createCompilerDiagnosticForInvalidCustomType(opt)); + } + } + + function convertJsonOptionOfListType(option: CommandLineOptionOfListType, values: any[], basePath: string, errors: Diagnostic[]): any[] { + return filter(map(values, v => convertJsonOption(option.element, v, basePath, errors)), v => !!v); } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 15a3a4e5ef4..b8dd62efa8e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2240,11 +2240,11 @@ "category": "Message", "code": 6002 }, - "Specifies the location where debugger should locate map files instead of generated locations.": { + "Specify the location where debugger should locate map files instead of generated locations.": { "category": "Message", "code": 6003 }, - "Specifies the location where debugger should locate TypeScript files instead of source locations.": { + "Specify the location where debugger should locate TypeScript files instead of source locations.": { "category": "Message", "code": 6004 }, @@ -2276,7 +2276,7 @@ "category": "Message", "code": 6011 }, - "Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": { + "Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015'": { "category": "Message", "code": 6015 }, @@ -2364,14 +2364,10 @@ "category": "Error", "code": 6045 }, - "Argument for '--module' option must be 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'none'.": { + "Argument for '{0}' option must be: {1}": { "category": "Error", "code": 6046 }, - "Argument for '--target' option must be 'ES3', 'ES5', or 'ES2015'.": { - "category": "Error", - "code": 6047 - }, "Locale must be of the form or -. For example '{0}' or '{1}'.": { "category": "Error", "code": 6048 @@ -2408,7 +2404,7 @@ "category": "Message", "code": 6056 }, - "Specifies the root directory of input files. Use to control the output directory structure with --outDir.": { + "Specify the root directory of input files. Use to control the output directory structure with --outDir.": { "category": "Message", "code": 6058 }, @@ -2416,7 +2412,7 @@ "category": "Error", "code": 6059 }, - "Specifies the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix).": { + "Specify the end of line sequence to be used when emitting files: 'CRLF' (dos) or 'LF' (unix).": { "category": "Message", "code": 6060 }, @@ -2424,14 +2420,6 @@ "category": "Message", "code": 6061 }, - "Argument for '--newLine' option must be 'CRLF' or 'LF'.": { - "category": "Error", - "code": 6062 - }, - "Argument for '--moduleResolution' option must be 'node' or 'classic'.": { - "category": "Error", - "code": 6063 - }, "Option '{0}' can only be specified in 'tsconfig.json' file.": { "category": "Error", "code": 6064 @@ -2448,7 +2436,7 @@ "category": "Message", "code": 6068 }, - "Specifies module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).": { + "Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).": { "category": "Message", "code": 6069 }, @@ -2492,10 +2480,6 @@ "category": "Message", "code": 6080 }, - "Argument for '--jsx' must be 'preserve' or 'react'.": { - "category": "Message", - "code": 6081 - }, "Only 'amd' and 'system' modules are supported alongside --{0}.": { "category": "Error", "code": 6082 @@ -2504,7 +2488,7 @@ "category": "Message", "code": 6083 }, - "Specifies the object invoked for createElement and __spread when targeting 'react' JSX emit": { + "Specify the object invoked for createElement and __spread when targeting 'react' JSX emit": { "category": "Message", "code": 6084 }, @@ -2620,7 +2604,6 @@ "category": "Message", "code": 6112 }, - "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", "code": 7005 diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d86bc87ce53..c5d822ad013 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2381,6 +2381,8 @@ namespace ts { export type PathSubstitutions = Map; export type TsConfigOnlyOptions = RootPaths | PathSubstitutions; + export type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions; + export interface CompilerOptions { allowNonTsExtensions?: boolean; charset?: string; @@ -2437,6 +2439,7 @@ namespace ts { allowSyntheticDefaultImports?: boolean; allowJs?: boolean; noImplicitUseStrict?: boolean; + lib?: string[]; /* @internal */ stripInternal?: boolean; // Skip checking lib.d.ts to help speed up tests. @@ -2444,7 +2447,9 @@ namespace ts { // Do not perform validation of output file name in transpile scenarios /* @internal */ suppressOutputPathCheck?: boolean; - [option: string]: string | number | boolean | TsConfigOnlyOptions; + list?: string[]; + + [option: string]: CompilerOptionsValue; } export interface TypingOptions { @@ -2529,7 +2534,7 @@ namespace ts { /* @internal */ export interface CommandLineOptionBase { name: string; - type: "string" | "number" | "boolean" | "object" | Map; // a value of a primitive type, or an object literal mapping named values to actual values + type: "string" | "number" | "boolean" | "object" | "list" | Map; // a value of a primitive type, or an object literal mapping named values to actual values isFilePath?: boolean; // True if option value is a path or fileName shortName?: string; // A short mnemonic for convenience - for instance, 'h' can be used in place of 'help' description?: DiagnosticMessage; // The message describing what the command line switch does @@ -2545,8 +2550,7 @@ namespace ts { /* @internal */ export interface CommandLineOptionOfCustomType extends CommandLineOptionBase { - type: Map; // an object literal mapping named values to actual values - error: DiagnosticMessage; // The error given when the argument does not fit a customized 'type' + type: Map; // an object literal mapping named values to actual values } /* @internal */ @@ -2555,7 +2559,13 @@ namespace ts { } /* @internal */ - export type CommandLineOption = CommandLineOptionOfCustomType | CommandLineOptionOfPrimitiveType | TsConfigOnlyOption; + export interface CommandLineOptionOfListType extends CommandLineOptionBase { + type: "list"; + element: CommandLineOptionOfCustomType | CommandLineOptionOfPrimitiveType; + } + + /* @internal */ + export type CommandLineOption = CommandLineOptionOfCustomType | CommandLineOptionOfPrimitiveType | TsConfigOnlyOption | CommandLineOptionOfListType; /* @internal */ export const enum CharacterCodes { diff --git a/tests/cases/unittests/commandLineParsing.ts b/tests/cases/unittests/commandLineParsing.ts new file mode 100644 index 00000000000..2b6b470a3b3 --- /dev/null +++ b/tests/cases/unittests/commandLineParsing.ts @@ -0,0 +1,191 @@ +/// +/// + +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); + assert.equal(parsedError.category, expectedError.category); + assert.equal(parsedError.messageText, expectedError.messageText); + } + + 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); + } + } + + it("Parse empty options of --jsx ", () => { + // 0.ts --jsx + assertParseResult(["0.ts", "--jsx"], + { + errors: [{ + messageText: "Compiler option 'jsx' expects an argument.", + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, + + file: undefined, + start: undefined, + length: undefined, + }, { + messageText: "Argument for '--jsx' option must be: 'preserve', 'react'", + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + + file: undefined, + start: undefined, + length: undefined, + }], + fileNames: ["0.ts"], + options: {} + }); + }); + + it("Parse empty options of --module ", () => { + // 0.ts -- + assertParseResult(["0.ts", "--module"], + { + errors: [{ + messageText: "Compiler option 'module' expects an argument.", + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, + + file: undefined, + start: undefined, + length: undefined, + }, { + messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'", + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + + file: undefined, + start: undefined, + length: undefined, + }], + fileNames: ["0.ts"], + options: {} + }); + }); + + it("Parse empty options of --newLine ", () => { + // 0.ts --newLine + assertParseResult(["0.ts", "--newLine"], + { + errors: [{ + messageText: "Compiler option 'newLine' expects an argument.", + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, + + file: undefined, + start: undefined, + length: undefined, + }, { + messageText: "Argument for '--newLine' option must be: 'crlf', 'lf'", + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + + file: undefined, + start: undefined, + length: undefined, + }], + fileNames: ["0.ts"], + options: {} + }); + }); + + it("Parse empty options of --target ", () => { + // 0.ts --target + assertParseResult(["0.ts", "--target"], + { + errors: [{ + messageText: "Compiler option 'target' expects an argument.", + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, + + file: undefined, + start: undefined, + length: undefined, + }, { + messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'", + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + + file: undefined, + start: undefined, + length: undefined, + }], + fileNames: ["0.ts"], + options: {} + }); + }); + + it("Parse empty options of --moduleResolution ", () => { + // 0.ts --moduleResolution + assertParseResult(["0.ts", "--moduleResolution"], + { + errors: [{ + messageText: "Compiler option 'moduleResolution' expects an argument.", + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, + + file: undefined, + start: undefined, + length: undefined, + }, { + messageText: "Argument for '--moduleResolution' option must be: 'node', 'classic'", + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + + file: undefined, + start: undefined, + length: undefined, + }], + fileNames: ["0.ts"], + options: {} + }); + }); + + it("Parse multiple compiler flags with input files at the end", () => { + // --module commonjs --target es5 0.ts + assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts"], + { + errors: [], + fileNames: ["0.ts"], + options: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES5, + } + }); + }); + + it("Parse multiple compiler flags with input files in the middle", () => { + // --module commonjs --target es5 0.ts --noImplicitAny + assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--noImplicitAny"], + { + errors: [], + fileNames: ["0.ts"], + options: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES5, + noImplicitAny: true, + } + }); + }); + }); +} diff --git a/tests/cases/unittests/convertCompilerOptionsFromJson.ts b/tests/cases/unittests/convertCompilerOptionsFromJson.ts new file mode 100644 index 00000000000..4edc3bdd200 --- /dev/null +++ b/tests/cases/unittests/convertCompilerOptionsFromJson.ts @@ -0,0 +1,324 @@ +/// +/// + +namespace ts { + describe('convertCompilerOptionsFromJson', () => { + function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) { + const actualErrors: Diagnostic[] = []; + const actualCompilerOptions: CompilerOptions = convertCompilerOptionsFromJson(optionDeclarations, json["compilerOptions"], "/apath/", actualErrors, configFileName); + + const parsedCompilerOptions = JSON.stringify(actualCompilerOptions); + const expectedCompilerOptions = JSON.stringify(expectedResult.compilerOptions); + assert.equal(parsedCompilerOptions, expectedCompilerOptions); + + const expectedErrors = expectedResult.errors; + assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`); + for (let i = 0; i < actualErrors.length; ++i) { + const actualError = actualErrors[i]; + const expectedError = expectedErrors[i]; + assert.equal(actualError.code, expectedError.code); + assert.equal(actualError.category, expectedError.category); + assert.equal(actualError.messageText, expectedError.messageText); + } + } + + // tsconfig.json tests + it("Convert correctly format tsconfig.json to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + } + }, "tsconfig.json", + { + compilerOptions: { + module: ModuleKind.CommonJS, + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [] + } + ); + }); + + it("Convert correctly format tsconfig.json with allowJs is false to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + "allowJs": false, + } + }, "tsconfig.json", + { + compilerOptions: { + module: ModuleKind.CommonJS, + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + allowJs: false, + }, + errors: [] + } + ); + }); + + it("Convert incorrectly option of jsx to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + "jsx": "" + } + }, "tsconfig.json", + { + compilerOptions: { + module: ModuleKind.CommonJS, + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Argument for '--jsx' option must be: 'preserve', 'react'", + code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category + }] + } + ); + }); + + it("Convert incorrectly option of module to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + } + }, "tsconfig.json", + { + compilerOptions: { + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'", + code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category + }] + } + ); + }); + + it("Convert incorrectly option of newLine to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "newLine": "", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + } + }, "tsconfig.json", + { + compilerOptions: { + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Argument for '--newLine' option must be: 'crlf', 'lf'", + code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category + }] + } + ); + }); + + it("Convert incorrectly option of target to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "target": "", + "noImplicitAny": false, + "sourceMap": false, + } + }, "tsconfig.json", + { + compilerOptions: { + noImplicitAny: false, + sourceMap: false, + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'", + code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category + }] + } + ); + }); + + it("Convert incorrectly option of module-resolution to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "moduleResolution": "", + "noImplicitAny": false, + "sourceMap": false, + } + }, "tsconfig.json", + { + compilerOptions: { + noImplicitAny: false, + sourceMap: false, + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Argument for '--moduleResolution' option must be: 'node', 'classic'", + code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, + category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category + }] + } + ); + }); + + it("Convert incorrectly format tsconfig.json to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "modu": "commonjs", + } + }, "tsconfig.json", + { + compilerOptions: {}, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Unknown compiler option 'modu'.", + code: Diagnostics.Unknown_compiler_option_0.code, + category: Diagnostics.Unknown_compiler_option_0.category + }] + } + ); + }); + + it("Convert default tsconfig.json to compiler-options ", () => { + assertCompilerOptions({}, "tsconfig.json", + { + compilerOptions: {} as CompilerOptions, + errors: [] + } + ); + }); + + // jsconfig.json + it("Convert correctly format jsconfig.json to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + } + }, "jsconfig.json", + { + compilerOptions: { + allowJs: true, + module: ModuleKind.CommonJS, + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [] + } + ); + }); + + it("Convert correctly format jsconfig.json with allowJs is false to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": false, + "sourceMap": false, + "allowJs": false, + } + }, "jsconfig.json", + { + compilerOptions: { + allowJs: false, + module: ModuleKind.CommonJS, + target: ScriptTarget.ES5, + noImplicitAny: false, + sourceMap: false, + }, + errors: [] + } + ); + }); + + it("Convert incorrectly format jsconfig.json to compiler-options ", () => { + assertCompilerOptions( + { + "compilerOptions": { + "modu": "commonjs", + } + }, "jsconfig.json", + { + compilerOptions: + { + allowJs: true + }, + errors: [{ + file: undefined, + start: 0, + length: 0, + messageText: "Unknown compiler option 'modu'.", + code: Diagnostics.Unknown_compiler_option_0.code, + category: Diagnostics.Unknown_compiler_option_0.category + }] + } + ); + }); + + it("Convert default jsconfig.json to compiler-options ", () => { + assertCompilerOptions({}, "jsconfig.json", + { + compilerOptions: + { + allowJs: true + }, + errors: [] + } + ); + }); + }); +} diff --git a/tests/cases/unittests/convertTypingOptionsFromJson.ts b/tests/cases/unittests/convertTypingOptionsFromJson.ts new file mode 100644 index 00000000000..92b450555a9 --- /dev/null +++ b/tests/cases/unittests/convertTypingOptionsFromJson.ts @@ -0,0 +1,188 @@ +/// +/// + +namespace ts { + describe('convertTypingOptionsFromJson', () => { + function assertTypingOptions(json: any, configFileName: string, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) { + const actualErrors: Diagnostic[] = []; + const actualTypingOptions = convertTypingOptionsFromJson(typingOptionDeclarations, json["typingOptions"], "/apath/", actualErrors, configFileName); + const parsedTypingOptions = JSON.stringify(actualTypingOptions); + const expectedTypingOptions = JSON.stringify(expectedResult.typingOptions); + assert.equal(parsedTypingOptions, expectedTypingOptions); + + const expectedErrors = expectedResult.errors; + assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`); + for (let i = 0; i < actualErrors.length; ++i) { + const actualError = actualErrors[i]; + const expectedError = expectedErrors[i]; + assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`); + assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`); + } + } + + // tsconfig.json + it("Convert correctly format tsconfig.json to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovery": true, + "include": ["0.d.ts", "1.d.ts"], + "exclude": ["0.js", "1.js"] + } + }, + "tsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: true, + include: ["0.d.ts", "1.d.ts"], + exclude: ["0.js", "1.js"] + }, + errors: [] + }); + }); + + it("Convert incorrect format tsconfig.json to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovy": true, + } + }, "tsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: false, + include: [], + exclude: [] + }, + errors: [ + { + category: Diagnostics.Unknown_typing_option_0.category, + code: Diagnostics.Unknown_typing_option_0.code, + file: undefined, + start: 0, + length: 0, + messageText: undefined + } + ] + }); + }); + + it("Convert default tsconfig.json to typing-options ", () => { + assertTypingOptions({}, "tsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: false, + include: [], + exclude: [] + }, + errors: [] + }); + }); + + it("Convert tsconfig.json with only enableAutoDiscovery property to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovery": true + } + }, "tsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: true, + include: [], + exclude: [] + }, + errors: [] + }); + }); + + // jsconfig.json + it("Convert jsconfig.json to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovery": false, + "include": ["0.d.ts"], + "exclude": ["0.js"] + } + }, "jsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: false, + include: ["0.d.ts"], + exclude: ["0.js"] + }, + errors: [] + }); + }); + + it("Convert default jsconfig.json to typing-options ", () => { + assertTypingOptions({ }, "jsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: true, + include: [], + exclude: [] + }, + errors: [] + }); + }); + + it("Convert incorrect format jsconfig.json to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovy": true, + } + }, "jsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: true, + include: [], + exclude: [] + }, + errors: [ + { + category: Diagnostics.Unknown_compiler_option_0.category, + code: Diagnostics.Unknown_typing_option_0.code, + file: undefined, + start: 0, + length: 0, + messageText: undefined + } + ] + }); + }); + + it("Convert jsconfig.json with only enableAutoDiscovery property to typing-options ", () => { + assertTypingOptions( + { + "typingOptions": + { + "enableAutoDiscovery": false + } + }, "jsconfig.json", + { + typingOptions: + { + enableAutoDiscovery: false, + include: [], + exclude: [] + }, + errors: [] + }); + }); + }); +} diff --git a/tests/cases/unittests/tsconfigParsing.ts b/tests/cases/unittests/tsconfigParsing.ts index 3603d22f314..622c3abfff2 100644 --- a/tests/cases/unittests/tsconfigParsing.ts +++ b/tests/cases/unittests/tsconfigParsing.ts @@ -82,5 +82,25 @@ namespace ts { it("returns object with error when json is invalid", () => { assertParseError("invalid"); }); + + it("returns object when users correctly specify library", () => { + assertParseResult( + `{ + "compilerOptions": { + "lib": "es5" + } + }`, { + config: { compilerOptions: { lib: "es5" } } + }); + + assertParseResult( + `{ + "compilerOptions": { + "lib": "es5,es6" + } + }`, { + config: { compilerOptions: { lib: "es5,es6" } } + }); + }); }); }