Addressing CR comments

- Adding check to ensure TypingOptions 'include' and 'exclude' arrays are composed of strings
- Allow leading whitespace when removing comments from json
This commit is contained in:
Jason Ramsay 2016-03-08 15:17:09 -08:00
parent 2f90271ca6
commit cc3d4d3291
2 changed files with 31 additions and 7 deletions

View File

@ -590,10 +590,10 @@ namespace ts {
}
}
else if (id === "include") {
options.include = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
options.include = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors);
}
else if (id === "exclude") {
options.exclude = isArray(jsonTypingOptions[id]) ? <string[]>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 <any[]>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;
}
}

View File

@ -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);