Commit Graph

1582 Commits

Author SHA1 Message Date
Don Isaac b0a1561ef5 refactor(parser): move empty ts type parameter checks to parser (#11696)
Part of an effort to move non symbol/scope checks from semantic ->
parser.

> I intend to only keep checks associated with symbols and scopes in the
checker, and move everything back to the parser.
> We need to stop the formatter from formatting code when a syntax error
occurs. The formatter don't run semantic.
2025-06-14 18:24:53 +08:00
Boshen 4e40089741 fix(parser): parse TSTypePredicate correctly (#11666)
fixses #11659
2025-06-13 10:16:19 +00:00
Boshen eb55d839c1 fix(parser): parse using() correctly (#11664)
closes #11658
2025-06-13 07:44:52 +00:00
Boshen b34c6f60c7 perf(parser,semantic): improve handling of diagnostics (#11641)
* Move some checks from semantic to parser
* Remove redundant indirections
2025-06-13 04:10:45 +00:00
Boshen 7266200faa fix(parser): parse @x() @y() export default abstract class {} (#11630)
Also took the opportunity to reduce a rewind on `export default interface`.
2025-06-12 10:00:46 +00:00
Boshen e0ae6b240c feat(parser): produce syntax error for decorator in class methods in js (#11624)
closes #11485
2025-06-12 03:58:17 +00:00
Boshen cdf7cdcac7 feat(parser): produce syntax error for decorators in non-class methods (#11614)
part of #11485

I searched around, there are no test cases in babel nor ts.
2025-06-11 17:15:56 +00:00
Boshen 3b03fd3559 feat(parser): produce correct syntax error for interface I extends (typeof T) (#11610)
closes #11571
2025-06-11 14:42:38 +00:00
Boshen ab3284addc feat(parser): produce syntax error for interface A implements B {} (#11608)
fixes #11591
2025-06-11 12:45:12 +00:00
Boshen 844a8a875a feat(parser): produce syntax error for declare function foo() {} (#11606)
closes #11590
2025-06-11 12:35:24 +00:00
Boshen b7b0dc3b79 refactor(parser): improve TSModuleDeclaration parsing (#11605)
closes #11592
2025-06-11 12:28:00 +00:00
Boshen e9a8832ce6 refactor(parser): rewrite decorator parsing (#11604)
fixes #11593

Parse decorators and pass them around explicitly instead of using a state to store them.
2025-06-11 11:11:58 +00:00
Boshen 7dd1002e72 chore(coverage): ignore .DS_Store (#11603)
I ran system disk storage check and it left some `.DS_Store`s,
leaving a very cryptic panic ...
2025-06-11 09:42:47 +00:00
Ulrich Stark b2bd741b9d refactor(parser): speed up and migrate ts errors for parsing ts tuple elements (#11580)
- migrate TS errors 5085, 5086, 5087
- avoid expensive lookahead in some cases
- improve code readability
2025-06-10 22:37:09 +08:00
overlookmotel f3eaefbf1b feat(ast)!: add value field to BigIntLiteral (#11564)
Add `value` field to `BigIntLiteral`, same as the other literal types.

Once upon a time, we stored the value as a `BigInt` which normalized the representation (removing `_`s, and converting e.g. `0xFF` to `255`). But we had to remove it because `BigInt` owns a heap allocation, and this caused a memory leak.

https://github.com/oxc-project/oxc/commit/50fe0fef73027f10740e68a65a78216933b5d8a0

This PR brings back the `value` field, but avoids memory leak by storing the normalized value as a string (`Atom`).

This has effects in various places in the codebase:

* Brings our JS-side AST into line with ESTree (fixing the test cases broken by #11563).
* Fixes edge cases in a couple of lint rules.
* Codegen now prints bigints in normalized form e.g. `0xFFn` is printed as `255n`.
* Changes behavior of 1 function in `oxc_ecmascript`.

I've made comments below on the parts I'm not so sure about. Review would be very welcome!
2025-06-09 02:28:46 +00:00
overlookmotel 2694d23021 test(ast/estree): bump acorn-test262 submodule (#11563)
Bump `acorn-test262` submodule to include https://github.com/oxc-project/acorn-test262/pull/47, which bumps Acorn dependency.

Acorn v8.15.0 fixes the formatting of the `bigint` field of `Literal`s to align with ESTree spec. Currently we are failing those test cases as our behavior is incorrect. Will fix it in following PR.
2025-06-09 02:28:45 +00:00
camc314 cb17dae916 fix(parser): report error on malformed template expressions (#11540)
fixes #11538
2025-06-08 11:46:56 +00:00
camc314 069c2b492c fix(parser): correct TemplateTail::to_str from $} to } (#11539)
not 100% sure this is correct
2025-06-07 14:25:29 +00:00
camchenry 551cd2a790 fix(parser): fix parsing of import source and import defer (#11537)
- closes https://github.com/oxc-project/oxc/issues/11532
2025-06-07 03:57:33 +00:00
Cam McHenry 8d9919fc49 fix(parser): parse import type from ... correctly in JS files (#11506)
- fixes https://github.com/oxc-project/oxc/issues/11505

I forgot to add a `self.is_ts` check for parsing `import type from ...`.
In JS files, this is just a normal default import.

Co-authored-by: Cameron McHenry <camchenry@lovelace.localdomain>
2025-06-06 15:26:35 +08:00
Boshen d2da854ebb feat(parser): produce syntax error for satisfies and as in js files (#11502)
fixes #11313
2025-06-05 17:09:42 +00:00
overlookmotel 53d1a851dd test(ast/estree): do not ignore repeated fields in TS-ESTree AST (#11501)
#11500 fixed the duplicate fields in TS-ESTree AST. TS-ESTree conformance no longer needs to ignore duplicate fields.
2025-06-05 15:54:56 +00:00
Boshen cd063d1ae5 feat(parser): produce syntax error for decorators on overload (#11496)
fixes #11029

```ts
class A {
    @decorator()
    method(): Array<string>
}
```
2025-06-05 15:02:36 +00:00
camchenry 392752f63e fix(parser): handle import {type as as} correctly (#11488)
- closes https://github.com/oxc-project/oxc/issues/11484
2025-06-05 05:37:27 +00:00
Boshen be3bd8ce22 fix(parser): fix panic while parsing async await => {} in module (#11493)
fixes #11487
2025-06-05 05:01:20 +00:00
Boshen 458c372a4f feat(parser): produce syntax error for decorators in incorrect places (#11491) 2025-06-05 03:12:18 +00:00
Boshen 7e884511bd feat(parser): syntax errors for decorators appearing in conflicting places (#11482)
fixes #11472

e.g.

```
@dec1 export @dec2 class C {}

@dec1 export default @dec2 class {}
```
2025-06-04 14:09:25 +00:00
Boshen c208a14a39 fix(coverage): handle errors correctly on multi-file ts test cases (#11477) 2025-06-04 09:00:02 +00:00
Boshen e291191a1e fix(parser): fix panic when parsing export import (#11473)
fixes #11453
2025-06-04 09:00:02 +00:00
overlookmotel 7360bd27bc test(ast/estree): conformance tests check field order in TS-ESTree AST (#11450)
ESTree conformance tests check field order is correct in TS-ESTree ASTs.

This revealed some mismatches, but they were fixed by #11449.

Need to skip 1 test which is only failing due to rounding errors in floating point numbers in the `serde_json` round-trip conversion JSON -> value -> JSON.
2025-06-02 21:20:26 +00:00
overlookmotel bc41bdf7ce test(ast/estree): bump acorn-test262 dependency (#11449)
Bump `acorn-test262` submodule, to integrate https://github.com/oxc-project/acorn-test262/pull/44 and https://github.com/oxc-project/acorn-test262/pull/45 which fix the field order in TS snapshots of `TemplateElement` and regexp `Literal`s.
2025-06-02 21:12:25 +00:00
Boshen 25167f23ec perf(parser): parse ts type signature without rewind (#11443)
part of #11334
2025-06-02 14:49:00 +00:00
camchenry bf974da3ad refactor(parser): remove lookahead for parsing import declarations and specifiers (#11381)
- towards https://github.com/oxc-project/oxc/issues/11334

Rewrites the parsing for import declarations to not require any lookahead. I've diverged from the TS reference implementation here because it uses a fair amount of `tryParse` and lookahead. The main trick used here is simply parsing as we go and then doing "lookback" to see if the identifier we just parsed was actually a keyword like `defer` or `type`. This allows us to not need to use rewind at all.

Also improves diagnostics slightly by emitting a new diagnostic message for `import ... =`. In the end, this didn't improve conformance at all, but it's a perf win of about 1% on some files.
2025-06-02 02:44:21 +00:00
Boshen 2e5a243bd3 perf(parser): rewrite parse object literal element to avoid rewind (#11431) 2025-06-01 17:07:31 +00:00
Boshen dc110f594d perf(parser): parse binding list without token peek (#11423) 2025-06-01 10:45:47 +00:00
Boshen 2953a07b9b perf(parser): parse < without token peek (#11422) 2025-06-01 08:53:20 +00:00
overlookmotel 1df6732b46 feat(ast/estree): add decorators to ESTree AST (#11393)
Close #10921.

Add decorators to ESTree AST. `decorators` field is added to `ClassDeclaration`, `ClassExpression`, `PropertyDefinition`, and `MethodDefinition` - as per [ESTree spec](https://github.com/estree/estree/blob/52399d923111fe2a4dcfe1ede795e08356f35ddc/stage3/decorators.md).

Bump `acorn-test262` dependency to include https://github.com/oxc-project/acorn-test262/pull/42 and https://github.com/oxc-project/acorn-test262/pull/43, which add test fixtures for decorators.
2025-05-31 01:20:06 +00:00
overlookmotel 12690a10bf refactor(ast/estree): re-order fields in visitation order (#11362)
Re-order fields in ESTree and TS-ESTree ASTs to be in visitation order (which is the order in which they're defined in Rust types).

This does not align with either Acorn or TS-ESLint, but for the reasons discussed in https://github.com/oxc-project/oxc/issues/9705#issuecomment-2859250834, I feel this isn't a concern. The field order doesn't matter much per se, only that it's consistent, and this change has other benefits - it will make it simpler to build a visitor which visits fields in the correct order, and allows adding other parsers to `acorn-test262` in order to produce test cases for decorators.

Visitation order aligns with TS-ESLint except for the following types:

* `ExportSpecifier`
* `TSImportType`
* `TSIndexedAccessType`
* `TSMethodSignature`
* `TSPropertySignature`
* `TSTypePredicate`

In these 6 cases, TS-ESLint's `visitorKeys` has the wrong visitation order, because it does not visit fields in source order. I will open an issue and hopefully they can fix.

This PR also bumps `acorn-test262` to include https://github.com/oxc-project/acorn-test262/pull/37 which re-orders the fields in fixture snapshots to match the new field order here.
2025-05-29 02:20:54 +00:00
camchenry 4c49274a9d refactor(parser): rewrite import/export specifier parsing (#11356)
- part of https://github.com/oxc-project/oxc/issues/11334

Rewrites the import/export specifier parsing to be much closer to the TS compiler reference version. The diagnostics should be nearly the same as before, but with a slight improvement for a few cases where we had "unexpected token" before and now we know it should be "identified expected." In theory this might restore the performance somewhat since we don't do any lookahead in this version. However, the initial version of this has some extra allocations there weren't there previously I think, so it may not be much faster.
2025-05-29 01:42:14 +00:00
leaysgur 069b843632 refactor(parser): Avoid peek in parse_delimited_list (#11343)
Out of curiosity, I've just deleted the peeking in `parse_delimited_list`.

Then, coverage errors show me there are only a few places missing error report for invalid trailing comma after rest element, etc.

So I added those checks manually.

In TSC, these checks seem to be done at checker, not parser. But we need this in parser for JS.

---

(Sorry, I have PASSED the test, but I am not very confident that this change is intrinsically sound.)
2025-05-28 08:30:28 +00:00
magic-akari 3249ab641e fix(isolated-declarations): correctly emit string property keys for non-identifiable name (#11329)
- Closes: #11318
- Closes: https://github.com/oxc-project/oxc/issues/11338
2025-05-28 10:46:34 +08:00
leaysgur 08eb1eb5bd refactor(parser): align jsx parsing to tsc (#11314)
My original plan was to rework the JSX parser from the entry point, as I had found in #11232.

However, when I read through all the TSC JSX parser code, I found that what we are doing is not much different.

---

As for perf perspective, the OXC currently requires rewind in 3 places for JSX:

- 1: `parse_jsx_expression()` - `lookahead`
  - To determine if it is a `JSXElement`(`<C`) OR `JSXFragment`(`<>`) at the entry point
- 2: `parse_jsx_child()` - `checkpoint+rewind`
  - The same above OR breaking list(`</`)
- 3: `parse_jsx_child()` - `checkpoint+rewind`
  - To determine if it is `JSXChild::Spread`(`{...Expression}`) OR `JSXChild::ExpressionContainer`(`{JSXExpression}`)

These don't exist in TSC, they just use union type or loose ASTs.

But we have different and explicit ASTs in Rust, it seems they can't be easily eliminated.

And unfortunately, I tried, but there was no remarkable improvement.

- For 1, 3045952596c7201348b26ef347bdd99e110384e4 => Minifier +3%, but no effect for parser
- For 2, I wish we had `Kind::LessThanSlash`...
- For 3, a12ff218e6d93be84a90ba1f8bcff25181b4b384 => No effect for parser

As a result, I think it would be better to keep the current clean and explicit code.
2025-05-27 08:59:09 +00:00
leaysgur 2e43b6f44e refactor(parser): Remove Lexer peeking for js/expression (#11298)
Part of #11194

- Snapshot diffs closer to TS
- `next_token_is_identifier_or_keyword_or_literal_on_same_line` is based on:
  - https://github.com/microsoft/TypeScript/blob/main/src/compiler/parser.ts#L7150
2025-05-26 07:33:45 +00:00
Boshen f88f666324 feat(minifier): normalize Number.NaN as NaN (#11275)
closes #11272
2025-05-25 05:19:23 +00:00
Boshen d6fc750840 feat(minifier): add pure to side-effect free global constructor during DCE (#11270)
So that codegen can print /* #__PURE__# */ during codegen.

This mimics esbuild https://github.com/evanw/esbuild/commit/79187160bcf3ca0e24d0371cdaf0defcd2fc6824

closes ENG-48
2025-05-24 14:27:34 +00:00
Boshen c4f8690c1e fix(parser): fix "Enum member must have initializer." false positive (#11265)
when parens are encountered in enum member value.
2025-05-24 14:01:25 +08:00
Boshen 2398906c50 feat(parser): check mixed coalesce and new exponential with preserveParens:false (#11264) 2025-05-24 05:18:01 +00:00
Dunqing ac9638ec0f fix(isolated-declarations): object property key was generated incorrectly (#11231)
Just for aligning with `TypeScript`, the object key should be converted to a static property key when possible

```ts
const ObjectKeys = {
	a: 0,
	["b"]: 1,
	[`c`]: 2,
	[3]: 3,
	[-3]: 4,
	[4n]: 5,
};
```

```ts
declare const ObjectKeys: {
	a: number
	b: number
	c: number
	3: number
	[-3]: number
};
```

`BigInt` is invalid as a property key in TypeScript, so `4n` doesn't emit.
2025-05-23 11:51:28 +00:00
leaysgur 86e753d6c0 refactor(parser): Remove Lexer peeking for jsx (#11232)
Part of #11194

Diffs in the snapshots are expected and closer to the TS.
2025-05-22 14:01:02 +00:00
camchenry def05bcab0 refactor(parser): remove lookahead usage in parsing arrow function expressions (#11220)
- towards https://github.com/oxc-project/oxc/issues/11194

Replaces all usage of the lexer lookahead functions like `nth_kind`, so that we only use checkpoints and rewinding, which is aligned with the TypeScript compiler implementation. This also will allow us to remove the lookahead field from the lexer in the future.
2025-05-22 02:58:59 +00:00