From 668c3f8f1e3d5146c524999580cef0ce306cc83d Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 17 Oct 2016 14:41:24 -0700 Subject: [PATCH] addressed PR feedback --- scripts/buildProtocol.ts | 1 + src/compiler/commandLineParser.ts | 120 +++++++++++++----------------- src/server/editorServices.ts | 36 +++++---- 3 files changed, 77 insertions(+), 80 deletions(-) diff --git a/scripts/buildProtocol.ts b/scripts/buildProtocol.ts index 3154037c746f3..66f29f577d933 100644 --- a/scripts/buildProtocol.ts +++ b/scripts/buildProtocol.ts @@ -162,6 +162,7 @@ function generateProtocolFile(protocolTs: string, typeScriptServicesDts: string) } protocolDts += "\nimport protocol = ts.server.protocol;"; protocolDts += "\nexport = protocol;"; + protocolDts += "\nexport as namespace protocol;"; // do sanity check and try to compile generated text as standalone program const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false); const diagnostics = [...sanityCheckProgram.getSyntacticDiagnostics(), ...sanityCheckProgram.getSemanticDiagnostics(), ...sanityCheckProgram.getGlobalDiagnostics()]; diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 502a3599f32d9..b8fa1423a4d74 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -7,68 +7,6 @@ namespace ts { /* @internal */ export const compileOnSaveCommandLineOption: CommandLineOption = { name: "compileOnSave", type: "boolean" }; - /* @internal */ - export const jsxCompilerOption: CommandLineOptionOfCustomType = { - name: "jsx", - type: createMap({ - "preserve": JsxEmit.Preserve, - "react": JsxEmit.React - }), - paramType: Diagnostics.KIND, - description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react, - }; - - /* @internal */ - export const moduleCommandLineOption: CommandLineOptionOfCustomType = { - name: "module", - shortName: "m", - type: createMap({ - "none": ModuleKind.None, - "commonjs": ModuleKind.CommonJS, - "amd": ModuleKind.AMD, - "system": ModuleKind.System, - "umd": ModuleKind.UMD, - "es6": ModuleKind.ES6, - "es2015": ModuleKind.ES2015, - }), - description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015, - paramType: Diagnostics.KIND, - }; - - /* @internal */ - export const newLineCommandLineOption: CommandLineOptionOfCustomType = { - name: "newLine", - type: createMap({ - "crlf": NewLineKind.CarriageReturnLineFeed, - "lf": NewLineKind.LineFeed - }), - description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix, - paramType: Diagnostics.NEWLINE, - }; - - /* @internal */ - export const moduleResolutionCommandLineOption: CommandLineOptionOfCustomType = { - name: "moduleResolution", - type: createMap({ - "node": ModuleResolutionKind.NodeJs, - "classic": ModuleResolutionKind.Classic, - }), - description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6, - }; - - /* @internal */ - export const targetCommandLineOption: CommandLineOptionOfCustomType = { - name: "target", - shortName: "t", - type: createMap({ - "es3": ScriptTarget.ES3, - "es5": ScriptTarget.ES5, - "es6": ScriptTarget.ES6, - "es2015": ScriptTarget.ES2015, - }), - description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015, - paramType: Diagnostics.VERSION, - }; /* @internal */ export const optionDeclarations: CommandLineOption[] = [ @@ -125,7 +63,15 @@ namespace ts { name: "inlineSources", type: "boolean", }, - jsxCompilerOption, + { + name: "jsx", + type: createMap({ + "preserve": JsxEmit.Preserve, + "react": JsxEmit.React + }), + paramType: Diagnostics.KIND, + description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react, + }, { name: "reactNamespace", type: "string", @@ -146,8 +92,30 @@ namespace ts { description: Diagnostics.Specify_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations, paramType: Diagnostics.LOCATION, }, - moduleCommandLineOption, - newLineCommandLineOption, + { + name: "module", + shortName: "m", + type: createMap({ + "none": ModuleKind.None, + "commonjs": ModuleKind.CommonJS, + "amd": ModuleKind.AMD, + "system": ModuleKind.System, + "umd": ModuleKind.UMD, + "es6": ModuleKind.ES6, + "es2015": ModuleKind.ES2015, + }), + description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015, + paramType: Diagnostics.KIND, + }, + { + name: "newLine", + type: createMap({ + "crlf": NewLineKind.CarriageReturnLineFeed, + "lf": NewLineKind.LineFeed + }), + description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix, + paramType: Diagnostics.NEWLINE, + }, { name: "noEmit", type: "boolean", @@ -287,7 +255,18 @@ namespace ts { description: Diagnostics.Do_not_emit_declarations_for_code_that_has_an_internal_annotation, experimental: true }, - targetCommandLineOption, + { + name: "target", + shortName: "t", + type: createMap({ + "es3": ScriptTarget.ES3, + "es5": ScriptTarget.ES5, + "es6": ScriptTarget.ES6, + "es2015": ScriptTarget.ES2015, + }), + description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015, + paramType: Diagnostics.VERSION, + }, { name: "version", shortName: "v", @@ -311,7 +290,14 @@ namespace ts { experimental: true, description: Diagnostics.Enables_experimental_support_for_emitting_type_metadata_for_decorators }, - moduleResolutionCommandLineOption, + { + name: "moduleResolution", + type: createMap({ + "node": ModuleResolutionKind.NodeJs, + "classic": ModuleResolutionKind.Classic, + }), + description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6, + }, { name: "allowUnusedLabels", type: "boolean", diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index db0f95709884c..da9fb49779c43 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -17,6 +17,22 @@ namespace ts.server { (event: ProjectServiceEvent): void; } + function prepareConvertersForEnumLikeCompilerOptions(commandLineOptions: CommandLineOption[]): Map> { + const map: Map> = createMap>(); + for (const option of commandLineOptions) { + if (typeof option.type === "object") { + const optionMap = >option.type; + // verify that map contains only numbers + for (const id in optionMap) { + Debug.assert(typeof optionMap[id] === "number"); + } + map[option.name] = optionMap; + } + } + return map; + } + + const compilerOptionConverters = prepareConvertersForEnumLikeCompilerOptions(optionDeclarations); const indentStyle = createMap({ "none": IndentStyle.None, "block": IndentStyle.Block, @@ -32,20 +48,14 @@ namespace ts.server { } export function convertCompilerOptions(protocolOptions: protocol.ExternalProjectCompilerOptions): CompilerOptions & protocol.CompileOnSaveMixin { - protocolOptions.target = convertCompilerOptionEnum(targetCommandLineOption, protocolOptions.target); - protocolOptions.module = convertCompilerOptionEnum(moduleCommandLineOption, protocolOptions.module); - protocolOptions.moduleResolution = convertCompilerOptionEnum(moduleResolutionCommandLineOption, protocolOptions.moduleResolution); - protocolOptions.jsx = convertCompilerOptionEnum(jsxCompilerOption, protocolOptions.jsx); - protocolOptions.newLine = convertCompilerOptionEnum(newLineCommandLineOption, protocolOptions.newLine); - return protocolOptions; - } - - function convertCompilerOptionEnum(option: CommandLineOptionOfCustomType, value: string | number): number { - if (typeof value === "string") { - value = option.type[value.toLowerCase()]; - Debug.assert(value !== undefined); + for (const id in compilerOptionConverters) { + const propertyValue = protocolOptions[id]; + if (typeof propertyValue === "string") { + const mappedValues = compilerOptionConverters[id]; + protocolOptions[id] = mappedValues[propertyValue.toLowerCase()]; + } } - return value; + return protocolOptions; } export function tryConvertScriptKindName(scriptKindName: protocol.ScriptKindName | ScriptKind): ScriptKind {