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(typescript-estree): handle 3.9's non-null assertion changes #2036

Merged
merged 3 commits into from May 21, 2020

Conversation

bradzacher
Copy link
Member

@bradzacher bradzacher commented May 18, 2020

TS 3.9 introduced a breaking change for how non-null assertions are handled in optional chains.

Pre-3.9, x?.y!.z means (x?.y).z - i.e. it essentially scrubbed the optionality from the chain
Post-3.9, x?.y!.z means x?.y!.z - i.e. it just asserts that the property y is non-null, not the result of x?.y

This PR:

updates the AST so it matches this

Previously x?.y!.z produced MemberExpression > TSNonNullAssertion > OptionalMemberExpression.
Now it produces OptionalMemberExpression > TSNonNullAssertion > OptionalMemberExpression.

Note that both (x?.y)!.z and (x?.y!).z still produce MemberExpression > TSNonNullAssertion > OptionalMemberExpression.

Same results apply for call expressions.

updates no-non-null-asserted-optional-chain to handle this

With the above AST change, the only change needed was a simple "is parent optional chain" check.

Both of these changes are gated behind a version check, so they are completely backwards compatible.

Fixes #1976

@bradzacher bradzacher added the enhancement New feature or request label May 18, 2020
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @bradzacher!

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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

[TS 3.9 introduced a breaking change for how non-null assertions are handled in optional chains](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#parsing-differences-in-optional-chaining-and-non-null-assertions).

Pre-3.9,  `x?.y!.z` means `(x?.y).z` - i.e. it essentially scrubbed the optionality from the chain
Post-3.9, `x?.y!.z` means `x?.y!.z`  - i.e. it just asserts that the property `y` is non-null, not the result of `x?.y`

This PR:

**updates the AST so it matches this**

Previously `x?.y!.z` produced `MemberExpression > TSNonNullAssertion > OptionalMemberExpression`.
Now it produces `OptionalMemberExpression > TSNonNullAssertion > OptionalMemberExpression`.

Note that
`(x?.y)!.z` still produces `MemberExpression > TSNonNullAssertion > OptionalMemberExpression`.

Same results apply for call expressions.

**updates `no-non-null-asserted-optional-chain` to handle this**

With the above AST change, the only change needed was a simple "is parent optional chain" check.

Both of these changes are gated behind a version check, so they are completely backwards compatible.
@bradzacher bradzacher force-pushed the ts3-9-non-null-assertion-change branch from e0f239e to 81741e6 Compare May 18, 2020 01:03
@codecov
Copy link

codecov bot commented May 18, 2020

Codecov Report

Merging #2036 into master will decrease coverage by 0.23%.
The diff coverage is 81.25%.

@@            Coverage Diff             @@
##           master    #2036      +/-   ##
==========================================
- Coverage   93.91%   93.68%   -0.24%     
==========================================
  Files         172      172              
  Lines        7843     7871      +28     
  Branches     2253     2263      +10     
==========================================
+ Hits         7366     7374       +8     
- Misses        217      233      +16     
- Partials      260      264       +4     
Flag Coverage Δ
#unittest 93.68% <81.25%> (-0.24%) ⬇️
Impacted Files Coverage Δ
...n/src/rules/no-non-null-asserted-optional-chain.ts 75.00% <60.00%> (-25.00%) ⬇️
packages/typescript-estree/src/node-utils.ts 96.21% <88.88%> (-0.79%) ⬇️
packages/typescript-estree/src/convert.ts 99.15% <100.00%> (ø)
...pt-estree/src/create-program/createWatchProgram.ts 79.67% <0.00%> (-7.49%) ⬇️

@bradzacher bradzacher merged commit 06bec63 into master May 21, 2020
@bradzacher bradzacher deleted the ts3-9-non-null-assertion-change branch May 21, 2020 16:26
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
1 participant