Skip to content

Commit 4935e14

Browse files
Use more explicit operations in core helpers (and other nits) (#58873)
1 parent 4239025 commit 4935e14

File tree

9 files changed

+102
-123
lines changed

9 files changed

+102
-123
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import {
1717
appendIfUnique,
1818
ArrayBindingPattern,
1919
arrayFrom,
20+
arrayIsEqualTo,
2021
arrayIsHomogeneous,
2122
ArrayLiteralExpression,
2223
arrayOf,
23-
arraysEqual,
2424
arrayToMultiMap,
2525
ArrayTypeNode,
2626
ArrowFunction,
@@ -25861,7 +25861,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2586125861
function inferTypesFromTemplateLiteralType(source: Type, target: TemplateLiteralType): Type[] | undefined {
2586225862
return source.flags & TypeFlags.StringLiteral ? inferFromLiteralPartsToTemplateLiteral([(source as StringLiteralType).value], emptyArray, target) :
2586325863
source.flags & TypeFlags.TemplateLiteral ?
25864-
arraysEqual((source as TemplateLiteralType).texts, target.texts) ? map((source as TemplateLiteralType).types, (s, i) => {
25864+
arrayIsEqualTo((source as TemplateLiteralType).texts, target.texts) ? map((source as TemplateLiteralType).types, (s, i) => {
2586525865
return isTypeAssignableTo(getBaseConstraintOrType(s), getBaseConstraintOrType(target.types[i])) ? s : getStringLikeTypeForType(s);
2586625866
}) :
2586725867
inferFromLiteralPartsToTemplateLiteral((source as TemplateLiteralType).texts, (source as TemplateLiteralType).types, target) :
@@ -28628,7 +28628,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2862828628
return getEvolvingArrayType(getUnionType(map(types, getElementTypeOfEvolvingArrayType)));
2862928629
}
2863028630
const result = recombineUnknownType(getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction));
28631-
if (result !== declaredType && result.flags & declaredType.flags & TypeFlags.Union && arraysEqual((result as UnionType).types, (declaredType as UnionType).types)) {
28631+
if (result !== declaredType && result.flags & declaredType.flags & TypeFlags.Union && arrayIsEqualTo((result as UnionType).types, (declaredType as UnionType).types)) {
2863228632
return declaredType;
2863328633
}
2863428634
return result;

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,9 +2576,7 @@ export function convertToTSConfig(configParseResult: ParsedCommandLine, configFi
25762576

25772577
/** @internal */
25782578
export function optionMapToObject(optionMap: Map<string, CompilerOptionsValue>): object {
2579-
return {
2580-
...arrayFrom(optionMap.entries()).reduce((prev, cur) => ({ ...prev, [cur[0]]: cur[1] }), {}),
2581-
};
2579+
return Object.fromEntries(optionMap);
25822580
}
25832581

25842582
function filterSameAsDefaultInclude(specs: readonly string[] | undefined) {

src/compiler/core.ts

Lines changed: 79 additions & 95 deletions
Large diffs are not rendered by default.

src/compiler/debug.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
LiteralType,
6262
map,
6363
MatchingKeys,
64+
maxBy,
6465
ModifierFlags,
6566
Node,
6667
NodeArray,
@@ -1126,7 +1127,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
11261127

11271128
function renderGraph() {
11281129
const columnCount = columnWidths.length;
1129-
const laneCount = nodes.reduce((x, n) => Math.max(x, n.lane), 0) + 1;
1130+
const laneCount = maxBy(nodes, 0, n => n.lane) + 1;
11301131
const lanes: string[] = fill(Array(laneCount), "");
11311132
const grid: (FlowGraphNode | undefined)[][] = columnWidths.map(() => Array(laneCount));
11321133
const connectors: Connection[][] = columnWidths.map(() => fill(Array(laneCount), 0));

src/compiler/scanner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
append,
3-
arraysEqual,
3+
arrayIsEqualTo,
44
binarySearch,
55
CharacterCodes,
66
CommentDirective,
@@ -475,7 +475,7 @@ export function computePositionOfLineAndCharacter(lineStarts: readonly number[],
475475
line = line < 0 ? 0 : line >= lineStarts.length ? lineStarts.length - 1 : line;
476476
}
477477
else {
478-
Debug.fail(`Bad line number. Line: ${line}, lineStarts.length: ${lineStarts.length} , line map is correct? ${debugText !== undefined ? arraysEqual(lineStarts, computeLineStarts(debugText)) : "unknown"}`);
478+
Debug.fail(`Bad line number. Line: ${line}, lineStarts.length: ${lineStarts.length} , line map is correct? ${debugText !== undefined ? arrayIsEqualTo(lineStarts, computeLineStarts(debugText)) : "unknown"}`);
479479
}
480480
}
481481

src/compiler/watch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
isReferenceFileLocation,
7474
isString,
7575
last,
76+
maxBy,
7677
maybeBind,
7778
memoize,
7879
ModuleKind,
@@ -304,7 +305,7 @@ function createTabularErrorsDisplay(filesInError: (ReportFileInError | undefined
304305

305306
const numberLength = (num: number) => Math.log(num) * Math.LOG10E + 1;
306307
const fileToErrorCount = distinctFiles.map(file => ([file, countWhere(filesInError, fileInError => fileInError!.fileName === file!.fileName)] as const));
307-
const maxErrors = fileToErrorCount.reduce((acc, value) => Math.max(acc, value[1] || 0), 0);
308+
const maxErrors = maxBy(fileToErrorCount, 0, value => value[1]);
308309

309310
const headerRow = Diagnostics.Errors_Files.message;
310311
const leftColumnHeadingLength = headerRow.split(" ")[0].length;

src/harness/fourslashImpl.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,17 +2673,12 @@ export class TestState {
26732673
if (info === undefined) return "No completion info.";
26742674
const { entries } = info;
26752675

2676-
function pad(s: string, length: number) {
2677-
return s + new Array(length - s.length + 1).join(" ");
2678-
}
2679-
function max<T>(arr: T[], selector: (x: T) => number): number {
2680-
return arr.reduce((prev, x) => Math.max(prev, selector(x)), 0);
2681-
}
2682-
const longestNameLength = max(entries, m => m.name.length);
2683-
const longestKindLength = max(entries, m => m.kind.length);
2676+
const longestNameLength = ts.maxBy(entries, 0, m => m.name.length);
2677+
const longestKindLength = ts.maxBy(entries, 0, m => m.kind.length);
26842678
entries.sort((m, n) => m.sortText > n.sortText ? 1 : m.sortText < n.sortText ? -1 : m.name > n.name ? 1 : m.name < n.name ? -1 : 0);
2685-
const membersString = entries.map(m => `${pad(m.name, longestNameLength)} ${pad(m.kind, longestKindLength)} ${m.kindModifiers} ${m.isRecommended ? "recommended " : ""}${m.source === undefined ? "" : m.source}`).join("\n");
2686-
Harness.IO.log(membersString);
2679+
2680+
const formattedEntries = entries.map(m => `${m.name.padEnd(longestNameLength)} ${m.kind.padEnd(longestKindLength)} ${m.kindModifiers} ${m.isRecommended ? "recommended " : ""}${m.source ?? ""}`);
2681+
Harness.IO.log(formattedEntries.join("\n"));
26872682
}
26882683

26892684
public printContext() {

src/server/editorServices.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import {
7777
LanguageServiceMode,
7878
length,
7979
map,
80-
mapDefinedEntries,
8180
mapDefinedIterator,
8281
missingFileModifiedTime,
8382
MultiMap,
@@ -4347,14 +4346,15 @@ export class ProjectService {
43474346

43484347
/** @internal */
43494348
loadAncestorProjectTree(forProjects?: ReadonlyCollection<string>) {
4350-
forProjects = forProjects || mapDefinedEntries(
4351-
this.configuredProjects,
4352-
(key, project) => !project.isInitialLoadPending() ? [key, true] : undefined,
4349+
forProjects ??= new Set(
4350+
mapDefinedIterator(this.configuredProjects.entries(), ([key, project]) => !project.isInitialLoadPending() ? key : undefined),
43534351
);
43544352

43554353
const seenProjects = new Set<NormalizedPath>();
4356-
// Work on array copy as we could add more projects as part of callback
4357-
for (const project of arrayFrom(this.configuredProjects.values())) {
4354+
// We must copy the current configured projects into a separate array,
4355+
// as we could end up creating and adding more projects indirectly.
4356+
const currentConfiguredProjects = arrayFrom(this.configuredProjects.values());
4357+
for (const project of currentConfiguredProjects) {
43584358
// If this project has potential project reference for any of the project we are loading ancestor tree for
43594359
// load this project first
43604360
if (forEachPotentialProjectReference(project, potentialRefPath => forProjects.has(potentialRefPath))) {

src/services/jsDoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
arraysEqual,
2+
arrayIsEqualTo,
33
ArrowFunction,
44
AssignmentDeclarationKind,
55
BinaryExpression,
@@ -222,7 +222,7 @@ export function getJsDocCommentsFromDeclarations(declarations: readonly Declarat
222222
}
223223

224224
function isIdenticalListOfDisplayParts(parts1: SymbolDisplayPart[], parts2: SymbolDisplayPart[]) {
225-
return arraysEqual(parts1, parts2, (p1, p2) => p1.kind === p2.kind && p1.text === p2.text);
225+
return arrayIsEqualTo(parts1, parts2, (p1, p2) => p1.kind === p2.kind && p1.text === p2.text);
226226
}
227227

228228
function getCommentHavingNodes(declaration: Declaration): readonly (JSDoc | JSDocTag)[] {

0 commit comments

Comments
 (0)