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): computed members discriminated unions #1349

Merged
merged 7 commits into from Dec 19, 2019

Conversation

bradzacher
Copy link
Member

@bradzacher bradzacher commented Dec 18, 2019

Fixes #1345

Property names are either computed: false with key: Identifier | StringLiteral | NumberLiteral, or they are computed: true with key: Expression.
the previous typings took the simple approach of just having a single type, but it made things a bit cumbersome to use.

This change creates two types for each, using the computed value as the key for a discriminated union.
This means that if you check node.computed === true, then TS will narrow the key type appropriately.

I also noticed a minor bug in the TSEnumMember handling, as the types didn't previously support the fact that it's syntactically valid to use an expression (note it's semantically invalid - TS error 1164).
It's also semantically and syntactically valid to do something like enum Foo { ['key'] }, which really should be handled differently to enum Foo { key }, even if they mean the same thing.
So I also added a computed prop to TSEnumMember, and gave it the same discriminated union treatment.

@bradzacher bradzacher added the enhancement New feature or request label Dec 18, 2019
@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.

@bradzacher bradzacher changed the title feat: use discriminated unions for member name types feat(typescript-estree): use discriminated unions for member name types Dec 18, 2019
@bradzacher bradzacher changed the base branch from minor-cleanup to master December 18, 2019 08:42
property names are either `computed: false` with `key: Identifier | StringLiteral | NumberLiteral`, or they are `computed: true` with `key: Expression`.
the previous typings took the simple approach of just having a single type, but it made things a bit more cumbersome.

This change creates two types for each, using the `computed` value as the key for a discriminated union.
This means that if you check `node.computed === true`, then TS will narrow the `key` type appropriately.

I also noticed a minor bug in the `TSEnumMember` handling, as the types didn't previously support the fact that it's syntactically valid to use an expression (note it's semantically invalid - TS error 1164).
It's also semantically and syntactically valid to do something like `enum Foo { ['key'] }`, which really should be handled differently to `enum Foo { key }`, even if they mean the same thing.
So I also added a `computed` prop to `TSEnumMember`, and gave it the same discriminated union treatment.
@codecov
Copy link

codecov bot commented Dec 19, 2019

Codecov Report

Merging #1349 into master will decrease coverage by 0.01%.
The diff coverage is 87.5%.

@@            Coverage Diff            @@
##           master   #1349      +/-   ##
=========================================
- Coverage   94.01%     94%   -0.02%     
=========================================
  Files         131     131              
  Lines        5867    5867              
  Branches     1662    1663       +1     
=========================================
- Hits         5516    5515       -1     
  Misses        186     186              
- Partials      165     166       +1
Impacted Files Coverage Δ
packages/eslint-plugin/src/rules/indent.ts 92.68% <ø> (ø) ⬆️
packages/eslint-plugin/src/rules/prefer-for-of.ts 87.32% <0%> (-1.25%) ⬇️
packages/eslint-plugin/src/util/misc.ts 92% <100%> (-0.86%) ⬇️
packages/typescript-estree/src/convert.ts 98.85% <100%> (ø) ⬆️
...nt-plugin/src/rules/no-untyped-public-signature.ts 100% <100%> (ø) ⬆️
...escript-estree/src/semantic-or-syntactic-errors.ts 87.5% <100%> (ø) ⬆️

armano2
armano2 previously approved these changes Dec 19, 2019
Copy link
Member

@armano2 armano2 left a comment

Choose a reason for hiding this comment

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

besides this 2 missing test cases

LGTM 👍

armano2
armano2 previously approved these changes Dec 19, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 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
Development

Successfully merging this pull request may close these issues.

Use discriminated unions to "branch" types for Property Names
2 participants