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

feat(eslint-plugin): [unbound-method] Support Array methods with 'this' argument #797

Conversation

jacekjagiello
Copy link

@jacekjagiello jacekjagiello commented Aug 3, 2019

#743

Hey, this is my first PR. I don't have much experience with AST, so it feels for me like walking in the fog 😄 I assume there is a better solution for this bug. Any help appreciated. I'll leave some questions in PR

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @jacekjagiello!

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

if (
parent.callee.type === AST_NODE_TYPES.MemberExpression &&
parent.callee.property.type === AST_NODE_TYPES.Identifier
) {
Copy link
Author

Choose a reason for hiding this comment

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

What I really would like to do, is to check if callee for ex. nodes is an Array. Is this possible?

Copy link
Member

Choose a reason for hiding this comment

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

you should be able to get the type of the node, and ensure its type is an array.
I can't remember the flags exactly, so you might want to spend some time with the debugger in vscode.

Copy link
Author

Choose a reason for hiding this comment

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

I've spent some time trying to play with the debugger to find any flags that might indicate Array type, but with no success for now. If you think we can't leave it like this, I'll spend more time and maybe find the better way.

Copy link
Member

Choose a reason for hiding this comment

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

Digging back into this, sorry a lot going on. Finally taken time to tackle the PR queue.

Feel free to add this file into your PR, and use that API.
https://github.com/typescript-eslint/typescript-eslint/blob/naming-conventions/packages/eslint-plugin/typings/typescript.d.ts

The API is "internal", but it's stable - they just haven't gotten around to exposing their type relationship API yet (there's a whole discussion around why they haven't yet...)

You can then pass the typechecker into this function and call checker.isArrayType(type) (you'll also have to pass the type map in).

Also just as an FYI - this site is amazing for playing with TS's type API: https://ts-ast-viewer.com/
You can open the chrome dev tools, and interrogate the selected type via the global variables it maintains: type, program and typeChecker.

Also as an example, here is another PR doing a similar thing in looking for array functions: #1206

@codecov
Copy link

codecov bot commented Aug 3, 2019

Codecov Report

Merging #797 into master will increase coverage by 0.03%.
The diff coverage is 85%.

@@            Coverage Diff             @@
##           master     #797      +/-   ##
==========================================
+ Coverage   94.13%   94.17%   +0.03%     
==========================================
  Files         115      112       -3     
  Lines        5016     4841     -175     
  Branches     1403     1341      -62     
==========================================
- Hits         4722     4559     -163     
+ Misses        166      160       -6     
+ Partials      128      122       -6
Impacted Files Coverage Δ
packages/eslint-plugin/src/rules/unbound-method.ts 92.3% <85%> (-4.76%) ⬇️
...-plugin/src/rules/explicit-member-accessibility.ts 89.58% <0%> (-1.33%) ⬇️
...kages/eslint-plugin/src/rules/class-name-casing.ts 85.71% <0%> (-1.25%) ⬇️
...ckages/eslint-plugin/src/rules/no-magic-numbers.ts 91.42% <0%> (-1.08%) ⬇️
packages/eslint-plugin/src/rules/require-await.ts 92.1% <0%> (-0.58%) ⬇️
...ackages/eslint-plugin/src/rules/prefer-readonly.ts 99% <0%> (-0.1%) ⬇️
...nt-plugin/src/rules/indent-new-do-not-use/index.ts 98.4% <0%> (ø) ⬆️
packages/eslint-plugin/src/rules/index.ts 100% <0%> (ø) ⬆️
packages/parser/src/parser.ts 100% <0%> (ø) ⬆️
packages/eslint-plugin/src/rules/ban-types.ts 100% <0%> (ø) ⬆️
... and 50 more

@bradzacher bradzacher added the enhancement New feature or request label Aug 6, 2019
packages/eslint-plugin/src/rules/unbound-method.ts Outdated Show resolved Hide resolved
if (
parent.callee.type === AST_NODE_TYPES.MemberExpression &&
parent.callee.property.type === AST_NODE_TYPES.Identifier
) {
Copy link
Member

Choose a reason for hiding this comment

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

you should be able to get the type of the node, and ensure its type is an array.
I can't remember the flags exactly, so you might want to spend some time with the debugger in vscode.

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
jacekjagiello added 2 commits October 25, 2019 10:34
@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Dec 20, 2019
@bradzacher bradzacher added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Apr 13, 2020
@bradzacher bradzacher closed this Apr 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement New feature or request stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[unbound-method] False positive with thisArg
2 participants