Skip to content

Commit 9d9f7e4

Browse files
committed
Feedback
1 parent 959dee4 commit 9d9f7e4

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/compiler/commandLineParser.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,10 +2308,26 @@ namespace ts {
23082308
};
23092309

23102310
function getFileNames(): ExpandResult {
2311-
const filesSpecs = getSpecs("files")?.specs;
2311+
const referencesOfRaw = getPropFromRaw<ProjectReference>("references", element => typeof element === "object", "object");
2312+
if (isArray(referencesOfRaw)) {
2313+
for (const ref of referencesOfRaw) {
2314+
if (typeof ref.path !== "string") {
2315+
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "reference.path", "string");
2316+
}
2317+
else {
2318+
(projectReferences || (projectReferences = [])).push({
2319+
path: getNormalizedAbsolutePath(ref.path, basePath),
2320+
originalPath: ref.path,
2321+
prepend: ref.prepend,
2322+
circular: ref.circular
2323+
});
2324+
}
2325+
}
2326+
}
2327+
2328+
const filesSpecs = toPropValue(getSpecsFromRaw("files"));
23122329
if (filesSpecs) {
2313-
const hasReferences = hasProperty(raw, "references") && !isNullOrUndefined(raw.references);
2314-
const hasZeroOrNoReferences = !hasReferences || raw.references.length === 0;
2330+
const hasZeroOrNoReferences = referencesOfRaw === "no-prop" || isArray(referencesOfRaw) && referencesOfRaw.length === 0;
23152331
const hasExtends = hasProperty(raw, "extends");
23162332
if (filesSpecs.length === 0 && hasZeroOrNoReferences && !hasExtends) {
23172333
if (sourceFile) {
@@ -2329,11 +2345,11 @@ namespace ts {
23292345
}
23302346
}
23312347

2332-
let includeSpecs = getSpecs("include")?.specs;
2348+
let includeSpecs = toPropValue(getSpecsFromRaw("include"));
23332349

2334-
const excludeSpecResult = getSpecs("exclude");
2335-
let excludeSpecs = excludeSpecResult?.specs;
2336-
if (!excludeSpecResult && raw.compilerOptions) {
2350+
const excludeOfRaw = getSpecsFromRaw("exclude");
2351+
let excludeSpecs = toPropValue(excludeOfRaw);
2352+
if (excludeOfRaw === "no-prop" && raw.compilerOptions) {
23372353
const outDir = raw.compilerOptions.outDir;
23382354
const declarationDir = raw.compilerOptions.declarationDir;
23392355

@@ -2351,44 +2367,33 @@ namespace ts {
23512367
errors.push(getErrorForNoInputFiles(result.spec, configFileName));
23522368
}
23532369

2354-
if (hasProperty(raw, "references") && !isNullOrUndefined(raw.references)) {
2355-
if (isArray(raw.references)) {
2356-
for (const ref of raw.references) {
2357-
if (typeof ref.path !== "string") {
2358-
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "reference.path", "string");
2359-
}
2360-
else {
2361-
(projectReferences || (projectReferences = [])).push({
2362-
path: getNormalizedAbsolutePath(ref.path, basePath),
2363-
originalPath: ref.path,
2364-
prepend: ref.prepend,
2365-
circular: ref.circular
2366-
});
2367-
}
2368-
}
2369-
}
2370-
else {
2371-
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "references", "Array");
2372-
}
2373-
}
2374-
23752370
return result;
23762371
}
23772372

2378-
function getSpecs(prop: "files" | "include" | "exclude") {
2373+
type PropOfRaw<T> = readonly T[] | "not-array" | "no-prop";
2374+
function toPropValue<T>(specResult: PropOfRaw<T>) {
2375+
return isArray(specResult) ? specResult : undefined;
2376+
}
2377+
2378+
function getSpecsFromRaw(prop: "files" | "include" | "exclude"): PropOfRaw<string> {
2379+
return getPropFromRaw(prop, isString, "string");
2380+
}
2381+
2382+
function getPropFromRaw<T>(prop: "files" | "include" | "exclude" | "references", validateElement: (value: unknown) => boolean, elementTypeName: string): PropOfRaw<T> {
23792383
if (hasProperty(raw, prop) && !isNullOrUndefined(raw[prop])) {
2380-
let specs: readonly string[] | undefined;
23812384
if (isArray(raw[prop])) {
2382-
specs = <readonly string[]>raw[prop];
2383-
if (!sourceFile && !every(specs, isString)) {
2384-
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, prop, "string"));
2385+
const result = raw[prop];
2386+
if (!sourceFile && !every(result, validateElement)) {
2387+
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, prop, elementTypeName));
23852388
}
2389+
return result;
23862390
}
23872391
else {
23882392
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, prop, "Array");
2393+
return "not-array";
23892394
}
2390-
return { specs };
23912395
}
2396+
return "no-prop";
23922397
}
23932398

23942399
function createCompilerDiagnosticOnlyIfJson(message: DiagnosticMessage, arg0?: string, arg1?: string) {

0 commit comments

Comments
 (0)