Commit graph

1277 commits

Author SHA1 Message Date
cinchen
c9367820ef
feat(linter): eslint-plugin-vitest/no-conditional-expect (#4425)
support
[eslint-plugin-vitest/no-conditional-expect](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-conditional-expect.md)
2024-07-23 21:50:45 +08:00
cinchen
27fdd69448
feat(linter): eslint-plugin-vitest/no-commented-out-tests (#4424) 2024-07-23 08:52:39 -04:00
DonIsaac
68efcd4000 feat(linter/react-perf): handle new objects and arrays in prop assignment patterns (#4396)
# What This PR Does

Massively improves all `react-perf` rules
- feat: handle new objects/etc assigned to variables
```tsx
const Foo = () => {
  const x = { foo: 'bar' } // <- now reports this new object
  return <Bar x={x} />
}
```
- feat: handle new objects/etc in binding patterns
```tsx
const Foo = ({ x = [] }) => {
  //           ^^^^^^ now reports this new array
  return <Bar x={x} />
}
```
-feat: nice and descriptive labels for new objects/etc assigned to intermediate variables
```
  ⚠ eslint-plugin-react-perf(jsx-no-new-object-as-prop): JSX attribute values should not contain objects created in the same scope.
   ╭─[jsx_no_new_object_as_prop.tsx:1:27]
 1 │ const Foo = () => { const x = {}; return <Bar x={x} /> }
   ·                           ┬   ─┬                 ┬
   ·                           │    │                 ╰── And used here
   ·                           │    ╰── And assigned a new value here
   ·                           ╰── The prop was declared here
   ╰────
  help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).
```
- feat: consider `Object.assign()` and `Object.create()` as a new object
- feat: consider `arr.[map, filter, concat]` as a new array
- refactor: move shared implementation code to `ReactPerfRule` in `oxc_linter::utils::react_perf`
2024-07-23 01:52:59 +00:00
DonIsaac
ac08de817e fix(linter/react_perf): allow new objects, array, fns, etc in top scope (#4395)
Consider the following code:
```tsx
import { FC } from 'react'
import { SvgIcon } from '@mui/material'

const StyledIcon = <SvgIcon sx={{ padding: 1, color: '#ff0000' }} />
//          reported violation  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
export const MyComponent: FC = () => {
    return (
        <div>
            {StyledIcon}
            {/* ... */}
        </div>
    )
}
```

This should not be a violation since the JSX is pre-computed and re-used, which
does not break React's `Object.is()` checks.
2024-07-23 01:39:07 +00:00
Dunqing
85a7cea9d1 refactor(semantic)!: remove name from reference (#4329)
related to: https://github.com/oxc-project/backlog/issues/32
2024-07-22 15:59:23 +00:00
DonIsaac
289dc39865 fix(linter): overflow in no-obj-calls (#4397)
Closes #4389
2024-07-21 23:35:47 +00:00
DonIsaac
f7da22da18 perf(linter): disable lint rules by file type (#4380)
### TL;DR

Added a `should_run` function to multiple lint rules to determine if a rule should be executed based on the source type. This change optimizes the linting process by avoiding unnecessary rule checks.

### What changed?

1. **New Method**: Introduced the `should_run` method in the `Rule` trait.
2. **Implementation**: Implemented the `should_run` method for various lint rules, particularly those related to React and TypeScript.
3. **Usage**: Updated the `Linter` to use the `should_run` method to filter rules before execution.
4. **Macro Update**: Modified the `declare_all_lint_rules` macro to incorporate the `should_run` method.

### How to test?

1. Run the linter on a project containing React and TypeScript files.
2. Verify that only relevant rules are executed based on the file type (e.g., JSX rules for React files).

### Why make this change?

This change improves the performance of the linter by ensuring that only applicable rules are run for a given file type, reducing unnecessary computation and potential false positives.

---
2024-07-21 15:22:54 +00:00
DonIsaac
51f5025c9c feat(linter): add fixer for unicorn/prefer-string-starts-ends-with (#4378)
Part of #4179
2024-07-21 07:20:22 +00:00
DonIsaac
a207923af1 perf: replace some CompactStr usages with Cows (#4377)
Reduce memory allocations in semantic and linter by using `Cow<'a, str>` over `CompactStr`
2024-07-20 19:19:55 +00:00
DonIsaac
7a75e0f8a7 refactor(linter): use diagnostic codes in lint rules (#4349)
> This PR is (unfortunately) quite large, but all changes are needed in tandem for this to work properly.

## What This PR Does

Updates the linter to populate diagnostics reported by rules with error codes statically derived from `RuleMeta` + `RuleEnum`.

Doing so required changing how we handle vitest rules. I know @mysterven was hoping to refactor that part of the code, and I think this approach is an improvement (but could probably be cleaned up further).

## Changes

### 1. Auto-Populate Error Codes
`LintContext` now sets an error code scope + error code number for diagnostics reported by lint rules. `LintContext` will not clobber existing codes set by rules, allowing for rule-specific override behavior (e.g. to use `eslint-plugin-react-hooks` as an error scope).

In order to accomplish this, I had to update every diagnostic factory for every rule. While doing this I found some incorrect error messages, or messages that could be easily improved. This is where a large majority of the snapshot diffs come from. Additionally, I was able to reduce string allocations from `format!` usages in diagnostic factories, especially within jest rules.

### 2. Framework and Library Detection
This PR adds `FrameworkFlags`, which specify what (if any) set of libraries and frameworks are being used by a project and/or file. They are passed in two ways:

1. `LintOptions` can specify a set of `framework_hints` that apply to the entire target codebase. Right now these are always empty, but I'm thinking in the future we could sniff `package.json`. It may be helpful for enabling/disabling default rules.
2. When `Linter` gets run on a file, framework information is sniffed from the `LintContext`. Right now, we are only checking for `vitest` imports in `ModuleRecord` and test path prefixes from `source_path`. It may be useful to do something similar for React/NextJS rules in the future. I know that [next/no-html-link-for-pages](https://nextjs.org/docs/messages/no-html-link-for-pages) could benefit greatly from this.
2024-07-20 03:35:00 +00:00
Jelle van der Waa
5f1e070c8b
feat(linter/eslint-plugin-unicorn): add fixer for prefer-code-point (#4353) 2024-07-20 10:33:15 +08:00
Jaden Rodriguez
3c0c7093b5
feat(linter): add typescript-eslint/no-extraneous-class (#4357)
Added rule for https://typescript-eslint.io/rules/no-extraneous-class/

Also, I chose to make the match the node against the class and derive
the body from the node, rather than matching against the body and using
the context to go back up to the parent class node as in the original
source.
2024-07-19 15:56:30 -04:00
DonIsaac
7afa1f06c3 feat(linter): support suggestions and dangerous fixes (#4223) 2024-07-18 02:20:30 +00:00
cinchen
acc57295d5
feat(linter): eslint-plugin-vitest/expect-expect (#4299)
support
[eslint-plugin-vitest/expect-expect](https://github.com/veritem/eslint-plugin-vitest/blob/main/src/rules/expect-expect.ts)

---------

Co-authored-by: wenzhe <mysteryven@gmail.com>
2024-07-18 10:13:45 +08:00
Jelle van der Waa
ed49e169cb
feat(linter/eslint-plugin-unicorn): implement fixer for prefer-dom-node-append (#4306) 2024-07-18 09:44:23 +08:00
Jelle van der Waa
9df7b5675f
feat(jsx-a11y/no-autofocus): implement fixer support (#4171)
Let oxlint automatically remove the autoFocus attribute when `--fix` is
passed.
2024-07-17 16:31:19 -04:00
cinchen
2213f9393b
feat(linter): eslint-plugin-vitest/no-alias-methods (#4301)
support
[eslint-plugin-vitest/no-alias-methods](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-alias-method.md)
2024-07-17 16:30:35 -04:00
Jelle van der Waa
4463eb4c48
chore(linter): add fixer test cases for prefer-dom-node-text-content (#4315) 2024-07-17 16:21:31 -04:00
Jelle van der Waa
db2fd70432
feat(linter/eslint-plugin-promise): implement no-webpack-loader-syntax (#4331)
Rule detail:

[link](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-webpack-loader-syntax.md)
2024-07-17 16:13:54 -04:00
github-actions[bot]
697c0efdef
Release oxlint v0.6.1 (#4326)
## [0.6.1] - 2024-07-17

### Features

- 83c2c62 codegen: Add option for choosing quotes; remove slow
`choose_quot` method (#4219) (Boshen)
- 1f8968a linter: Add eslint-plugin-promise rules: avoid-new,
no-new-statics, params-names (#4293) (Jelle van der Waa)
- a4dc56c linter: Add fixer for
unicorn/no_useless_promise_resolve_reject (#4244) (Burlin)
- 6fb808f linter: Add typescript-eslint/no-confusing-non-null-assertion
(#4224) (Jaden Rodriguez)
- 126b66c linter: Support eslint-plugin-vitest/valid-describe-callback
(#4185) (cinchen)
- 05b9a73 linter: Support eslint-plugin-vitest/valid-expect (#4183)
(cinchen)
- 3e56b2b linter: Support eslint-plugin-vitest/no-test-prefixes (#4182)
(cinchen)
- 3016f03 linter: Let fixer functions return a `None` fix (#4210)
(DonIsaac)
- bbe6137 linter: Implement unicorn/no-useless-undefined (#4079)
(Burlin)
- 20cdb1f semantic: Align class scope with typescript (#4195) (Dunqing)

### Bug Fixes

- 9df60da linter: Correct find first non whitespace logic in
@typescript-eslint/consistent-type-imports (#4198) (mysteryven)
- 67240dc linter: Not ignore adjacent spans when fixing (#4217)
(mysteryven)
- dd07a54 linter: Global variables should always check the builtin
variables (#4209) (Jelle van der Waa)
- 351ecf2 semantic: Incorrect resolve references for `TSTypeQuery`
(#4310) (Dunqing)
- 1108f2a semantic: Resolve references to the incorrect symbol (#4280)
(Dunqing)

### Performance

- 0fdc88b linter: Optimize no-dupe-keys (#4292) (lucab)

### Refactor

- 2c7bb9f ast: Pass final `ScopeFlags` into `visit_function` (#4283)
(overlookmotel)
- aa22073 codegen: Improve print API (#4196) (Boshen)
- b5a8f3c linter: Use get_first_parameter_name from unicorn utils
(#4255) (Jelle van der Waa)
- 7089a3d linter: Split up fixer code into separate files (#4222)
(DonIsaac)
- ace4f1f semantic: Update the order of `visit_function` and `Visit`
fields in the builder to be consistent (#4248) (Dunqing)
- 7f1addd semantic: Correct scope in CatchClause (#4192) (Dunqing)

Co-authored-by: Boshen <Boshen@users.noreply.github.com>
2024-07-17 21:18:28 +08:00
Dunqing
a88d588a07 feat(semantic): add ReferenceFlags::TSTypeQuery to indicate referenced by TSTypeQuery (#4317)
`ReferenceFlags::TSTypeQuery` can be used to help us insist on whether the reference is referenced by the type or not.
2024-07-17 09:52:57 +00:00
Dunqing
351ecf2707 fix(semantic): incorrect resolve references for TSTypeQuery (#4310)
```ts
type A = typeof Foo
                ^^^ Only allow reference to value symbol
```

I have verified the changed snapshot. That's correct
2024-07-17 03:33:00 +00:00
Dunqing
1108f2a700 fix(semantic): resolve references to the incorrect symbol (#4280)
close: #3799
related: #3863
2024-07-17 02:50:48 +00:00
Jelle van der Waa
1f8968a521
feat(linter): Add eslint-plugin-promise rules: avoid-new, no-new-statics, params-names (#4293)
This introduces the `eslint-plugin-promise` plugin and implements three
relatively simple rules.

Split off from https://github.com/oxc-project/oxc/pull/4252
2024-07-17 09:21:20 +08:00
lucab
0fdc88beee perf(linter): optimize no-dupe-keys (#4292)
This tweaks the `no-dupe-keys` lint in order to try to optimize it.
The lints reliably shows up in both CPU and memory profiling,
due to the cost of allocating/growing the hashmap and hashing into it.
This PR tries to tackle both by skipping trivial cases and by
pre-allocating a larger hashmap.
2024-07-16 11:08:23 +00:00
overlookmotel
2c7bb9f6c8 refactor(ast): pass final ScopeFlags into visit_function (#4283)
We have a strange workaround for `visit_function` where we pass in `ScopeFlags`, to support creating the scope inside `Function`, but setting different flags for `MethodDefinition`s.

Previously `visit_function` took `Option<ScopeFlags>` and then did `flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function` to it. Personally, I found this confusing. When I was looking at `MethodDefinition`, I was wondering "It's a function, why doesn't it set Function flag too?"

This changes makes it more explicit and clear what `ScopeFlags` everything has.
2024-07-16 07:22:07 +00:00
Jelle van der Waa
b5a8f3c60c
refactor(linter): use get_first_parameter_name from unicorn utils (#4255) 2024-07-16 09:41:38 +08:00
Jelle van der Waa
3416099a18
eslint-plugin-unicorn: prefer-string-trim-start-end rule fixer and fixes (#4285) 2024-07-16 01:45:33 +03:30
Boshen
a71787572e
chore: remove unsafe_code = "warn" rust lint
Feels too verbose as we already have unsafe comment turned on
2024-07-15 10:39:08 +08:00
Burlin
a4dc56c36d
feat(linter): add fixer for unicorn/no_useless_promise_resolve_reject (#4244)
Part of https://github.com/oxc-project/oxc/issues/4179

add fixer for unicorn/no_useless_promise_resolve_reject
2024-07-14 20:19:52 +08:00
Dunqing
ace4f1ff77 refactor(semantic): update the order of visit_function and Visit fields in the builder to be consistent (#4248)
Same as #4195
2024-07-14 11:39:15 +00:00
Dunqing
20cdb1fe0a feat(semantic): align class scope with typescript (#4195)
```ts
class Klass <T>   extends Root       <R>                   {}
//    ^^^^^ ^^^           ^^^^       ^^^                   ^^
//    id type_paramter super_class super_type_parameters  body
```
I reorder fields according to the order above

The class scope is not defined in the spec. But we need to create a scope for `class` to store `TypeParamters`
2024-07-14 03:19:30 +00:00
mysteryven
9df60da6f3 fix(linter): correct find first non whitespace logic in @typescript-eslint/consistent-type-imports (#4198) 2024-07-13 02:33:03 +00:00
DonIsaac
7089a3d67b refactor(linter): split up fixer code into separate files (#4222)
refactor(linter): split up fixer code into separate files

refactor: mark Fix as non_exhaustive
2024-07-13 02:26:00 +00:00
mysteryven
67240dce85 fix(linter): not ignore adjacent spans when fixing (#4217)
fixes: #4204
2024-07-13 02:11:40 +00:00
Jaden Rodriguez
6fb808febf
feat(linter): add typescript-eslint/no-confusing-non-null-assertion (#4224)
Added logic for
https://typescript-eslint.io/rules/no-confusing-non-null-assertion

Left comments for when Suggest feature is added

---------

Co-authored-by: Don Isaac <donald.isaac@gmail.com>
2024-07-12 11:12:43 -04:00
cinchen
126b66c4f8
feat(linter): support eslint-plugin-vitest/valid-describe-callback (#4185)
Rule detail:
[link](https://github.com/veritem/eslint-plugin-vitest/blob/main/src/rules/valid-describe-callback.ts)

---------

Co-authored-by: wenzhe <mysteryven@gmail.com>
2024-07-12 11:35:00 +08:00
cinchen
05b9a7375a
feat(linter): support eslint-plugin-vitest/valid-expect (#4183)
Rule detail:
[link](https://github.com/veritem/eslint-plugin-vitest/blob/main/src/rules/valid-expect.ts)

---------

Co-authored-by: wenzhe <mysteryven@gmail.com>
2024-07-12 11:24:57 +08:00
Boshen
83c2c62f7b feat(codegen): add option for choosing quotes; remove slow choose_quot method (#4219) 2024-07-12 03:08:22 +00:00
cinchen
3e56b2bd16
feat(linter): support eslint-plugin-vitest/no-test-prefixes (#4182)
Rule detail:
[link](https://github.com/veritem/eslint-plugin-vitest/blob/main/src/rules/no-test-prefixes.ts)
2024-07-12 11:03:39 +08:00
DonIsaac
3016f03578 feat(linter): let fixer functions return a None fix (#4210)
Part of #4187.

Adds `CompositeFix::None`, which enables fixer functions to decide not to fix some code.

While I was in the area, I took the liberty of adding some doc comments.
2024-07-12 01:12:42 +00:00
Jelle van der Waa
dd07a5475b
fix(linter): global variables should always check the builtin variables (#4209)
This fixes commit dbbb6fca56.

Closes #3374
2024-07-11 13:22:34 -04:00
Burlin
bbe6137dc9
feat(linter): implement unicorn/no-useless-undefined (#4079)
Resolves #3870

Hey there, thought I'd give this a try as it's tagged "good first issue"
:) Let me know if there's anything that needs to change

Another part of the code also needs modification:

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/rules/no-useless-undefined.js#L95

I tried to modify it. If there are any issues with these modifications,
please let me know.
```
fn is_has_function_return_type(node: &AstNode, ctx: &LintContext<'_>) -> bool {
    let Some(parent_node) = ctx.nodes().parent_node(node.id()) else {
        return false;
    };
    match parent_node.kind() {
        AstKind::ArrowFunctionExpression(arrow_func_express) => {
            arrow_func_express.return_type.is_some()
        }
        AstKind::Function(func) => func.return_type.is_some(),
        _ => is_has_function_return_type(parent_node, ctx),
    }
}
```
2024-07-11 22:09:25 +08:00
Dunqing
7f1adddaf0 refactor(semantic): correct scope in CatchClause (#4192)
close: #4186

CatchClause has two scopes. The first one is `CatchClause`, which will add a `CatchParameter` to it. The second one is `Block`, which will add binding that declares in the current block scope.

The spec has a syntax error about `CatchParameter`
- It is a Syntax Error if any element of the BoundNames of CatchParameter also occurs in the LexicallyDeclaredNames of Block.
2024-07-11 08:45:30 +00:00
Boshen
aa22073736 refactor(codegen): improve print API (#4196) 2024-07-11 08:41:04 +00:00
github-actions[bot]
4f26e51b74
Release oxlint v0.6.0 (#4194)
## [0.6.0] - 2024-07-11

- 5731e39 ast: [**BREAKING**] Store span details inside comment struct
(#4132) (Luca Bruno)

### Features

- fb549e1 linter: Add vitest/no-focused-tests rule (#4178) (mysteryven)
- 6c49007 linter: Add fixer for
@typescript-eslint/consistent-type-imports (#3984) (mysteryven)
- 278c3e9 linter: Add fixer for jsx-a11y/aria-props (#4176) (DonIsaac)
- 2188144 linter: Eslint-plugin-jest/prefer-hooks-in-order (#4052)
(cinchen)
- cc58614 linter: Better schemas for allow/warn/deny (#4150) (DonIsaac)
- c5b4be0 linter: Add fixer for prefer-node-protocol (#4129) (DonIsaac)
- 7ec0c0b linter/eslint: Implement no-label-var (#4087) (Jelle van der
Waa)

### Bug Fixes

- ed4c54c eslint/radix: Detect yield Number.parseInt variant (#4110)
(Jelle van der Waa)
- e9ad03b linter: Fixer for no-debugger creates incorrect code (#4184)
(DonIsaac)
- bd69571 linter: Fix top level return panic in
eslint/array_callback_return (#4167) (Boshen)
- c8f5664 linter: Fix panic with unicode in
unicorn/prefer_dom_node_dataset (#4166) (Boshen)
- f2b3273 linter: Fix fixer panic in
typescript/consistent_indexed_object_style (#4165) (Boshen)
- 2334515 linter: Panic in `get_enclosing_function` (#4121) (DonIsaac)
- 1b91d40 linter: Incorrect fixer for `no-unused-labels` (#4123) (Don
Isaac)
- 1729249 linter: Incorrect fix in
`no-single-promise-in-promise-methods` rule; (#4094) (DonIsaac)
- cc7e893 linter/tree-shaking: Avoid recursive function stackoverflow
(#4191) (mysteryven)
- 28eeee0 parser: Fix asi error diagnostic pointing at invalid text
causing crash (#4163) (Boshen)
- 0f02608 semantic: Bind `TSImportEqualsDeclaration`s (#4100) (Don
Isaac)

### Performance

- ddfa343 diagnostic: Use `Cow<'static, str>` over `String` (#4175)
(DonIsaac)

### Refactor

- 2687ebc react: Use find_binding helper for finding React binding
(#4108) (Jelle van der Waa)

Co-authored-by: Boshen <Boshen@users.noreply.github.com>
2024-07-11 14:38:41 +08:00
mysteryven
fb549e1288 feat(linter): add vitest/no-focused-tests rule (#4178)
Rule detail: https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-focused-tests.md
2024-07-11 05:37:04 +00:00
mysteryven
cc7e893634 fix(linter/tree-shaking): avoid recursive function stackoverflow (#4191)
fixes: #4164
2024-07-11 05:28:35 +00:00
mysteryven
6c49007b27 feat(linter): add fixer for @typescript-eslint/consistent-type-imports (#3984)
This rule's fixer is complicated, I do line by line copy as possible.

7b13dae347/packages/eslint-plugin/src/rules/consistent-type-imports.ts
2024-07-11 04:48:04 +00:00
DonIsaac
e9ad03bb62 fix(linter): fixer for no-debugger creates incorrect code (#4184)
Part of #4179.

Fixes cases like:
```js
if (foo) debugger
// got fixed into
if (foo)
// but now gets fixed to
if (foo) {}
```
2024-07-11 04:33:43 +00:00