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(linter/eslint): Implement prefer-numeric-literals #4109

Merged
merged 3 commits into from
Jul 31, 2024

Conversation

jelly
Copy link
Contributor

@jelly jelly commented Jul 8, 2024

Rule Detail:
link


This lacks the fix, which I intend to work on as a follow up or if preferred I can leave the PR open an hack on it this week.

One interesting note is that eslint(radix) does not handle:

        "function *f(){ yield(Number).parseInt('11', 2) }", // { "ecmaVersion": 6 },

Maybe it is an idea to introduce a helper is_parseint_stmt which returns the call_expr? As the code is almost identical to the radix rule.

Copy link

graphite-app bot commented Jul 8, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@jelly
Copy link
Contributor Author

jelly commented Jul 8, 2024

Just send #4110 for the radix issue.

Copy link

codspeed-hq bot commented Jul 8, 2024

CodSpeed Performance Report

Merging #4109 will not alter performance

Comparing jelly:eslint/numeric-literals (a627281) with main (bb33bcc)

Summary

✅ 32 untouched benchmarks

Boshen pushed a commit that referenced this pull request Jul 8, 2024
The eslint rule perfer-numeric-literals has a test for the yield variant
which the radix testsuite did not have. See
#4109
@jelly
Copy link
Contributor Author

jelly commented Jul 10, 2024

Implemented the fixer, but I need to figure out if there is space on the start of the span and end of the previous span are only one apart and I'm not sure how to do that.

@Boshen Boshen requested review from DonIsaac and mysteryven July 11, 2024 02:45
@mysteryven
Copy link
Contributor

Implemented the fixer, but I need to figure out if there is space on the start of the span and end of the previous span are only one apart and I'm not sure how to do that.

Sound difficult for us, do you have any thoughts @DonIsaac

@DonIsaac
Copy link
Contributor

Implemented the fixer, but I need to figure out if there is space on the start of the span and end of the previous span are only one apart and I'm not sure how to do that.

Sound difficult for us, do you have any thoughts @DonIsaac

Just saw this, my bad! I'll review this tomorrow.

@jelly
Copy link
Contributor Author

jelly commented Jul 18, 2024

Implemented the fixer, but I need to figure out if there is space on the start of the span and end of the previous span are only one apart and I'm not sure how to do that.

Sound difficult for us, do you have any thoughts @DonIsaac

Just saw this, my bad! I'll review this tomorrow.

So I know now that I can look up the parent_node easily in the fixer. But I think the AST does not really record the previous node.

Maybe we only need to check if we are part of a YieldExpression? Turns out this is indeed legal:

[jelle@t14s][~]%cat -p /tmp/foo.js
function *f(){ yield(Number).parseInt('11', 2) }

let x = f();
console.log(x.next());
[jelle@t14s][~]%node /tmp/foo.js
{ value: 3, done: false }

@jelly jelly force-pushed the eslint/numeric-literals branch from 53b55e9 to 43bd104 Compare July 23, 2024 19:49
@jelly jelly force-pushed the eslint/numeric-literals branch from 43bd104 to 5703f43 Compare July 29, 2024 21:07
@jelly jelly requested review from DonIsaac and mysteryven July 29, 2024 21:10
@jelly
Copy link
Contributor Author

jelly commented Jul 29, 2024

I've pushed work on a fixer to https://github.com/jelly/oxc/tree/eslint/numeric-literals-fixer, the tests pass but the eslint tests aren't great and allow for creation of invalid syntactical code for example:

parseInt('', 16); and parseInt('1234.5', 8) => 0o1234.5. So I'll need to make the fixer conditional, not entirely sure yet how I check fractions.

@mysteryven
Copy link
Contributor

not entirely sure yet how I check fractions.

Looks from_str_radix can solve this: playground.

@jelly
Copy link
Contributor Author

jelly commented Jul 30, 2024

not entirely sure yet how I check fractions.

Looks from_str_radix can solve this: playground.

Thanks! That would solve everything I needed for the fixer!

@Boshen
Copy link
Member

Boshen commented Jul 30, 2024

@mysteryven Would you like to help out and land this?

@DonIsaac
Copy link
Contributor

I've pushed work on a fixer to https://github.com/jelly/oxc/tree/eslint/numeric-literals-fixer, the tests pass but the eslint tests aren't great and allow for creation of invalid syntactical code for example:

parseInt('', 16); and parseInt('1234.5', 8) => 0o1234.5. So I'll need to make the fixer conditional, not entirely sure yet how I check fractions.

If you cannot figure out a perfectly safe fix, consider making it dangerous.

pub fn dangerously(mut self) -> Self {

Copy link
Contributor

@mysteryven mysteryven left a comment

Choose a reason for hiding this comment

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

Let's merge this, the fixer part will be the next separate PR.

@mysteryven mysteryven merged commit eaf834f into oxc-project:main Jul 31, 2024
26 checks passed
@jelly jelly deleted the eslint/numeric-literals branch July 31, 2024 07:48
@oxc-bot oxc-bot mentioned this pull request Aug 5, 2024
Boshen added a commit that referenced this pull request Aug 5, 2024
## [0.7.0] - 2024-08-05

- 85a7cea semantic: [**BREAKING**] Remove name from `reference` (#4329)
(Dunqing)

### Features

- aaee07e ast: Add `AstKind::AssignmentTargetPattern`,
`AstKind::ArrayAssignmentTarget` and `AstKind::ObjectAssignmentTarget`
(#4456) (Dunqing)
- 9df7b56 jsx-a11y/no-autofocus: Implement fixer support (#4171) (Jelle
van der Waa)
- b87bf70 linter: Add fix capabilties to existing lint rules (#4560)
(DonIsaac)
- ddd8b27 linter: Support conditional fix capabilities (#4559)
(DonIsaac)
- b952942 linter: Add eslint/no-unused-vars (⭐ attempt 3.2) (#4445)
(DonIsaac)
- 6543958 linter: Add auto-fix metadata to RuleMeta (#4557) (Don Isaac)
- 85e8418 linter: Add react/jsx-curly-brace-presence (#3949) (Don Isaac)
- 4c4da56 linter: Add typescript-eslint/prefer-keyword-namespce (#4438)
(Aza Walker)
- d8c2a83 linter: Eslint-plugin-vitest/no-import-node-test (#4440)
(cinchen)
- e3b0c40 linter: Eslint-plugin-vitest/no-identical-title (#4422)
(cinchen)
- c936782 linter: Eslint-plugin-vitest/no-conditional-expect (#4425)
(cinchen)
- 27fdd69 linter: Eslint-plugin-vitest/no-commented-out-tests (#4424)
(cinchen)
- 51f5025 linter: Add fixer for unicorn/prefer-string-starts-ends-with
(#4378) (DonIsaac)
- 3c0c709 linter: Add typescript-eslint/no-extraneous-class (#4357)
(Jaden Rodriguez)
- 7afa1f0 linter: Support suggestions and dangerous fixes (#4223)
(DonIsaac)
- acc5729 linter: Eslint-plugin-vitest/expect-expect (#4299) (cinchen)
- 2213f93 linter: Eslint-plugin-vitest/no-alias-methods (#4301)
(cinchen)
- c296bc3 linter/eslint: Implement func-names (#4618) (Alexander S.)
- e116ae0 linter/eslint: Implement fixer for prefer-numeric-literals
(#4591) (Jelle van der Waa)
- eaf834f linter/eslint: Implement prefer-numeric-literals (#4109)
(Jelle van der Waa)
- db2fd70 linter/eslint-plugin-promise: Implement
no-webpack-loader-syntax (#4331) (Jelle van der Waa)
- 5f1e070 linter/eslint-plugin-unicorn: Add fixer for prefer-code-point
(#4353) (Jelle van der Waa)
- ed49e16 linter/eslint-plugin-unicorn: Implement fixer for
prefer-dom-node-append (#4306) (Jelle van der Waa)
- e2b15ac linter/react: Implement react-jsx-boolean-value (#4613) (Jelle
van der Waa)
- 68efcd4 linter/react-perf: Handle new objects and arrays in prop
assignment patterns (#4396) (DonIsaac)

### Bug Fixes

- 368112c ast: Remove `#[visit(ignore)]` from
`ExportDefaultDeclarationKind`'s `TSInterfaceDeclaration` (#4497)
(Dunqing)
- d384f60 ci: Remove unused(?) .html file (#4545) (Yuji Sugiura)
- 06aec77 linter: Invalid binary expression with overflow (#4647)
(DonIsaac)
- b2da22b linter: Invalid tags in rule docs (#4646) (DonIsaac)
- 94440ad linter: Panic on invalid lang in `a11y/lang`. (#4630) (rzvxa)
- e0b03f8 linter: Improve the boundary for eslint/for-direction (#4590)
(heygsc)
- 70b8cfa linter: Missing return in no-obj-calls recursion (#4594)
(DonIsaac)
- fe1356d linter: Change no-unused-vars to nursery (#4588) (DonIsaac)
- 72337b1 linter: Change typescript-eslint/no-namespace to restriction
(#4539) (Don Isaac)
- 732f4e2 linter: Fix `oxlint` allocator cfg (#4527) (overlookmotel)
- 289dc39 linter: Overflow in no-obj-calls (#4397) (DonIsaac)
- a664715 linter/eslint: Fix invalid regexp in no_regex_spaces test
(#4605) (Yuji Sugiura)
- 74fa75a linter/eslint: Drop quotes around max-params lint warning
(#4608) (Jelle van der Waa)
- 9fcd9ae linter/eslint: Fix invalid regexp in no_control_regex test
(#4544) (leaysgur)
- ac08de8 linter/react_perf: Allow new objects, array, fns, etc in top
scope (#4395) (DonIsaac)
- 0fba738 npm: SyntaxError caused by optional chaining in low version
node (#4650) (heygsc)
- 73d2558 oxlint: Fix oxlint failed to build due to missing feature
(Boshen)

### Performance

- 6ff200d linter: Change react rules and utils to use `Cow` and
`CompactStr` instead of `String` (#4603) (DonIsaac)
- f259df0 linter: Make img-redundant-alt only build a regex once (#4604)
(DonIsaac)
- 7585e16 linter: Remove allocations for string comparisons (#4570)
(DonIsaac)
- b60bdf1 linter: `no_shadow_restricted_names` only look up name in
hashmap once (#4472) (overlookmotel)
- 81384f5 linter: Avoid unnecessary work in `nextjs:no_duplicate_head`
rule (#4465) (overlookmotel)
- f7da22d linter: Disable lint rules by file type (#4380) (DonIsaac)
- 348c1ad semantic: Remove `span` field from `Reference` (#4464)
(overlookmotel)
- 6a9f4db semantic: Reduce storage size for symbol redeclarations
(#4463) (overlookmotel)- a207923 Replace some CompactStr usages with
Cows (#4377) (DonIsaac)

### Refactor

- 7a75e0f linter: Use diagnostic codes in lint rules (#4349) (DonIsaac)
- ccb1835 semantic: Methods take `Span` as param, not `&Span` (#4470)
(overlookmotel)
- 7cd53f3 semantic: Var hoisting (#4379) (Dunqing)
- c99b3eb syntax: Give `ScopeId` a niche (#4468) (overlookmotel)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants