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): [unbound-method] report on destructuring in function parameters #8952

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

auvred
Copy link
Member

@auvred auvred commented Apr 19, 2024

PR Checklist

Overview

Old approach: query for VariableDeclarator, AssignmentExpression and check if they have ObjectPattern

New approach: query for ObjectPattern, check if VariableDeclarator, AssignmentExpression, AssignmentPattern its parent


We're iterating over ObjectPattern's properties:

  1. If ObjectPatterns's parent has init node, check if it's safe to destructure this init node:
    • if it's unsafe, then report on this property and go to the next property
    • if it's safe, then check if ObjectPatterns's parent is AssignmentPattern:
      • if false, go to the next property
      • if true, continue working on this property
  2. If ObjectPattern has a type annotation, then check if it's safe to destructure this type
  3. If there is no type annotation, check the inferred property type

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @auvred!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Apr 19, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 7dafb3a
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66352c91184fe100088c2860
😎 Deploy Preview https://deploy-preview-8952--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 92 (🔴 down 5 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

I really like these changes! The algorithm change makes sense to me, the code is easy to follow, and the new code for type inspection is very thorough.

Added a few questions and some test cases to flesh out edge cases.

node.parent.type === AST_NODE_TYPES.AssignmentExpression
) {
initNode = node.parent.right;
}
Copy link
Member

Choose a reason for hiding this comment

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

should there be some kind of assertion here to ensure that every way that ObjectPattern could occur is handled (especially in light of the bug that this PR fixes)? Relates to #6225

packages/eslint-plugin/src/rules/unbound-method.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/unbound-method.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/unbound-method.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/rules/unbound-method.ts Outdated Show resolved Hide resolved
@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Apr 20, 2024
nativelyBoundMembers.has(getMemberFullName(node)) &&
isNotImported(objectSymbol, currentSourceFile)
notImported &&
nativelyBoundMembers.has(`${object.name}.${property.name}`)
Copy link
Member Author

Choose a reason for hiding this comment

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

const { log } = console;

In our current configuration, the console.log declarations are under @types/node. So if we remove this check, the rule will report on this case. Not sure what's the best solution there..

@auvred auvred requested a review from kirkwaiblinger May 3, 2024 18:43
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [unbound-method] Does not fail for destructured parameters
2 participants