Skip to content

Commit f0bd91c

Browse files
author
Andy
authored
Convert Array to ReadonlyArray/Push in commandLineParser.ts (microsoft#17323)
1 parent fe86d2f commit f0bd91c

3 files changed

Lines changed: 54 additions & 53 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -728,12 +728,12 @@ namespace ts {
728728
}
729729

730730
/* @internal */
731-
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
731+
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Push<Diagnostic>) {
732732
return convertJsonOptionOfCustomType(opt, trimString(value || ""), errors);
733733
}
734734

735735
/* @internal */
736-
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Diagnostic[]): (string | number)[] | undefined {
736+
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Push<Diagnostic>): (string | number)[] | undefined {
737737
value = trimString(value);
738738
if (startsWith(value, "-")) {
739739
return undefined;
@@ -752,7 +752,7 @@ namespace ts {
752752
}
753753
}
754754

755-
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string | undefined): ParsedCommandLine {
755+
export function parseCommandLine(commandLine: ReadonlyArray<string>, readFile?: (path: string) => string | undefined): ParsedCommandLine {
756756
const options: CompilerOptions = {};
757757
const fileNames: string[] = [];
758758
const errors: Diagnostic[] = [];
@@ -764,7 +764,7 @@ namespace ts {
764764
errors
765765
};
766766

767-
function parseStrings(args: string[]) {
767+
function parseStrings(args: ReadonlyArray<string>) {
768768
let i = 0;
769769
while (i < args.length) {
770770
const s = args[i];
@@ -916,7 +916,7 @@ namespace ts {
916916
return text === undefined ? createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, fileName) : text;
917917
}
918918

919-
function commandLineOptionsToMap(options: CommandLineOption[]) {
919+
function commandLineOptionsToMap(options: ReadonlyArray<CommandLineOption>) {
920920
return arrayToMap(options, option => option.name);
921921
}
922922

@@ -1007,7 +1007,7 @@ namespace ts {
10071007
/**
10081008
* Convert the json syntax tree into the json value
10091009
*/
1010-
export function convertToObject(sourceFile: JsonSourceFile, errors: Diagnostic[]): any {
1010+
export function convertToObject(sourceFile: JsonSourceFile, errors: Push<Diagnostic>): any {
10111011
return convertToObjectWorker(sourceFile, errors, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined);
10121012
}
10131013

@@ -1016,7 +1016,7 @@ namespace ts {
10161016
*/
10171017
function convertToObjectWorker(
10181018
sourceFile: JsonSourceFile,
1019-
errors: Diagnostic[],
1019+
errors: Push<Diagnostic>,
10201020
knownRootOptions: Map<CommandLineOption> | undefined,
10211021
jsonConversionNotifier: JsonConversionNotifier | undefined): any {
10221022
if (!sourceFile.jsonObject) {
@@ -1085,11 +1085,7 @@ namespace ts {
10851085
elements: NodeArray<Expression>,
10861086
elementOption: CommandLineOption | undefined
10871087
): any[] {
1088-
const result: any[] = [];
1089-
for (const element of elements) {
1090-
result.push(convertPropertyValueToJson(element, elementOption));
1091-
}
1092-
return result;
1088+
return elements.map(element => convertPropertyValueToJson(element, elementOption));
10931089
}
10941090

10951091
function convertPropertyValueToJson(valueExpression: Expression, option: CommandLineOption): any {
@@ -1202,9 +1198,9 @@ namespace ts {
12021198
* @param fileNames array of filenames to be generated into tsconfig.json
12031199
*/
12041200
/* @internal */
1205-
export function generateTSConfig(options: CompilerOptions, fileNames: string[], newLine: string): string {
1201+
export function generateTSConfig(options: CompilerOptions, fileNames: ReadonlyArray<string>, newLine: string): string {
12061202
const compilerOptions = extend(options, defaultInitCompilerOptions);
1207-
const configurations: { compilerOptions: MapLike<CompilerOptionsValue>; files?: string[] } = {
1203+
const configurations: { compilerOptions: MapLike<CompilerOptionsValue>; files?: ReadonlyArray<string> } = {
12081204
compilerOptions: serializeCompilerOptions(compilerOptions)
12091205
};
12101206
if (fileNames && fileNames.length) {
@@ -1259,11 +1255,7 @@ namespace ts {
12591255
}
12601256
else {
12611257
if (optionDefinition.type === "list") {
1262-
const convertedValue: string[] = [];
1263-
for (const element of value as (string | number)[]) {
1264-
convertedValue.push(getNameOfCompilerOptionValue(element, customTypeMap));
1265-
}
1266-
result[name] = convertedValue;
1258+
result[name] = (value as ReadonlyArray<string | number>).map(element => getNameOfCompilerOptionValue(element, customTypeMap));
12671259
}
12681260
else {
12691261
// There is a typeMap associated with this command-line option so use it to map value back to its name
@@ -1371,7 +1363,7 @@ namespace ts {
13711363
* @param basePath A root directory to resolve relative path entries in the config
13721364
* file to. e.g. outDir
13731365
*/
1374-
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: JsFileExtensionInfo[]): ParsedCommandLine {
1366+
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine {
13751367
return parseJsonConfigFileContentWorker(json, /*sourceFile*/ undefined, host, basePath, existingOptions, configFileName, resolutionStack, extraFileExtensions);
13761368
}
13771369

@@ -1382,7 +1374,7 @@ namespace ts {
13821374
* @param basePath A root directory to resolve relative path entries in the config
13831375
* file to. e.g. outDir
13841376
*/
1385-
export function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: JsFileExtensionInfo[]): ParsedCommandLine {
1377+
export function parseJsonSourceFileConfigFileContent(sourceFile: JsonSourceFile, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[], extraFileExtensions?: ReadonlyArray<JsFileExtensionInfo>): ParsedCommandLine {
13861378
return parseJsonConfigFileContentWorker(/*json*/ undefined, sourceFile, host, basePath, existingOptions, configFileName, resolutionStack, extraFileExtensions);
13871379
}
13881380

@@ -1410,7 +1402,7 @@ namespace ts {
14101402
existingOptions: CompilerOptions = {},
14111403
configFileName?: string,
14121404
resolutionStack: Path[] = [],
1413-
extraFileExtensions: JsFileExtensionInfo[] = [],
1405+
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo> = [],
14141406
): ParsedCommandLine {
14151407
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
14161408
const errors: Diagnostic[] = [];
@@ -1432,10 +1424,10 @@ namespace ts {
14321424
};
14331425

14341426
function getFileNames(): ExpandResult {
1435-
let fileNames: string[];
1427+
let fileNames: ReadonlyArray<string>;
14361428
if (hasProperty(raw, "files")) {
14371429
if (isArray(raw["files"])) {
1438-
fileNames = <string[]>raw["files"];
1430+
fileNames = <ReadonlyArray<string>>raw["files"];
14391431
if (fileNames.length === 0) {
14401432
createCompilerDiagnosticOnlyIfJson(Diagnostics.The_files_list_in_config_file_0_is_empty, configFileName || "tsconfig.json");
14411433
}
@@ -1445,33 +1437,34 @@ namespace ts {
14451437
}
14461438
}
14471439

1448-
let includeSpecs: string[];
1440+
let includeSpecs: ReadonlyArray<string>;
14491441
if (hasProperty(raw, "include")) {
14501442
if (isArray(raw["include"])) {
1451-
includeSpecs = <string[]>raw["include"];
1443+
includeSpecs = <ReadonlyArray<string>>raw["include"];
14521444
}
14531445
else {
14541446
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "include", "Array");
14551447
}
14561448
}
14571449

1458-
let excludeSpecs: string[];
1450+
let excludeSpecs: ReadonlyArray<string>;
14591451
if (hasProperty(raw, "exclude")) {
14601452
if (isArray(raw["exclude"])) {
1461-
excludeSpecs = <string[]>raw["exclude"];
1453+
excludeSpecs = <ReadonlyArray<string>>raw["exclude"];
14621454
}
14631455
else {
14641456
createCompilerDiagnosticOnlyIfJson(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "exclude", "Array");
14651457
}
14661458
}
14671459
else {
14681460
// If no includes were specified, exclude common package folders and the outDir
1469-
excludeSpecs = includeSpecs ? [] : ["node_modules", "bower_components", "jspm_packages"];
1461+
const specs = includeSpecs ? [] : ["node_modules", "bower_components", "jspm_packages"];
14701462

14711463
const outDir = raw["compilerOptions"] && raw["compilerOptions"]["outDir"];
14721464
if (outDir) {
1473-
excludeSpecs.push(outDir);
1465+
specs.push(outDir);
14741466
}
1467+
excludeSpecs = specs;
14751468
}
14761469

14771470
if (fileNames === undefined && includeSpecs === undefined) {
@@ -1521,7 +1514,7 @@ namespace ts {
15211514
basePath: string,
15221515
configFileName: string,
15231516
resolutionStack: Path[],
1524-
errors: Diagnostic[],
1517+
errors: Push<Diagnostic>,
15251518
): ParsedTsconfig {
15261519
basePath = normalizeSlashes(basePath);
15271520
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames);
@@ -1570,7 +1563,7 @@ namespace ts {
15701563
basePath: string,
15711564
getCanonicalFileName: (fileName: string) => string,
15721565
configFileName: string,
1573-
errors: Diagnostic[]
1566+
errors: Push<Diagnostic>
15741567
): ParsedTsconfig {
15751568
if (hasProperty(json, "excludes")) {
15761569
errors.push(createCompilerDiagnostic(Diagnostics.Unknown_option_excludes_Did_you_mean_exclude));
@@ -1600,7 +1593,7 @@ namespace ts {
16001593
basePath: string,
16011594
getCanonicalFileName: (fileName: string) => string,
16021595
configFileName: string,
1603-
errors: Diagnostic[]
1596+
errors: Push<Diagnostic>
16041597
): ParsedTsconfig {
16051598
const options = getDefaultCompilerOptions(configFileName);
16061599
let typeAcquisition: TypeAcquisition, typingOptionstypeAcquisition: TypeAcquisition;
@@ -1631,7 +1624,7 @@ namespace ts {
16311624
);
16321625
return;
16331626
case "files":
1634-
if ((<string[]>value).length === 0) {
1627+
if ((<ReadonlyArray<string>>value).length === 0) {
16351628
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, valueNode, Diagnostics.The_files_list_in_config_file_0_is_empty, configFileName || "tsconfig.json"));
16361629
}
16371630
return;
@@ -1667,7 +1660,7 @@ namespace ts {
16671660
host: ParseConfigHost,
16681661
basePath: string,
16691662
getCanonicalFileName: (fileName: string) => string,
1670-
errors: Diagnostic[],
1663+
errors: Push<Diagnostic>,
16711664
createDiagnostic: (message: DiagnosticMessage, arg1?: string) => Diagnostic) {
16721665
extendedConfig = normalizeSlashes(extendedConfig);
16731666
// If the path isn't a rooted or relative path, don't try to resolve it (we reserve the right to special case module-id like paths in the future)
@@ -1693,7 +1686,7 @@ namespace ts {
16931686
basePath: string,
16941687
getCanonicalFileName: (fileName: string) => string,
16951688
resolutionStack: Path[],
1696-
errors: Diagnostic[],
1689+
errors: Push<Diagnostic>,
16971690
): ParsedTsconfig | undefined {
16981691
const extendedResult = readJsonConfigFile(extendedConfigPath, path => host.readFile(path));
16991692
if (sourceFile) {
@@ -1730,7 +1723,7 @@ namespace ts {
17301723
return extendedConfig;
17311724
}
17321725

1733-
function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean {
1726+
function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Push<Diagnostic>): boolean {
17341727
if (!hasProperty(jsonOption, compileOnSaveCommandLineOption.name)) {
17351728
return undefined;
17361729
}
@@ -1761,7 +1754,7 @@ namespace ts {
17611754
}
17621755

17631756
function convertCompilerOptionsFromJsonWorker(jsonOptions: any,
1764-
basePath: string, errors: Diagnostic[], configFileName?: string): CompilerOptions {
1757+
basePath: string, errors: Push<Diagnostic>, configFileName?: string): CompilerOptions {
17651758

17661759
const options = getDefaultCompilerOptions(configFileName);
17671760
convertOptionsFromJson(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors);
@@ -1774,7 +1767,7 @@ namespace ts {
17741767
}
17751768

17761769
function convertTypeAcquisitionFromJsonWorker(jsonOptions: any,
1777-
basePath: string, errors: Diagnostic[], configFileName?: string): TypeAcquisition {
1770+
basePath: string, errors: Push<Diagnostic>, configFileName?: string): TypeAcquisition {
17781771

17791772
const options = getDefaultTypeAcquisition(configFileName);
17801773
const typeAcquisition = convertEnableAutoDiscoveryToEnable(jsonOptions);
@@ -1783,8 +1776,8 @@ namespace ts {
17831776
return options;
17841777
}
17851778

1786-
function convertOptionsFromJson(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string,
1787-
defaultOptions: CompilerOptions | TypeAcquisition, diagnosticMessage: DiagnosticMessage, errors: Diagnostic[]) {
1779+
function convertOptionsFromJson(optionDeclarations: ReadonlyArray<CommandLineOption>, jsonOptions: any, basePath: string,
1780+
defaultOptions: CompilerOptions | TypeAcquisition, diagnosticMessage: DiagnosticMessage, errors: Push<Diagnostic>) {
17881781

17891782
if (!jsonOptions) {
17901783
return;
@@ -1803,7 +1796,7 @@ namespace ts {
18031796
}
18041797
}
18051798

1806-
function convertJsonOption(opt: CommandLineOption, value: any, basePath: string, errors: Diagnostic[]): CompilerOptionsValue {
1799+
function convertJsonOption(opt: CommandLineOption, value: any, basePath: string, errors: Push<Diagnostic>): CompilerOptionsValue {
18071800
if (isCompilerOptionsValue(opt, value)) {
18081801
const optType = opt.type;
18091802
if (optType === "list" && isArray(value)) {
@@ -1843,7 +1836,7 @@ namespace ts {
18431836
return value;
18441837
}
18451838

1846-
function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
1839+
function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Push<Diagnostic>) {
18471840
const key = value.toLowerCase();
18481841
const val = opt.type.get(key);
18491842
if (val !== undefined) {
@@ -1854,7 +1847,7 @@ namespace ts {
18541847
}
18551848
}
18561849

1857-
function convertJsonOptionOfListType(option: CommandLineOptionOfListType, values: any[], basePath: string, errors: Diagnostic[]): any[] {
1850+
function convertJsonOptionOfListType(option: CommandLineOptionOfListType, values: ReadonlyArray<any>, basePath: string, errors: Push<Diagnostic>): any[] {
18581851
return filter(map(values, v => convertJsonOption(option.element, v, basePath, errors)), v => !!v);
18591852
}
18601853

@@ -1945,7 +1938,16 @@ namespace ts {
19451938
* @param host The host used to resolve files and directories.
19461939
* @param errors An array for diagnostic reporting.
19471940
*/
1948-
function matchFileNames(fileNames: string[], include: string[], exclude: string[], basePath: string, options: CompilerOptions, host: ParseConfigHost, errors: Diagnostic[], extraFileExtensions: JsFileExtensionInfo[], jsonSourceFile: JsonSourceFile): ExpandResult {
1941+
function matchFileNames(
1942+
fileNames: ReadonlyArray<string>,
1943+
include: ReadonlyArray<string>,
1944+
exclude: ReadonlyArray<string>,
1945+
basePath: string,
1946+
options: CompilerOptions,
1947+
host: ParseConfigHost,
1948+
errors: Push<Diagnostic>,
1949+
extraFileExtensions: ReadonlyArray<JsFileExtensionInfo>,
1950+
jsonSourceFile: JsonSourceFile): ExpandResult {
19491951
basePath = normalizePath(basePath);
19501952

19511953
// The exclude spec list is converted into a regular expression, which allows us to quickly
@@ -2023,7 +2025,7 @@ namespace ts {
20232025
};
20242026
}
20252027

2026-
function validateSpecs(specs: string[], errors: Diagnostic[], allowTrailingRecursion: boolean, jsonSourceFile: JsonSourceFile, specKey: string) {
2028+
function validateSpecs(specs: ReadonlyArray<string>, errors: Push<Diagnostic>, allowTrailingRecursion: boolean, jsonSourceFile: JsonSourceFile, specKey: string) {
20272029
const validSpecs: string[] = [];
20282030
for (const spec of specs) {
20292031
if (!allowTrailingRecursion && invalidTrailingRecursionPattern.test(spec)) {
@@ -2061,7 +2063,7 @@ namespace ts {
20612063
/**
20622064
* Gets directories in a set of include patterns that should be watched for changes.
20632065
*/
2064-
function getWildcardDirectories(include: string[], exclude: string[], path: string, useCaseSensitiveFileNames: boolean): MapLike<WatchDirectoryFlags> {
2066+
function getWildcardDirectories(include: ReadonlyArray<string>, exclude: ReadonlyArray<string>, path: string, useCaseSensitiveFileNames: boolean): MapLike<WatchDirectoryFlags> {
20652067
// We watch a directory recursively if it contains a wildcard anywhere in a directory segment
20662068
// of the pattern:
20672069
//

src/compiler/moduleNameResolver.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ namespace ts {
1313
return compilerOptions.traceResolution && host.trace !== undefined;
1414
}
1515

16-
/** Array that is only intended to be pushed to, never read. */
17-
/* @internal */
18-
export interface Push<T> {
19-
push(value: T): void;
20-
}
21-
2216
/**
2317
* Result of trying to resolve a module.
2418
* At least one of `ts` and `js` should be defined, or the whole thing should be `undefined`.

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace ts {
3131
next(): { value: T, done: false } | { value: never, done: true };
3232
}
3333

34+
/** Array that is only intended to be pushed to, never read. */
35+
export interface Push<T> {
36+
push(...values: T[]): void;
37+
}
38+
3439
// branded string type used to store absolute, normalized and canonicalized paths
3540
// arbitrary file name can be converted to Path via toPath function
3641
export type Path = string & { __pathBrand: any };

0 commit comments

Comments
 (0)