TypeScript/src/compiler/commandLineParser.ts

833 lines
32 KiB
TypeScript
Raw Normal View History

2014-07-13 01:04:16 +02:00
/// <reference path="sys.ts"/>
/// <reference path="types.ts"/>
/// <reference path="core.ts"/>
/// <reference path="diagnosticInformationMap.generated.ts"/>
2014-07-13 01:04:16 +02:00
/// <reference path="scanner.ts"/>
namespace ts {
/* @internal */
2015-06-26 02:36:19 +02:00
export let optionDeclarations: CommandLineOption[] = [
2014-08-02 02:12:29 +02:00
{
name: "charset",
type: "string",
},
{
name: "declaration",
shortName: "d",
type: "boolean",
description: Diagnostics.Generates_corresponding_d_ts_file,
},
2016-02-20 19:27:51 +01:00
{
name: "declarationDir",
type: "string",
isFilePath: true,
paramType: Diagnostics.DIRECTORY,
},
2014-08-02 02:12:29 +02:00
{
name: "diagnostics",
type: "boolean",
},
{
name: "emitBOM",
type: "boolean"
},
2014-08-02 02:12:29 +02:00
{
name: "help",
shortName: "h",
type: "boolean",
description: Diagnostics.Print_this_message,
},
2015-07-27 13:52:57 +02:00
{
name: "init",
type: "boolean",
description: Diagnostics.Initializes_a_TypeScript_project_and_creates_a_tsconfig_json_file,
},
2015-04-08 09:14:23 +02:00
{
name: "inlineSourceMap",
type: "boolean",
},
2015-04-21 05:33:31 +02:00
{
name: "inlineSources",
type: "boolean",
},
{
name: "jsx",
type: {
"preserve": JsxEmit.Preserve,
"react": JsxEmit.React
},
paramType: Diagnostics.KIND,
2015-06-26 18:38:21 +02:00
description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react,
},
{
2016-01-07 22:59:25 +01:00
name: "reactNamespace",
type: "string",
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_the_object_invoked_for_createElement_and_spread_when_targeting_react_JSX_emit
},
{
name: "listFiles",
type: "boolean",
},
2014-08-02 02:12:29 +02:00
{
name: "locale",
type: "string",
},
{
name: "mapRoot",
type: "string",
isFilePath: true,
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
2014-08-02 02:12:29 +02:00
paramType: Diagnostics.LOCATION,
},
{
name: "module",
shortName: "m",
type: {
"none": ModuleKind.None,
2014-08-02 02:12:29 +02:00
"commonjs": ModuleKind.CommonJS,
2015-04-10 21:10:38 +02:00
"amd": ModuleKind.AMD,
"system": ModuleKind.System,
2015-04-24 05:50:35 +02:00
"umd": ModuleKind.UMD,
2015-09-17 22:14:31 +02:00
"es6": ModuleKind.ES6,
"es2015": ModuleKind.ES2015,
2014-08-02 02:12:29 +02:00
},
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015,
2014-08-02 02:12:29 +02:00
paramType: Diagnostics.KIND,
},
{
name: "newLine",
type: {
"crlf": NewLineKind.CarriageReturnLineFeed,
"lf": NewLineKind.LineFeed
},
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix,
paramType: Diagnostics.NEWLINE,
},
2014-12-17 05:25:19 +01:00
{
name: "noEmit",
type: "boolean",
description: Diagnostics.Do_not_emit_outputs,
},
{
name: "noEmitHelpers",
type: "boolean"
},
2014-10-27 20:48:46 +01:00
{
name: "noEmitOnError",
type: "boolean",
description: Diagnostics.Do_not_emit_outputs_if_any_errors_were_reported,
2014-10-27 20:48:46 +01:00
},
2014-08-02 02:12:29 +02:00
{
name: "noImplicitAny",
type: "boolean",
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
2014-08-02 02:12:29 +02:00
},
{
name: "noImplicitThis",
type: "boolean",
2016-03-31 00:01:16 +02:00
description: Diagnostics.Raise_error_on_this_expressions_with_an_implied_any_type,
},
2014-08-02 02:12:29 +02:00
{
name: "noLib",
type: "boolean",
},
{
name: "noResolve",
type: "boolean",
},
{
name: "skipDefaultLibCheck",
type: "boolean",
},
2014-08-02 02:12:29 +02:00
{
name: "out",
type: "string",
isFilePath: false, // This is intentionally broken to support compatability with existing tsconfig files
// for correct behaviour, please use outFile
paramType: Diagnostics.FILE,
},
{
name: "outFile",
type: "string",
isFilePath: true,
2014-08-02 02:12:29 +02:00
description: Diagnostics.Concatenate_and_emit_output_to_single_file,
paramType: Diagnostics.FILE,
},
{
name: "outDir",
type: "string",
isFilePath: true,
2014-08-02 02:12:29 +02:00
description: Diagnostics.Redirect_output_structure_to_the_directory,
paramType: Diagnostics.DIRECTORY,
},
2014-12-10 21:37:09 +01:00
{
name: "preserveConstEnums",
type: "boolean",
description: Diagnostics.Do_not_erase_const_enum_declarations_in_generated_code
},
2015-11-02 22:20:48 +01:00
{
name: "pretty",
2015-11-03 01:40:35 +01:00
description: Diagnostics.Stylize_errors_and_messages_using_color_and_context_experimental,
2015-11-02 22:20:48 +01:00
type: "boolean"
},
{
name: "project",
shortName: "p",
type: "string",
isFilePath: true,
description: Diagnostics.Compile_the_project_in_the_given_directory,
paramType: Diagnostics.DIRECTORY
},
2014-08-02 02:12:29 +02:00
{
name: "removeComments",
type: "boolean",
description: Diagnostics.Do_not_emit_comments_to_output,
},
2015-04-15 07:11:25 +02:00
{
name: "rootDir",
type: "string",
isFilePath: true,
paramType: Diagnostics.LOCATION,
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_the_root_directory_of_input_files_Use_to_control_the_output_directory_structure_with_outDir,
2015-04-15 07:11:25 +02:00
},
{
name: "isolatedModules",
type: "boolean",
},
2014-08-02 02:12:29 +02:00
{
2014-08-10 14:02:49 +02:00
name: "sourceMap",
2014-08-02 02:12:29 +02:00
type: "boolean",
description: Diagnostics.Generates_corresponding_map_file,
},
{
name: "sourceRoot",
type: "string",
isFilePath: true,
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
2014-08-02 02:12:29 +02:00
paramType: Diagnostics.LOCATION,
},
{
name: "suppressExcessPropertyErrors",
type: "boolean",
description: Diagnostics.Suppress_excess_property_checks_for_object_literals,
experimental: true
},
2014-12-10 21:37:09 +01:00
{
name: "suppressImplicitAnyIndexErrors",
type: "boolean",
2014-12-11 02:51:14 +01:00
description: Diagnostics.Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures,
2014-12-10 21:37:09 +01:00
},
{
name: "stripInternal",
type: "boolean",
description: Diagnostics.Do_not_emit_declarations_for_code_that_has_an_internal_annotation,
experimental: true
},
2014-08-02 02:12:29 +02:00
{
name: "target",
shortName: "t",
type: {
"es3": ScriptTarget.ES3,
"es5": ScriptTarget.ES5,
"es6": ScriptTarget.ES6,
"es2015": ScriptTarget.ES2015,
},
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015,
2014-08-02 02:12:29 +02:00
paramType: Diagnostics.VERSION,
},
{
name: "version",
shortName: "v",
type: "boolean",
description: Diagnostics.Print_the_compiler_s_version,
},
{
name: "watch",
shortName: "w",
type: "boolean",
description: Diagnostics.Watch_input_files,
},
2015-06-02 00:01:24 +02:00
{
name: "experimentalDecorators",
type: "boolean",
description: Diagnostics.Enables_experimental_support_for_ES7_decorators
},
{
name: "emitDecoratorMetadata",
type: "boolean",
2015-06-02 00:01:24 +02:00
experimental: true,
description: Diagnostics.Enables_experimental_support_for_emitting_type_metadata_for_decorators
2015-08-21 01:13:49 +02:00
},
{
name: "moduleResolution",
type: {
"node": ModuleResolutionKind.NodeJs,
"classic": ModuleResolutionKind.Classic,
2015-08-21 01:13:49 +02:00
},
2016-03-14 23:49:29 +01:00
description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6,
},
{
name: "allowUnusedLabels",
type: "boolean",
description: Diagnostics.Do_not_report_errors_on_unused_labels
},
{
name: "noImplicitReturns",
type: "boolean",
description: Diagnostics.Report_error_when_not_all_code_paths_in_function_return_a_value
},
{
name: "noFallthroughCasesInSwitch",
type: "boolean",
description: Diagnostics.Report_errors_for_fallthrough_cases_in_switch_statement
},
{
name: "allowUnreachableCode",
type: "boolean",
description: Diagnostics.Do_not_report_errors_on_unreachable_code
2015-10-29 17:33:27 +01:00
},
{
name: "forceConsistentCasingInFileNames",
type: "boolean",
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
},
{
name: "baseUrl",
type: "string",
isFilePath: true,
2015-11-25 01:41:41 +01:00
description: Diagnostics.Base_directory_to_resolve_non_absolute_module_names
},
{
// this option can only be specified in tsconfig.json
// use type = object to copy the value as-is
name: "paths",
type: "object",
isTSConfigOnly: true
},
{
// this option can only be specified in tsconfig.json
// use type = object to copy the value as-is
name: "rootDirs",
type: "list",
2015-11-26 01:41:09 +01:00
isTSConfigOnly: true,
element: {
name: "rootDirs",
type: "string",
isFilePath: true
}
2015-11-20 06:11:57 +01:00
},
2016-03-23 19:41:34 +01:00
{
name: "typesSearchPaths",
2016-03-23 19:41:34 +01:00
type: "list",
isTSConfigOnly: true,
element: {
name: "typesSearchPaths",
2016-03-23 19:41:34 +01:00
type: "string",
isFilePath: true
}
},
{
name: "typesRoot",
type: "string"
},
{
name: "types",
type: "list",
element: {
name: "types",
type: "string"
},
description: Diagnostics.Type_declaration_files_to_be_included_in_compilation
},
2015-11-20 06:33:33 +01:00
{
name: "traceResolution",
2015-11-20 06:33:33 +01:00
type: "boolean",
description: Diagnostics.Enable_tracing_of_the_name_resolution_process
2015-11-20 06:33:33 +01:00
},
{
name: "allowJs",
type: "boolean",
description: Diagnostics.Allow_javascript_files_to_be_compiled
},
{
name: "allowSyntheticDefaultImports",
type: "boolean",
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
2016-02-03 23:16:09 +01:00
},
{
2016-02-06 01:30:01 +01:00
name: "noImplicitUseStrict",
2016-02-03 23:16:09 +01:00
type: "boolean",
description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output
},
{
name: "listEmittedFiles",
2016-04-08 01:47:52 +02:00
type: "boolean"
},
{
name: "lib",
type: "list",
element: {
name: "lib",
type: {
// JavaScript only
"es5": "lib.es5.d.ts",
"es6": "lib.es2015.d.ts",
"es2015": "lib.es2015.d.ts",
"es7": "lib.es2016.d.ts",
"es2016": "lib.es2016.d.ts",
// Host only
"dom": "lib.dom.d.ts",
"webworker": "lib.webworker.d.ts",
"scripthost": "lib.scripthost.d.ts",
// ES2015 Or ESNext By-feature options
"es2015.core": "lib.es2015.core.d.ts",
"es2015.collection": "lib.es2015.collection.d.ts",
"es2015.generator": "lib.es2015.generator.d.ts",
"es2015.iterable": "lib.es2015.iterable.d.ts",
"es2015.promise": "lib.es2015.promise.d.ts",
"es2015.proxy": "lib.es2015.proxy.d.ts",
"es2015.reflect": "lib.es2015.reflect.d.ts",
"es2015.symbol": "lib.es2015.symbol.d.ts",
"es2015.symbol.wellknown": "lib.es2015.symbol.wellknown.d.ts",
"es2016.array.include": "lib.es2016.array.include.d.ts"
},
},
description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon
},
{
name: "strictNullChecks",
type: "boolean",
description: Diagnostics.Enable_strict_null_checks
}
2014-07-13 01:04:16 +02:00
];
/* @internal */
export let typingOptionDeclarations: CommandLineOption[] = [
{
name: "enableAutoDiscovery",
type: "boolean",
},
{
name: "include",
type: "list",
element: {
name: "include",
type: "string"
}
},
{
name: "exclude",
type: "list",
element: {
2016-03-16 22:08:49 +01:00
name: "exclude",
type: "string"
}
}
];
2015-08-25 23:34:34 +02:00
/* @internal */
2015-07-27 13:52:57 +02:00
export interface OptionNameMap {
optionNameMap: Map<CommandLineOption>;
shortOptionNames: Map<string>;
}
2014-07-13 01:04:16 +02:00
2015-07-27 13:52:57 +02:00
let optionNameMapCache: OptionNameMap;
2015-08-25 23:34:34 +02:00
/* @internal */
2015-07-27 13:52:57 +02:00
export function getOptionNameMap(): OptionNameMap {
if (optionNameMapCache) {
return optionNameMapCache;
}
2014-07-13 01:04:16 +02:00
2015-11-04 23:02:33 +01:00
const optionNameMap: Map<CommandLineOption> = {};
const shortOptionNames: Map<string> = {};
forEach(optionDeclarations, option => {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
2015-07-27 13:52:57 +02:00
optionNameMapCache = { optionNameMap, shortOptionNames };
return optionNameMapCache;
}
2016-03-14 23:53:42 +01:00
/* @internal */
export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType): Diagnostic {
2016-03-14 23:53:42 +01:00
const namesOfType: string[] = [];
2016-03-16 23:29:13 +01:00
forEachKey(opt.type, key => {
namesOfType.push(` '${key}'`);
});
2016-03-14 23:53:42 +01:00
return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType);
2016-03-14 23:53:42 +01:00
}
/* @internal */
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
const key = (value || "").trim().toLowerCase();
const map = opt.type;
if (hasProperty(map, key)) {
return map[key];
}
else {
errors.push(createCompilerDiagnosticForInvalidCustomType(opt));
}
}
/* @internal */
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (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(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
}
}
/* @internal */
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine {
2015-11-04 23:02:33 +01:00
const options: CompilerOptions = {};
const fileNames: string[] = [];
const errors: Diagnostic[] = [];
const { optionNameMap, shortOptionNames } = getOptionNameMap();
2015-07-27 13:52:57 +02:00
2014-07-13 01:04:16 +02:00
parseStrings(commandLine);
return {
options,
fileNames,
errors
2014-07-13 01:04:16 +02:00
};
function parseStrings(args: string[]) {
2015-06-26 02:36:19 +02:00
let i = 0;
2014-07-13 01:04:16 +02:00
while (i < args.length) {
2015-12-23 00:45:00 +01:00
let s = args[i];
i++;
2014-07-13 01:04:16 +02:00
if (s.charCodeAt(0) === CharacterCodes.at) {
parseResponseFile(s.slice(1));
}
else if (s.charCodeAt(0) === CharacterCodes.minus) {
s = s.slice(s.charCodeAt(1) === CharacterCodes.minus ? 2 : 1).toLowerCase();
// Try to translate short option names to their full equivalents.
if (hasProperty(shortOptionNames, s)) {
s = shortOptionNames[s];
}
2014-08-02 02:12:29 +02:00
if (hasProperty(optionNameMap, s)) {
const opt = optionNameMap[s];
if (opt.isTSConfigOnly) {
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_can_only_be_specified_in_tsconfig_json_file, opt.name));
}
else {
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
if (!args[i] && opt.type !== "boolean") {
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_expects_an_argument, opt.name));
}
switch (opt.type) {
case "number":
options[opt.name] = parseInt(args[i]);
i++;
break;
case "boolean":
options[opt.name] = true;
break;
case "string":
options[opt.name] = args[i] || "";
i++;
break;
case "list":
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
i++;
break;
// If not a primitive, the possible types are specified in what is effectively a map of options.
default:
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i], errors);
i++;
break;
}
}
2014-07-13 01:04:16 +02:00
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, s));
}
}
else {
fileNames.push(s);
2014-07-13 01:04:16 +02:00
}
2016-03-10 01:35:01 +01:00
}
2014-07-13 01:04:16 +02:00
}
function parseResponseFile(fileName: string) {
2015-11-04 23:02:33 +01:00
const text = readFile ? readFile(fileName) : sys.readFile(fileName);
2014-07-13 01:04:16 +02:00
if (!text) {
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName));
2014-07-13 01:04:16 +02:00
return;
}
2015-11-04 23:02:33 +01:00
const args: string[] = [];
2015-06-26 02:36:19 +02:00
let pos = 0;
2014-07-13 01:04:16 +02:00
while (true) {
while (pos < text.length && text.charCodeAt(pos) <= CharacterCodes.space) pos++;
if (pos >= text.length) break;
2015-11-04 23:02:33 +01:00
const start = pos;
2014-07-13 01:04:16 +02:00
if (text.charCodeAt(start) === CharacterCodes.doubleQuote) {
pos++;
while (pos < text.length && text.charCodeAt(pos) !== CharacterCodes.doubleQuote) pos++;
if (pos < text.length) {
args.push(text.substring(start + 1, pos));
pos++;
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Unterminated_quoted_string_in_response_file_0, fileName));
2014-07-13 01:04:16 +02:00
}
}
else {
while (text.charCodeAt(pos) > CharacterCodes.space) pos++;
args.push(text.substring(start, pos));
}
}
parseStrings(args);
}
}
/**
* Read tsconfig.json file
* @param fileName The path to the config file
*/
2015-08-28 01:52:49 +02:00
export function readConfigFile(fileName: string, readFile: (path: string) => string): { config?: any; error?: Diagnostic } {
let text = "";
try {
2015-08-28 01:52:49 +02:00
text = readFile(fileName);
}
catch (e) {
2015-04-23 03:09:55 +02:00
return { error: createCompilerDiagnostic(Diagnostics.Cannot_read_file_0_Colon_1, fileName, e.message) };
}
2015-10-15 00:10:05 +02:00
return parseConfigFileTextToJson(fileName, text);
2015-04-23 03:09:55 +02:00
}
/**
* Parse the text of the tsconfig.json file
* @param fileName The path to the config file
* @param jsonText The text of the config file
*/
2015-10-15 00:10:05 +02:00
export function parseConfigFileTextToJson(fileName: string, jsonText: string): { config?: any; error?: Diagnostic } {
2015-04-23 03:09:55 +02:00
try {
2015-11-04 23:02:33 +01:00
const jsonTextWithoutComments = removeComments(jsonText);
return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} };
}
catch (e) {
return { error: createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) };
}
}
/**
* Remove the comments from a json like text.
* Comments can be single line comments (starting with # or //) or multiline comments using / * * /
*
* This method replace comment content by whitespace rather than completely remove them to keep positions in json parsing error reporting accurate.
*/
function removeComments(jsonText: string): string {
let output = "";
2015-11-04 23:02:33 +01:00
const scanner = createScanner(ScriptTarget.ES5, /* skipTrivia */ false, LanguageVariant.Standard, jsonText);
let token: SyntaxKind;
while ((token = scanner.scan()) !== SyntaxKind.EndOfFileToken) {
switch (token) {
case SyntaxKind.SingleLineCommentTrivia:
case SyntaxKind.MultiLineCommentTrivia:
// replace comments with whitespace to preserve original character positions
output += scanner.getTokenText().replace(/\S/g, " ");
break;
default:
output += scanner.getTokenText();
break;
}
}
return output;
}
/**
* Parse the contents of a config file (tsconfig.json).
* @param json The contents of the config file to parse
* @param host Instance of ParseConfigHost used to enumerate files in folder.
* @param basePath A root directory to resolve relative path entries in the config
2015-07-09 00:35:49 +02:00
* file to. e.g. outDir
*/
2016-01-21 01:53:15 +01:00
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string): ParsedCommandLine {
const errors: Diagnostic[] = [];
const compilerOptions: CompilerOptions = convertCompilerOptionsFromJsonWorker(json["compilerOptions"], basePath, errors, configFileName);
const options = extend(existingOptions, compilerOptions);
const typingOptions: TypingOptions = convertTypingOptionsFromJsonWorker(json["typingOptions"], basePath, errors, configFileName);
2016-03-23 19:41:34 +01:00
options.configFilePath = configFileName;
const fileNames = getFileNames(errors);
return {
options,
fileNames,
typingOptions,
errors
};
function getFileNames(errors: Diagnostic[]): string[] {
2015-06-26 02:36:19 +02:00
let fileNames: string[] = [];
if (hasProperty(json, "files")) {
if (isArray(json["files"])) {
fileNames = map(<string[]>json["files"], s => combinePaths(basePath, s));
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "files", "Array"));
}
}
else {
const filesSeen: Map<boolean> = {};
let exclude: string[] = [];
if (isArray(json["exclude"])) {
exclude = json["exclude"];
}
else {
// by default exclude node_modules, and any specificied output directory
exclude = ["node_modules"];
const outDir = json["compilerOptions"] && json["compilerOptions"]["outDir"];
if (outDir) {
exclude.push(outDir);
}
}
exclude = map(exclude, normalizeSlashes);
const supportedExtensions = getSupportedExtensions(options);
Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick");
// Get files of supported extensions in their order of resolution
for (const extension of supportedExtensions) {
const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude);
for (const fileName of filesInDirWithExtension) {
2016-01-21 01:53:15 +01:00
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
// lets pick them when its turn comes up
if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) {
continue;
}
// Skip over any minified JavaScript files (ending in ".min.js")
if (/\.min\.js$/.test(fileName)) {
continue;
}
// If this is one of the output extension (which would be .d.ts and .js if we are allowing compilation of js files)
// do not include this file if we included .ts or .tsx file with same base name as it could be output of the earlier compilation
if (extension === ".d.ts" || (options.allowJs && contains(supportedJavascriptExtensions, extension))) {
const baseName = fileName.substr(0, fileName.length - extension.length);
if (hasProperty(filesSeen, baseName + ".ts") || hasProperty(filesSeen, baseName + ".tsx")) {
continue;
}
}
filesSeen[fileName] = true;
fileNames.push(fileName);
}
}
}
if (hasProperty(json, "excludes") && !hasProperty(json, "exclude")) {
2016-03-28 23:40:34 +02:00
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_option_excludes_Did_you_mean_exclude));
}
return fileNames;
}
}
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
const errors: Diagnostic[] = [];
const options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName);
return { options, errors };
}
export function convertTypingOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
const errors: Diagnostic[] = [];
const options = convertTypingOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName);
return { options, errors };
}
function convertCompilerOptionsFromJsonWorker(jsonOptions: any,
2016-03-17 00:35:51 +01:00
basePath: string, errors: Diagnostic[], configFileName?: string): CompilerOptions {
const options: CompilerOptions = getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true } : {};
convertOptionsFromJson(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors);
return options;
}
function convertTypingOptionsFromJsonWorker(jsonOptions: any,
2016-03-17 00:35:51 +01:00
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: CompilerOptions | TypingOptions, diagnosticMessage: DiagnosticMessage, errors: Diagnostic[]) {
if (!jsonOptions) {
return ;
}
2015-11-04 23:02:33 +01:00
const optionNameMap = arrayToMap(optionDeclarations, opt => opt.name);
2015-11-04 23:02:33 +01:00
for (const id in jsonOptions) {
if (hasProperty(optionNameMap, id)) {
2015-11-04 23:02:33 +01:00
const opt = optionNameMap[id];
defaultOptions[opt.name] = convertJsonOption(opt, jsonOptions[id], basePath, errors);
}
else {
errors.push(createCompilerDiagnostic(diagnosticMessage, id));
}
}
}
2016-03-17 00:35:51 +01:00
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(<CommandLineOptionOfListType>opt, value, basePath, errors);
}
else if (typeof value === expectedType) {
if (typeof optType !== "string") {
return convertJsonOptionOfCustomType(<CommandLineOptionOfCustomType>opt, value, errors);
}
else {
if (opt.isFilePath) {
value = normalizePath(combinePaths(basePath, value));
if (value === "") {
value = ".";
}
}
}
return value;
}
else {
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, opt.name, expectedType));
}
}
function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
const key = value.toLowerCase();
if (hasProperty(opt.type, key)) {
return opt.type[key];
}
else {
errors.push(createCompilerDiagnosticForInvalidCustomType(opt));
}
}
function convertJsonOptionOfListType(option: CommandLineOptionOfListType, values: any[], basePath: string, errors: Diagnostic[]): any[] {
2016-03-16 23:29:13 +01:00
return filter(map(values, v => convertJsonOption(option.element, v, basePath, errors)), v => !!v);
}
}