Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [prefer-find] stop throwing type errors when converting symbols to numbers #8390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change just reverses the order of whether .at, or the (0), are inspected first. It's not needed for the fix, but I found it surprising that foo(Symbol.for('throws')) would throw, since it's not even an at call; it's just any old function. WIth this change, you have to actually have
x.at(Symbol.for('throws')) in order to trigger the faulty code.

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