Skip to content

Commit 5b06edb

Browse files
committed
Addressing CR comments
- Adding check to ensure TypingOptions 'include' and 'exclude' arrays are composed of strings - Allow leading whitespace when removing comments from json
1 parent 0aaedc5 commit 5b06edb

2 files changed

Lines changed: 30 additions & 27 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ namespace ts {
616616
}
617617
}
618618
else if (id === "include") {
619-
options.include = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
619+
options.include = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors);
620620
}
621621
else if (id === "exclude") {
622-
options.exclude = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
622+
options.exclude = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors);
623623
}
624624
else {
625625
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id));
@@ -668,28 +668,7 @@ namespace ts {
668668
break;
669669
case "object":
670670
// "object" options with 'isFilePath' = true expected to be string arrays
671-
let paths: string[] = [];
672-
let invalidOptionType = false;
673-
if (!isArray(value)) {
674-
invalidOptionType = true;
675-
}
676-
else {
677-
for (const element of <any[]>value) {
678-
if (typeof element === "string") {
679-
paths.push(normalizePath(combinePaths(basePath, element)));
680-
}
681-
else {
682-
invalidOptionType = true;
683-
break;
684-
}
685-
}
686-
}
687-
if (invalidOptionType) {
688-
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, opt.name));
689-
}
690-
else {
691-
value = paths;
692-
}
671+
value = ConvertJsonOptionToStringArray(opt.name, value, errors, (element) => normalizePath(combinePaths(basePath, element)));
693672
break;
694673
}
695674
if (value === "") {
@@ -709,4 +688,28 @@ namespace ts {
709688

710689
return { options, errors };
711690
}
691+
692+
function ConvertJsonOptionToStringArray(optionName: string, optionJson: any, errors: Diagnostic[], func?: (element: string) => string): string[] {
693+
let items: string[] = [];
694+
let invalidOptionType = false;
695+
if (!isArray(optionJson)) {
696+
invalidOptionType = true;
697+
}
698+
else {
699+
for (const element of <any[]>optionJson) {
700+
if (typeof element === "string") {
701+
const item = func ? func(element) : element;
702+
items.push(item);
703+
}
704+
else {
705+
invalidOptionType = true;
706+
break;
707+
}
708+
}
709+
}
710+
if (invalidOptionType) {
711+
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, optionName));
712+
}
713+
return items;
714+
}
712715
}

src/services/jsTyping.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace ts.JsTyping {
2222
if (host.fileExists(jsonPath)) {
2323
try {
2424
// Strip out single-line comments
25-
const contents = host.readFile(jsonPath).replace(/^\/\/(.*)$/gm, "");
25+
const contents = host.readFile(jsonPath).replace(/^\s*\/\/(.*)$/gm, "");
2626
return JSON.parse(contents);
2727
}
2828
catch (e) { }
@@ -65,7 +65,7 @@ namespace ts.JsTyping {
6565
return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] };
6666
}
6767

68-
const cachePath = projectRootPath ? projectRootPath : globalCachePath;
68+
const cachePath = projectRootPath || globalCachePath;
6969
// Only infer typings for .js and .jsx files
7070
fileNames = fileNames
7171
.map(ts.normalizePath)
@@ -82,7 +82,7 @@ namespace ts.JsTyping {
8282
let exclude: string[] = [];
8383

8484
mergeTypings(typingOptions.include);
85-
exclude = typingOptions.exclude ? typingOptions.exclude : [];
85+
exclude = typingOptions.exclude || [];
8686

8787
if (typingOptions.enableAutoDiscovery) {
8888
const possibleSearchDirs = fileNames.map(ts.getDirectoryPath);

0 commit comments

Comments
 (0)