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 1 commit
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
29 changes: 18 additions & 11 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,7 +153,14 @@ export default createRule({
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at#parameters
*/
function isTreatedAsZeroByArrayAt(value: unknown): boolean {
const asNumber = Number(value);
let asNumber: number;

try {
Copy link
Member

Choose a reason for hiding this comment

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

[Refactor] I'm not a big fan of try/catch as a form of program control flow. It somewhat hides the intent of the code. Thrown errors are generally a sign of something truly breaking - IMO it's better to be intentional around code always working as expected.

Instead of generally catching all errors, how about checking if typeof value is 'symbol'?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, I was 50/50 on which way to go on this. Changed!

asNumber = Number(value);
} catch (e) {
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 is all that's strictly necessary for the fix

kirkwaiblinger marked this conversation as resolved.
Show resolved Hide resolved
// This will happen if trying to convert a symbol.
return false;
}

if (isNaN(asNumber)) {
return true;
Expand Down Expand Up @@ -215,7 +222,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
18 changes: 18 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-find.test.ts
Expand Up @@ -56,6 +56,24 @@ 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')];
`,
kirkwaiblinger marked this conversation as resolved.
Show resolved Hide resolved
],

invalid: [
Expand Down