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: update regex for methods with thisArg #17439

Merged
merged 2 commits into from Aug 2, 2023
Merged

Conversation

fasttime
Copy link
Member

@fasttime fasttime commented Jul 31, 2023

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What rule do you want to change?

no-eval and no-invalid-this

What change do you want to make (place an "X" next to just one item)?

[ ] Generate more warnings
[X] Generate fewer warnings
[ ] Implement autofix
[ ] Implement suggestions

How will the change be implemented (place an "X" next to just one item)?

[ ] A new option
[X] A new default behavior
[ ] Other

Please provide some example code that this change will affect:

/* eslint no-eval: error */
array.findLast(function (x) { return this.eval.includes(x); }, { eval: ['foo', 'bar'] });

/* eslint no-invalid-this: error */
array.flatMap(function(value) { "use strict"; return this.doSomething(value); }, obj);

Playground Link

Find more examples in the updated unit tests.

What does the rule currently do for this code?

Report an error for each rule.

What will the rule do after it's changed?

Report no error.

What changes did you make? (Give an overview)

This PR updates a shared regex in ast-utils.js to add support for method names that were added to the spec since ES2019.

The regex is used in the rules no-eval and no-invalid-this to avoid flagging some special cases, trivially relying on the name of a method being called.
So the net effect of this PR will be less problems reported by those two rules.

The updated regex is intended to match names of built-in methods that expect a callback function as a first argument, and the value bound to this in calls to the callback as a second argument.
These are mostly methods of arrays and typed arrays, although forEach is also defined on sets and maps.

The new method names in this PR are:

I've also renamed the regex for more clarity and added unit tests for the no-eval rule and extended the tests for no-invalid-this.

Is there anything you'd like reviewers to focus on?

  • Should this be tagged fix or feat?
  • The shared function astUtils.isDefaultThisBinding is currently only tested indirectly. Do we need any specific unit tests?

@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Jul 31, 2023
@netlify
Copy link

netlify bot commented Jul 31, 2023

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 01dfe5a
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/64c9d915f3b724000824c462
😎 Deploy Preview https://deploy-preview-17439--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@fasttime fasttime added the rule Relates to ESLint's core rules label Jul 31, 2023
@fasttime fasttime marked this pull request as ready for review August 1, 2023 06:19
@fasttime fasttime requested a review from a team as a code owner August 1, 2023 06:19
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

The change overall looks good to me. I left one suggestion.

I believe this should be "feat" because we are adding support for more recent methods.

const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/u;
const methodWithThisArgPattern = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|some)$/u;
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep "array" in the name?

Suggested change
const methodWithThisArgPattern = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|some)$/u;
const arrayMethodWithThisArgPattern = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|some)$/u;

Copy link
Member Author

@fasttime fasttime Aug 2, 2023

Choose a reason for hiding this comment

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

I think it's fine to keep array since these are all names of array methods at this time. We will notice if the situation will change one day. Updated in 01dfe5a, thanks.

@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Aug 2, 2023
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Leaving open for @nzakas to verify.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks

@nzakas nzakas merged commit 53d7508 into main Aug 2, 2023
22 checks passed
@nzakas nzakas deleted the methodWithThisArgPattern branch August 2, 2023 14:52
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jan 30, 2024
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jan 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

None yet

3 participants