Remove 'indexOf' helper, use 'arr.indexOf()' (#20194)

This commit is contained in:
Andy 2018-01-08 10:39:52 -08:00 committed by GitHub
parent f83283c068
commit fc18f08e63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 38 deletions

View File

@ -303,7 +303,7 @@ namespace ts {
// without names can only come from JSDocFunctionTypes.
Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType);
const functionType = <JSDocFunctionType>node.parent;
const index = indexOf(functionType.parameters, node);
const index = functionType.parameters.indexOf(node as ParameterDeclaration);
return "arg" + index as __String;
case SyntaxKind.JSDocTypedefTag:
const name = getNameOfJSDocTypedef(node as JSDocTypedefTag);
@ -2538,7 +2538,7 @@ namespace ts {
}
if (isBindingPattern(node.name)) {
bindAnonymousDeclaration(node, SymbolFlags.FunctionScopedVariable, "__" + indexOf(node.parent.parameters, node) as __String);
bindAnonymousDeclaration(node, SymbolFlags.FunctionScopedVariable, "__" + node.parent.parameters.indexOf(node) as __String);
}
else {
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes);

View File

@ -845,7 +845,7 @@ namespace ts {
return true;
}
const sourceFiles = host.getSourceFiles();
return indexOf(sourceFiles, declarationFile) <= indexOf(sourceFiles, useFile);
return sourceFiles.indexOf(declarationFile) <= sourceFiles.indexOf(useFile);
}
if (declaration.pos <= usage.pos) {
@ -4336,7 +4336,7 @@ namespace ts {
}
else {
// Use specific property type when parent is a tuple or numeric index type when parent is an array
const propName = "" + indexOf(pattern.elements, declaration);
const propName = "" + pattern.elements.indexOf(declaration);
type = isTupleLikeType(parentType)
? getTypeOfPropertyOfType(parentType, propName as __String)
: elementType;
@ -6758,7 +6758,7 @@ namespace ts {
if (node.initializer) {
const signatureDeclaration = <SignatureDeclaration>node.parent;
const signature = getSignatureFromDeclaration(signatureDeclaration);
const parameterIndex = ts.indexOf(signatureDeclaration.parameters, node);
const parameterIndex = signatureDeclaration.parameters.indexOf(node);
Debug.assert(parameterIndex >= 0);
return parameterIndex >= signature.minArgumentCount;
}
@ -6766,7 +6766,7 @@ namespace ts {
if (iife) {
return !node.type &&
!node.dotDotDotToken &&
indexOf((node.parent as SignatureDeclaration).parameters, node) >= iife.arguments.length;
node.parent.parameters.indexOf(node) >= iife.arguments.length;
}
return false;
@ -8707,7 +8707,7 @@ namespace ts {
* This is used during inference when instantiating type parameter defaults.
*/
function createBackreferenceMapper(typeParameters: TypeParameter[], index: number): TypeMapper {
return t => indexOf(typeParameters, t) >= index ? emptyObjectType : t;
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
}
function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
@ -10550,7 +10550,7 @@ namespace ts {
let result = "" + type.target.id;
for (const t of type.typeArguments) {
if (isUnconstrainedTypeParameter(t)) {
let index = indexOf(typeParameters, t);
let index = typeParameters.indexOf(t);
if (index < 0) {
index = typeParameters.length;
typeParameters.push(t);
@ -12120,7 +12120,7 @@ namespace ts {
}
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
return getTypeOfDestructuredArrayElement(getAssignedType(node), indexOf(node.elements, element));
return getTypeOfDestructuredArrayElement(getAssignedType(node), node.elements.indexOf(element));
}
function getAssignedTypeOfSpreadExpression(node: SpreadElement): Type {
@ -12164,7 +12164,7 @@ namespace ts {
const type = pattern.kind === SyntaxKind.ObjectBindingPattern ?
getTypeOfDestructuredProperty(parentType, node.propertyName || <Identifier>node.name) :
!node.dotDotDotToken ?
getTypeOfDestructuredArrayElement(parentType, indexOf(pattern.elements, node)) :
getTypeOfDestructuredArrayElement(parentType, pattern.elements.indexOf(node)) :
getTypeOfDestructuredSpreadExpression(parentType);
return getTypeWithDefault(type, node.initializer);
}
@ -13918,7 +13918,7 @@ namespace ts {
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
const iife = getImmediatelyInvokedFunctionExpression(func);
if (iife && iife.arguments) {
const indexOfParameter = indexOf(func.parameters, parameter);
const indexOfParameter = func.parameters.indexOf(parameter);
if (parameter.dotDotDotToken) {
const restTypes: Type[] = [];
for (let i = indexOfParameter; i < iife.arguments.length; i++) {
@ -13939,7 +13939,7 @@ namespace ts {
if (contextualSignature) {
const funcHasRestParameters = hasRestParameter(func);
const len = func.parameters.length - (funcHasRestParameters ? 1 : 0);
let indexOfParameter = indexOf(func.parameters, parameter);
let indexOfParameter = func.parameters.indexOf(parameter);
if (getThisParameter(func) !== undefined && !contextualSignature.thisParameter) {
Debug.assert(indexOfParameter !== 0); // Otherwise we should not have called `getContextuallyTypedParameterType`.
indexOfParameter -= 1;
@ -14070,7 +14070,7 @@ namespace ts {
// In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter.
function getContextualTypeForArgument(callTarget: CallLikeExpression, arg: Expression): Type {
const args = getEffectiveCallArguments(callTarget);
const argIndex = indexOf(args, arg);
const argIndex = args.indexOf(arg);
if (argIndex >= 0) {
// If we're already in the process of resolving the given signature, don't resolve again as
// that could cause infinite recursion. Instead, return anySignature.
@ -17236,7 +17236,7 @@ namespace ts {
}
excludeCount--;
if (excludeCount > 0) {
excludeArgument[indexOf(excludeArgument, /*value*/ true)] = false;
excludeArgument[excludeArgument.indexOf(/*value*/ true)] = false;
}
else {
excludeArgument = undefined;
@ -19497,7 +19497,7 @@ namespace ts {
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
}
if (node.name && isIdentifier(node.name) && (node.name.escapedText === "this" || node.name.escapedText === "new")) {
if (indexOf(func.parameters, node) !== 0) {
if (func.parameters.indexOf(node) !== 0) {
error(node, Diagnostics.A_0_parameter_must_be_the_first_parameter, node.name.escapedText as string);
}
if (func.kind === SyntaxKind.Constructor || func.kind === SyntaxKind.ConstructSignature || func.kind === SyntaxKind.ConstructorType) {
@ -20604,7 +20604,7 @@ namespace ts {
const promisedType = getPromisedTypeOfPromise(type);
if (promisedType) {
if (type.id === promisedType.id || indexOf(awaitedTypeStack, promisedType.id) >= 0) {
if (type.id === promisedType.id || awaitedTypeStack.indexOf(promisedType.id) >= 0) {
// Verify that we don't have a bad actor in the form of a promise whose
// promised type is the same as the promise type, or a mutually recursive
// promise. If so, we return undefined as we cannot guess the shape. If this
@ -24651,7 +24651,7 @@ namespace ts {
const typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>expr.parent);
const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false, /*allowAsyncIterables*/ false) || unknownType;
return checkArrayLiteralDestructuringElementAssignment(<ArrayLiteralExpression>expr.parent, typeOfArrayLiteral,
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
(<ArrayLiteralExpression>expr.parent).elements.indexOf(expr), elementType || unknownType);
}
// Gets the property symbol corresponding to the property in destructuring assignment

View File

@ -335,17 +335,6 @@ namespace ts {
return false;
}
export function indexOf<T>(array: ReadonlyArray<T>, value: T): number {
if (array) {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}
}
return -1;
}
export function indexOfAnyCharCode(text: string, charCodes: ReadonlyArray<number>, start?: number): number {
for (let i = start || 0; i < text.length; i++) {
if (contains(charCodes, text.charCodeAt(i))) {

View File

@ -420,7 +420,7 @@ namespace ts {
host.getCanonicalFileName,
/*isAbsolutePathAnUrl*/ true);
sourceMapSourceIndex = indexOf(sourceMapData.sourceMapSources, source);
sourceMapSourceIndex = sourceMapData.sourceMapSources.indexOf(source);
if (sourceMapSourceIndex === -1) {
sourceMapSourceIndex = sourceMapData.sourceMapSources.length;
sourceMapData.sourceMapSources.push(source);

View File

@ -803,7 +803,7 @@ namespace ts {
return node === (<TypeAssertion>parent).type;
case SyntaxKind.CallExpression:
case SyntaxKind.NewExpression:
return (<CallExpression>parent).typeArguments && indexOf((<CallExpression>parent).typeArguments, node) >= 0;
return contains((<CallExpression>parent).typeArguments, node);
case SyntaxKind.TaggedTemplateExpression:
// TODO (drosen): TaggedTemplateExpressions may eventually support type arguments.
return false;

View File

@ -427,8 +427,9 @@ namespace ts.BreakpointResolver {
}
else {
const functionDeclaration = <FunctionLikeDeclaration>parameter.parent;
const indexOfParameter = indexOf(functionDeclaration.parameters, parameter);
if (indexOfParameter) {
const indexOfParameter = functionDeclaration.parameters.indexOf(parameter);
Debug.assert(indexOfParameter !== -1);
if (indexOfParameter !== 0) {
// Not a first parameter, go to previous parameter
return spanInParameterDeclaration(functionDeclaration.parameters[indexOfParameter - 1]);
}

View File

@ -367,12 +367,13 @@ namespace ts.formatting {
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorSettings): number {
const containingList = getContainingList(node, sourceFile);
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
function getActualIndentationFromList(list: ReadonlyArray<Node>): number {
const index = indexOf(list, node);
return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : Value.Unknown;
if (containingList) {
const index = containingList.indexOf(node);
if (index !== -1) {
return deriveActualIndentationFromList(containingList, index, sourceFile, options);
}
}
return Value.Unknown;
}
function getLineIndentationWhenExpressionIsInMultiLine(node: Node, sourceFile: SourceFile, options: EditorSettings): number {

View File

@ -112,7 +112,7 @@ namespace ts.JsDoc {
function forEachUnique<T, U>(array: T[], callback: (element: T, index: number) => U): U {
if (array) {
for (let i = 0; i < array.length; i++) {
if (indexOf(array, array[i]) === i) {
if (array.indexOf(array[i]) === i) {
const result = callback(array[i], i);
if (result) {
return result;