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

Update: optional chaining support (fixes #12642) #13416

Merged
merged 78 commits into from
Jul 18, 2020
Merged

Conversation

mysticatea
Copy link
Member

@mysticatea mysticatea commented Jun 13, 2020

Prerequisites checklist

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

[X] Other, please explain: Add support for optional chaining syntax. Fixes #12642.

What changes did you make? (Give an overview)

This PR adds support for optional chaining syntax into ESLint. It includes the following changes:

  • ESTree update: Add optional chaining estree/estree#204
  • Acorn update: add Optional Chaining acornjs/acorn#891
  • esquery update: Added support for ChainExpression node estools/estraverse#113 and for it.
  • eslint-utils update: Supports for Optional chaining & Nullish coalescing mysticatea/eslint-utils#8
  • eslint-visitor-keys update: Update: add ChainExpression node eslint-visitor-keys#12
  • espree update: Update: support optional chaining js#446 (not released yet)
  • Code path analysis update: a574e27 ... It recognizes short-circuiting of optional chaining.
  • Rules update:
    • accessor-pairs 9125cf1 ... It handles ChainExpression nodes.
    • array-callback-return 102c4db ... It handles ChainExpression nodes.
    • camelcase 3de0a1c ... Some tests are added.
    • computed-property-spacing f2dc7f2 ... Some tests are added.
    • constructor-super 80e5e92 ... It handles ChainExpression nodes.
    • dot-location 33e4986, b91d17b ... It fixes code with ?. correctly.
    • dot-notation f66ce6c ... It fixes code with ?. correctly.
    • func-call-spacing 31412c2 ... It fixes code with ?. correctly.
    • func-name-matching 206d25f ... It handles ChainExpression nodes.
    • getter-return 276526b ... Some tests are added.
    • global-require 2824b22 ... It handles ChainExpression nodes.
    • indent 9a14a09 ... It handles ChainExpression nodes and ?. tokens correctly.
    • new-cap 7574cfd ... It handles ChainExpression nodes.
    • newline-per-chained-call ed71bd4 ... It handles ChainExpression nodes and ?. tokens correctly.
    • no-alert 9fd0338 ... It handles ChainExpression nodes.
    • no-eval abcceae ... It handles eval?.(code) as indirect eval as per the spec. It handles ChainExpression nodes.
    • no-extend-native 45c5aea ... It handles ChainExpression nodes. And it fixes a bug of edge cases.
    • no-extra-bind 38592d2 ... It handles ChainExpression nodes and ?. tokens correctly. And it fixes a bug of edge cases.
    • no-extra-boolean-cast d0c2f82 ... It handles ChainExpression nodes.
    • no-extra-parens a395b10 ... It recognizes necessary parentheses of avoiding syntax errors (e.g., new (obj?.p)(), (obj?.p)`template` , etc) and necessary parentheses of disconnecting chaining. It handles ChainExpression nodes.
    • no-implicit-coercion e550194 ... It creates the proper error message for ~foo?.indexOf(bar)-like code. It handles ChainExpression nodes.
    • no-implicit-eval 248faf1 ... It handles ChainExpression nodes.
    • no-import-assign b3dee67, f65df75 ... It handles ChainExpression nodes.
    • no-magic-numbers c63d3c1 ... It handles ChainExpression nodes.
    • no-obj-calls e13059f ... It handles ChainExpression nodes.
    • no-prototype-builtins 5286f83 ... It handles ChainExpression nodes.
    • no-restricted-syntax 8a0097a ... Some tests are added. We need to fix esquery to support optional chaining with some selectors (Added support for ChainExpression node estools/estraverse#113).
    • no-self-assign d12b2be ... It handles ChainExpression nodes.
    • no-setter-return a31e04c ... It handles ChainExpression nodes.
    • no-unexpected-multiline 6c6bd95, 776e96b ... It handles ?. tokens correctly. Should this rule ignore the case the previous line ends with ?. token?
    • no-unused-expression 6c00e33, 9a91ba1 ... It recognizes ChainExpression nodes. I updated this rule to enumerate disallowed node types rather than allowed node types in order to ignore unknown nodes.
    • no-useless-call f7fd797 ... It handles ChainExpression nodes.
    • no-whitespace-before-property b5fa99a ... It handles ?. tokens correctly.
    • operator-assignment 6468c0c ... It handles ChainExpression nodes.
    • padding-line-between-statements 5740538 ... It handles ChainExpression nodes.
    • prefer-arrow-callback d0fc1b1 ... It handles ChainExpression nodes.
    • prefer-destructuring a443a1e ... Some tests are added.
    • prefer-exponentiation-operator ca0b2a8 ... It handles ChainExpression nodes.
    • prefer-numeric-literals d978d36 ... It handles ChainExpression nodes.
    • prefer-promise-reject-errors 5c7368c ... It handles ChainExpression nodes.
    • prefer-regex-literals 651bac3 ... It handles ChainExpression nodes.
    • prefer-spread a3cbe6e ... It handles ChainExpression nodes.
    • use-isnan 17ec3d0 ... It handles ChainExpression nodes.
    • wrap-iife 233e737 ... It handles ChainExpression nodes.
    • yoda 89b65bf ... It handles ChainExpression nodes.

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

  • The most rule updates are to handle ChainExpression nodes that are at the callee|object properties. And some rules needed fixing autofix logic for the ?. token.
  • How about dot-location for obj?.[key]? The rule ignores computed properties currently, but we can insert line breaks between ?. and [, so we can choose either obj?.\n[key] or obj\n?.[key]. I guess we have to add a new option for that.

Remaining steps:

  1. Merge Update: support optional chaining js#446.
  2. Release a new version of espree.
  3. Update this PR with the new espree.

@mysticatea mysticatea added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules core Relates to ESLint's core APIs and features do not merge This pull request should not be merged yet new syntax This issue is related to new syntax that has reached stage 4 labels Jun 13, 2020
@mysticatea mysticatea self-assigned this Jun 13, 2020
@mdjermanovic
Copy link
Member

@mysticatea would it be possible to merge and release this without the esquery/estraverse update, or is it a blocker?

@mysticatea
Copy link
Member Author

In particular, shouldn't no-trow-literal report throw a || 5?

I feel that the rule should report a || 5. We can open an issue for that.

would it be possible to merge and release this without the esquery/estraverse update, or is it a blocker?

I don't think it's a blocker of this PR.
We might need to fork esquery to support visitorKeys in query...


@mdjermanovic I appreciate your exhaustive review. Thank you very much! I should address your review, but please tell me if not enough.

@mdjermanovic
Copy link
Member

would it be possible to merge and release this without the esquery/estraverse update, or is it a blocker?

I don't think it's a blocker of this PR.

I'd also vote to merge this without the esquery update.

It seems that only some rarely used selectors (those that internally use estraverse) won't work well with chain expressions in some cases.

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

@kaicataldo
Copy link
Member

PR to upgrade Espree: #13501.

@kaicataldo kaicataldo removed the do not merge This pull request should not be merged yet label Jul 18, 2020
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint new syntax This issue is related to new syntax that has reached stage 4 rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional Chaining and Nullish coalescing Operator arrived at Stage 4
4 participants