Skip to content

Commit fc18f08

Browse files
author
Andy
authored
Remove 'indexOf' helper, use 'arr.indexOf()' (microsoft#20194)
1 parent f83283c commit fc18f08

File tree

8 files changed

+29
-38
lines changed

8 files changed

+29
-38
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ namespace ts {
303303
// without names can only come from JSDocFunctionTypes.
304304
Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType);
305305
const functionType = <JSDocFunctionType>node.parent;
306-
const index = indexOf(functionType.parameters, node);
306+
const index = functionType.parameters.indexOf(node as ParameterDeclaration);
307307
return "arg" + index as __String;
308308
case SyntaxKind.JSDocTypedefTag:
309309
const name = getNameOfJSDocTypedef(node as JSDocTypedefTag);
@@ -2538,7 +2538,7 @@ namespace ts {
25382538
}
25392539

25402540
if (isBindingPattern(node.name)) {
2541-
bindAnonymousDeclaration(node, SymbolFlags.FunctionScopedVariable, "__" + indexOf(node.parent.parameters, node) as __String);
2541+
bindAnonymousDeclaration(node, SymbolFlags.FunctionScopedVariable, "__" + node.parent.parameters.indexOf(node) as __String);
25422542
}
25432543
else {
25442544
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes);

src/compiler/checker.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ namespace ts {
845845
return true;
846846
}
847847
const sourceFiles = host.getSourceFiles();
848-
return indexOf(sourceFiles, declarationFile) <= indexOf(sourceFiles, useFile);
848+
return sourceFiles.indexOf(declarationFile) <= sourceFiles.indexOf(useFile);
849849
}
850850

851851
if (declaration.pos <= usage.pos) {
@@ -4336,7 +4336,7 @@ namespace ts {
43364336
}
43374337
else {
43384338
// Use specific property type when parent is a tuple or numeric index type when parent is an array
4339-
const propName = "" + indexOf(pattern.elements, declaration);
4339+
const propName = "" + pattern.elements.indexOf(declaration);
43404340
type = isTupleLikeType(parentType)
43414341
? getTypeOfPropertyOfType(parentType, propName as __String)
43424342
: elementType;
@@ -6758,15 +6758,15 @@ namespace ts {
67586758
if (node.initializer) {
67596759
const signatureDeclaration = <SignatureDeclaration>node.parent;
67606760
const signature = getSignatureFromDeclaration(signatureDeclaration);
6761-
const parameterIndex = ts.indexOf(signatureDeclaration.parameters, node);
6761+
const parameterIndex = signatureDeclaration.parameters.indexOf(node);
67626762
Debug.assert(parameterIndex >= 0);
67636763
return parameterIndex >= signature.minArgumentCount;
67646764
}
67656765
const iife = getImmediatelyInvokedFunctionExpression(node.parent);
67666766
if (iife) {
67676767
return !node.type &&
67686768
!node.dotDotDotToken &&
6769-
indexOf((node.parent as SignatureDeclaration).parameters, node) >= iife.arguments.length;
6769+
node.parent.parameters.indexOf(node) >= iife.arguments.length;
67706770
}
67716771

67726772
return false;
@@ -8707,7 +8707,7 @@ namespace ts {
87078707
* This is used during inference when instantiating type parameter defaults.
87088708
*/
87098709
function createBackreferenceMapper(typeParameters: TypeParameter[], index: number): TypeMapper {
8710-
return t => indexOf(typeParameters, t) >= index ? emptyObjectType : t;
8710+
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
87118711
}
87128712

87138713
function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
@@ -10550,7 +10550,7 @@ namespace ts {
1055010550
let result = "" + type.target.id;
1055110551
for (const t of type.typeArguments) {
1055210552
if (isUnconstrainedTypeParameter(t)) {
10553-
let index = indexOf(typeParameters, t);
10553+
let index = typeParameters.indexOf(t);
1055410554
if (index < 0) {
1055510555
index = typeParameters.length;
1055610556
typeParameters.push(t);
@@ -12120,7 +12120,7 @@ namespace ts {
1212012120
}
1212112121

1212212122
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
12123-
return getTypeOfDestructuredArrayElement(getAssignedType(node), indexOf(node.elements, element));
12123+
return getTypeOfDestructuredArrayElement(getAssignedType(node), node.elements.indexOf(element));
1212412124
}
1212512125

1212612126
function getAssignedTypeOfSpreadExpression(node: SpreadElement): Type {
@@ -12164,7 +12164,7 @@ namespace ts {
1216412164
const type = pattern.kind === SyntaxKind.ObjectBindingPattern ?
1216512165
getTypeOfDestructuredProperty(parentType, node.propertyName || <Identifier>node.name) :
1216612166
!node.dotDotDotToken ?
12167-
getTypeOfDestructuredArrayElement(parentType, indexOf(pattern.elements, node)) :
12167+
getTypeOfDestructuredArrayElement(parentType, pattern.elements.indexOf(node)) :
1216812168
getTypeOfDestructuredSpreadExpression(parentType);
1216912169
return getTypeWithDefault(type, node.initializer);
1217012170
}
@@ -13918,7 +13918,7 @@ namespace ts {
1391813918
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
1391913919
const iife = getImmediatelyInvokedFunctionExpression(func);
1392013920
if (iife && iife.arguments) {
13921-
const indexOfParameter = indexOf(func.parameters, parameter);
13921+
const indexOfParameter = func.parameters.indexOf(parameter);
1392213922
if (parameter.dotDotDotToken) {
1392313923
const restTypes: Type[] = [];
1392413924
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
@@ -13939,7 +13939,7 @@ namespace ts {
1393913939
if (contextualSignature) {
1394013940
const funcHasRestParameters = hasRestParameter(func);
1394113941
const len = func.parameters.length - (funcHasRestParameters ? 1 : 0);
13942-
let indexOfParameter = indexOf(func.parameters, parameter);
13942+
let indexOfParameter = func.parameters.indexOf(parameter);
1394313943
if (getThisParameter(func) !== undefined && !contextualSignature.thisParameter) {
1394413944
Debug.assert(indexOfParameter !== 0); // Otherwise we should not have called `getContextuallyTypedParameterType`.
1394513945
indexOfParameter -= 1;
@@ -14070,7 +14070,7 @@ namespace ts {
1407014070
// In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter.
1407114071
function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression): Type {
1407214072
const args = getEffectiveCallArguments(callTarget);
14073-
const argIndex = indexOf(args, arg);
14073+
const argIndex = args.indexOf(arg);
1407414074
if (argIndex >= 0) {
1407514075
// If we're already in the process of resolving the given signature, don't resolve again as
1407614076
// that could cause infinite recursion. Instead, return anySignature.
@@ -17236,7 +17236,7 @@ namespace ts {
1723617236
}
1723717237
excludeCount--;
1723817238
if (excludeCount > 0) {
17239-
excludeArgument[indexOf(excludeArgument, /*value*/ true)] = false;
17239+
excludeArgument[excludeArgument.indexOf(/*value*/ true)] = false;
1724017240
}
1724117241
else {
1724217242
excludeArgument = undefined;
@@ -19497,7 +19497,7 @@ namespace ts {
1949719497
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
1949819498
}
1949919499
if (node.name && isIdentifier(node.name) && (node.name.escapedText === "this" || node.name.escapedText === "new")) {
19500-
if (indexOf(func.parameters, node) !== 0) {
19500+
if (func.parameters.indexOf(node) !== 0) {
1950119501
error(node, Diagnostics.A_0_parameter_must_be_the_first_parameter, node.name.escapedText as string);
1950219502
}
1950319503
if (func.kind === SyntaxKind.Constructor || func.kind === SyntaxKind.ConstructSignature || func.kind === SyntaxKind.ConstructorType) {
@@ -20604,7 +20604,7 @@ namespace ts {
2060420604

2060520605
const promisedType = getPromisedTypeOfPromise(type);
2060620606
if (promisedType) {
20607-
if (type.id === promisedType.id || indexOf(awaitedTypeStack, promisedType.id) >= 0) {
20607+
if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
2060820608
// Verify that we don't have a bad actor in the form of a promise whose
2060920609
// promised type is the same as the promise type, or a mutually recursive
2061020610
// promise. If so, we return undefined as we cannot guess the shape. If this
@@ -24651,7 +24651,7 @@ namespace ts {
2465124651
const typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>expr.parent);
2465224652
const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false, /*allowAsyncIterables*/ false) || unknownType;
2465324653
return checkArrayLiteralDestructuringElementAssignment(<ArrayLiteralExpression>expr.parent, typeOfArrayLiteral,
24654-
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
24654+
(<ArrayLiteralExpression>expr.parent).elements.indexOf(expr), elementType || unknownType);
2465524655
}
2465624656

2465724657
// Gets the property symbol corresponding to the property in destructuring assignment

src/compiler/core.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,6 @@ namespace ts {
335335
return false;
336336
}
337337

338-
export function indexOf<T>(array: ReadonlyArray<T>, value: T): number {
339-
if (array) {
340-
for (let i = 0; i < array.length; i++) {
341-
if (array[i] === value) {
342-
return i;
343-
}
344-
}
345-
}
346-
return -1;
347-
}
348-
349338
export function indexOfAnyCharCode(text: string, charCodes: ReadonlyArray<number>, start?: number): number {
350339
for (let i = start || 0; i < text.length; i++) {
351340
if (contains(charCodes, text.charCodeAt(i))) {

src/compiler/sourcemap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ namespace ts {
420420
host.getCanonicalFileName,
421421
/*isAbsolutePathAnUrl*/ true);
422422

423-
sourceMapSourceIndex = indexOf(sourceMapData.sourceMapSources, source);
423+
sourceMapSourceIndex = sourceMapData.sourceMapSources.indexOf(source);
424424
if (sourceMapSourceIndex === -1) {
425425
sourceMapSourceIndex = sourceMapData.sourceMapSources.length;
426426
sourceMapData.sourceMapSources.push(source);

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ namespace ts {
803803
return node === (<TypeAssertion>parent).type;
804804
case SyntaxKind.CallExpression:
805805
case SyntaxKind.NewExpression:
806-
return (<CallExpression>parent).typeArguments && indexOf((<CallExpression>parent).typeArguments, node) >= 0;
806+
return contains((<CallExpression>parent).typeArguments, node);
807807
case SyntaxKind.TaggedTemplateExpression:
808808
// TODO (drosen): TaggedTemplateExpressions may eventually support type arguments.
809809
return false;

src/services/breakpoints.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ namespace ts.BreakpointResolver {
427427
}
428428
else {
429429
const functionDeclaration = <FunctionLikeDeclaration>parameter.parent;
430-
const indexOfParameter = indexOf(functionDeclaration.parameters, parameter);
431-
if (indexOfParameter) {
430+
const indexOfParameter = functionDeclaration.parameters.indexOf(parameter);
431+
Debug.assert(indexOfParameter !== -1);
432+
if (indexOfParameter !== 0) {
432433
// Not a first parameter, go to previous parameter
433434
return spanInParameterDeclaration(functionDeclaration.parameters[indexOfParameter - 1]);
434435
}

src/services/formatting/smartIndenter.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,13 @@ namespace ts.formatting {
367367

368368
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings): number {
369369
const containingList = getContainingList(node, sourceFile);
370-
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
371-
372-
function getActualIndentationFromList(list: ReadonlyArray<Node>): number {
373-
const index = indexOf(list, node);
374-
return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : Value.Unknown;
370+
if (containingList) {
371+
const index = containingList.indexOf(node);
372+
if (index !== -1) {
373+
return deriveActualIndentationFromList(containingList, index, sourceFile, options);
374+
}
375375
}
376+
return Value.Unknown;
376377
}
377378

378379
function getLineIndentationWhenExpressionIsInMultiLine(node: Node, sourceFile: SourceFile, options: EditorSettings): number {

src/services/jsDoc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace ts.JsDoc {
112112
function forEachUnique<T, U>(array: T[], callback: (element: T, index: number) => U): U {
113113
if (array) {
114114
for (let i = 0; i < array.length; i++) {
115-
if (indexOf(array, array[i]) === i) {
115+
if (array.indexOf(array[i]) === i) {
116116
const result = callback(array[i], i);
117117
if (result) {
118118
return result;

0 commit comments

Comments
 (0)