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(eslint-plugin): [no-duplicate-enum-values] add rule #4833

Merged

Conversation

aifreedom
Copy link
Contributor

PR Checklist

Overview

This PR creates a new rule in eslint-plugin to disallow duplicate enum values. Per discussion in the issue (#2693), in this version, it only checks enum members initialized with literals, and does not cover the ones without an initializer or initialized with an expression.

@nx-cloud
Copy link

nx-cloud bot commented Apr 17, 2022

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 42515fd. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 43 targets

Sent with 💌 from NxCloud.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @aifreedom!

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.

@netlify
Copy link

netlify bot commented Apr 17, 2022

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 42515fd
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/62688bbc120c8e0009f39bff
😎 Deploy Preview https://deploy-preview-4833--typescript-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 settings.

@aifreedom aifreedom changed the title feat(eslint-plugin): create a new rule to disallow duplicate enum values feat(eslint-plugin): new rule to disallow duplicate enum values Apr 17, 2022
@codecov
Copy link

codecov bot commented Apr 17, 2022

Codecov Report

Merging #4833 (5484161) into main (f3cf87b) will increase coverage by 2.48%.
The diff coverage is 100.00%.

❗ Current head 5484161 differs from pull request most recent head 42515fd. Consider uploading reports for the commit 42515fd to get more accurate results

@@            Coverage Diff             @@
##             main    #4833      +/-   ##
==========================================
+ Coverage   91.77%   94.25%   +2.48%     
==========================================
  Files         227      153      -74     
  Lines       10611     8305    -2306     
  Branches     3283     2703     -580     
==========================================
- Hits         9738     7828    -1910     
+ Misses        591      263     -328     
+ Partials      282      214      -68     
Flag Coverage Δ
unittest 94.25% <100.00%> (+2.48%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/eslint-plugin/src/configs/all.ts 100.00% <ø> (ø)
...slint-plugin/src/rules/no-duplicate-enum-values.ts 100.00% <100.00%> (ø)
...ges/type-utils/src/getConstrainedTypeAtLocation.ts
...ils/src/ast-utils/eslint-utils/ReferenceTracker.ts
...s/utils/src/eslint-utils/batchedSingleLineTests.ts
packages/utils/src/ts-eslint-scope/Referencer.ts
packages/eslint-plugin-tslint/src/custom-linter.ts
packages/type-utils/src/isUnsafeAssignment.ts
...pt-estree/src/create-program/createWatchProgram.ts
packages/utils/src/ast-utils/index.ts
... and 67 more

@bradzacher bradzacher changed the title feat(eslint-plugin): new rule to disallow duplicate enum values feat(eslint-plugin): [no-duplicate-enum-values] add rule Apr 18, 2022
@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Apr 18, 2022
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

The new rule and tests generally looks great, very nicely done @aifreedom!

Requesting changes for added test coverage. I can't view the Codecov coverage report at the moment to see why the file only has 80% 🙃 -- will take another look when more tests are added.

return (
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number'
);
}
Copy link
Member

Choose a reason for hiding this comment

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

[Nitpicking] Is there a need to separate these two functions out? They both check that node.type === AST_NODE_TYPES.Literal.

I'd suggest either making a single separate function:

function getExpressionLiteralValue(node: TSESTree.Expression) {
  if (node.type === AST_NODE_TYPES.Literal) {
    switch (typeof node.value) {
      // ...
    }  
  }
}

...or inlining them below:

```ts
let value: string | number | undefined;

if (node.type === AST_NODE_TYPES.Literal) {
  // ...
}

Just suggestions, feel free to ignore if you don't like either of these changes 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to do this because typescript doesn't know the type of member.initializer without the util functions, and would complain when I cast its value to String or Number.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 21, 2022
Copy link
Contributor Author

@aifreedom aifreedom left a comment

Choose a reason for hiding this comment

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

The coverage drop is a bit weird. It's an unrelated rule's coverage that dropped (no-magic-numbers). The coverage for the new rule is 100%.

image

return (
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number'
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to do this because typescript doesn't know the type of member.initializer without the util functions, and would complain when I cast its value to String or Number.

@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 25, 2022
@aifreedom
Copy link
Contributor Author

Oh, no… I updated the branch using the “rebase” button and it seemed to have forced push the commits. Sorry about the inconvenience. @JoshuaKGoldberg you can review the last two commits which have the updates addressing your comments.

@JoshuaKGoldberg
Copy link
Member

Ha, no worries - thanks for the heads up!

And yeah I don't know what's going on with Codecov. The service has been flaky off and on the last few... years. 🙃

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Fantastic, thanks so much @aifreedom!

@JoshuaKGoldberg JoshuaKGoldberg enabled auto-merge (squash) April 27, 2022 00:18
@JoshuaKGoldberg JoshuaKGoldberg merged commit 5899164 into typescript-eslint:main Apr 27, 2022
@aifreedom aifreedom deleted the no-duplicate-enum-members branch April 27, 2022 00:24
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request May 8, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) | devDependencies | minor | [`5.21.0` -> `5.22.0`](https://renovatebot.com/diffs/npm/@typescript-eslint%2feslint-plugin/5.21.0/5.22.0) |
| [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint) | devDependencies | minor | [`5.21.0` -> `5.22.0`](https://renovatebot.com/diffs/npm/@typescript-eslint%2fparser/5.21.0/5.22.0) |

---

### Release Notes

<details>
<summary>typescript-eslint/typescript-eslint (@&#8203;typescript-eslint/eslint-plugin)</summary>

### [`v5.22.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#&#8203;5220-httpsgithubcomtypescript-eslinttypescript-eslintcomparev5210v5220-2022-05-02)

[Compare Source](typescript-eslint/typescript-eslint@v5.21.0...v5.22.0)

##### Bug Fixes

-   **eslint-plugin:** \[comma-spacing] verify `nextToken` exists ([#&#8203;4868](typescript-eslint/typescript-eslint#4868)) ([23746f8](typescript-eslint/typescript-eslint@23746f8))

##### Features

-   **eslint-plugin:** \[no-duplicate-enum-values] add rule ([#&#8203;4833](typescript-eslint/typescript-eslint#4833)) ([5899164](typescript-eslint/typescript-eslint@5899164))

</details>

<details>
<summary>typescript-eslint/typescript-eslint (@&#8203;typescript-eslint/parser)</summary>

### [`v5.22.0`](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/parser/CHANGELOG.md#&#8203;5220-httpsgithubcomtypescript-eslinttypescript-eslintcomparev5210v5220-2022-05-02)

[Compare Source](typescript-eslint/typescript-eslint@v5.21.0...v5.22.0)

**Note:** Version bump only for package [@&#8203;typescript-eslint/parser](https://github.com/typescript-eslint/parser)

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1338
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

disallow duplicate enum values
3 participants