Navigation Menu

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

New: no-unsafe-optional-chaining rule (fixes #13431) #13859

Merged
merged 20 commits into from Dec 5, 2020

Conversation

yeonjuan
Copy link
Member

@yeonjuan yeonjuan commented Nov 17, 2020

Prerequisites checklist

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

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

What changes did you make? (Give an overview)

Added a new rule no-unsafe-optional-chaining which proposed by @mysticatea #13431

This rule will catch cases that may possibly TypeError when using optional chaining.

var obj = {};

// TypeError

(obj?.foo).bar;

(obj?.foo)();

(obj?.foo)`template`;

new (obj?.foo)();

class A extends obj?.foo {};

[...obj?.foo];

bar(...obj?.foo);

new Bar(...obj?.foo);

1 in obj?.foo;

var { a } = obj?.foo;

var [a] = obj?.foo;

// [edited]
foo instanceof obj?.prop

for (foo of obj?.prop);

[{ foo } = obj?.prop] = [];

with (obj?.prop);

// [edited] - handle logical result.

(obj?.foo ?? obj?.bar)();

(foo || obj?.foo)();

And by the option disallowArithmeticOperators: true it also catches the operation which possibly results in unexpected NaN. - comment

obj?.foo * bar;
obj?.foo - bar;
obj?.foo + bar;
obj?.foo % bar;
obj?.foo ** bar;

a *= obj?.foo;
a -= obj?.foo;
a += obj?.foo;
a %= obj?.foo;
a **= obj?.foo;

+obj?.foo;
-obj?.foo;

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

Maybe there are some other cases we should handle?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Nov 17, 2020
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Nov 17, 2020
@mdjermanovic
Copy link
Member

mdjermanovic commented Nov 17, 2020

Maybe there are some other cases we should handle?

I think all of these will also throw on undefined:

  • foo instanceof obj?.prop
  • for (foo of obj?.prop);
  • [{ foo } = obj?.prop] = []; (AssignmentPattern#right if AssignmentPattern#left is a pattern)
  • with (obj?.prop);

@yeonjuan
Copy link
Member Author

@mdjermanovic Thanks 👍 I added handling those.

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.

Looks great in general. I didn't review everything in detail yet, but noticed a couple of problems.

lib/rules/no-unsafe-optional-chaining.js Outdated Show resolved Hide resolved
lib/rules/no-unsafe-optional-chaining.js Outdated Show resolved Hide resolved
@yeonjuan
Copy link
Member Author

@mdjermanovic Thanks
I fixed it and refactor the tests. :)

docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
tests/lib/rules/no-unsafe-optional-chaining.js Outdated Show resolved Hide resolved
tests/lib/rules/no-unsafe-optional-chaining.js Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
- add test cases (*=, /=)
- improve Rule Details doc
- fix wrong example
- add unary, assignment operation test cases
- edit test cases - move optional chain to right side
- change to use Set
- fix jsdoc typo and type
- add examples on docs
- handle conditional, sequence expressions
- remove useless check
- fix doc
- add test cases
- fix typo
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Outdated Show resolved Hide resolved
docs/rules/no-unsafe-optional-chaining.md Show resolved Hide resolved
@yeonjuan
Copy link
Member Author

yeonjuan commented Dec 1, 2020

@mdjermanovic Thanks for all reviews. It seems to be fixed but, I'll take a look once again and request a review. :)

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! 🎉

@mdjermanovic mdjermanovic changed the title New: no-unsafe-optional-chaining rule (fixes #13431) New: no-unsafe-optional-chaining rule (fixes #13431) Dec 2, 2020
@yeonjuan
Copy link
Member Author

yeonjuan commented Dec 3, 2020

Hi @mdjermanovic I have a question :) Maybe this rule should handle await also?
IMO It should be, but want to know others' opinion.

(async function main() {
          (await (foo?.bar))?.baz // ok

          (await (foo?.bar)).baz; // Maybe error? It's lint free in current version.
})();

I marked it as do not merge because it seems to need a decision.

@yeonjuan yeonjuan added the do not merge This pull request should not be merged yet label Dec 3, 2020
@mdjermanovic
Copy link
Member

(await (foo?.bar)).baz; // Maybe error? It's lint free in current version.

Makes sense to me, we can handle await in function checkUndefinedShortCircuit since await undefined evaluates to undefined.

@yeonjuan yeonjuan removed the do not merge This pull request should not be merged yet label Dec 3, 2020
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!

Copy link
Member

@btmills btmills 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 @yeonjuan!

@btmills btmills merged commit 683ad00 into master Dec 5, 2020
@btmills btmills deleted the new-no-unsafe-optional-chaining branch December 5, 2020 07:32
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 4, 2021
@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 Jun 4, 2021
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
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants