Commit graph

2496 commits

Author SHA1 Message Date
Wang Wenzhe
cba1e7f463
chore(linter): shorten eslint/eqeqeq rule error message's span (#3193) 2024-05-08 14:32:57 +08:00
Boshen
7363e14335
feat(sourcemap): add "rayon" feature (#3198) 2024-05-07 23:47:36 +08:00
overlookmotel
5329b0f260
refactor(transform): fix doc comments for methods generated by inherit_variants! macro (#3195)
Nit: Remove whitespace from doc comments for methods generated by
`inherit_variants!` macro.
2024-05-07 15:16:43 +01:00
overlookmotel
a4f881fff4
docs(transform): improve docs for TraverseCtx::ancestors_depth (#3194)
Update doc comment to clarify what `TraverseCtx::ancestors_depth`
returns.
2024-05-07 15:15:40 +01:00
Boshen
ed3fa399a6
feat(linter): add --format github for github check annotation (#3191)
closes #480
2024-05-07 20:55:10 +08:00
overlookmotel
c6bd616456
docs(ast): document enum inheritance (#3192)
Add more docs for AST type enum inheritance and the `inherit_variants!`
macro.

This covers the changes made in #3115.
2024-05-07 20:28:15 +08:00
Boshen
82bd97d420
refactor(diagnostics): use a trait to implement the reporters (#3190) 2024-05-07 18:44:03 +08:00
Dunqing
fa0093b222
feat(linter): eslint-plugin-next/no-page-custom-font (#3185) 2024-05-07 09:53:43 +00:00
Boshen
4defe37f12
feat(linter): remove deprecated eslint v9 rules no-return-await and no-mixed-operators (#3188) 2024-05-07 17:17:31 +08:00
Boshen
ca9f13f4f2
feat(linter): eslint/no-new-native-nonconstructor (#3187)
feat(linter): eslint/no-new-native-nonconstructor

closes #3179

remove no new symbol
2024-05-07 17:10:17 +08:00
Boshen
7338364219
perf(lexer): improve comment building performance by using a vec instead of btreemap (#3186)
closes #2693
2024-05-07 16:43:27 +08:00
Jelle van der Waa
5081652bc1
feat(linter/eslint): Implement no-empty-function rule (#3181)
Rule Detail:
[link](https://eslint.org/docs/latest/rules/no-empty-function)

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-05-07 02:56:13 +00:00
Dunqing
5514936f51
feat(linter): eslint-plugin-next/no-styled-jsx-in-document (#3184) 2024-05-07 10:35:28 +08:00
overlookmotel
762677e17b
refactor(transform): retag_stack use AncestorType (#3173)
Make the code for `retag_stack` in `walk_*` functions more
comprehensible, by replacing hard-coded enum discriminants as integers
with an `AncestorType` type.

Alternative solution to the problem raised in #3170.

close: #3170
2024-05-06 18:53:41 +01:00
Wang Wenzhe
c0abbbd204
feat(linter/tree-shaking): add isPureFunction (#3175) 2024-05-07 00:03:04 +08:00
Cameron
07076d9765
feat(linter) improve prefer-string-starts-ends-with rule (#3176)
basically:
`^#/i.test(hex)` is the same as `/^#/.test(hex)`
so, in this case the `i` flag does nothing and we can safely ignore it


inspired by https://x.com/the_moisrex/status/1787444601571221892


---

This could potentially lead a new `oxc` rule called
`no_useless_case_insensitive_regex_flag` that reports if you have a
regex with the `i` flag with no ascii alphabetic chars.
2024-05-06 23:52:19 +08:00
Boshen
cb2e651eea
feat(linter): eslint-plugin-next/no-duplicate-head (#3174)
closes #2438
2024-05-06 19:43:38 +08:00
Boshen
a84454cf87
refactor(linter): clean up prefer_node_protocol and move to restriction (#3171)
closes #3161
2024-05-06 12:43:24 +08:00
overlookmotel
be87ca8419
feat(transform): oxc_traverse crate (#3169)
First part of #3152.

This adds a crate `oxc_traverse`, but doesn't connect it up to the
transformer or anything else yet.

I think we could merge this now - as it doesn't affect any code that's
in use - and then iterate on it to add scopes before using it in
transformer. Please see
https://github.com/oxc-project/oxc/pull/3152#issuecomment-2094965406 for
the broader picture.
2024-05-06 09:37:04 +08:00
Dunqing
f88f330426
feat(linter/import): improve multiple exports error message (#3160)
https://github.com/oxc-project/oxc/issues/3157#issuecomment-2094113518
2024-05-04 22:14:26 +08:00
Boshen
d78edb1eb5
chore(linter): move more import plugin rules out of nursery 2024-05-04 19:07:25 +08:00
Dunqing
6f5df113af
feat(linter/import): move some rules out of nursery (#2841)
### Correctness
- named
- export
- default
- namespace
- no-unresolved

The above rules will prevent bugs or broken, so I changed these to
correctness

### Suspicious
- no-duplicates
- no-named-as-default-member
- no-named-as-default

I'm still not sure whether the `no-unresolved` rule should be changed to
`correctness`. Because I found that the rule reported a lot in the
AFFiNE. But in fact, they are all correct reports

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-05-04 18:51:26 +08:00
Boshen
fde7d65992
fix(linter): handle named export default in import-plugin/named (#3158) 2024-05-04 17:40:56 +08:00
Boshen
b1bddacd09
fix(linter): fix hang if a file fails to parse while using --import-plugin 2024-05-04 17:16:43 +08:00
Boshen
222030c4c5
refactor(linter): render --rules in a table 2024-05-04 15:31:20 +08:00
Wang Wenzhe
32df6d779c
feat(linter/tree-shaking): support While/Switch/Yield Statement (#3155)
All test cases passed now. There are something remain. I'll change the category when it's ready.

- [ ] Port [`isPureFuncion`](96f0d1c825/src/utils/helpers.ts (L53-L67)), [pure-function](198432ecc9/src/utils/pure-functions.ts) 
- [ ] Support [options](463fa1f0be/src/rules/no-side-effects-in-initialization.ts (L1130-L1138))
- [ ] Clean TODO
- [ ] Add more support of operator computation. [The original version](463fa1f0be/src/rules/no-side-effects-in-initialization.ts (L139-L210)) is straightforward benefit of JS. We only [support Number](f91a063616/crates/oxc_linter/src/utils/tree_shaking.rs (L194)) now.
2024-05-03 23:21:28 +08:00
Wang Wenzhe
8290421e72
feat(linter/tree-shaking): support SequenceExpression (#3154) 2024-05-03 23:19:36 +08:00
Wang Wenzhe
5c21b7f843
feat(linter/tree-shaking): support UnaryExpression (#3153) 2024-05-03 23:17:15 +08:00
renovate[bot]
56a0db8621
chore(deps): update dependency rust to v1.78.0 (#3151) 2024-05-03 00:01:42 +08:00
Yuji Sugiura
5a1d63afed
feat(linter/jsdoc): Implement require-yields rule (#3150)
Part of #1170 

>
https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-yields.md

With:

- Fix typo of JSDocPluginSettings
- Update utils/jsdoc
2024-05-02 22:49:19 +08:00
Yuji Sugiura
d7a8345e4c
feat(linter/jsdoc): Support settings.ignore(Private|Internal) (#3147)
>
https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/settings.md#user-content-settings-allow-tags-private-or-internal-to-disable-rules-for-that-comment-block

...and fixed the issue that intended initial settings values are not
used in some cases.
2024-05-01 19:48:28 +08:00
Wang Wenzhe
8cdd5b0fd8
feat(linter/tree_shaking): support LogicExpression and MemberExpression (#3148)
MemberExpression's message is not accurate, I will update later.
2024-05-01 19:46:40 +08:00
kaykdm
80cf0b2b2e
feat(linter): @typescript-eslint/prefer-literal-enum-member (#3134)
Related issue: https://github.com/oxc-project/oxc/issues/2180

original implementation

- code: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/prefer-literal-enum-member.ts
- test: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/prefer-literal-enum-member.test.ts
- doc: https://typescript-eslint.io/rules/prefer-literal-enum-member/
2024-05-01 11:17:46 +08:00
Dunqing
a52e321b25
feat(transformer/jsx-source): get the correct lineNumber and columnNumber from the span. (#3142) 2024-05-01 00:30:47 +00:00
Boshen
f5dccc96fd
refactor(coverage): avoid an String::from_utf8 over head during serialization (#3145) 2024-04-30 23:11:22 +08:00
Dunqing
be8fabedab
feat(transformer/react): enable jsx plugin when development is true (#3141) 2024-04-30 15:48:17 +08:00
Boshen
a63a45d5b2
refactor(transformer): remove the requirement of Semantic (#3140)
It seems like we need to rebuild the scopes and symbols while
traversing. We can't utilize the scopes and symbols built by semantic
because they are immutable.
2024-04-30 12:48:21 +08:00
Wang Wenzhe
733361822e
feat(linter/tree-shaking): support JSX (#3139) 2024-04-30 11:25:39 +08:00
Wang Wenzhe
16a31e95b8
feat(linter/tree-shaking): support import statement (#3138) 2024-04-30 11:23:23 +08:00
Dunqing
b5894964d1
fix(transformer/arrow-functions): should not transform this in class (#3129) 2024-04-29 13:44:10 +00:00
Boshen
f1ccbd4856
feat(syntax): add ToJsInt32 trait for f64 (#3132) 2024-04-29 21:13:04 +08:00
Boshen
870d11f1bb
feat(syntax): add ToJsString trait for f64 (#3131) 2024-04-29 21:00:04 +08:00
Boshen
a8af5de8f5
refactor(syntax): move number related functions to number module (#3130) 2024-04-29 18:54:35 +08:00
Dunqing
843318cdbe
refactor(transformer/typescript): reimplementation of Enum conversion based on Babel (#3102)
The remaining test cases will perform better with a scope
implementation, and while we can implement them without the scope, it
still requires us to do what the scope did.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-04-29 16:26:22 +08:00
Dunqing
905ee3fef1
feat(transformer): add arrow-functions plugin (#3083)
close: #2983
2024-04-29 08:04:01 +00:00
Boshen
ae6561308c
refactor(syntax): use FxHashMap for ModuleRecord::request_modules (#3124)
closes #3121

The ordering is not important here.
2024-04-29 03:37:34 +00:00
overlookmotel
514228ad42
deps(allocator): disable serde dep by default (#3120)
`oxc_allocator` currently depends on `serde`, although it's generally
not required.

This PR puts the dependency behind a feature `serialize`.

NB: `serde` is needed for the crate's tests, but this can be enabled by
adding it to `dev-dependencies` and putting the impls behind
`#[cfg(any(feature = "serialize", test))]`.
2024-04-28 22:17:32 +08:00
Wang Wenzhe
8f31ad618e
chore(linter/tree-shaking): uncomment function and identifier test cases (#3119) 2024-04-28 22:05:33 +08:00
overlookmotel
7e1fe36c68
refactor(ast): squash nested enums (#3115)
OK, this is a big one...

I have done this as part of work on Traversable AST, but I believe it
has wider benefits, so thought better to spin it off into its own PR.

## What this PR does

This PR squashes all nested AST enum types (#2685).

e.g.: Previously:

```rs
pub enum Statement<'a> {
    BlockStatement(Box<'a, BlockStatement<'a>>),
    /* ...other Statement variants... */
    Declaration(Declaration<'a>),
}

pub enum Declaration<'a> {
    VariableDeclaration(Box<'a, VariableDeclaration<'a>>),
    /* ...other Declaration variants... */
}
```

After this PR:

```rs
#[repr(C, u8)]
pub enum Statement<'a> {
    BlockStatement(Box<'a, BlockStatement<'a>>) = 0,
    /* ...other Statement variants... */

    VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 32,
    /* ...other Declaration variants... */
}

#[repr(C, u8)]
pub enum Declaration<'a> {
    VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 32,
    /* ...other Declaration variants... */
}
```

All `Declaration`'s variants are combined into `Statement`, but
`Declaration` type still exists.

As both types are `#[repr(C, u8)]`, and the discriminants are aligned, a
`Declaration` can be transmuted to a `Statement` at zero cost.

This is the same thing as #2847, but here applied to *all* nested enums
in the AST, and with improved helper methods.

No enums increase in size, and a few get smaller. Indirection is reduced
for some types (this removes multiple levels of boxing).

## Why?

1. It is a prerequisite for Traversable AST (#2987).
2. It would help a lot with AST Transfer (#2409) - it solves the only
remaining blocker for this.
3. It is a step closer to making the whole AST `#[repr(C)]`.

## Why is it a good thing for the AST to be `#[repr(C)]`?

Oxc's direction appears to be increasingly to build up control over the
fundamental primitives we use, in order to unlock performance and
features. We have our own allocator, our own custom implementations for
`Box` and `Vec`, our own `IndexVec` (TBC). The AST is the central
building block of Oxc, and taking control of its memory layout feels
like a step in this same direction.

Oxc has a major advantage over other similar libraries in that it keeps
all the AST data in an arena. This opens the door to treating the AST
either as Rust types or as *pure data* (just bytes). That data can be
moved around and manipulated beyond what Rust natively allows.

However, to enable that, the types need to be well-specified, with
completely stable layouts. `#[repr(C)]` is the only tool Rust provides
to do this.

Once the types are `#[repr(C)]`, various features become possible:

1. Cheap transfer of the AST across boundaries without ser/deser - the
property used by AST Transfer.
2. Having multiple versions of the AST (standard, read-only,
traversable), and these AST representations can be converted to one
other at zero cost via transmute - the property used by Traversable AST
scheme.
3. Caching AST data on disk (#3079) or transferring across network.
4. Stuff we haven't thought of yet!

Allowing the AST to be treated as pure data will likely unlock other
"next level" features further down the track (caching for "edge
bundling" comes to mind).

## The problem with `#[repr(C)]`

It's not *required* to squash nested enums to make the AST `#[repr(C)]`.

But the problem with `#[repr(C)]` is that it disables some compiler
optimizations. Without `#[repr(C)]`, the compiler squashes enums itself
in some cases (which is how `Statement` is currently 16 bytes). But
making the types `#[repr(C)]` as they are currently disables this
optimization.

So this PR essentially makes explicit what the compiler is already doing
- and in fact goes a bit further with the optimization than the compiler
is able to, in squashing 3 or 4 layers of nested enums (the compiler
only does up to 2 layers).

## Implementation

One enum "inheriting" variants from another is implemented with
`inherit_variants!` macro.

```rs
inherit_variants! {
#[repr(C, u8)]
pub enum Statement<'a> {
    BlockStatement(Box<'a, BlockStatement<'a>>),
    /* ...other Statement variants... */
    
    // `Declaration` variants added here by `inherit_variants!` macro
    @inherit Declaration
    // `ModuleDeclaration` variants added here by `inherit_variants!` macro
    @inherit ModuleDeclaration
}
}
```

The macro is *fairly* lightweight, and I think the above is quite easy
to understand. No proc macros.

The macro also implements utility methods for converting between enums
e.g. `Statement::as_declaration`. These methods are all zero-cost
(essentially transmutes).

New patterns for dealing with nested enums are introduced:

Creation:

```rs
// Old
let stmt = Statement::Declaration(Declaration::VariableDeclaration(var_decl));

// New
let stmt = Statement::VariableDeclaration(var_decl);
```

Conversion:

```rs
// Old
let stmt = Statement::Declaration(decl);

// New
let stmt = Statement::from(decl);
```

Testing:

```rs
// Old
if matches!(stmt, Statement::Declaration(_)) { }
if matches!(stmt, Statement::ModuleDeclaration(m) if m.is_import()) { }

// New
if stmt.is_declaration() { }
if matches!(stmt, Statement::ImportDeclaration(_)) { }
```

Branching:

```rs
// Old
if let Statement::Declaration(decl) = &stmt { decl.do_stuff() };

// New
if let Some(decl) = stmt.as_declaration() { decl.do_stuff() };
```

Matching:

```rs
// Old
match stmt {
    Statement::Declaration(decl) => visitor.visit(decl),
}

// New (exhaustive match)
match stmt {
    match_declaration!(Statement) => visitor.visit(stmt.to_declaration()),
}

// New (alternative)
match stmt {
    _ if stmt.is_declaration() => visitor.visit(stmt.to_declaration()),
}
```

New syntax has pluses and minuses vs the old. `match` syntax is worse,
but when working with a deeply nested enum, the code is much nicer -
it's shorter and easier to read.

This PR removes 200 lines from the linter with changes like this:


https://github.com/oxc-project/oxc/pull/3115/files#diff-dc417ff57352da6727a760ec6dee22de6816f8231fb69dbef1bf05d478699103L92-R95

```diff
- let AssignmentTarget::SimpleAssignmentTarget(simple_assignment_target) =
-     &assignment_expr.left
- else {
-     return;
- };
- let SimpleAssignmentTarget::AssignmentTargetIdentifier(ident) =
-     simple_assignment_target
+ let AssignmentTarget::AssignmentTargetIdentifier(ident) = &assignment_expr.left
else {
    return;
};
```
2024-04-28 20:40:37 +08:00
Dunqing
c3d8a85eb5
feat(semantic): report that enum member must have initializer (#3113)
See
https://www.typescriptlang.org/play/?target=99#code/KYOwrgtgBAglDeAoKKoEMoF4oFk0BcALAOgGcBLEACgEYBKAGmVQCMmBfRRUSKAIQTMUGbACIaAJgDMopqihtEnRABtg+KADMwIAMZYoVOlgB8USVIDc3cNADCg+SK07dRuaw6IgA
2024-04-27 22:08:02 +08:00