Skip to content

Commit

Permalink
Merge branch 'main' into flat-config-support-core-package
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Feb 7, 2024
2 parents 69084bd + c298350 commit 8713862
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/eslint-plugin/src/rules/prefer-find.ts
Expand Up @@ -125,22 +125,22 @@ export default createRule({
return isAtLeastOneArrayishComponent;
}

function getObjectIfArrayAtExpression(
function getObjectIfArrayAtZeroExpression(
node: TSESTree.CallExpression,
): TSESTree.Expression | undefined {
// .at() should take exactly one argument.
if (node.arguments.length !== 1) {
return undefined;
}

const atArgument = getStaticValue(node.arguments[0], globalScope);
if (atArgument != null && isTreatedAsZeroByArrayAt(atArgument.value)) {
const callee = node.callee;
if (
callee.type === AST_NODE_TYPES.MemberExpression &&
!callee.optional &&
isStaticMemberAccessOfValue(callee, 'at', globalScope)
) {
const callee = node.callee;
if (
callee.type === AST_NODE_TYPES.MemberExpression &&
!callee.optional &&
isStaticMemberAccessOfValue(callee, 'at', globalScope)
) {
const atArgument = getStaticValue(node.arguments[0], globalScope);
if (atArgument != null && isTreatedAsZeroByArrayAt(atArgument.value)) {
return callee.object;
}
}
Expand All @@ -153,6 +153,12 @@ export default createRule({
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at#parameters
*/
function isTreatedAsZeroByArrayAt(value: unknown): boolean {
// This would cause the number constructor coercion to throw. Other static
// values are safe.
if (typeof value === 'symbol') {
return false;
}

const asNumber = Number(value);

if (isNaN(asNumber)) {
Expand Down Expand Up @@ -215,7 +221,7 @@ export default createRule({
return {
// This query will be used to find things like `filteredResults.at(0)`.
CallExpression(node): void {
const object = getObjectIfArrayAtExpression(node);
const object = getObjectIfArrayAtZeroExpression(node);
if (object) {
const filterExpression = parseIfArrayFilterExpression(object);
if (filterExpression) {
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-find.test.ts
Expand Up @@ -56,6 +56,20 @@ ruleTester.run('prefer-find', rule, {
"['Just', 'a', 'find'].find(x => x.length > 4);",
'undefined.filter(x => x)[0];',
'null?.filter(x => x)[0];',
// Should not throw. See https://github.com/typescript-eslint/typescript-eslint/issues/8386
`
declare function foo(param: any): any;
foo(Symbol.for('foo'));
`,
// Specifically need to test Symbol.for(), not just Symbol(), since only
// Symbol.for() creates a static value that the rule inspects.
`
declare const arr: string[];
const s = Symbol.for("Don't throw!");
arr.filter(item => item === 'aha').at(s);
`,
"[1, 2, 3].filter(x => x)[Symbol('0')];",
"[1, 2, 3].filter(x => x)[Symbol.for('0')];",
],

invalid: [
Expand Down

0 comments on commit 8713862

Please sign in to comment.