Skip to content

Commit f1ff0de

Browse files
authored
Use native generators/iterables, remove helper cruft (#51921)
1 parent e60c210 commit f1ff0de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+251
-532
lines changed

src/compiler/builderState.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export namespace BuilderState {
108108
export interface ReadonlyManyToManyPathMap {
109109
getKeys(v: Path): ReadonlySet<Path> | undefined;
110110
getValues(k: Path): ReadonlySet<Path> | undefined;
111-
keys(): Iterator<Path>;
111+
keys(): IterableIterator<Path>;
112112
}
113113

114114
export interface ManyToManyPathMap extends ReadonlyManyToManyPathMap {
@@ -521,9 +521,8 @@ export namespace BuilderState {
521521
seenMap.add(path);
522522
const references = state.referencedMap.getValues(path);
523523
if (references) {
524-
const iterator = references.keys();
525-
for (let iterResult = iterator.next(); !iterResult.done; iterResult = iterator.next()) {
526-
queue.push(iterResult.value);
524+
for (const key of references.keys()) {
525+
queue.push(key);
527526
}
528527
}
529528
}

src/compiler/checker.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ import {
181181
first,
182182
firstDefined,
183183
firstOrUndefined,
184+
firstOrUndefinedIterator,
184185
flatMap,
185186
flatten,
186187
FlowArrayMutation,
@@ -259,7 +260,6 @@ import {
259260
getEmitScriptTarget,
260261
getEnclosingBlockScopeContainer,
261262
getEntityNameFromTypeNode,
262-
getEntries,
263263
getErrorSpanForNode,
264264
getEscapedTextOfIdentifierOrLiteral,
265265
getESModuleInterop,
@@ -347,8 +347,8 @@ import {
347347
hasAccessorModifier,
348348
hasAmbientModifier,
349349
hasContextSensitiveParameters,
350-
hasDecorators,
351350
HasDecorators,
351+
hasDecorators,
352352
hasDynamicName,
353353
hasEffectiveModifier,
354354
hasEffectiveModifiers,
@@ -357,8 +357,8 @@ import {
357357
hasExtension,
358358
HasIllegalDecorators,
359359
HasIllegalModifiers,
360-
hasInitializer,
361360
HasInitializer,
361+
hasInitializer,
362362
hasJSDocNodes,
363363
hasJSDocParameterTags,
364364
hasJsonModuleEmitEnabled,
@@ -926,7 +926,6 @@ import {
926926
stripQuotes,
927927
StructuredType,
928928
SubstitutionType,
929-
sum,
930929
SuperCall,
931930
SwitchStatement,
932931
Symbol,
@@ -1180,7 +1179,7 @@ export const enum TypeFacts {
11801179
AndFactsMask = All & ~OrFactsMask,
11811180
}
11821181

1183-
const typeofNEFacts: ReadonlyMap<string, TypeFacts> = new Map(getEntries({
1182+
const typeofNEFacts: ReadonlyMap<string, TypeFacts> = new Map(Object.entries({
11841183
string: TypeFacts.TypeofNEString,
11851184
number: TypeFacts.TypeofNENumber,
11861185
bigint: TypeFacts.TypeofNEBigInt,
@@ -1301,7 +1300,7 @@ const enum IntrinsicTypeKind {
13011300
Uncapitalize
13021301
}
13031302

1304-
const intrinsicTypeKinds: ReadonlyMap<string, IntrinsicTypeKind> = new Map(getEntries({
1303+
const intrinsicTypeKinds: ReadonlyMap<string, IntrinsicTypeKind> = new Map(Object.entries({
13051304
Uppercase: IntrinsicTypeKind.Uppercase,
13061305
Lowercase: IntrinsicTypeKind.Lowercase,
13071306
Capitalize: IntrinsicTypeKind.Capitalize,
@@ -1436,9 +1435,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
14361435
// extra cost of calling `getParseTreeNode` when calling these functions from inside the
14371436
// checker.
14381437
const checker: TypeChecker = {
1439-
getNodeCount: () => sum(host.getSourceFiles(), "nodeCount"),
1440-
getIdentifierCount: () => sum(host.getSourceFiles(), "identifierCount"),
1441-
getSymbolCount: () => sum(host.getSourceFiles(), "symbolCount") + symbolCount,
1438+
getNodeCount: () => reduceLeft(host.getSourceFiles(), (n, s) => n + s.nodeCount, 0),
1439+
getIdentifierCount: () => reduceLeft(host.getSourceFiles(), (n, s) => n + s.identifierCount, 0),
1440+
getSymbolCount: () => reduceLeft(host.getSourceFiles(), (n, s) => n + s.symbolCount, symbolCount),
14421441
getTypeCount: () => typeCount,
14431442
getInstantiationCount: () => totalInstantiationCount,
14441443
getRelationCacheSizes: () => ({
@@ -13649,7 +13648,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1364913648
isUnion &&
1365013649
(propSet || checkFlags & CheckFlags.Partial) &&
1365113650
checkFlags & (CheckFlags.ContainsPrivate | CheckFlags.ContainsProtected) &&
13652-
!(propSet && getCommonDeclarationsOfSymbols(arrayFrom(propSet.values())))
13651+
!(propSet && getCommonDeclarationsOfSymbols(propSet.values()))
1365313652
) {
1365413653
// No property was found, or, in a union, a property has a private or protected declaration in one
1365513654
// constituent, but is missing or has a different declaration in another constituent.
@@ -13757,7 +13756,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1375713756
return property;
1375813757
}
1375913758

13760-
function getCommonDeclarationsOfSymbols(symbols: readonly Symbol[]) {
13759+
function getCommonDeclarationsOfSymbols(symbols: Iterable<Symbol>) {
1376113760
let commonDeclarations: Set<Node> | undefined;
1376213761
for (const symbol of symbols) {
1376313762
if (!symbol.declarations) {
@@ -19009,8 +19008,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1900919008
) {
1901019009
// Assignability failure - check each prop individually, and if that fails, fall back on the bad error span
1901119010
let reportedError = false;
19012-
for (let status = iterator.next(); !status.done; status = iterator.next()) {
19013-
const { errorNode: prop, innerExpression: next, nameType, errorMessage } = status.value;
19011+
for (const value of iterator) {
19012+
const { errorNode: prop, innerExpression: next, nameType, errorMessage } = value;
1901419013
let targetPropType = getBestMatchIndexedAccessTypeOrUndefined(source, target, nameType);
1901519014
if (!targetPropType || targetPropType.flags & TypeFlags.IndexedAccess) continue; // Don't elaborate on indexes on generic variables
1901619015
let sourcePropType = getIndexedAccessTypeOrUndefined(source, nameType);
@@ -23563,8 +23562,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2356323562
}
2356423563

2356523564
function getUnmatchedProperty(source: Type, target: Type, requireOptionalProperties: boolean, matchDiscriminantProperties: boolean): Symbol | undefined {
23566-
const result = getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties).next();
23567-
if (!result.done) return result.value;
23565+
return firstOrUndefinedIterator(getUnmatchedProperties(source, target, requireOptionalProperties, matchDiscriminantProperties));
2356823566
}
2356923567

2357023568
function tupleTypesDefinitelyUnrelated(source: TupleTypeReference, target: TupleTypeReference) {

src/compiler/commandLineParser.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ import {
4141
find,
4242
findIndex,
4343
firstDefined,
44+
firstOrUndefinedIterator,
4445
flatten,
4546
forEach,
4647
forEachEntry,
4748
getBaseFileName,
4849
getDirectoryPath,
49-
getEntries,
5050
getFileMatcherPatterns,
5151
getLocaleSpecificMessage,
5252
getNormalizedAbsolutePath,
@@ -128,7 +128,7 @@ export const compileOnSaveCommandLineOption: CommandLineOption = {
128128
defaultValueDescription: false,
129129
};
130130

131-
const jsxOptionMap = new Map(getEntries({
131+
const jsxOptionMap = new Map(Object.entries({
132132
"preserve": JsxEmit.Preserve,
133133
"react-native": JsxEmit.ReactNative,
134134
"react": JsxEmit.React,
@@ -137,7 +137,7 @@ const jsxOptionMap = new Map(getEntries({
137137
}));
138138

139139
/** @internal */
140-
export const inverseJsxOptionMap = new Map(arrayFrom(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const)));
140+
export const inverseJsxOptionMap = new Map(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const));
141141

142142
// NOTE: The order here is important to default lib ordering as entries will have the same
143143
// order in the generated program (see `getDefaultLibPriority` in program.ts). This
@@ -241,7 +241,7 @@ export const libMap = new Map(libEntries);
241241
export const optionsForWatch: CommandLineOption[] = [
242242
{
243243
name: "watchFile",
244-
type: new Map(getEntries({
244+
type: new Map(Object.entries({
245245
fixedpollinginterval: WatchFileKind.FixedPollingInterval,
246246
prioritypollinginterval: WatchFileKind.PriorityPollingInterval,
247247
dynamicprioritypolling: WatchFileKind.DynamicPriorityPolling,
@@ -255,7 +255,7 @@ export const optionsForWatch: CommandLineOption[] = [
255255
},
256256
{
257257
name: "watchDirectory",
258-
type: new Map(getEntries({
258+
type: new Map(Object.entries({
259259
usefsevents: WatchDirectoryKind.UseFsEvents,
260260
fixedpollinginterval: WatchDirectoryKind.FixedPollingInterval,
261261
dynamicprioritypolling: WatchDirectoryKind.DynamicPriorityPolling,
@@ -267,7 +267,7 @@ export const optionsForWatch: CommandLineOption[] = [
267267
},
268268
{
269269
name: "fallbackPolling",
270-
type: new Map(getEntries({
270+
type: new Map(Object.entries({
271271
fixedinterval: PollingWatchKind.FixedInterval,
272272
priorityinterval: PollingWatchKind.PriorityInterval,
273273
dynamicpriority: PollingWatchKind.DynamicPriority,
@@ -502,7 +502,7 @@ export const commonOptionsWithBuild: CommandLineOption[] = [
502502
export const targetOptionDeclaration: CommandLineOptionOfCustomType = {
503503
name: "target",
504504
shortName: "t",
505-
type: new Map(getEntries({
505+
type: new Map(Object.entries({
506506
es3: ScriptTarget.ES3,
507507
es5: ScriptTarget.ES5,
508508
es6: ScriptTarget.ES2015,
@@ -531,7 +531,7 @@ export const targetOptionDeclaration: CommandLineOptionOfCustomType = {
531531
export const moduleOptionDeclaration: CommandLineOptionOfCustomType = {
532532
name: "module",
533533
shortName: "m",
534-
type: new Map(getEntries({
534+
type: new Map(Object.entries({
535535
none: ModuleKind.None,
536536
commonjs: ModuleKind.CommonJS,
537537
amd: ModuleKind.AMD,
@@ -756,7 +756,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
756756
},
757757
{
758758
name: "importsNotUsedAsValues",
759-
type: new Map(getEntries({
759+
type: new Map(Object.entries({
760760
remove: ImportsNotUsedAsValues.Remove,
761761
preserve: ImportsNotUsedAsValues.Preserve,
762762
error: ImportsNotUsedAsValues.Error,
@@ -961,7 +961,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
961961
// Module Resolution
962962
{
963963
name: "moduleResolution",
964-
type: new Map(getEntries({
964+
type: new Map(Object.entries({
965965
// N.B. The first entry specifies the value shown in `tsc --init`
966966
node10: ModuleResolutionKind.Node10,
967967
node: ModuleResolutionKind.Node10,
@@ -1256,7 +1256,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
12561256
},
12571257
{
12581258
name: "newLine",
1259-
type: new Map(getEntries({
1259+
type: new Map(Object.entries({
12601260
crlf: NewLineKind.CarriageReturnLineFeed,
12611261
lf: NewLineKind.LineFeed
12621262
})),
@@ -1502,7 +1502,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
15021502
},
15031503
{
15041504
name: "moduleDetection",
1505-
type: new Map(getEntries({
1505+
type: new Map(Object.entries({
15061506
auto: ModuleDetectionKind.Auto,
15071507
legacy: ModuleDetectionKind.Legacy,
15081508
force: ModuleDetectionKind.Force,
@@ -4033,8 +4033,8 @@ function getDefaultValueForOption(option: CommandLineOption): {} {
40334033
case "object":
40344034
return {};
40354035
default:
4036-
const iterResult = option.type.keys().next();
4037-
if (!iterResult.done) return iterResult.value;
4036+
const value = firstOrUndefinedIterator(option.type.keys());
4037+
if (value !== undefined) return value;
40384038
return Debug.fail("Expected 'option.type' to have entries.");
40394039
}
40404040
}

0 commit comments

Comments
 (0)