From cc3d4d3291e2fc3c52ee960d2b423ed2c4d06ba8 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Tue, 8 Mar 2016 15:17:09 -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 | 32 +++++++++++++++++++++++++++---- src/services/jsTyping.ts | 6 +++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index e1e05c0a888..7d365c6b283 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -590,10 +590,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)); @@ -636,8 +636,8 @@ namespace ts { } } if (opt.isFilePath) { - value = normalizePath(combinePaths(basePath, value)); - if (value === "") { + value = normalizePath(combinePaths(basePath, value)); + if (value === "") { value = "."; } } @@ -654,4 +654,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);