Commit Graph

110 Commits

Author SHA1 Message Date
sapphi-red 77efb7698d feat(parser): improve error message for invalid switch clauses (#15728)
Improved error message for invalid switch clauses.

**Before**
```
  x Unexpected token
   ,-[:2:3]
 1 | switch (foo) {
 2 |   caze 1:
   :   ^^^^
 3 | }
   `----
```

**After**
```
  × Expected switch clause
   ╭─[misc/fail/switch-invalid-clause.js:2:3]
 1 │ switch (foo) {
 2 │   caze 1:
   ·   ──┬─
   ·     ╰── `case` or `default` clause expected here
 3 │ }
   ╰────
  help: If this is intended to be the condition for the switch statement, add `case` before it.
```
2025-11-16 02:57:35 +00:00
sapphi-red 56917278ca feat(parser): improve import source from error message (#15727)
Improved error message for `import source` + something + `from`.

**Before**
```
  x Unexpected token
   ,-[:1:19]
 1 | import source foo bar from 'module';
   :                   ^^^
   `----
```

**After**
```
  × Expected `from` but found `Identifier`
   ╭─[misc/fail/import-source-non-from.js:1:19]
 1 │ import source foo bar from 'module';
   ·                   ─┬─
   ·                    ╰── `from` expected
   ╰────
```
2025-11-16 02:57:35 +00:00
sapphi-red 71c2fb05bc feat(parser): improve error message when JSX is found while not enabled (#15725)
Improved the error message when JSX syntax is found while not enabled.

The idea came from esbuild's error message ([esbuild try](https://esbuild.github.io/try/#dAAwLjI3LjAAAGV4cG9ydCBjb25zdCBmb28gPSA8Rm9vIC8+Ow)).

**Before**
```
  x Unexpected token
   ,-[:1:20]
 1 | export const foo = <Foo />;
   :                    ^
   `----
```

**After**
```
  × Unexpected JSX expression
   ╭─[misc/fail/jsx-in-js.js:1:20]
 1 │ export const foo = <Foo />;
   ·                    ───────
   ╰────
  help: JSX syntax is disabled and should be enabled via the parser options
```
2025-11-16 02:57:34 +00:00
camc314 8951953008 feat(parser): improve diagnostic messages for merge conflicts (#15443)
Adds detection and improved error messages for version control merge conflicts encountered in the parser.

This is based upon rustc's implementation.
2025-11-08 06:53:50 +00:00
sapphi-red d4f6545078 feat(parser): improve error message for missing closing tokens (#15269)
Improves error message for imbalanced tokens.
```
 × Expected `,` or `]` but found `const`
   ╭─[misc/fail/imbalanced-array-expr.js:2:1]
 1 │ const foo = [0, 1
   ·             ┬
   ·             ╰── Opened here
 2 │ const bar = 2
   · ──┬──
   ·   ╰── `,` or `]` expected
   ╰────
```

The error message for this case was:
```
  x Expected `,` but found `const`
   ,-[:2:1]
 1 | const foo = [0,1
 2 | const bar = 2
   : ^^|^^
   :   `-- `,` expected
   `----
```

This change makes it clear why the parser is expecting `,` and also tell that it can be fixed by adding the closing token somewhere.
2025-11-04 13:13:00 +00:00
sapphi-red 2ef8f01624 feat(parser): improve error message for missing conditional alternative (#15268)
Improve error message for missing alternative for conditional expressions.
```
 × Expected `:` but found `EOF`
   ╭─[misc/fail/missing-conditional-alternative-type.ts:2:1]
 1 │ type A = 1 extends 2 ? 3
   ·                      ┬
   ·                      ╰── Conditional starts here
   ╰────

  × Expected `:` but found `EOF`
   ╭─[misc/fail/missing-conditional-alternative.js:2:1]
 1 │ const foo = 1 ? 2
   ·               ┬
   ·               ╰── Conditional starts here
   ╰────
```
Without this PR, the error mesage is a simple ``Expected `:` but found `EOF` `` only.
2025-11-04 13:12:59 +00:00
sapphi-red c28807b32c fix(parser): reject async await => {} in scripts (#15222)
`async await => {}` was rejected in modules but should be rejected in scripts as well.
2025-11-02 12:08:56 +00:00
camc314 384ea3c7be fix(parser): report err on missing function body in expression (#14946)
partially addresses #14944
2025-10-27 11:04:22 +00:00
Ulrich Stark 81a28d4bff fix(parser): forbid abstract class members with implementation in parser instead of semantic (#14325)
The following four cases are now errors in parser instead of semantic
errors.
[TS
Playground](https://www.typescriptlang.org/play/?#code/IYIwzgLgTsDGEAJYBthjAgYge2wg3gFAIKiQzwIAOU2VCAvAgMwDchxp40ciAtgFMIAC2wATABQBKAggC+HEmR6UA5kITqIEAVGkFOJKEICuUAHYIAjOxILOyiojAaX23RIBuwZCYEAuBHMTPhBdGSISEk4FOSA)
for reference.
Closes #14013 

```typescript
abstract class Foo {
  abstract prop = 3;

  abstract method() { }

  abstract get getter() {
    return 1;
  }

  abstract set setter(value: number) {
    
  }
}
```
2025-10-05 09:11:58 +08:00
Don Isaac f4d6790d27 fix(parser): forbid accessors named constructor (#14017)
Re-creation of #14015. Closes #14014.
2025-09-23 12:56:23 +08:00
Karan Kiri db33196f16 feat(parser): adds typescript rule for empty argument list (#13730)
Co-authored-by: Boshen <boshenc@gmail.com>
2025-09-13 21:27:09 +08:00
overlookmotel 9fa551a6ce fix(semantic): handle edge cases checking super (#13499)
Fixes #13284.

Fix semantic's checking of `super()` and `super.prop`.

This fixes various TS test cases, notably correctly identifying that these are all illegal:

```js
class C {
  prop = function() { super.foo };
  [super.foo]() {}
  @super.foo method() {}
};
```

It also fixes various edge cases related to classes nested within other classes that aren't covered by existing conformance tests.

It's pretty much a complete re-write. I've combined the 2 paths for in a class and not in a class into one, to avoid repeated code.

This PR includes pass + fail test fixtures which include `super` in every conceivable position I could think of. There are so many errors in the fail test fixtures, that I had to write a script to check the snapshot includes them all!

Hopefully this solves this issue for once and for all!

Note: The large pass fixture seems to be revealing bugs somewhere in transformer - there's a mismatch, as well as lots of wrong scopes. But that's incidental to this PR, and can be addressed separately.
2025-09-10 03:23:41 +00:00
Ulrich Stark f795d69ce0 fix(parser): improve diagnostics around modifier checks (#13526)
Follow up to #13368.
Closes #11713 by fixing five more cases mentioned in
https://github.com/oxc-project/oxc/issues/11713#issuecomment-3239687597.

Other changes:
- Added TS(1024) diagnostic
- Fixed the broken oxc-11713-13.ts test file
- Made TS(1070) not report redundantly if it's actually the
responsibility of TS(1071)
- Branch off the more specific TS(1028) diagnostic from the general
TS(1030) diagnostic like TypeScript does
- Remove incorrect code to parse modifiers on typescript this parameter
2025-09-09 09:28:39 +08:00
Ulrich Stark 65aca9eeb9 fix(parser): reset popped lexer errors when rewinding (#13494)
Closes #12816.
Alternative to #13460.

This PR is the slightly cleaner way to fix the issue compared to the
other PR, but comes at a potential performance cost because now instead
of remembering how many errors there were, we are storing a clone of all
errors and re-assign them when rewinding.

This makes it possible to fix the issue because now even errors before a
checkpoint, that were popped/removed after the checkpoint, are properly
rewinded.
2025-09-01 22:54:27 +08:00
Boshen dc9645f98f fix(semantic): allow super in object literal method inside a class (#13463)
fixes #13284
2025-08-31 21:06:40 +00:00
overlookmotel fd3233cf95 fix(semantic): flag super in function inside ObjectExpression (#13325)
Fix #13323.

Make semantic raise errors for the following illegal `super` uses:

```js
o = {
  foo: function() {
    super.foo;
  },

  bar() {
    return function() {
      super.bar;
    };
  },

  [ function() { super.qux; } ]() {},

  get [ function() { super.bing; } ]() {},

  set [ function() { super.bong; } ](v) {},
};
```
2025-08-29 13:10:08 +00:00
Ulrich Stark 903a150bf8 feat(parser): report more invalid modifier locations (#13368)
### Changes

- Added all 22 examples of #11713 to `tasks/coverage/misc/fail`
- Added `Default` and `Export` to modifiers
- Added three new TS diagnostics: 1029, 1031, and 1070
- Split off `FunctionKind::Constructor` from `FunctionKind::ClassMethod`
to differentiate when verifying modifiers of formal parameter
- Added multiple checks to verify modifiers

### Results
- "Negative Passed" percentage of `parser_babel.snap` and
`parser_typescript.snap` increased
- Better diagnostic messages in both snapshots
- All new failing examples of original issue do really fail
- Closes #11713

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2025-08-29 11:33:21 +08:00
Boshen 8a43068a27 fix(semantic): allow super in object method in js but not in ts (#13318)
closes #13284

Either TypeScript implemented the spec incorrectly or made it more
strict.

```js
class C {
  foo() {
    return {
      bar() {
        super.bar(); // syntax error in ts but not in js
      },
    };
  }
}
```
2025-08-27 02:20:08 +00:00
Cameron e836e552f3 fix(parser): prevent panic when parsing invalid extends clause (#12551)
fixes #12546
2025-08-01 16:19:30 +08:00
camc314 ce5876d5b5 fix(parser): validate inner expression of type assertions in assignment targets (#12614)
fixes #12612
2025-08-01 04:42:56 +00:00
camc314 6fa23ad24e test(semantic): add failing tests for arguments and eval use in strict mode (#12298) 2025-07-16 07:49:38 +00:00
camc314 6c9c580989 fix(parser): panic when parsing interface with missing implements (#11898) 2025-06-25 12:42:32 +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 b7b0dc3b79 refactor(parser): improve TSModuleDeclaration parsing (#11605)
closes #11592
2025-06-11 12:28:00 +00:00
camc314 cb17dae916 fix(parser): report error on malformed template expressions (#11540)
fixes #11538
2025-06-08 11:46:56 +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
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 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 e291191a1e fix(parser): fix panic when parsing export import (#11473)
fixes #11453
2025-06-04 09:00:02 +00:00
Boshen 4c9a9b35e0 fix(parser): guard against re-lex tokens when fatal error (#11023)
fixes #10977
2025-05-14 05:37:08 +00:00
Boshen 2718f29fcf fix(parser): fix panic when the parser tries to re-lex >> (#10756)
fixes #10639
2025-05-01 15:04:48 +00:00
Boshen f803807968 fix(parser): fix crash when parsing for(in (#10640) 2025-04-27 15:42:11 +00:00
Dunqing bc54d1fabd fix(semantic): redeclare variable with function declaration inside the function should not throw error (#10160)
close: #10159
2025-04-01 13:05:30 +00:00
camc314 2c6e3f176f fix(parser): fix false positive parsing optional member expr (#9534)
closes #9525
2025-03-04 10:42:54 +00:00
camc314 01de74c2a2 fix(parser): correctly fail parsing when parsing foo.bar?. (#9499)
closes #9497

[swc](https://play.swc.rs/?version=1.11.5&code=H4sIAAAAAAAAA8tJLVEoSi0oylewVaiuteYCs%2FXS7PUAMGIdLBkAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BAWdSIifrKJVBTl7iUE0maH3xsz8jooBbNoeKq1PMsQkYX4nEsi2Sf8lARIOxTNJia49XaWvRrRCtVoOxpIyBOluiX3hoMNQajjLXPGmzH%2FC3VwkUnkCu4o%2BsnSVTc0JbjwXmrZDkk50qF%2FwA%2FqsvNjMPLqm4kXGrYvhlQioBQBAAA%3D)

[typescript](https://www.typescriptlang.org/play/?#code/PTAEAEBcGcFoDsD2BjAFgU2QawFABt1JQAndAB2MVAF5QBvAXwG4dSLEA6AMwH4Og)

[babel](https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=DYUwLgBATiAOUHsIF4IG8C-BuAUDeCAdAGYD8hQA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2%2Cstage-3&prettier=false&targets=&version=7.26.9&externalPlugins=&assumptions=%7B%7D)

[esbuild](https://esbuild.github.io/try/#dAAwLjI1LjAAAGxldCByZXBybyA9IHt9OwpyZXByby5mPy4)
2025-03-03 11:57:38 +00:00
Boshen 1486849ec2 fix(semantic): syntax error for undeclared private field access (#7635)
closes #7582
2024-12-04 05:33:04 +00:00
Song Gao cf3415b0e4 chore(doc): replace main/master to tag/commit to make the url always accessible (#7298) 2024-11-16 21:00:30 +08:00
Boshen 0e4adc15dd feat(ast)!: remove invalid expressions from TSEnumMemberName (#7219) 2024-11-09 08:48:14 +00:00
DonIsaac f1551d64bc fix(semantic): ? on variable declaration type annotations is a syntax error (#5956)
Closes #5955
2024-09-22 00:01:47 +00:00
Boshen 603817bef9 feat(oxc)!: add SourceType::Unambiguous; parse .js as unambiguous (#5557)
See https://babel.dev/docs/options#misc-options for background on `unambiguous`

Once `SourceType::Unambiguous` is parsed, it will correctly set the returned `Program::source_type` to either `module` or `script`.
2024-09-07 10:48:58 +00:00
overlookmotel dc924892cc test: add trailing line breaks to conformance fixtures (#5541)
Continuation of #5537. Ensure all conformance fixture files have a trailing line break.
2024-09-06 12:55:17 +00:00
Boshen 10279f55d9 feat(parser): add syntax error for hyphen in JSXMemberExpression <Foo.bar-baz /> (#5440)
closes #5355
2024-09-04 14:09:06 +00:00
DonIsaac c2fa72571f feat(ast,parser): parse TSTypeAnnotations on AccessorProperty (#5179)
Closes #5177

While making this, I noticed an uncaught parse error for accessors: accessors cannot be optional. I'll add a fix for this in an up-stack PR.
2024-08-25 01:02:48 +00:00
heygsc ad2be97078 fix(semantic): incorrect semantic check for label has same name (#5041)
fix: #5036

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-08-23 10:36:12 +08:00
DonIsaac d5c4b190aa fix(parser): fix enum member parsing (#4543)
Closes #4449
2024-07-30 10:43:09 +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
Boshen 28eeee0f71 fix(parser): fix asi error diagnostic pointing at invalid text causing crash (#4163) 2024-07-10 14:45:10 +00:00
DonIsaac 63f36daae0 feat(parser): parse modifiers with parse_modifiers (take 2) (#3977)
Same as #3948, with fixes for bugs found by @Boshen.
2024-06-30 03:46:34 +00:00