Commit graph

4358 commits

Author SHA1 Message Date
Dunqing
c362bf7edf fix(semantic): incorrect resolve references for TSInterfaceHeritage (#4311)
related issue: https://github.com/oxc-project/oxc/issues/3963
fixes: https://github.com/oxc-project/monitor-oxc/actions/runs/9960183591/job/27518854841
2024-07-17 03:33:02 +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
48724a0d44 chore(semantic): copy tests from typescript-eslint’s scope-manager (#3990)
close:  #2947

These fixtures were copied from a5b652da1e/packages/scope-manager/tests/fixtures. We used them to test out semantic `ScopeTree` and `SymbolTable`
2024-07-17 02:50:50 +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
Dunqing
af4dc0112e feat(ast): align ts ast scope with typescript (#4253)
close: #3969
close: #2023

We need to add scope according to [this](d8086f14b6/src/compiler/binder.ts (L3883-L3962)). There are still some ASTs that need to be added to the scope.

---

Context from @Boshen:

Before this whole journey of fixing symbols and scopes I asked @Dunqing to debug through binder.ts via a debugger to fully understand how it does resolution.

We then agreed to align the implementation so that when a problem occurs, we can debug through both implementations and find where our problem is.

tsc doesn't have a specification, so we need to align with the reference implementation instead.
2024-07-17 02:50:47 +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
rzvxa
fc0b17d5a0 refactor(syntax): turn the AstNodeId::dummy into a constant field. (#4308) 2024-07-16 22:43:21 +00:00
rzvxa
96af45933a
chore(justfile): add just check to the just ast command. (#4302)
I use this in my local environment when I'm developing the ast_codegen
and want a thorough build with the newly generated files. If you find it
useful merge otherwise feel free to close.
2024-07-16 23:58:29 +08:00
Boshen
8e2fd8c5f3
ci: remove graphite optimize_ci
The savings does not out weight the confusion this feature brings yet.
2024-07-16 21:26:33 +08:00
Boshen
4a50b88af4
ci: remove instructions for sccache
Performance improvement does not out weight cost yet.
2024-07-16 21:24:32 +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
c5731a5431 refactor(semantic): remove defunct code setting ScopeFlags twice (#4286)
Scope flags for functions is set when the scope is created. Remove redundant code that sets them again.
2024-07-16 07:32:36 +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
Boshen
bf3d8d3e8f fix(codegen): print annotation comment inside parens for new and call expressions (#4290) 2024-07-16 06:55:36 +00:00
Boshen
107e57019c feat(coverage): run multi-file typescript tests (#4256)
The code is really ugly because I didn't anticipate multi-test files from typescript when I started writing the runner :-(
2024-07-16 06:15:04 +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
overlookmotel
16698bc191 refactor(semantic): move function/class-specific code into specific visitors (#4278)
Instead of calling `bind_function_or_class_expression` for every scope, and then branching on the AST node kind, insert the relevant code into the visitors for functions and classes. This reduces work on all other kinds of scopes e.g. block statements.
2024-07-15 18:36:20 +00:00
overlookmotel
ee16668168 refactor(semantic): rename function param (#4277)
Small style nit. It's nicer to have Rust Analyser's param name hint show `builder` rather than `_builder`.
2024-07-15 15:06:59 +00:00
overlookmotel
25f0771185 refactor(semantic): alter syntax of control_flow! macro (#4275)
Previously:

```rs
let ix = control_flow!(|self, cfg| cfg.current_node_ix);
```

after this PR:

```rs
let ix = control_flow!(self, |cfg| cfg.current_node_ix);
```

It expands to:

```rs
let ix = if let Some(ref mut cfg) = self.cfg {
    cfg.current_node_ix
} else {
    Default::default()
};
```

So rationale for this change is that it makes it clearer that `self` is passed *in* and `cfg` comes *out* into the "closure".
2024-07-15 14:31:04 +00:00
overlookmotel
639fd48227 refactor(semantic): comment why extra CFG enabled check (#4274)
Add a comment referencing conclusions of #4273.
2024-07-15 12:11:04 +00:00
overlookmotel
f9d3f2ef55 perf(semantic): inline ast record functions (#4272)
Inline these functions so that when CFG is disabled, it doesn't cost a function call just for the trivial `if self.cfg.is_some()` check.

Based on @rzvxa's suggestion in https://github.com/oxc-project/oxc/pull/4263#pullrequestreview-2176762670.
2024-07-15 11:43:48 +00:00
lucab
9a87e41332 fix(parser): avoid crashing on invalid const modifier (#4267)
Followup on https://github.com/oxc-project/oxc/pull/3977, fixing one of the crashes at https://github.com/oxc-project/oxc/pull/3977.
2024-07-15 11:37:31 +00:00
underfin
f144082a60
fix(minifier): RemoveDeadCode should visit nested expression (#4268) 2024-07-15 19:37:12 +08:00
overlookmotel
8fad7dbac3 perf(semantic): reduce AstNodeId to u32 (#4264)
`AstNodeId` was a `usize`. This seems excessive.

Parser has a limit on size of a JS file of 4 GiB. While it is *possible* for a JS file of that size to create an AST with more than `1 << 32` (~4 billion) AST nodes, that would be insanely large.

So make `AstNodeId` `u32` instead.
2024-07-15 04:31:51 +00:00
overlookmotel
23743dbd59 perf(semantic): do not record ast nodes for cfg if cfg disabled (#4263)
Control flow graph builder records AST node IDs in a temp structure `ast_node_records`. Disable this if CFG is disabled.
2024-07-15 04:31:47 +00:00
overlookmotel
da69076c98 perf(semantic): reduce overhead of cfg recording ast nodes (#4262)
Control flow graph builder records AST node IDs in a temp structure `ast_nodes_records`. Only the first AST node ID inserted into the `Vec` is ever used, so turn it into just a single value.

Using `AstNodeId::dummy()` as sentinel for "not set" instead of using `Option<AstNodeId>` to reduce size of the records stack. `Option<AstNodeId>` is 16 bytes.
2024-07-15 04:31:43 +00:00
overlookmotel
a8dc4f34b2
perf(parser): speed up parsing numbers with _ separators (#4259)
When parsing a number which contains `_` separators, rather than looping
through the string once to remove separators, and then again to convert
to an `f64`, do it all in a single loop.

Co-authored-by: overlookmotel <557937+overlookmotel@users.noreply.github.com>
2024-07-15 12:27:47 +08:00
overlookmotel
b94540d0fb perf(parser): speed up parsing octal literals (#4258)
Micro-optimization to parsing octal numbers. This removes usage of `char`, so removes a surprising amount of instructions.
2024-07-15 03:42:47 +00:00
overlookmotel
a7b328c4f5 perf(parser): faster parsing decimal numbers (#4257)
Closes #3288.

Speed up parsing decimal literals (e.g. `123`), using same techniques as `parse_octal` etc.
2024-07-15 03:38:09 +00:00
Dunqing
c418bf53ce refactor(semantic): directly record current_node_id when adding a scope (#4265)
close: #4234
2024-07-15 03:02:18 +00:00
Boshen
f85188b1af
chore: sync ast changes 2024-07-15 10:51:38 +08:00
renovate[bot]
d3df3c20d9
chore(deps): update rust crates (#4261)
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [compact_str](https://togithub.com/ParkMyCar/compact_str) |
workspace.dependencies | minor | `0.7.1` -> `0.8.0` |
| [napi](https://togithub.com/napi-rs/napi-rs) | workspace.dependencies
| patch | `3.0.0-alpha.3` -> `3.0.0-alpha` |
| [napi-derive](https://togithub.com/napi-rs/napi-rs) |
workspace.dependencies | patch | `3.0.0-alpha.2` -> `3.0.0-alpha` |
| [oxc_resolver](https://togithub.com/oxc-project/oxc-resolver) |
workspace.dependencies | minor | `1.9.3` -> `1.10.0` |

---

### Release Notes

<details>
<summary>ParkMyCar/compact_str (compact_str)</summary>

###
[`v0.8.0`](https://togithub.com/ParkMyCar/compact_str/blob/HEAD/CHANGELOG.md#080)

##### July 8, 2024

#### Breaking Changes 💥

- Consolidate `CompactString::new_inline(...)` and
`CompactString::from_static_str(...)` into
`CompactString::const:new(...)`. Methods are currently marked as
deprecated and will be removed in `v0.9.0`.
- Implemented in [`Add const_new(); remove new_inline() and
from_static_str()`](https://togithub.com/ParkMyCar/compact_str/pull/336)
-   Minimum supported Rust version (MSRV) was bumped to `v1.60`
- Implemented in [`fix: MSRV check and change MSRV to
1.60`](https://togithub.com/ParkMyCar/compact_str/pull/395)

#### Changes

- Add support for [`borsh`](https://crates.io/crates/borsh) under an
optional feature.
- Implemented in [`Add borsh
support`](https://togithub.com/ParkMyCar/compact_str/pull/393)
-   Add additial `PartialEq` impls for `CompactString`
- Implemented in [`fix: More PartialEq
impls`](https://togithub.com/ParkMyCar/compact_str/pull/381)
-   Match alignment of internal `InlineBuffer` and `Repr`.
- Implemeneted in [`Align InlineBuffer same as
Repr`](https://togithub.com/ParkMyCar/compact_str/pull/358)
-   Fix conflict between `serde` and `no_std` features.
- Implemented in [`fix serde no-std
issue`](https://togithub.com/ParkMyCar/compact_str/pull/347)
-   Improve performance of `CompactString::is_empty`.
- Implemented in [`Simplify
is_empty()`](https://togithub.com/ParkMyCar/compact_str/pull/330)
-   Implement additional `From` impls that `std::string::String` has.
- Implemented in [`Add missing From impls that String
has`](https://togithub.com/ParkMyCar/compact_str/pull/328)
- Implement
[`Clone::clone_from`](https://doc.rust-lang.org/std/clone/trait.Clone.html#method.clone_from)
for `CompactString`.
- Implemented in [`Impl Clone::clone_from for
CompactString`](https://togithub.com/ParkMyCar/compact_str/pull/325)
-   Make re-allocations of a heap-based `CompactString` fallible.
- Implemented in [`Make (re)allocations
fallible`](https://togithub.com/ParkMyCar/compact_str/pull/323)
-   Inline short `&'static str`s
- Implemented in [`Inline short static
strings`](https://togithub.com/ParkMyCar/compact_str/pull/321)
- Add support for serializing a `CompactString` from
[`diesel`](https://crates.io/crates/diesel) and
[`sqlx`](https://crates.io/crates/sqlx)
- Implemented in [`Implement diesel
compatibility`](https://togithub.com/ParkMyCar/compact_str/pull/318)
- Implemented in [`Implement for
sqlx`](https://togithub.com/ParkMyCar/compact_str/pull/329)

... and everything from `v0.8.0-beta`

#### Fixed Issues

- Re-enabled specialization for `String` in `trait ToCompactString` by
upgrading to `castaway v0.2.3`
- Implemented in [`deps: Upgrade to castaway
v0.2.3`](https://togithub.com/ParkMyCar/compact_str/pull/394)

</details>

<details>
<summary>napi-rs/napi-rs (napi)</summary>

###
[`v3.0.0-alpha.7`](https://togithub.com/napi-rs/napi-rs/compare/napi@3.0.0-alpha.6...napi@3.0.0-alpha.7)

[Compare
Source](https://togithub.com/napi-rs/napi-rs/compare/napi@3.0.0-alpha.6...napi@3.0.0-alpha.7)

</details>

<details>
<summary>oxc-project/oxc-resolver (oxc_resolver)</summary>

###
[`v1.10.0`](https://togithub.com/oxc-project/oxc-resolver/blob/HEAD/CHANGELOG.md#1100---2024-07-11)

[Compare
Source](https://togithub.com/oxc-project/oxc-resolver/compare/oxc_resolver-v1.9.4...oxc_resolver-v1.10.0)

##### Added

- *(napi)* expose module type info in ResolveResult
([#&#8203;223](https://togithub.com/oxc-project/oxc_resolver/pull/223))

##### Fixed

- remove `#[cfg(target_os = "windows")]` logic in `canonicalize`
([#&#8203;221](https://togithub.com/oxc-project/oxc_resolver/pull/221))

##### Other

- update `cargo deny`
([#&#8203;222](https://togithub.com/oxc-project/oxc_resolver/pull/222))
-   pin crate-ci/typos version

###
[`v1.9.4`](https://togithub.com/oxc-project/oxc-resolver/blob/HEAD/CHANGELOG.md#194---2024-07-10)

[Compare
Source](https://togithub.com/oxc-project/oxc-resolver/compare/oxc_resolver-v1.9.3...oxc_resolver-v1.9.4)

##### Other

- use custom canonicalize impl to avoid useless syscall
([#&#8203;220](https://togithub.com/oxc-project/oxc_resolver/pull/220))
- add symlink fixtures
([#&#8203;219](https://togithub.com/oxc-project/oxc_resolver/pull/219))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 10am on monday" in timezone
Asia/Shanghai, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **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:eyJjcmVhdGVkSW5WZXIiOiIzNy40MzEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjQzMS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: rzvxa <rzvxa@protonmail.com>
2024-07-15 10:49:08 +08:00
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
overlookmotel
641a78bc2b fix(parser): fix tests for number parsing (#4254)
Fix tests for parsing large numbers.

* Tests for imprecision decimals and octals weren't using numbers larger than `u64::MAX`, so were not testing what they were meant to be - that parser can handle larger numbers.
* Tests for decimals (e.g. `123.5`) were calling `parse_int` which isn't meant to handle that (`parse_float` does that).
* Tests were calling `parse_int` with negative numbers, which it's not meant to handle.

Also, re-order the const assertions in same order as the code above.
2024-07-15 01:26:13 +00:00
renovate[bot]
fef5d053e7
chore(deps): update crate-ci/typos action to v1.23.2 (#4260)
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [crate-ci/typos](https://togithub.com/crate-ci/typos) | action | patch
| `v1.23.1` -> `v1.23.2` |

---

### Release Notes

<details>
<summary>crate-ci/typos (crate-ci/typos)</summary>

###
[`v1.23.2`](https://togithub.com/crate-ci/typos/releases/tag/v1.23.2)

[Compare
Source](https://togithub.com/crate-ci/typos/compare/v1.23.1...v1.23.2)

#### \[1.23.2] - 2024-07-10

##### Features

-   Automatically ignore JWT tokens

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 10am on monday" in timezone
Asia/Shanghai, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- 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:eyJjcmVhdGVkSW5WZXIiOiIzNy40MzEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjQzMS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-07-14 21:15:01 +00:00
Boshen
9a094e861e
chore(codegen): add ts snapshot test 2024-07-14 20:46:22 +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
8bfeabfe6a refactor(semantic): simplify adding SymbolFlags::Export (#4249)
```ts
export default class Binding {}
//                   ^^^^^^^ SymbolFlags::Export

export default function Binding () {}
//                      ^^^^^^^ SymbolFlags::Export

// No binding, so we should not have SymbolFlags::Export
export default function() {}
export default class {}

```
2024-07-14 11:39:13 +00:00
Boshen
83bd40db4e
chore: turn off doctest for all [[bin]] 2024-07-14 16:55:19 +08:00
Boshen
d1c4be0020
refactor(codegen): clean up annotation_comment 2024-07-14 15:55:50 +08:00
Boshen
9fbe094948
chore: update logo and asset links 2024-07-14 13:38:35 +08:00
Boshen
084ab7602d
fix(codegen): use ryu-js for f64 to string 2024-07-14 13:23:30 +08:00
Boshen
06197b8be4
refactor(codegen): separate tests 2024-07-14 12:56:02 +08:00
Boshen
e167ef79c6 fix(codegen): print parenthesis properly (#4245)
`TSParenthesizedType` handles parenthesis in ts types.

It should be considered a bug if parenthesis is not printed correctly after this PR.
2024-07-14 04:13:10 +00:00
Dunqing
3e099febf6 refactor(ast): move enter_scope after visit_binding_identifier (#4246)
This is now consistent with other ASTs that require bind

2019043e72/crates/oxc_ast/src/ast/ts.rs (L814-L815)
2024-07-14 04:06:02 +00:00
Dunqing
dc2b3c44fb refactor(semantic): add strict mode in scope flags for class definitions (#4156)
related: https://github.com/oxc-project/oxc/issues/4142#issuecomment-2219125356

Although we called `enter_node(Class)`, that doesn't mean we're in the `class` scope. It causes our must to visit decorators before `enter_node`.

Let's look at this case. It causes a syntax error if we don't visit decorators before `enter_node`
```js
// This file was procedurally generated from the following sources:
// - src/decorator/decorator-call-expr-identifier-reference-yield.case
// - src/decorator/syntax/valid/cls-expr-decorators-valid-syntax.template
/*---
description: Decorator @ DecoratorCallExpression (Valid syntax for decorator on class expression)
esid: prod-ClassExpression
features: [class, decorators]
flags: [generated, noStrict]
info: |
    ClassExpression[Yield, Await] :
      DecoratorList[?Yield, ?Await]opt class BindingIdentifier[?Yield, ?Await]opt ClassTail[?Yield, ?Await]

    DecoratorList[Yield, Await] :
      DecoratorList[?Yield, ?Await]opt Decorator[?Yield, ?Await]

    Decorator[Yield, Await] :
      @ DecoratorMemberExpression[?Yield, ?Await]
      @ DecoratorParenthesizedExpression[?Yield, ?Await]
      @ DecoratorCallExpression[?Yield, ?Await]

    ...

    DecoratorCallExpression[Yield, Await] :
      DecoratorMemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await]

    DecoratorMemberExpression[Yield, Await] :
      IdentifierReference[?Yield, ?Await]
      DecoratorMemberExpression[?Yield, ?Await] . IdentifierName
      DecoratorMemberExpression[?Yield, ?Await] . PrivateIdentifier

    IdentifierReference[Yield, Await] :
      [~Yield] yield
      ...

---*/
function decorator() {
  return () => {};
}
var yield = decorator;

var C = @yield() class {};

```
Errors:
```shell
  × The keyword 'yield' is reserved
    ╭─[language/statements/class/decorator/syntax/valid/decorator-call-expr-identifier-reference-yield.js:45:2]
 44 │
 45 │ @yield() class C {}
    ·  ─────
    ╰────
```

The changed code makes more sense. Only if we call `enter_scope` for class, the flags will contain `StrictMode`. Also, we can get the exact `flags` of the `scope` in the `class` at the transformer

For example:

```
class A {
   B() {
       // Before: flags is `Function`
      //  After: flags is `Function | StrictMode`
   }
}
```

The regression tests will be fixed in follow-up PRs
2024-07-14 03:35:12 +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
Boshen
ab41c52485
chore(minifier): add two || test cases to dce_if_statement 2024-07-13 11:41:43 +08:00