Commit graph

3429 commits

Author SHA1 Message Date
overlookmotel
a23ba71689 fix(traverse): allow TraverseCtx::find_ancestor closure to return AST node (#3236)
Add a lifetime to `TraverseCtx::find_ancestor` to allow the closure to return an AST node.
2024-05-11 12:07:06 +00:00
overlookmotel
4e20b04acc fix(traverse): create scope for function nested in class method (#3234)
Fixes a bug in #3229.

The logic to prevent a duplicate scope being created for a `Function` which is a `MethodDefinition` would also stop a scope being created for inner function in:

```rs
class X {
  foo() {
    function bar() {}
  }
}
```

or

```rs
class X {
  foo( bar = function() {} ) {}
}
```

This PR fixes that. This change also allows removing `ScopeFlags::Method` which #3229 added.
2024-05-11 12:07:00 +00:00
Dunqing
fd6a1aa1d2 feat(transformer_conformance): correct source type (#3233) 2024-05-11 09:30:43 +00:00
overlookmotel
ec41dba197
refactor(traverse): simplify build script (#3231)
Refactor build script to simplify it. No changes to the `.rs` files the
script creates, only the script itself.
2024-05-11 09:03:16 +01:00
Boshen
2064ae9e0a refactor(parser,diagnostic): one diagnostic struct to eliminate monomorphization of generic types (#3214)
part of #3213

We should only have one diagnostic struct instead 353 copies of them, so we don't end up choking LLVM with 50k lines of the same code due to monomorphization.

If the proposed approach is good, then I'll start writing a codemod to turn all the existing structs to plain functions.

---

Background:

Using `--timings`, we see `oxc_linter` is slow on codegen (the purple part).

![image](https://github.com/zkat/miette/assets/1430279/c1df4f7d-90ef-4c0f-9956-2ec3194db7ca)

The crate currently contains 353 miette errors. [cargo-llvm-lines](https://github.com/dtolnay/cargo-llvm-lines) displays

```
cargo llvm-lines -p oxc_linter --lib --release

  Lines                 Copies               Function name
  -----                 ------               -------------
  830350                33438                (TOTAL)
   29252 (3.5%,  3.5%)    808 (2.4%,  2.4%)  <alloc::boxed::Box<T,A> as core::ops::drop::Drop>::drop
   23298 (2.8%,  6.3%)    353 (1.1%,  3.5%)  miette::eyreish::error::object_downcast
   19062 (2.3%,  8.6%)    706 (2.1%,  5.6%)  core::error::Error::type_id
   12610 (1.5%, 10.1%)     65 (0.2%,  5.8%)  alloc::raw_vec::RawVec<T,A>::grow_amortized
   12002 (1.4%, 11.6%)    706 (2.1%,  7.9%)  miette::eyreish::ptr::Own<T>::boxed
    9215 (1.1%, 12.7%)    115 (0.3%,  8.2%)  core::iter::traits::iterator::Iterator::try_fold
    9150 (1.1%, 13.8%)      1 (0.0%,  8.2%)  oxc_linter::rules::RuleEnum::read_json
    8825 (1.1%, 14.9%)    353 (1.1%,  9.3%)  <miette::eyreish::error::ErrorImpl<E> as core::error::Error>::source
    8822 (1.1%, 15.9%)    353 (1.1%, 10.3%)  miette::eyreish::error::<impl miette::eyreish::Report>::construct
    8119 (1.0%, 16.9%)    353 (1.1%, 11.4%)  miette::eyreish::error::object_ref
    8119 (1.0%, 17.9%)    353 (1.1%, 12.5%)  miette::eyreish::error::object_ref_stderr
    7413 (0.9%, 18.8%)    353 (1.1%, 13.5%)  <miette::eyreish::error::ErrorImpl<E> as core::fmt::Display>::fmt
    7413 (0.9%, 19.7%)    353 (1.1%, 14.6%)  miette::eyreish::ptr::Own<T>::new
    6669 (0.8%, 20.5%)     39 (0.1%, 14.7%)  alloc::raw_vec::RawVec<T,A>::try_allocate_in
    6173 (0.7%, 21.2%)    353 (1.1%, 15.7%)  miette::eyreish::error::<impl miette::eyreish::Report>::from_std
    6027 (0.7%, 21.9%)     70 (0.2%, 16.0%)  <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
    6001 (0.7%, 22.7%)    353 (1.1%, 17.0%)  miette::eyreish::error::object_drop
    6001 (0.7%, 23.4%)    353 (1.1%, 18.1%)  miette::eyreish::error::object_drop_front
    5648 (0.7%, 24.1%)    353 (1.1%, 19.1%)  <miette::eyreish::error::ErrorImpl<E> as core::fmt::Debug>::fmt
```

It's totalling more than 50k llvm lines, and is putting pressure on rustc codegen (the purple part on `oxc_linter` in the image above.

---

It's pretty obvious by looking at https://github.com/zkat/miette/blob/main/src/eyreish/error.rs, the generics can expand out to lots of code.
2024-05-11 04:56:22 +00:00
overlookmotel
46c02aee61 feat(traverse): add scope flags to TraverseCtx (#3229)
Add scope flags to `TraverseCtx`.

Closes #3189.

`walk_*` functions build a stack of `ScopeFlags` as AST is traversed, and they can be queried from within visitors with `ctx.scope()`, `ctx.ancestor_scope()` and `ctx.find_scope()`.

The codegen which generates `walk_*` functions gets the info about which AST types have scopes, and how to check for strict mode from the `#[visited_node]` attrs on AST type definitions in `oxc_ast`.

A few notes:

Each scope inherits the strict mode flag from the level before it in the stack, so if you need to know "am I in strict mode context here?", `ctx.scope().is_strict_mode()` will tell you - no need to travel back up the stack to find out.

Scopes do *not* inherit any other flags from level before it. So `ctx.scope()` in a block nested in a function will return `ScopeFlags::empty()` not `ScopeFlags::Function`.

I had to add an extra flag `ScopeFlags::Method`. The reason for this is to deal with when a `Function` is actually a `MethodDefinition`, and to avoid creating 2 scopes in this case. The principle I'm trying to follow is to encode as little logic in the codegen as possible, as it's rather hidden away. Instead the codegen follows a standard logic for every node, guided by attributes which are visible next to the types in `oxc_ast`. This hopefully makes how `Traverse`'s visitors are generated less mysterious, and easier to change.

The case of `Function` within `MethodDefinition` is a weird one and would not be possible to implement without encoding a magic "special case" within the codegen without this extra `ScopeFlags::Method` variant. Its existence does not alter the operation of any other code in Oxc which uses `ScopeFlags`.

In my view `ScopeFlags` might benefit from a little bit of an overhaul anyway. I believe we could pack more information into the bits and make it more useful.
2024-05-11 04:39:42 +00:00
overlookmotel
4208733180 refactor(ast): order AST type fields in visitation order (#3228)
Reorder the fields in a couple of AST types.

`walk_*` functions in `oxc_traverse` visit the fields in the order they appear in AST. So this change means that decorators are visited before classes and methods that they decorate.
2024-05-11 04:39:39 +00:00
overlookmotel
c84c116ac3 refactor(ast): add is_strict methods (#3227)
De-duplicate logic for checking for `"use strict"` directives.
2024-05-11 04:39:36 +00:00
overlookmotel
132db7d2a1
refactor(traverse): do not expose TraverseCtx::new (#3226)
Creating a `TraverseCtx` with `TraverseCtx::new` should be an internal API within `oxc_traverse`. Don't expose it outside the crate.
2024-05-11 12:17:05 +08:00
Dunqing
6ac8a8479e
fix(transformer): correctly jsx-self inside arrow-function (#3224) 2024-05-11 00:48:18 +00:00
Ali Rezvani
20f643063e
improvement(semantic/cfg): add explicit break block element. (#3223)
Works similar to how `throw` is working but is done for `break` statements.
2024-05-10 22:22:30 +08:00
Ali Rezvani
5e36e0d575
fix(semantic): add cfg nodes for ConditionalExpressions. (#3127)
It is done similarly to how `IfStatement`s are structured at the moment.
2024-05-10 22:16:55 +08:00
Ali Rezvani
c91d26129c
fix(semantic): connect test expression of for statements to the cfg. (#3122)
I don't know if it is correct or not, Fixes my issues with dangling cfg nodes created in for statements. #3071
2024-05-10 22:11:25 +08:00
Dunqing
18d853bb2b
feat(transformer/react): support development mode (#3143) 2024-05-10 22:07:33 +08:00
overlookmotel
f0cbbbe28c
ci: build each benchmark only with deps it needs (#3221)
This PR builds on #3201 to further speed up the benchmarks and reduce CI
time.

* Build and run each benchmark as separate job (like before).
* But now each bench is only built with the dependencies it needs.
* For linter benchmarks, build benchmark in 1 job (like #3201 does).
* Run each linter fixture in a separate job as they're slow.

This reduces total time to complete benchmarks from between 6m-7m to
~4m40s.

All the individual jobs complete in under 1m30s, except for building
linter benchmark which takes 2m30s. So there won't be the problem of
blocking the CI queue that there was before.

NB: I did try this before, and didn't see a benefit. But I realized
today what I was doing wrong - it only works once the caches are
populated by a previous run on main branch.

So the CI times in this PR won't look good, but once it's merged to
main, it will take effect. Here it is running on main branch of my fork:

https://github.com/overlookmotel/oxc/actions/runs/9030511348

I also added a step to delete the temp artefacts which aren't needed
once the run has completed.
2024-05-10 22:01:24 +08:00
overlookmotel
952565329b
refactor(transformer): remove no-op scopes code (#3210)
Remove some code from the transformer which doesn't do anything.
2024-05-09 19:40:48 +08:00
Dunqing
0ba7778e5e
fix(parser): correctly parse cls.fn<C> = x (#3208)
close: #3206
2024-05-09 10:23:45 +08:00
Kuba Jastrzębski
7113e850b4
feat(linter): add radix rule (#3167) 2024-05-08 23:44:12 +08:00
overlookmotel
9590eb0cf4
fix(transform): implement transform-react-display-name with bottom-up lookup (#3183)
Sliced off from #3152.

Re-implement `transform-react-display-name` using bottom-up lookup
provided by `Traverse` trait.

This fixes the 1 remaining failing test case for this plugin (see
#2937).

`Traverse` is not complete yet (see #3182), so this is also not ready to
merge yet.
2024-05-08 15:33:39 +00:00
Boshen
be2aaa1d7e
ci: add release crates workflow
closes #2518
2024-05-08 22:32:05 +08:00
Boshen
15f275f572
refactor(linter): reduce llvm lines generated by RuleEnum::read_json (#3207)
Previous: 

```
  Lines                 Copies               Function name
  -----                 ------               -------------
    9150 (1.1%, 13.8%)      1 (0.0%,  8.2%)  oxc_linter::rules::RuleEnum::read_json
```

After:

```
    2285 (0.3%, 36.2%)      1 (0.0%, 40.3%)  oxc_linter::rules::RuleEnum::read_json
```
2024-05-08 21:53:06 +08:00
Dunqing
a227050ed0
chore: update babel repo (#3205) 2024-05-08 20:15:15 +08:00
overlookmotel
be958ce299
refactor(transform): transformer use Traverse (#3182)
Sliced off from #3152.

This switches the transformer over to use `Traverse` instead of
`VisitMut`.

This is incomplete - scopes are not implemented yet. At present, no
transforms use scopes anyway, so all tests pass, but regardless I don't
think should be merged until the implementation is complete.
2024-05-08 17:18:40 +08:00
Boshen
5683aceae9
ci: improve benchmark build time by only building once (#3201)
relates #3200
2024-05-08 14:54:10 +08:00
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
Boshen
9b93a17429
chore(tasks/lint_rules): remove the now fixed @ts-expect-error
relates #2932
2024-05-07 23:21:12 +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
Boshen
b66d578fed
chore: update renovate.json 2024-05-07 12:33:40 +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
Boshen
4e9b9d9e09
chore(renovate): migrate renovate config 2024-05-07 00:59:43 +08:00
Boshen
51855d6734
chore(renovate): remove unused ignoreDeps from rust crates 2024-05-07 00:37:07 +08: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
d91b688f3a
chore(rulegen): default rule to nursery 2024-05-06 17:17:36 +08:00
Boshen
8fbba03afd
ci: reduce the total number of linter benchmarks (#3172)
closes #2981
2024-05-06 12:54:50 +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
renovate[bot]
e6027beec3
chore(deps): lock file maintenance (#3163)
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Update | Change |
|---|---|
| lockFileMaintenance | All locks refreshed |

🔧 This Pull Request updates lock files to use the latest dependency
versions.

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on monday" in timezone
Asia/Shanghai, Automerge - 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.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

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

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/oxc-project/oxc).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNDAuMTAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4zNDAuMTAiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbXX0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-06 12:34:01 +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