Commit Graph

811 Commits

Author SHA1 Message Date
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
overlookmotel 721280353a refactor(ast)!: change TSInterfaceDeclaration::extends from Option<Vec> to Vec (#10472)
It wasn't possible for `extends` field to be `Some` containing an empty `Vec` because this is not legal code:

```ts
interface Foo extends {}
```

So use `Vec` for `extends` field, instead of `Option<Vec>`. This prevents creating an invalid AST, and simplifies code in various places.
2025-04-18 09:38:25 +00:00
Yuji Sugiura fbf0ae2265 fix(estree): Add missing fixed fields to AssignmentPattern (#10490)
Part of #9705
2025-04-18 10:37:40 +01:00
Yuji Sugiura a42d85f031 fix(estree): FormalParameters serializer for TS types (#10462)
Fixes #10068

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-18 10:05:06 +09:00
overlookmotel 109cb2187d docs(napi/parser): remove raw transfer from example (#10486)
Raw transfer is an experimental API and shouldn't be in the example for `napi/parser`, which represents recommended usage.
2025-04-17 22:08:45 +00:00
overlookmotel 1952e30fd8 fix(ast/estree): serialize class constructor key as Identifier in TS-ESTree AST (#10471)
Part of #9705. Align with TS-ESTree for `class C { "constructor"() {} }`.

I think this is a bug in TS-ESLint, but for the moment, we follow their behavior.
https://github.com/typescript-eslint/typescript-eslint/issues/11084
2025-04-17 12:26:53 +00:00
oxc-bot 619488a05a release(crates): v0.64.0 (#10459) 2025-04-17 10:09:07 +08:00
overlookmotel f3ddefbf6c fix(ast/estree): add missing fields to AssignmentTargetRest in TS-ESTree AST (#10456)
Part of #9705.
2025-04-16 19:35:15 +00:00
overlookmotel 77b6f7e92e fix(ast/estree): fix start span of Program in TS-ESTree AST where first statement is @dec export class C {} (#10448)
#10438 aligned the spans of `@dec export class C {}` and `@dec export default class {}` with TS-ESLint.

```ts
@dec export class C {}
     ^^^^^^^^^^^^^^^^^ ExportNamedDeclaration
            ^^^^^^^^^^ Class
^^^^                   Decorator

@dec export default class {}
     ^^^^^^^^^^^^^^^^^^^^^^^ ExportDefaultDeclaration
                    ^^^^^^^^ Class
^^^^                         Decorator
```

However, this causes a problem where one of these is the first statement in the file. In TS-ESTree, `Program` start is the start of the first token (excluding whitespace and comments). So we need to set `Program` start to the start of the decorator in these cases.
2025-04-16 15:47:42 +00:00
Boshen c538efa1e2 fix(ast)!: ImportExpression only allows one option argument (#10432)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-04-16 13:13:28 +08:00
Boshen 7284135135 refactor(ast)!: remove trailing_commas from ArrayExpression and ObjectExpression (#10431)
closes #10419

This change reduces the size of these AST nodes, saving a bit of memory.

`ArrayExpression`: 56 -> 40
`ObjectExpression`: 56 -> 40
`ArrayAssignmentTarget`: 80 -> 64

Also removes the annoying `None` argument from these AST node builders.
2025-04-16 04:04:12 +00:00
Boshen 771d50f2c6 fix(ast)!: change Class::implements to Vec<TSClassImplements> (#10430)
fixes #10421
2025-04-16 02:58:11 +00:00
overlookmotel 4817c7eacf fix(ast/estree): add fields to AssignmentTargetPattern in TS-ESTree AST (#10423)
Add missing fields to `ArrayAssignmentTarget` and `ObjectAssignmentTarget` in TS-ESTree AST.
2025-04-15 15:35:00 +00:00
Yuji Sugiura 4c246fb834 feat(ast): Add override field in AccessorProperty (#10415)
Fixes #10414
2025-04-15 16:18:54 +09:00
overlookmotel b3094b3858 fix(ast/estree): add optional field to AssignmentTargetProperty in TS-ESTree AST (#10412)
Add `optional` field to `AssignmentTargetPropertyIdentifier` and `AssignmentTargetPropertyProperty` in TS-ESTree AST.

This doesn't pass any more tests because those test cases all also include mismatches on `ArrayPattern` or `ObjectPattern`, but it does reduce the size of mismatch diffs.
2025-04-14 23:42:08 +00:00
overlookmotel a7fd30f9f4 fix(ast/estree): add fields to BindingRestElement in TS-ESTree AST (#10411) 2025-04-14 23:26:24 +00:00
Yuji Sugiura 2520b25e0b fix(estree): Align TSMappedType fields (#10392)
Part of #9705 

In this PR, I've tried to align `TSMappedType` with TS-ESLint.

>
https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/type/TSMappedType/spec.ts

```js
// BEFORE
{
  type: "TSMappedType",
  typeParameter: {
    name: Identifier,
    constraint: TypeNode,
  },
  // ...
}

// AFTER
{
  type: "TSMappedType",  
  key: Identifier,
  constraint: TypeNode,
  // ...
}
```

And also fixed serializer for `optional` and `readonly` types.

They are defined as `boolean | '+' | '-' | undefined`, but we use `true
| '+' | '-' | null`.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-14 22:31:53 +01:00
Yuji Sugiura 3ed3669b4a fix(estree): Rename JSDocXxxType to TSJSDocXxxType (#10358)
Part of #9705 

Just renamed them, no difference appeared in the coverage ATM.

From my understanding, there is a problem with the parser logic for now.
I'll try to fix it later.
2025-04-11 12:15:09 +01:00
Yuji Sugiura 6e6c777e6b refactor(ast): Add TSEnumMemberName variant to replace computed field (#10346)
Fixes #10332

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-11 11:56:01 +01:00
Yuji Sugiura 521de23190 feat(ast)!: Add computed property to TSEnumMember and TSEnumMemberName::TemplateString (#10092)
Fixes #10088

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-09 12:51:34 +01:00
Yuji Sugiura 49732ffb45 feat(ast)!: Re-introduce TSEnumBody AST node (#10284)
Fixes #10087 

---

Follow up for #10017 .

This PR eliminates the part where nodes were manually generated for
JS-land AST.

NOTE: This node was originally present, but was removed at #2509 and is
now being reintroduced to get the correct `Span` position for #9705 .

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-04-09 12:06:20 +01:00
Yuji Sugiura b54fb3e115 fix(estree): Rename TSInstantiationExpression.type_parameters to type_arguments (#10327)
Part of #9705
2025-04-09 16:13:24 +08:00
therewillbecode cc07efd29f fix(ast/estree): Fix JSXOpeningFragment (#10208)
Fix `JSXOpeningFragment` node estree TS serialization by skipping
`selfClosing` and `attributes` fields when AST is TS.

Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-08 16:55:36 +01:00
overlookmotel 48ed6a1644 fix(ast/estree): fix span for TemplateElement in TS AST (#10315)
Part of #9705.

TS-ESLint differs from Acorn in the `span` of `TemplateElement`.

TS-ESLint includes the preceding `` ` `` or `}` and following `${` or `` ` `` in the span.

```js
const template = `abc${x}def${x}ghi`;
// Acorn:         ^^^    ^^^    ^^^
// TS-ESLint:    ^^^^^^ ^^^^^^ ^^^^^
```

Make the span follow TS-ESLint in the TS AST.
2025-04-08 13:07:27 +00:00
oxc-bot eba5fcf33b release(crates): v0.63.0 (#10309) 2025-04-08 13:52:15 +08:00
overlookmotel e42c040c85 fix(ast/estree): add TS fields to LabelIdentifier (#10295)
Add missing fields to `LabelIdentifier` in TS AST. Part of #9705.
2025-04-07 14:34:54 +00:00
overlookmotel a26fd3430a refactor(ast)!: remove JSXOpeningElement::self_closing field (#10275)
Remove `self_closing` field from `JSXOpeningElement`. Whether the tag is self-closing can be determined by whether the `JSXElement` has a `closing_element` or not.

This is advantageous for 2 reasons:

1. Reduce size of `JSXOpeningElement` by 8 bytes.
2. Make it impossible for `self_closing` and `closing_element` fields to get out of sync. Only one source of truth.
2025-04-07 14:27:20 +00:00
renovate[bot] e34967eb20 chore(deps): lock file maintenance npm packages (#10273) 2025-04-07 16:43:16 +08:00
overlookmotel bdded7e80d test(ast/estree): add tests for JSX via raw transfer (#10241)
Add tests for JSX syntax parsing via raw transfer.
2025-04-04 17:13:50 +00:00
Dunqing 52f2a40990 fix(span/estree): skip ModuleKind::Unambiguous varient for estree (#10146)
close: #10139

The `ModuleKind::Unambiguous` can only occur in the parser stage, after being parsed, the `ModuleKind` certainly changes to `ModuleKind::Script` or `ModuleKind::Module`; otherwise is a bug in the parser.
2025-04-03 16:47:07 +00:00
therewillbecode 06fc07ceb5 fix(ast/estree): Fix TSImportType (#10200)
Skips the `isTypeOf` field for the Estree serialiser on the
`TSImportType` node.

Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705
2025-04-03 17:16:40 +01:00
therewillbecode 760188e680 fix(ast/estree): Fix BindingProperty (#10193)
Adds the `optional` field to the Estree serialiser for the
`BindingProperty` node.

Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-03 12:08:31 +01:00
overlookmotel b662df4eaf refactor(ast/estree): alter Program start span with converter (#10195)
#10134 fixed the start span of `Program` in TS AST to align with TS-ESLint.

Change the method this is achieved by - use a custom converter instead of mutating the AST in `to_estree_ts_json` and `to_pretty_estree_ts_json`. This method is more convoluted, but I think it's preferable if serialization does not have side effect of mutating the AST.
2025-04-03 09:36:14 +00:00
Yuji Sugiura f547d769f3 fix(ast/estree): Add TSEnumBody to TSEnumDeclaration.body (#10017)
Part of #9705 

This PR fixes AST structure of `TSEnumDeclaration` from:

```js
{
  "type": "TSEnumDeclaration",
  "id": { /* ... */ },
  "members": [
    {
      "type": "TSEnumMember",
      "id": { /* ... */ },
      "initializer": null
    }
  ],
  "const": false,
  "declare": false
}
```

to:

```js
{
  "type": "TSEnumDeclaration",
  "id": { /* ... */ },
  "body": {
    "type": "TSEnumBody",
    "members": [
	    {
	      "type": "TSEnumMember",
          "id": { /* ... */ },
	      "initializer": null
	    }
	  ],
  },
  "const": false,
  "declare": false
}
```

>
https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts
>
https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/special/TSEnumBody/spec.ts

Historically, we had this node in the past, but it seems to have been
deleted in #2509 .

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-04-03 08:42:05 +01:00
overlookmotel 27768a5f19 fix(parser): store lone surrogates in TemplateElementValue as escape sequence (#10182)
Encode lone surrogates in `cooked` property of `TemplateElementValue` using same encoding scheme as for `StringLiteral`s.

In fact, they were already being encoded like this after #10041, but add a `lone_surrogates` flag to `TemplateLiteral` to decode them correctly in ESTree AST.

`oxc_codegen` ignores `cooked` and just prints `raw`, so needs no alteration.
2025-04-02 12:57:52 +00:00
overlookmotel 38d2beaf60 fix(parser): fix parsing lone surrogates in StringLiterals (#10180)
Fix 2 edge cases when parsing lone surrogates in `StringLiteral`s:

* Lone surrogate followed by `\u{...}` escape e.g. `"\uD800\u{41}"`.
* Escaped lossy replacement character after lone surrogate e.g. `"\uD800 \u{FFFD}"`.
2025-04-02 12:57:52 +00:00
therewillbecode 34d5c0094e fix(ast/estree): Fix ExportDefaultDeclaration node (#10165)
Create new `TsValue` struct and use in new `exportKind` field on the
`ExportDefaultDeclaration` node.

Align our AST's ESTree output with that of TS-ESLint's for the
`ExportDefaultDeclaration` node. Relates to #9705
2025-04-02 13:49:26 +01:00
therewillbecode 498b4791a2 fix(ast/estree): Fix AccessorProperty node (#10067)
Align our AST's ESTree output with that of TS-ESLint's for
the`AccessorProperty` node. Relates to #9705

Add `optional`, `override`, `readonly`, and `declare` fields to
`AccessorProperty` when serializing to TS estree.

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-04-02 13:19:06 +01:00