Skip to content

Commit cc3d4d3

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 2f90271 commit cc3d4d3

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,10 @@ namespace ts {
590590
}
591591
}
592592
else if (id === "include") {
593-
options.include = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
593+
options.include = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors);
594594
}
595595
else if (id === "exclude") {
596-
options.exclude = isArray(jsonTypingOptions[id]) ? <string[]>jsonTypingOptions[id] : [];
596+
options.exclude = ConvertJsonOptionToStringArray(id, jsonTypingOptions[id], errors);
597597
}
598598
else {
599599
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_typing_option_0, id));
@@ -636,8 +636,8 @@ namespace ts {
636636
}
637637
}
638638
if (opt.isFilePath) {
639-
value = normalizePath(combinePaths(basePath, value));
640-
if (value === "") {
639+
value = normalizePath(combinePaths(basePath, value));
640+
if (value === "") {
641641
value = ".";
642642
}
643643
}
@@ -654,4 +654,28 @@ namespace ts {
654654

655655
return { options, errors };
656656
}
657+
658+
function ConvertJsonOptionToStringArray(optionName: string, optionJson: any, errors: Diagnostic[], func?: (element: string) => string): string[] {
659+
let items: string[] = [];
660+
let invalidOptionType = false;
661+
if (!isArray(optionJson)) {
662+
invalidOptionType = true;
663+
}
664+
else {
665+
for (const element of <any[]>optionJson) {
666+
if (typeof element === "string") {
667+
const item = func ? func(element) : element;
668+
items.push(item);
669+
}
670+
else {
671+
invalidOptionType = true;
672+
break;
673+
}
674+
}
675+
}
676+
if (invalidOptionType) {
677+
errors.push(createCompilerDiagnostic(Diagnostics.Option_0_should_have_array_of_strings_as_a_value, optionName));
678+
}
679+
return items;
680+
}
657681
}

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)