From 5b06edbc5428fbd8fd711ff53ded87aa6dc48f86 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Mon, 22 Feb 2016 19:00:06 -0800 Subject: [PATCH] Addressing CR comments - Adding check to ensure TypingOptions 'include' and 'exclude' arrays are composed of strings - Allow leading whitespace when removing comments from json --- src/compiler/commandLineParser.ts | 51 ++++++++++++++++--------------- src/services/jsTyping.ts | 6 ++-- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index af7b3a747cb..254fc434f64 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -616,10 +616,10 @@ namespace ts { } } else if (id === "include") { - options.include = isArray(jsonTypingOptions[id]) ? jsonTypingOptions[id] : []; + options.include = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); } else if (id === "exclude") { - options.exclude = isArray(jsonTypingOptions[id]) ? jsonTypingOptions[id] : []; + options.exclude = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors); } else { errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id)); @@ -668,28 +668,7 @@ namespace ts { break; case "object": // "object" options with 'isFilePath' = true expected to be string arrays - let paths: string[] = []; - let invalidOptionType = false; - if (!isArray(value)) { - invalidOptionType = true; - } - else { - for (const element of value) { - if (typeof element === "string") { - paths.push(normalizePath(combinePaths(basePath, element))); - } - else { - invalidOptionType = true; - break; - } - } - } - if (invalidOptionType) { - errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, opt.name)); - } - else { - value = paths; - } + value = ConvertJsonOptionToStringArray(opt.name, value, errors, (element) => normalizePath(combinePaths(basePath, element))); break; } if (value === "") { @@ -709,4 +688,28 @@ namespace ts { return { options, errors }; } + + function ConvertJsonOptionToStringArray(optionName: string, optionJson: any, errors: Diagnostic[], func?: (element: string) => string): string[] { + let items: string[] = []; + let invalidOptionType = false; + if (!isArray(optionJson)) { + invalidOptionType = true; + } + else { + for (const element of optionJson) { + if (typeof element === "string") { + const item = func ? func(element) : element; + items.push(item); + } + else { + invalidOptionType = true; + break; + } + } + } + if (invalidOptionType) { + errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, optionName)); + } + return items; + } } diff --git a/src/services/jsTyping.ts b/src/services/jsTyping.ts index e4b221bf051..bfb62e396c6 100644 --- a/src/services/jsTyping.ts +++ b/src/services/jsTyping.ts @@ -22,7 +22,7 @@ namespace ts.JsTyping { if (host.fileExists(jsonPath)) { try { // Strip out single-line comments - const contents = host.readFile(jsonPath).replace(/^\/\/(.*)$/gm, ""); + const contents = host.readFile(jsonPath).replace(/^\s*\/\/(.*)$/gm, ""); return JSON.parse(contents); } catch (e) { } @@ -65,7 +65,7 @@ namespace ts.JsTyping { return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; } - const cachePath = projectRootPath ? projectRootPath : globalCachePath; + const cachePath = projectRootPath || globalCachePath; // Only infer typings for .js and .jsx files fileNames = fileNames .map(ts.normalizePath) @@ -82,7 +82,7 @@ namespace ts.JsTyping { let exclude: string[] = []; mergeTypings(typingOptions.include); - exclude = typingOptions.exclude ? typingOptions.exclude : []; + exclude = typingOptions.exclude || []; if (typingOptions.enableAutoDiscovery) { const possibleSearchDirs = fileNames.map(ts.getDirectoryPath);