Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// a[i]++, --a[i], etc.
if (
parent.type === AST_NODE_TYPES.UpdateExpression &&
parent.argument === node
) {
return true;
}
// [a[i]] = [0]
if (parent.type === AST_NODE_TYPES.ArrayPattern) {
return true;
}
// [...a[i]] = [0]
if (parent.type === AST_NODE_TYPES.RestElement) {
return true;
}
// ({ foo: a[i] }) = { foo: 0 }
if (
parent.type === AST_NODE_TYPES.Property &&
parent.value === node &&
parent.parent?.type === AST_NODE_TYPES.ObjectExpression &&
isAssignee(parent.parent)
) {
return true;
}
return false;
}
pattern.properties.forEach(property => {
if (property.type === AST_NODE_TYPES.RestElement) {
getIdentifiersFromPattern(property, identifiers);
} else {
// this is a bit weird, but it's because ESTree doesn't have a new node type
// for object destructuring properties - it just reuses Property...
// https://github.com/estree/estree/blob/9ae284b71130d53226e7153b42f01bf819e6e657/es2015.md#L206-L211
// However, the parser guarantees this is safe (and there is error handling)
getIdentifiersFromPattern(
property.value as TSESTree.DestructuringPattern,
identifiers,
);
}
});
break;
if (
!util.arraysAreEqual(
types1.slice(index + 1),
types2.slice(index + 1),
parametersAreEqual,
)
) {
return undefined;
}
const a = types1[index];
const b = types2[index];
// Can unify `a?: string` and `b?: number`. Can't unify `...args: string[]` and `...args: number[]`.
// See https://github.com/Microsoft/TypeScript/issues/5077
return parametersHaveEqualSigils(a, b) &&
a.type !== AST_NODE_TYPES.RestElement
? { kind: 'single-parameter-difference', p0: a, p1: b }
: undefined;
}
if (property.type === AST_NODE_TYPES.RestElement) {
getIdentifiersFromPattern(property, identifiers);
} else {
// this is a bit weird, but it's because ESTree doesn't have a new node type
// for object destructuring properties - it just reuses Property...
// https://github.com/estree/estree/blob/9ae284b71130d53226e7153b42f01bf819e6e657/es2015.md#L206-L211
// However, the parser guarantees this is safe (and there is error handling)
getIdentifiersFromPattern(
property.value as TSESTree.DestructuringPattern,
identifiers,
);
}
});
break;
case AST_NODE_TYPES.RestElement:
getIdentifiersFromPattern(pattern.argument, identifiers);
break;
case AST_NODE_TYPES.AssignmentPattern:
getIdentifiersFromPattern(pattern.left, identifiers);
break;
case AST_NODE_TYPES.MemberExpression:
// ignore member expressions, as the everything must already be defined
break;
default:
// https://github.com/typescript-eslint/typescript-eslint/issues/1282
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
throw new Error(`Unexpected pattern type ${pattern!.type}`);
}
function isNodeRestElementInFunction(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.RestElement &&
typeof node.parent !== 'undefined' &&
isNodeValidFunction(node.parent)
);
}
function parametersHaveEqualSigils(
a: TSESTree.Parameter,
b: TSESTree.Parameter,
): boolean {
const optionalA = isTSParameterProperty(a)
? a.parameter.optional
: a.optional;
const optionalB = isTSParameterProperty(b)
? b.parameter.optional
: b.optional;
return (
(a.type === AST_NODE_TYPES.RestElement) ===
(b.type === AST_NODE_TYPES.RestElement) &&
(optionalA !== undefined) === (optionalB !== undefined)
);
}
const sig2i = sig2[i];
const typeAnnotation1 = isTSParameterProperty(sig1i)
? sig1i.parameter.typeAnnotation
: sig1i.typeAnnotation;
const typeAnnotation2 = isTSParameterProperty(sig2i)
? sig2i.parameter.typeAnnotation
: sig2i.typeAnnotation;
if (!typesAreEqual(typeAnnotation1, typeAnnotation2)) {
return undefined;
}
}
if (
minLength > 0 &&
shorter[minLength - 1].type === AST_NODE_TYPES.RestElement
) {
return undefined;
}
return {
extraParameter: longer[longer.length - 1],
kind: 'extra-parameter',
otherSignature: shorterSig,
};
}
export function isRestElement(
node: TSESTree.Node
): node is TSESTree.RestElement {
return node.type === AST_NODE_TYPES.RestElement;
}