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-02-22 19:00:06 -08:00
parent 0aaedc5df4
commit 5b06edbc54
2 changed files with 30 additions and 27 deletions

View file

@ -616,10 +616,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));
@ -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 <any[]>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 <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);