Commit graph

4748 commits

Author SHA1 Message Date
camc314
d7fce589f8 feat(linter) add suggestion fix for oxc missing throw (#4806) 2024-08-10 16:13:44 +00:00
DonIsaac
d2734f3bc4 feat(linter): start fixer for no-unused-vars (#4718)
Start to add dangerous suggestions for `no-unused-vars`. This PR focuses on fixing unused variable declarations.
- declarations with no references of any kind will be removed with a `FixKind` of dangerous suggestion.
- declarations with some usages will be renamed when the rule is configured with certain `"varsIgnorePattern"`s with a `FixKind` of dangerous fix.
2024-08-10 15:27:12 +00:00
camc314
070ae53ad6 feat(linter): add fixer for unicorn prefer-string-replace-all (#4801) 2024-08-10 14:46:12 +00:00
overlookmotel
504ac0b178 perf(minifier): InjectGlobalVariables only add to replaced_dot_defines once for each (#4803)
`InjectGlobalVariables` minifier plugin was previously adding an entry to `replaced_dot_defines` each time it made a replacement, resulting in many duplicate entries if a single dot define gets replaced multiple times. Only add once for each dot define.
2024-08-10 13:46:15 +00:00
overlookmotel
35f27420b9 perf(minifier): avoid repeated Atom creation in InjectGlobalVariables (#4802)
Re-use `Atom`s in `InjectGlobalVariables` minifier plugin.

Instead of allocating a new `Atom` on every replacement, create `Atom` lazily when making first replacement, and cache it.

As discussed in: https://github.com/oxc-project/oxc/pull/4759#discussion_r1711669464
2024-08-10 13:46:14 +00:00
Dunqing
62f759c1f2 fix(transformer/typescript): generated assignment for constructor arguments with access modifiers should be injected to the top of the constructor (#4808)
fix: #4789
2024-08-10 11:21:45 +00:00
oxc-bot
857bc73302
Release crates v0.24.1 (#4798)
## [0.24.1] - 2024-08-10

### Features

- b3c3125 linter: Overhaul unicorn/no-useless-spread (#4791) (DonIsaac)
- c519295 minifier: Add `InjectGlobalVariables` plugin
(`@rollup/plugin-inject`) (#4759) (Boshen)

### Bug Fixes

- fff9da3 ast, ast_codegen: Use `generate_derive` instead of visitable
for generating span derives. (#4747) (rzvxa)
- f5eeebd ast_macros: Raise compile error on invalid `generate_derive`
input. (#4766) (rzvxa)
- 4d0b40a napi/transform: Fix wrong isolated declarations emit (Boshen)

### Refactor

- daa0b2e ast: `oxc_ast` crate re-export AST types from other crates
(#4773) (overlookmotel)
- d4a3be8 ast_codegen: Line breaks between types in layout assertions
(#4781) (overlookmotel)
- dbb5f4c ast_codegen: Remove unnecessary imports from generated files
(#4774) (overlookmotel)
- 7ea058d ast_codegen: Replace Windows-style line breaks with Unix-style
(#4769) (overlookmotel)
- 2dea0ca ast_codegen: Consistent import order (#4761) (overlookmotel)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-08-10 15:26:40 +08:00
dalaoshu
970c36942c
chore: support windows shell for just (#4795)
closes #4794
2024-08-10 14:24:14 +08:00
DonIsaac
8f2a566f9f test(linter): ensure rule docs have valid syntax (#4644)
Adds tests for rule documentation by
1. Compiling doc markdown into HTML, which ensures docs use valid markdown syntax
2. Converts the generated HTML into JSX and parses the results with the parser, ensuring the generated HTML is valid

Has the added benefit of adding a lot of JSX test cases to the parser. I've also fixed all violations for these tests in this PR.
2024-08-10 05:03:09 +00:00
DonIsaac
b3c3125138 feat(linter): overhaul unicorn/no-useless-spread (#4791)
I got tired of seeing useless spreads on ternaries and `arr.reduce()` within my company's internal codebase so I overhauled this rule.

## Changes
- add fixer for object spreads
  ```js
  const before = { a, ...{ b, c }, d }
  const after = { a,  b, c, d } // fixer does not dedupe spaces before `b`
  ```
- recursively check for useless clones on complex expressions. This rule now catches and auto-fixes the following cases:
   ```js
  // ternaries when both branches create a new array or object
   const obj = { ...(foo ? { a: 1 } : { b: 2 }) }
  // recursive, so this can support complex cases
  const arr = [ ...(foo ? a.map(fn) : bar ? Array.from(iter) : await Promise.all(bar)) ]
  // reduce functions where the initial accumulator creates a new object or array
   const obj = { ...(arr.reduce(fn, {}) }
  ```
2024-08-10 04:50:09 +00:00
Jelle van der Waa
5992b7575e
feat(linter): implement eslint-plugin-promise/no-return-in-finally, prefer-await-to-then rule (#4318)
Part of https://github.com/oxc-project/oxc/pull/4252

---------

Co-authored-by: wenzhe <mysteryven@gmail.com>
2024-08-10 10:41:29 +08:00
Boshen
c51929558d feat(minifier): add InjectGlobalVariables plugin (@rollup/plugin-inject) (#4759) 2024-08-10 01:12:31 +00:00
DonIsaac
f62951411d feat(website): auto-generate rule docs pages (#4640)
> AI-generated description because I'm lazy
### TL;DR

This PR introduces the ability to generate documentation for linter rules and adds new methods and metadata for rule fix capabilities.

To see what this looks like, please check out https://github.com/oxc-project/oxc-project.github.io/pull/165.

## Screenshots
Hyperlinks to rule doc pages in auto-generated rules table
<img width="809" alt="image" src="https://github.com/user-attachments/assets/e09eb47d-e86a-4ed1-b1f9-5034f33c71a2">

Example of a docs page
<img width="1273" alt="image" src="https://github.com/user-attachments/assets/78f7e9e6-f4dd-4cc9-aebc-1cdd64b024ec">

### What changed?

- Added `RuleFixMeta` to indicate rule fix capabilities
- Introduced methods `is_none` and `is_pending` in `RuleFixMeta`
- Modified `render_markdown_table` in `RuleTableSection` to accept an optional link prefix
- Created new modules for rule documentation and HTML rendering
- Updated `print_rules` function to generate markdown for rules and detailed documentation pages

### How to test?

Run the `linter-rules` task with appropriate arguments to generate the markdown table and documentation pages.
Verify the generated files for correctness and that all metadata is correctly displayed.

### Why make this change?

To enhance the project documentation and provide clear rule fix capabilities, thereby improving the developer experience and easing the integration process.

---
2024-08-10 00:13:06 +00:00
lucab
d191823a0a perf(linter): optmize allocations in jest fn parsing (#4787) 2024-08-10 00:09:27 +00:00
heygsc
4dd29dbc2b
test(linter): add fixer test for unicorn/no-zero-fractions (#4783)
Add fixer test.
Some of them have not passed yet, so I have temporarily made comments as
todo.
2024-08-09 16:46:24 +03:30
Jelle van der Waa
c3c5766601
feat(linter/eslint-plugin-promise): implement valid-params (#4598) 2024-08-09 09:02:51 -04:00
heygsc
b259f47007
feat(linter): add fixer for unicorn/no-length-as-slice-end (#4780)
part of #4179 

test:
https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/test/snapshots/no-length-as-slice-end.mjs.md
2024-08-09 15:11:52 +03:30
overlookmotel
92777d0aae refactor(ast_codegen): replace ///@@ with ///@@line_break (#4786)
Follow-on after #4778. `///@@line_break` is more verbose, but it's clearer what it does.
2024-08-09 10:52:22 +00:00
overlookmotel
ec82a79ebe refactor(ast_codegen): trim r# from start of field names (#4785)
Some struct fields are reserved names e.g. `type`. They are written in source as `r#type`. Trim off the `r#` prefix in `FieldDef::name` and add it back when generating output.

Main motivation is to have the unescaped field names in JSON schema.
2024-08-09 10:52:21 +00:00
overlookmotel
31311877f1 refactor(ast_codegen): alter JSON schema format (#4784)
Change the format of schema JSON by altering `serde` attrs.
2024-08-09 10:52:20 +00:00
overlookmotel
c79ca40b77 refactor(ast_codegen): remove excess space from start of doc comments (#4782)
I noticed that in JSON schema the `docs` property contains e.g. `" The name of the identifier being referenced."` (with an excess space on the start). Trim that off.
2024-08-09 10:25:28 +00:00
overlookmotel
d4a3be86ed refactor(ast_codegen): line breaks between types in layout assertions (#4781)
Small style nit. Add line breaks between types in generated layout assertions, to make the file easier to read.
2024-08-09 10:17:21 +00:00
overlookmotel
966fcc925a refactor(ast_codegen): re-order code in fmt module (#4779)
Pure refactor. Just move code which is most used to top of `fmt` module. And rename `pprint` to `pretty_print` to be a little clearer.
2024-08-09 10:01:14 +00:00
overlookmotel
2c1c7050ea refactor(ast_codegen): use doc comments instead of endl! (#4778)
Similar to #4777.

Use `///@@` instead of `endl!();` in AST codegen to create line breaks.

NB: `///@@` needs to be before an item, not after it.
2024-08-09 08:57:18 +00:00
overlookmotel
f418f62e7b refactor(ast_codegen): use doc comments instead of insert! (#4777)
Avoid the `insert!` macro in AST codegen. Use doc comments starting with special symbol `@` instead.

* Before: `insert!("// plain comment");`
* After: `///@ plain comment`
* Or: `//!@ plain comment`

Either `///@` or `//!@` is converted to plain `//` in output.

`//!@` is legal in top-of-file position, which allows us to inline `#![allow(...)]` attributes, which in my opinion makes the generators a bit easier to read.
2024-08-09 08:19:15 +00:00
Burlin
b22ed4512d
fix(linter): improve prefer_namespace_keyword rule (#4751)
fix: #4651
This commit enhances the `prefer_namespace_keyword` rule in the
TypeScript linter: Add support for detecting and fixing nested module
declarations (e.g., module A.B {})

- Introduce helper functions `is_nest_module`, `is_valid_module`, and
`is_invalid_module` to improve code readability and maintainability
- Refactor the main `run` function to use these new helper functions
- Update test cases to cover nested module scenarios
- Improve error reporting for nested modules
2024-08-09 11:46:50 +03:30
overlookmotel
c15c931a3f refactor(ast_codegen): move formatting regex definitions (#4775)
Pure refactor. Move the regex definitions used in formatting to next to the `Replacer`s for those regexes.
2024-08-09 07:48:13 +00:00
overlookmotel
dbb5f4c75e refactor(ast_codegen): remove unnecessary imports from generated files (#4774)
#4773 makes types like `Span` importable from `oxc_ast::ast`, so remove the imports from other crates in generated code.

I'm not sure why clippy's `wildcard_imports` rule was not being triggered for `use crate::ast::*;`, but add `#[allow(clippy::wildcard_imports)]` on these statements just to make sure.
2024-08-09 07:48:09 +00:00
overlookmotel
daa0b2e66e refactor(ast): oxc_ast crate re-export AST types from other crates (#4773)
Some AST types are defined in crates outside `oxc_ast` crate. Re-export these types from `oxc_ast` crate for ease - so you don't need to remember where they're defined to use them.
2024-08-09 07:13:56 +00:00
heygsc
abd83fa998
feat(linter): add fixer for jsx_ally/no_aria_hidden_on_focusable (#4772)
part of #4179
2024-08-09 13:28:28 +08:00
dalaoshu
c509a21a1f
feat(linter/eslint-plugin-vitest): implement prefer-to-be-falsy (#4770)
Related to https://github.com/oxc-project/oxc/issues/4656
2024-08-09 13:17:45 +08:00
rzvxa
0816255e5d
chore: add editors/README.md (#4760) 2024-08-09 08:31:38 +08:00
lucab
63f274c7ee refactor(linter): simplify NoObjCalls resolution logic (#4765) 2024-08-09 00:27:07 +00:00
rzvxa
f5eeebdcec fix(ast_macros): raise compile error on invalid generate_derive input. (#4766)
It checks 2 things. 1) The input is a supported derive 2) The given identifier is the same as the fully qualified target trait.

The latter makes sure that the trait for derive is included in the scope.

Part of #4704

Here's an expanded example of how we assert traits:

```rust
const _:() = {
    {
        trait AssertionTrait: ::oxc_allocator::CloneIn<'static> {}
        impl<T: CloneIn<'static>> AssertionTrait for T {}
    };
};
```

It makes sure `CloneIn` is the same as `::oxc_allocator::CloneIn` and more importantly requires the user to include the trait if they wish to use it with `generate_derive`.

It also provides LSP jump to definition.
2024-08-08 19:06:43 +00:00
overlookmotel
670868035b refactor(linter): replace Windows-style line breaks with Unix-style in test fixture (#4768)
I happened to notice that 1 test fixture file had Windows-style CRLF line breaks rather than Unix-style LF. Fixing it for consistency.
2024-08-08 17:33:04 +00:00
overlookmotel
7ea058d84f refactor(ast_codegen): replace Windows-style line breaks with Unix-style (#4769)
2 files in AST codegen had Windows-style CRLF line breaks. Change them to Unix-style LF. Does not alter contents of the files aside from the line breaks.
2024-08-08 17:28:58 +00:00
rzvxa
fff9da319d fix(ast, ast_codegen): use generate_derive instead of visitable for generating span derives. (#4747)
follow-up to #4735
I was accidentally using the old code to filter the viable target types to derive. It means before this PR we were still using the `#[ast(visit)]` for this purpose.
2024-08-08 17:06:48 +00:00
rzvxa
abe8202ba8 refactor(ast_codegen): declutter the main file. (#4744)
same as #4741
2024-08-08 16:38:13 +00:00
overlookmotel
7345f68d9e refactor(ast_codegen): remove Generator::name and Pass::name methods (#4764)
Remove `Generator::name` and `Pass::name` methods. All impls for these methods return a string identical to the struct name, so can set return value of `Runner::name` in `define_generator!` and `define_pass!` macros instead.
2024-08-08 15:39:10 +00:00
overlookmotel
d32fb6f5c5 refactor(ast_codegen): re-order imports (#4763)
Move imports to top of file in `generators/mod.rs`, just for consistency.
2024-08-08 15:39:09 +00:00
overlookmotel
80046d1144 refactor(ast_codegen): consistent endl! position (#4762)
Pure refactor (style nit). Place `endl!()` macro calls in consistent position throughout codegen.
2024-08-08 15:39:08 +00:00
overlookmotel
2dea0caae0 refactor(ast_codegen): consistent import order (#4761)
Pure refactor. Alter import order in generated code to consistent order - `std`, external crates, `crate`, `super`.
2024-08-08 15:24:45 +00:00
dalaoshu
41f861fd15
feat(linter/eslint-plugin-vitest): implement prefer-to-be-truthy (#4755)
Related to #4656
2024-08-08 22:07:43 +08:00
heygsc
b20e3351fb
feat(linter): add fixer for eslint/no-eq-null (#4758) 2024-08-08 09:12:19 -04:00
overlookmotel
790c551c5e refactor(ast_codegen): simplify derive_get_span generator (#4757)
Simplify `derive_get_span` generator that was introduced in #4735. No change to functionality, just aiming for greater readability.

In particular:

* Move defining idents/tokens which are specific to `GetSpan` / `GetSpanMut` into those specific generators, rather than branching on `MUT` later on.
* Remove `const MUT` param.
* Remove the confusing pairs of closures and functions both called `derive_enum` / `derive_struct`.
* Inline function which generates the impls - prioritizing readability over DRY code.
2024-08-08 10:47:37 +00:00
heygsc
bd56b6b56b
chore(tasks): change comment fix-dangerous to fix_dangerous (#4756) 2024-08-08 18:42:29 +08:00
heygsc
2f6c3b9cec
feat(linter): add fixer for eslint/no-compare-neg-zero (#4748)
part of #4179

---------

Co-authored-by: wenzhe <mysteryven@gmail.com>
2024-08-08 17:12:02 +08:00
dalaoshu
6cf38cb776
chore(tasks): support init vitest lint rule (#4752)
Related to #4656
2024-08-08 16:27:50 +08:00
Boshen
4d0b40ad10
fix(napi/transform): fix wrong isolated declarations emit 2024-08-08 14:36:41 +08:00
oxc-bot
69dc6c9cb1
Release crates v0.24.0 (#4749)
## [0.24.0] - 2024-08-08

- 75f2207 traverse: [**BREAKING**] Replace `find_scope` with
`ancestor_scopes` returning iterator (#4693) (overlookmotel)

- 506709f traverse: [**BREAKING**] Replace `find_ancestor` with
`ancestors` returning iterator (#4692) (overlookmotel)

### Features

- 23b0040 allocator: Introduce `CloneIn` trait. (#4726) (rzvxa)
- 51c1ca0 ast: Derive `CloneIn` for AST types, using `generate_derive`.
(#4732) (rzvxa)
- e12bd1e ast: Allow conversion from TSAccessibility into &'static str
(#4711) (DonIsaac)
- fd2d9da ast: Improve `AstKind::debug_name` (#4553) (DonIsaac)
- b3b7028 ast: Implement missing Clone, Hash, and Display traits for
literals (#4552) (DonIsaac)
- 54047e0 ast: `GetSpanMut` trait (#4609) (overlookmotel)
- eae401c ast, ast_macros: Apply stable repr to all `#[ast]` enums
(#4373) (rzvxa)
- ec0b4cb ast_codegen: Add `derive_clone_in` generator. (#4731) (rzvxa)
- 2e91ad6 ast_codegen: Support for `generate_derive` marker. (#4728)
(rzvxa)
- 82e2f6b ast_codegen: Process AST-related `syntax` types. (#4694)
(rzvxa)
- 0c52c0d ast_codegen: Add alignment and size data to the schema.
(#4615) (rzvxa)
- 07607d3 ast_codegen, span: Process `Span` through ast_codegen (#4703)
(overlookmotel)
- 125c5fd ast_codegen, span: Process `SourceType` through ast_codegen.
(#4696) (rzvxa)
- eaddc8f linter: Add fixer for eslint/func_names (#4714) (DonIsaac)
- 229a0e9 minifier: Implement dot define for member expressions (#3959)
(camc314)
- 33f1312 semantic: Impl GetSpan for AstNode (#4717) (DonIsaac)
- e42ac3a sourcemap: Add `ConcatSourceMapBuilder::from_sourcemaps`
(#4639) (overlookmotel)
- 2e63618 span: Implement `CloneIn` for the AST-related items. (#4729)
(rzvxa)
- 6a36616 syntax: Derive `CloneIn` for the AST-related items. (#4730)
(rzvxa)

### Bug Fixes

- 4a56954 codegen: Print raw if value is number is Infinity (#4676)
(Boshen)
- 94d3c31 minifier: Avoid removing function declaration from `KeepVar`
(#4722) (Boshen)
- bf43148 minifier: Do not `remove_syntax` in dead_code_elimination
(Boshen)
- bf48c7f minifier: Fix `keep_var` keeping vars from arrow functions
(#4680) (Boshen)
- 9be29af minifier: Temporarily fix shadowed `undefined` variable
(#4678) (Boshen)
- e8b662a minifier: Various fixes to pass minifier conformance (#4667)
(Boshen)
- 01d85de napi/transform: Update napi files (Boshen)
- f290191 oxc_ast_macros: Fix `syn` lacking features to build (Boshen)
- a40a217 parser: Parse `assert` keyword in `TSImportAttributes` (#4610)
(Boshen)
- 03c643a semantic: Incorrect `scope_id` for catch parameter symbols
(#4659) (Dunqing)
- 6c612d1 semantic/jsdoc: Handle whitespace absence (#4642) (leaysgur)
- 0d2c41a semantic/jsdoc: Panic on parsing `type_name_comment`. (#4632)
(rzvxa)
- 9f8f299 syntax: Prevent creating invalid u32 IDs (#4675)
(overlookmotel)
- 4797eaa transformer: Strip TS statements from for in/of statement
bodies (#4686) (overlookmotel)
- 5327acd transformer/react: The `require` IdentifierReference does not
have a `reference_id` (#4658) (Dunqing)
- 3987665 transformer/typescript: Incorrect enum-related
`symbol_id`/`reference_id` (#4660) (Dunqing)
- 4efd54b transformer/typescript: Incorrect `SymbolFlags` for jsx
imports (#4549) (Dunqing)

### Performance

- 8dd76e4 codegen: Reduce size of `LineOffsetTable` (#4643)
(overlookmotel)
- b8e6753 codegen: `u32` indexes in `LineOffsetTable` for source maps
(#4641) (overlookmotel)
- 6ff200d linter: Change react rules and utils to use `Cow` and
`CompactStr` instead of `String` (#4603) (DonIsaac)
- 0f5e982 minifier: Only visit arrow expression after dropping
`console.log` (#4677) (Boshen)
- ff43dff sourcemap: Speed up VLQ encoding (#4633) (overlookmotel)
- a330773 sourcemap: Reduce string copying in `ConcatSourceMapBuilder`
(#4638) (overlookmotel)
- 372316b sourcemap: `ConcatSourceMapBuilder` extend `source_contents`
in separate loop (#4634) (overlookmotel)
- c7f1d48 sourcemap: Keep local copy of previous token in VLQ encode
(#4596) (overlookmotel)
- 590d795 sourcemap: Shorten main loop encoding VLQ (#4586)
(overlookmotel)

### Documentation

- c69ada4 ast: Improve AST node documentation (#4051) (Rintaro Itokawa)

### Refactor

- 579b797 ast: Use type identifier instead of `CloneIn::Cloned` GAT.
(#4738) (rzvxa)
- 475266d ast: Use correct lifetimes for name-related methods (#4712)
(DonIsaac)
- 83b6ca9 ast: Add explicit enum discriminants. (#4689) (rzvxa)
- ba70001 ast: Put `assert_layouts.rs` behind `debug_assertions` (#4621)
(rzvxa)
- 3f53b6f ast: Make AST structs `repr(C)`. (#4614) (rzvxa)
- 452e0ee ast: Remove defunct `visit_as` + `visit_args` attrs from
`#[ast]` macro (#4599) (overlookmotel)
- 2218340 ast, ast_codegen: Use `generate_derive` for implementing
`GetSpan` and `GetSpanMut` traits. (#4735) (rzvxa)
- fbfd852 minifier: Add `NodeUtil` trait for accessing symbols on ast
nodes (#4734) (Boshen)
- e0832f8 minifier: Use `oxc_traverse` for AST passes (#4725) (Boshen)
- 17602db minifier: Move tests and files around (Boshen)
- 3289477 minifier: Clean up tests (#4724) (Boshen)
- e78cba6 minifier: Ast passes infrastructure (#4625) (Boshen)
- d25dea7 parser: Use `ast_builder` in more places. (#4612) (rzvxa)
- 09d9822 semantic: Simplify setting scope flags (#4674) (overlookmotel)
- 6e453db semantic: Simplify inherit scope flags from parent scope
(#4664) (Dunqing)
- e1429e5 span: Reduce #[cfg_attr] boilerplate in type defs (#4702)
(overlookmotel)
- e24fb5b syntax: Add explicit enum discriminants to AST related types.
(#4691) (rzvxa)
- 3f3cb62 syntax, span: Reduce #[cfg_attr] boilerplate in type defs
(#4698) (overlookmotel)
- 54f9897 traverse: Simpler code for entering/exiting unconditional
scopes (#4685) (overlookmotel)
- 83546d3 traverse: Enter node before entering scope (#4684)
(overlookmotel)- 9b51e04 Overhaul napi transformer package (#4592)
(DonIsaac)

### Testing

- 49d5196 ast: Fix `assert_layouts.rs` offset tests on 32bit platforms.
(#4620) (rzvxa)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-08-08 14:00:57 +08:00