Commit graph

111 commits

Author SHA1 Message Date
Boshen
86beca5379
Release crates v0.13.1 2024-05-22 16:50:30 +08:00
Boshen
e2dd8ac8fc feat(syntax): export is_reserved_keyword and is_global_object method (#3384) 2024-05-22 04:58:04 +00:00
underfin
e879685a25
refactor(sourcemap): using binary search to search original position (#3360)
The ast span is not ordering at rolldown, eg the module original ast is
`a,b,c`, after mutate could be `b,c,a`. So here revert changes from
[here](https://github.com/oxc-project/oxc/pull/2728).
2024-05-20 22:35:25 +08:00
Boshen
9ced605487
refactor(parser): start porting arrow function parsing from tsc (#3340)
relates #3320
2024-05-18 22:35:29 +08:00
Don Isaac
a12ed0fd26
fix(codegen): using declaration in for statement (#3285)
Fixes conformance tests for `using` declarations in for statements, e.g.
```ts
for (using x = 1;;) {}
```
2024-05-14 23:10:47 -04:00
Boshen
c395f8641e
Release crates v0.13.0 2024-05-14 23:14:15 +08:00
Dunqing
eefb66f750 feat(ast): add type to AccessorProperty to support TSAbractAccessorProperty (#3256) 2024-05-13 12:35:58 +00:00
Dunqing
0ba7778e5e
fix(parser): correctly parse cls.fn<C> = x (#3208)
close: #3206
2024-05-09 10:23:45 +08:00
Boshen
a8af5de8f5
refactor(syntax): move number related functions to number module (#3130) 2024-04-29 18:54:35 +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
overlookmotel
0185eb2edc
refactor(ast): remove duplicate TSNamedTupleMember representation (#3101)
Removes duplicate representation of a `TSTupleElement` which is a
`TSNamedTupleMember`.

Closes #3100.
2024-04-25 19:16:24 +08:00
Boshen
559bca86c5
Release crates v0.12.5 2024-04-22 12:52:17 +08:00
Boshen
92d709bf21
feat(ast): add CatchParameter node (#3049) 2024-04-21 23:43:39 +08:00
Boshen
a05c4e39b8
Release crates v0.12.4 2024-04-19 16:40:05 +08:00
Dunqing
fd5002bc51
feat(codegen): correctly print type-only imports/exports (#2993) 2024-04-15 18:33:02 +08:00
branchseer
f159f60084
Make ast types covariant over the allocator lifetime. (#2943)
## Why

Due to the usage of `&'alloc mut T` in `oxc_allocator::Box`, and
`bumpalo::collections::Vec` in `oxc_allocator::Vec`, ast types are
currently invariant over their allocator lifetime `'a`. This prevents
`ouroboros` from generating `borrow_*` on ast type fields, leading to
the unfriendly `with_*` api:
c250b288ef/crates/oxc_parser/examples/multi-thread.rs (L82-L84)

## How

- For `oxc_allocator::Vec`, switch to `allocator_api2::vec::Vec`, which
has a covariant relationship with the allocator lifetime.
- For `oxc_allocator::Box`, use `std::ptr::NonNull` which is
specifically designed to be covariant. I don't use
`allocator_api2::boxed::Box` because it holds the allocator for
dropping, so the size is bigger.

## Downside

Now that `oxc_allocator::Box` uses the unsafe `NonNull`. It has to be a
private field to be safe. This make it impossible to do `Box(....)`
pattern matching.
2024-04-12 18:12:18 +08:00
Boshen
614f73b66c
Release crates v0.12.3 2024-04-11 16:18:17 +08:00
Boshen
09452659e2
Release crates v0.12.2 2024-04-08 11:13:13 +08:00
Boshen
fb2ebf462e
chore: fix clippy on unsafe comment 2024-04-03 19:57:21 +08:00
Boshen
366a7fb0d4
Release crates v0.11.2 2024-04-03 19:36:54 +08:00
Boshen
504698ab4a
chore: guard against unsafe code as much as possible. 2024-04-03 19:35:07 +08:00
underfin
ef4b898be8
chore: avoid unsafe code search original name (#2895)
close https://github.com/rolldown/rolldown/issues/716

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-04-03 11:34:21 +00:00
Boshen
54f7cd3978
Release crates v0.11.1 2024-04-03 16:57:52 +08:00
underfin
114f68ea7c
refactor(codegen): make codegen sourcemap builder clearer (#2894)
Avoid `enable_sourcemap` appear multiply times.
2024-04-03 15:07:19 +08:00
underfin
28fae2e80a
fix(sourcemap): using serde_json::to_string to quote sourcemap string (#2889) 2024-04-03 12:14:45 +08:00
Boshen
23d3c4e0a4
chore: add changelogs via git cliff (#2878)
This is generated alongside https://github.com/oxc-project/release-oxc
2024-04-01 20:04:48 +08:00
Boshen
31ed532b79
Release crates v0.11.0 2024-03-30 13:54:53 +08:00
underfin
6177c2f7ef
fix(codegen): sourcemap token name should be original name (#2843)
close https://github.com/oxc-project/oxc/issues/2753.
2024-03-28 20:14:10 +08:00
underfin
b199cb89a2
feat: add oxc sourcemap crate (#2825)
The sourcemap implement port from
[rust-sourcemap](https://github.com/getsentry/rust-sourcemap), but has
some different with it.

- Encode sourcemap at parallel, including quote `sourceContent` and
encode token to `vlq` mappings.
- Avoid `Sourcemap` some methods overhead, like `SourceMap::tokens()`
caused extra overhead at common cases. Here using `SourceViewToken` to
instead of it.
2024-03-28 19:36:38 +08:00
Ali Rezvani
b76b02d019
fix(parser): add support for empty module declaration (#2834)
Should be merged after #2829, Tried a few times to get it done with
graphite stacking but found no success. I guess it either doesn't work
with forks or It is just a skill issue since I'm not familiar with it.

closes: #2829
closes: #2830

---------

Co-authored-by: Dmytro Maretskyi <maretskii@gmail.com>
2024-03-27 13:48:03 +08:00
Boshen
95fc28168c
chore: apply cargo autoinherit (#2826)
See https://github.com/mainmatter/cargo-autoinherit
2024-03-26 23:57:50 +08:00
Ali Rezvani
243131d7a4
feat(transformer): numeric separator plugin. (#2795)
[es2021 numeric
separator](https://babeljs.io/docs/babel-plugin-transform-numeric-separator)
2024-03-26 18:15:12 +08:00
underfin
d9b77d853b
refactor(sourcemap): change sourcemap name to take a reference (#2779) 2024-03-23 21:40:05 +08:00
underfin
dc3f6e7570
chore: update sourcemap 8.0.0 (#2788) 2024-03-22 21:52:21 +08:00
underfin
a2cfc867cb
feat: SourcemapVisualizer (#2773)
Export `SourcemapVisualizer` from codegen, it will be used oxc and
rolldown sourcemap test, so it support multiply source print, it will
using sourcemap `sourcesContent` as original source.
2024-03-21 11:19:09 +08:00
underfin
d7004da253
perf(sourcemap): remove unnecessary binary search (#2728)
Becuase `position` argument is ordering, using binary search is
unnecessary.
2024-03-15 18:50:53 +08:00
underfin
2be5f9d6c9
perf(codegen): avoid unnecessary copy (#2727) 2024-03-15 16:09:24 +08:00
Boshen
a5ddb5b452
Release crates v0.10.0 2024-03-14 18:23:34 +08:00
underfin
9609c34e6d
fix(codegen): CallExpression sourcemap (#2717) 2024-03-14 07:53:22 +00:00
underfin
2dfc0cc28e
chore: upgrade sourcemap 7.1.1 (#2701) 2024-03-13 13:52:36 +08:00
Arnaud Barré
c3477de64e
fix(ast)!: rename BigintLiteral to BigIntLiteral (#2659)
This matches the case for the name in Babel. (in ESTree everything is a
`Literal`)
2024-03-10 13:31:51 +08:00
Arnaud Barré
b453a072cc
fix(parser): parse named rest element in type tuple (#2655)
This is fixing the parser for `type X = [...args: string[]];`

In TSESLint TSNamedTupleMember in part of the TSType union, so I did the
same.
2024-03-10 13:25:15 +08:00
Boshen
c72675e89e
chore: Rust v1.76.0 (#2643) 2024-03-08 20:54:36 +08:00
Boshen
cca6eb073c
Release crates v0.9.0 2024-03-05 15:57:31 +08:00
Boshen
ef932a3c27
refactor(codegen): clean up API around building sourcemaps (#2602)
closes #2564
2024-03-04 16:03:33 +08:00
Boshen
20c7bf7835
feat(ast): add AssignmentTargetRest (#2601)
closes #2598
2024-03-04 14:31:22 +08:00
overlookmotel
b7f5c63833
perf(codegen): speed up generating sourcemap mappings (#2597)
This PR optimizes the `update_generated_line_and_column` function used in generating source maps.

The main change is that `generated_column` only depends on the last line, so just spin through bytes to find the last line break, and then only convert last line UTF-8 to UTF-16. There's also a fast path for when last line is ASCII, to avoid iterating over the last line twice in that common case.
2024-03-04 12:46:32 +08:00
overlookmotel
42fa8ebbc2
perf(codegen): speed up building sourcemap line tables (#2591)
Speed up building the line tables for source maps, using same kind of techniques as have been using in the lexer:

* Iterate byte-by-byte not char-by-char (`chars` iterator is slow).
* Fast path for ASCII (common case).
2024-03-04 12:40:39 +08:00
overlookmotel
ea30fd5b12
fix(codegen): fix adding mapping to sourcemaps (#2590)
Fix creating a sourcemap mapping when last byte of output in `\r`. Currently this panics because it assumes there's another byte after it when checking for `\n`, and reads out of bounds.
2024-03-04 12:33:28 +08:00
Arnaud Barré
f66059e91b
fix(ast)!: align TSImportType with ESTree (#2578)
Implements
https://github.com/typescript-eslint/typescript-eslint/issues/2998

The copy of props feels wrong, but could not get it working otherwise
with the box and borrow things 😅

Also I found that TSImportType was missing some entries for visitors and
codegen.

In the case of codegen I'm not really understand the need as all the
types seems to be dismissed?
2024-03-04 12:28:18 +08:00