Commit Graph

798 Commits

Author SHA1 Message Date
overlookmotel 2b5d82687a fix(ast)!: fix field order for TSTypeAssertion (#10906)
Fix field order for `TSTypeAssertion`. Move `type_annotation` to before `expression`, because it comes first in source:

```ts
//                ___ expression
let foo = <number>bar;
//        ^^^^^^^^ type_annotation
```
2025-05-09 01:35:42 +00:00
overlookmotel 1f3591076f fix(ast)!: fix field order for TSNamedTupleMember (#10905)
Fix field order for `TSNamedTupleMember`. Move `label` to before `element_type`, because it comes first in source:

```ts
type Foo = [first: string, second: number];
//    label ^^^^^  ^^^^^^ element_type
```
2025-05-09 01:35:42 +00:00
overlookmotel 8a3bba8356 fix(ast)!: fix field order for PropertyDefinition (#10902)
Fix field order for `PropertyDefinition`. Move `type_annotation` to before `value`, because it comes first in source:

```ts
class C {
    x: number = 123;
}
```
2025-05-08 15:34:06 +00:00
overlookmotel 5746d36f04 fix(ast)!: fix field order for NewExpression (#10893)
Fix field order for `NewExpression`. Move `type_arguments` to before `arguments`, because they come first in source:

```tsx
const foo = new Foo<T>(1, 2, 3);
```
2025-05-08 15:34:05 +00:00
Boshen 3cf867cc93 feat(napi/parser): expose module record data for export default interface (#10894)
fixes #10644
2025-05-08 13:04:59 +00:00
Boshen 22ba60b6c4 feat(napi): add s390x-unknown-linux-gnu build (#10892) 2025-05-08 20:13:41 +08:00
overlookmotel 013979359e fix(ast)!: re-order fields of TaggedTemplateExpression (#10889)
Fix field order for `TaggedTemplateExpression`. Move `type_arguments` to before `quasi`, because they come first in source:

```ts
const foo = tag<T>`quasi`;
```
2025-05-08 11:59:35 +00:00
Boshen 308fe732d9 feat(napi): add x86_64-unknown-freebsd and riscv64gc-unknown-linux-gnu builds (#10886)
closes #10822
2025-05-08 18:26:22 +08:00
overlookmotel 6646b6b481 fix(ast)!: fix field order for JSXOpeningElement (#10882)
Fix field order for `JSXOpeningElement`. Move `type_arguments` to before `attributes`, because they come first in source:

```tsx
<Component<T> foo={123} />
```
2025-05-08 07:58:23 +00:00
overlookmotel cc2ed216f5 fix(ast)!: fix field order for JSXElement and JSXFragment (#10881)
Fix field order for `JSXElement` and `JSXFragment`. Move `children` field to before closing field, because `children` comes first in source.
2025-05-08 07:58:23 +00:00
overlookmotel 2c09243a27 fix(ast): fix field order for AccessorProperty (#10878)
In `AccessorProperty`, move `type_annotation` field to before `value`, because it appears first in AST.

```ts
class C {
  accessor x: number = 123;
}
```

This also aligns with TS-ESLint's visitation order.
2025-05-08 01:39:52 +00:00
overlookmotel e7228faf2b fix(ast/estree): fix optional field of TSMappedType in TS-ESTree AST (#10874)
Part of #9705.

Recent changes in TS-ESLint made `TSMappedType`'s `optional` field default to `false` instead of `null`. Update our TS-ESTree AST to match it.
2025-05-07 16:46:32 +00:00
overlookmotel 6f0638ae17 fix(ast/estree): remove TSImportTypeOptions custom serializer (#10873)
`TSImportTypeOptions` custom serializer was to work around a bug in TS-ESLint parser. That bug has now been fixed, so we don't need this workaround any more.

https://github.com/typescript-eslint/typescript-eslint/issues/11114
2025-05-07 17:37:24 +01:00
overlookmotel e6657ae13a fix(ast/estree): reorder fields for TS Identifier types in TS-ESTree AST (#10864)
Change field order of TS types which are converted to `Identifier`s in TS-ESTree AST, to be consistent with other `Identifier`s.
2025-05-07 14:14:33 +00:00
overlookmotel d066516b6a feat(ast_tools): support #[estree(prepend_to)] (#10849)
Add an `#[estree(prepend_to = ...)]` attribute to accompany `#[estree(append_to = ...)]`.

* `#[estree(prepend_to = target)]` prepends the field to *start* of the target field.
* `#[estree(append_to = target)]` appends the field to *end* of the target field.

This also allow both prepending *and* appending to the same field. e.g. combining `TSThisParameter`, `FormalParameter`s, and `BindingRestElement` into a single array.
2025-05-07 13:25:17 +00:00
overlookmotel b16331ecfa refactor(ast/estree): generalize concatenating fields with Concat2 (#10848)
Pure refactor. Replace `AppendTo` and `AppendToConcat` which are used in ESTree serializer to concatenate 2 fields with a more generalized `Concat2`, which performs both functions.
2025-05-07 13:25:17 +00:00
overlookmotel daba0a7c2a refactor(estree): remove regular expression types from ESTree AST (#10855)
`Pattern` and other types in `oxc_regular_expression` are not part of ESTree AST. Don't implement `ESTree`, and don't generate raw transfer deserializers for these types. None of that code is actually used.

This reduces the scope of ESTree serialization.
2025-05-07 13:46:32 +01:00
overlookmotel ad4fbf44db refactor(ast)!: simplify RegExpPattern (#10834)
`RegExpPattern` was an enum with `Raw`, `Invalid`, and `Pattern` variants. i.e. only the raw string *or* the `Pattern` could be stored, but not both.

This required complications in various places, because if the regexp has been parsed, in order to examine or print it, you have to either convert the `Pattern` back to a `String`, or rely on the `Span` and slice the source text.

The `Invalid` variant is not very useful. It's only created if `parse_regular_expression` option is enabled, in which case an error is reported from the parser anyway.

Instead, always store the pattern string as an `Atom`, and store the parsed pattern as `Option<Box<Pattern>>`.

This simplifies code in various places, and makes printing `RegExpLiteral`s faster in `oxc_codegen` when `parse_regular_expression` is enabled, because it can just print the pattern string, rather than converting a `Pattern` back to a `String`.
2025-05-07 10:05:37 +00:00
overlookmotel 49a6f97388 perf(napi/parser): faster fixup of BigInts and RegExps (#10820)
#10791 fixed the performance of `oxc-parser` NPM package by removing the reviver from `JSON.parse` call. However, it replaced it with a complete traversal of the AST on JS side to locate `Literal` nodes and update them.

Instead, generate a list of paths to `Literal` nodes needing fixing on Rust side in `ESTree` serializer.

e.g. for this program:

```js
123n;
foo(/xyz/);
```

the fix paths are:

```json
[
    ["body", 0, "expression"],
    ["body", 1, "expression", "arguments", 2]
]
```

Having the location of nodes which need updating reduces work on JS side, as no need to search the entire AST.

Running `pnpm run bench` (in `napi/parser`) locally shows between 8% and 15% speed-up from this change.
2025-05-06 02:34:24 +00:00
overlookmotel 87fc90365a fix(napi/parser): expose visitor keys files in NPM package (#10817)
#10791 added 2 files `generated/visitor-keys.cjs` and `visitor-keys.mjs` to the NAPI package. When I amended the original PR, I forgot to update `package.json` to include them both in NPM package.
2025-05-06 02:34:24 +00:00
overlookmotel 09057676fd perf(napi/parser): simplify recursion and avoid function calls in fixup visitor (#10813)
Follow-on after #10791, part of #10783.

In the fixup visitor, replace `visitNode` with a more specialized
`transformNode` function. The difference is that `transformNode` doesn't
need to be passed `fn` as 2nd param.

Also, move the check for whether a node is a `Literal` into
`transformNode` to avoid the cost of a function call for every node
(most of which are not `Literal`s).

Running benchmarks locally (`pnpm run bench` in `napi/parser`), I'm
seeing a 2%-3% speed-up.
2025-05-05 22:38:47 +08:00
Arnaud Barré f85bda4fda perf(parser): use visitor instead of JSON.parse reviver (#10791)
Fixes #10783

This requires having a list of ~135 nodes with their non primitive keys.
Technically this list could be generated alongside the raw deserializer
but I don't know this code enough to do it.
I've chosen to inline visitor keys to avoid adding [3
dependencies](https://pkg-size.dev/@typescript-eslint%2Fvisitor-keys)
for that.
I didn't find how the main LICENCE was injected during the publish so I
couldn't append the bundled LICENSES and choose to use `@licence`
comments with links

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-05-05 13:11:43 +01:00
overlookmotel 62c3a4a576 style(ast_tools): add full stop to end of generated comments (#10809) 2025-05-05 12:52:20 +01:00
oxc-bot e6074275ae release(crates): v0.68.1 (#10796) 2025-05-05 00:53:44 +08:00
overlookmotel c33eb9c937 fix(ast/estree): fix raw deser for TSTypeReference (#10787)
Fix #10782.
2025-05-04 07:38:17 +00:00
oxc-bot 61dfcfdfd8 release(crates): v0.68.0 (#10777) 2025-05-04 09:06:24 +08:00
overlookmotel 28ceb90228 refactor(ast)!: remove TSMappedTypeModifierOperator::None variant (#10749)
It seems odd for `TSMappedTypeModifierOperator` to have a `None` variant. It's more conventional to represent "doesn't exist" as `Option::None`.

Remove the `TSMappedTypeModifierOperator::None` variant, and use `Option<TSMappedTypeModifierOperator>` instead of `TSMappedTypeModifierOperator`.

Both are the same to the compiler due to the niche optimization on `Option` (`Option<TSMappedTypeModifierOperator>` is 1 byte), but it just seems more logical to me this way.

Also an unrelated change: Shorten code in codegen and formatter.
2025-05-01 14:50:17 +00:00
overlookmotel 050ecd90fc refactor(ast/estree): remove custom serializer for TSMappedTypeModifierOperator (#10747)
Use the support for `#[estree(via = ...)]` on fieldless enum variants added in #10746 to remove custom serializer for `TSMappedTypeModifierOperator`. This serializer can now be auto-generated.

Does not effect output, only simplifies how it's generated.
2025-05-01 11:14:23 +00:00
overlookmotel a2ba7c35f9 refactor(napi/parser): add comments about hashbang comments (#10692)
Add comments about hashbang comments which I should have included in #10669.
2025-04-29 11:13:47 +00:00
overlookmotel d882eaa876 perf(napi/parser): lazy load raw transfer deserializers (#10482)
Don't eagerly load raw transfer deserializer code. These files are large and will take JS engine some time to parse. Instead, `require` the appropriate deserializer lazily, when it's first needed.

For the majority of users who aren't using raw transfer (almost everyone, as it's currently behind an experimental option), avoid loading this code entirely.
2025-04-29 10:41:41 +01:00
overlookmotel 3b6d52d009 refactor(napi/parser): move generated deserializer files (#10481)
Pure refactor. Move generated deserializer files for `napi/parser` into a `generated/deserialize` directory. This is to make room for more generated files (e.g. visitor, more specialized deserializers), without the number of file in root of the package exploding.
2025-04-29 10:30:27 +01:00
overlookmotel 61d825b8bf fix(ast/estree): rename assert to with in TSImportType options in TS-ESTree AST (#10681)
TS-ESLint parser replaces a property of `options` called `assert` with one called `with`.

https://github.com/typescript-eslint/typescript-eslint/issues/11114

This looks like it's an error on their side, but in the meantime replicate this behavior.
2025-04-28 18:48:52 +00:00
overlookmotel c8005adae1 fix(ast/estree): add line comment for hashbang in ESTree AST (#10669)
In ESTree (plain ESTree for JS i.e. Acorn), a hashbang is recorded as a line comment. Follow that behavior in our ESTree AST.

In TS-ESTree, hashbangs are ignored. That's not how it looks in AST explorer, because there's some post-processing going on, but `@typescript-eslint/parser` itself ignores hashbangs. So only add the comment in ESTree AST (not TS-ESTree AST).

More info on this rather confusing subtlety: https://github.com/typescript-eslint/typescript-eslint/issues/6500
2025-04-28 14:33:49 +00:00
overlookmotel 14c4bbb4e7 test(ast/estree): fix raw transfer tests (#10666)
The tests for ensuring field order is the same between JSON-based transfer and raw transfer were not working correctly. Fix them. Thankfully they all still pass!
2025-04-28 14:09:03 +00:00
oxc-bot 7cf80ef7fd release(crates): v0.67.0 (#10637) 2025-04-27 22:30:07 +08:00
Ulrich Stark a9785e347e fix(parser,linter): consider typescript declarations for named exports (#10532)
Only VariableDeclaration, FunctionDeclaration and ClassDeclaration were
considered when collecting named export declarations for the module
record. Now the parser also considers TSTypeAliasDeclaration,
TSInterfaceDeclaration, TSEnumDeclaration, TSImportEqualsDeclaration and
TSModuleDeclaration named export declarations of the module.

Maybe the other `decl.declaration.is_typescript_syntax()` check in
`module_record.rs` also needs to be removed?

Closes #10318
Closes #10556
2025-04-25 14:44:12 +08:00
overlookmotel 24ab2f36d2 fix(ast/estree): convert TSClassImplements::expression to MemberExpression in TS-ESTree AST (#10607)
Part of #9705.

TS-ESLint parser represents `X.Y` in `class C implements X.Y {}` as a `MemberExpression`, whereas in our AST it's a `TSQualifiedName`. Convert to `MemberExpression` in TS-ESTree AST.
2025-04-24 19:31:17 +00:00
overlookmotel 0825834b3d fix(ast/estree): correct this in TSTypeName in TS-ESTree AST (#10603)
Part of #9705.

TS-ESTree presents a `TSTypeName` which is an identifier `this` as a `ThisExpression`.

e.g. `type T = typeof this;` or `type T = typeof this.foo;`
2025-04-24 17:48:27 +00:00
overlookmotel 936f885af6 refactor(napi/parser): refactor wrap files (#10480)
Refactor `wrap.cjs` and `wrap.mjs` in `napi/parser` package.

* Update outdated comments.
* Move JSON transform function to top level to avoid creating a new closure each time `jsonParseAst` is called.
* Make the 2 files as similar as possible so it's easier to keep them both in sync.
2025-04-24 03:21:48 +00:00
overlookmotel d1f5abb655 fix(ast/estree): fix TS-ESTree AST for TSModuleDeclaration (#10574)
Part of #9705.

Our AST represents `module X.Y.Z {}` as 3 x nested `TSModuleDeclaration`s. TS-ESTree represents it as a single `TSModuleDeclaration`, with a nested tree of `TSQualifiedName`s as `id`.

Additionally, TS-ESTree skips the `body` field if the module declaration has no body e.g. `module "foo";`.

Align our TS-ESTree AST with these oddities.
2025-04-23 20:30:07 +00:00
overlookmotel 66e384cfd3 fix(ast/estree): add missing fields to ObjectPattern in TS-ESTree AST (#10570)
Part of #9705.
2025-04-23 15:11:25 +00:00
oxc-bot ff67f13f99 release(crates): v0.66.0 (#10565)
## [0.66.0] - 2025-04-23

### Features

- 7d5ad7d parser: Report error when `import type { type }` is used
(#10528) (camc314)
- 6e40fac parser: Report error when `export type { type }` is used
(#10524) (camc314)

### Bug Fixes

- 43ad4e9 ast: Box `this_param` in `TSCallSignatureDeclaration` (#10558)
(Yuji Sugiura)
- 8eb3c0a ast/estree: Fix raw deser for `FormalParameter` (#10548)
(overlookmotel)
- 3ebf220 codegen: Generate missing `type` in `export type {} from
'mod'` (#10539) (Boshen)
- f19b287 estree: Add `TSParameterProperty` (#10534) (Yuji Sugiura)
- 7284f16 isolated-declarations: Leading comments of
`ExportDefaultDeclaration` and `TSExportAssignment` appear in incorrect
places (#10559) (Dunqing)
- 4c316a5 isolated_declarations: Fix broken snapshot files (#10561)
(Boshen)
- 84742d8 transformer/module_runner: Fix export default live binding
(#10560) (hi-ogawa)
- 6c115c7 transformer/module_runner: Fix execution order of re-export
(#10362) (hi-ogawa)
- b9d84b0 transformer/typescript: Remove `StringLiteral::raw` when
rewriting extensions (#10554) (overlookmotel)

### Performance

- 6a045c8 codegen: Speed up printing `Directive`s (#10551)
(overlookmotel)
- 3418ae5 transformer/class-properties: Re-use
`InstanceInitializerVisitor` (#10543) (overlookmotel)
- 7fcf0ac transformer/typescript: Reduce allocations renaming extensions
(#10555) (overlookmotel)

### Documentation

- 1a9530c transformer/class-properties: Fix typo (#10542)
(overlookmotel)

### Refactor

- 7e71282 transformer/class-properties: Correct outdated comment
(#10546) (overlookmotel)
- 596978b transformer/class-properties: Simplify to get `constructor`
method only once (#10493) (Dunqing)
- f13fe0a transformer/class-properties: Defer re-parenting initializers
scopes until all instance properties are transformed (#10495) (Dunqing)
- c83dad6 transformer/class-properties: Streamline handling scope of
instance property initializer (#10492) (Dunqing)

### Styling

- e10dfc8 traverse: Remove excess whitespace (#10544) (overlookmotel)

### Testing

- 227febf allocator: Ignore a slow doc test (#10521) (Dunqing)
- 14bb2be codegen: Add more tests for strings (#10552) (overlookmotel)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-04-23 22:42:12 +08:00
Yuji Sugiura 198827c71e fix(estree): Add missing fields for RestElement raw deser (#10557)
I happened to find this when checking #10548 👀 

(It might not have been necessary to fix it right away, but since I
found it 😅)
2025-04-23 13:51:22 +01:00
Yuji Sugiura 43ad4e9ca5 fix(ast): Box this_param in TSCallSignatureDeclaration (#10558)
All other types of `this_param` were alreadey `Box`ed, but only here it
was not.

Because of this, `raw_deser` was behaving wrongly.


https://github.com/oxc-project/oxc/blob/2eb1f88a7d8f382a9a0d87f177e1a043eaf1e32b/crates/oxc_ast/src/serialize.rs#L1197
2025-04-23 10:52:53 +01:00
overlookmotel 8eb3c0a720 fix(ast/estree): fix raw deser for FormalParameter (#10548)
Follow-on after #10534. Fix the raw deser implementation for
`FormalParameter` to handle `TSParameterProperty`.

This involves moving some of the logic into a converter for
`FormalParameter`, instead of doing it all in `FormalParameters`
converter.
2025-04-23 12:12:27 +09:00
Yuji Sugiura f19b2878b0 fix(estree): Add TSParameterProperty (#10534)
Part of #9705, fixes #10070 

TODO:

- [x] Base rust serializer
- [x] Check anything missing
- [x] TS type defs
- [ ] ~~Raw deser~~

---------

Co-authored-by: Ulrich Stark <8657779+ulrichstark@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-22 21:42:14 +09:00
renovate[bot] 1d2154e0c9 chore(deps): lock file maintenance npm packages (#10530) 2025-04-22 12:08:36 +08:00
oxc-bot 0ab2cb4a22 release(crates): v0.65.0 (#10522) 2025-04-21 18:03:15 +08:00
Ulrich Stark 4f1343b235 fix(parser): fix missing type export in module information (#10516)
Closes #10505

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2025-04-21 17:18:50 +08:00
overlookmotel 99d82db2a3 refactor(ast)!: move type_parameters field to before extends in TSInterfaceDeclaration (#10476)
```ts
interface Foo<T> extends Bar { }
```

Move `type_parameters` field (`<T>`) to before `extends` field (`Bar`), because it comes first.
2025-04-18 09:47:08 +00:00