Commit graph

138 commits

Author SHA1 Message Date
Boshen
93ce5a919a
chore: fix internal doc warnings 2024-04-13 15:59:24 +08:00
Boshen
6c6adb46d1
fix(ast): parse rest parameter with the correct optional and type annotation syntax (#2686)
closes #2653
2024-03-12 15:47:22 +08:00
Boshen
8a73d18fcf
chore(parser): make sure all span.end >= span.start (#2681)
closes #2679
2024-03-11 19:49:51 +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é
776812315d
fix(parser)!: drop TSImportEqualsDeclaration.is_export (#2654)
This is one point where Babel and TSESLint diverge. For linter purposes
TSESLint structure makes more sense and that the reason of
https://github.com/typescript-eslint/typescript-eslint/issues/4130

The remaining `is_export` was creating redundant information and made
prettier (and the WIP oxc/prettier) print the AST of `export import X =
Y` as `export export import X = Y`.
2024-03-10 13:22:18 +08:00
Boshen
32303b20fb
New tool: oxc_module_lexer (#2650)
# Oxc Module Lexer

This is not a lexer. The name "lexer" is used for easier recognition.

## [es-module-lexer](https://github.com/guybedford/es-module-lexer)

Outputs the list of exports and locations of import specifiers,
including dynamic import and import meta handling.

Does not have any
[limitations](https://github.com/guybedford/es-module-lexer?tab=readme-ov-file#limitations)
mentioned in `es-module-lexer`.

I'll also work on the following cases to make this feature complete.

- [ ] get imported variables
https://github.com/guybedford/es-module-lexer/issues/163
- [ ] track star exports as imports as well
https://github.com/guybedford/es-module-lexer/issues/76
- [ ] TypeScript specific syntax
- [ ] TypeScript `type` import / export keyword

## [cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer)

- [ ] TODO

## Benchmark

This is 2 times slower than `es-module-lexer`, but will be significantly
faster when TypeScript is processed.

The difference is around 10ms vs 20ms on a large file (700k).
2024-03-09 23:23:55 +08:00
magic-akari
2a235d3b8c
fix(ast): parse with_clause in re-export declaration (#2634) 2024-03-07 14:09:31 +08:00
Boshen
240ff19675
refactor(parser): improve parsing of BindingPattern in TypeScript (#2624)
closes #2622
2024-03-06 16:16:03 +08:00
overlookmotel
0646bf34fa
refactor: rename CompactString to CompactStr (#2619)
Preparatory step for #2620.

This PR purely changes names of types and methods:

* `CompactString` -> `CompactStr`
* `Atom::to_compact_string` -> `to_compact_str`
* `Atom::into_compact_string` -> `into_compact_str`

Have split this into a separate PR as the diff is large, but it does absolutely nothing but renaming (I've checked the whole diff twice, so feel free not to check it again!). This should make it easier to see the content of the substantive change in #2620.
2024-03-06 12:24:23 +08:00
Boshen
bf42158ad7
perf(parser): inline end_span and parse_identifier_kind which are on the hot path (#2612) 2024-03-05 15:39:53 +08:00
Arnaud Barré
97aa9cfd54
fix(parser): fix span end for TSEmptyBodyFunctionExpression (#2606)
https://oxc-project.github.io/oxc/playground/?code=3YCAAICXgICAgICAgICxG4jI43W9aqTWr3WzyAs1pNu4fnf%2FXXEB3Yt%2FfiBIgA%3D%3D

The conformance test are going back to initial state. I think the `.asi`
should be smarter, i can rethink that later

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-03-05 12:45:11 +08:00
Boshen
e2d2ce3369
feat(parser): parse decorators properly (#2603)
closes #2562
2024-03-04 19:30:41 +08:00
Boshen
20c7bf7835
feat(ast): add AssignmentTargetRest (#2601)
closes #2598
2024-03-04 14:31:22 +08:00
Arnaud Barré
ac520d01a9
fix(parser): fix span start for TSExportAssignment (#2594)
[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIDRgICAgICAgICymcgR7351T2PErukD7UXVyom%2F90V9gN2l18vERCm6ABJAvuZFDanGfdTjE8wAVsdwZMQHIswA2DgraPxXUAF2ua%2F8b0XoyRw%2FfyXZIIA%3D)
2024-03-04 11:41:23 +08:00
Arnaud Barré
d9cc429d4a
fix(parser): parse empty method declaration as TSEmptyBodyFunctionExpression (#2574)
That a tricky one, because it's time to decide what does ESTree
compliant means in the TS world (re #2463)

This code:

```ts
export declare class ByteBuffer {
  clear(): void;
     // ^^
}
```

- Is parsed by
[Babel](d38530204e)
as `FunctionExpression` with an empty body
- By
[TS-ESLint](4ea4e2feb5)
as
[TSEmptyBodyFunctionExpression](https://github.com/typescript-eslint/typescript-eslint/pull/1289)
- By
[OXC](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC1gICAgICAgICyHorESipoTXPdvBaE9wxzlOraoWs19SUxDvdcwSVU0kbBO2b7ppX3x2P5IhQlpGHOYEHNCEfLf38HUICA)
as `TSDeclareFunction`

I'm going the easy way to fix this to the Babel way, but I think
following TS-ESLint would make sense. There is an [open babel
issue](https://github.com/babel/babel/issues/13878) about that.

Edit: Ok that not so easy and require updating some logic.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-03-03 13:59:17 +00:00
Arnaud Barré
258b9b1c14
fix(ast): support FormalParameter.override (#2577)
This
[code](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC1gICAgICAgICxG4jI43W9aqTWr3WzyA0TqSOjtB34F78iblvTQruFcqR6BUbbiLtWhj5rEL0NnFkDs4pF3dHiw39X7YCA)
can't be represented in the current OXC AST:

```ts
class Foo {
  constructor(override bar: string) {}
}
```
2024-03-03 14:41:42 +08:00
Boshen
3efbbb2e1f
feat(ast): add "abstract" type to MethodDefinition and PropertyDefinition (#2536)
closes #2532

```
pub enum PropertyDefinitionType {
    PropertyDefinition,
    TSAbstractPropertyDefinition,
}

pub enum MethodDefinitionType {
    MethodDefinition,
    TSAbstractMethodDefinition,
}
```
2024-02-28 17:33:11 +08:00
Boshen
be6b8b7ce6
[BREAKING CHANGE] Change Atom to Atom<'a> to make it safe (#2497)
Part of #2295

This PR splits the `Atom` type into `Atom<'a>` and `CompactString`.

All the AST node strings now use `Atom<'a>` instead of `Atom` to signify
it belongs to the arena.

It is now up to the user to select which form of the string to use.

This PR essentially removes the really unsafe code 


93742f89e9/crates/oxc_span/src/atom.rs (L98-L107)

which can lead to 

![image](https://github.com/oxc-project/oxc/assets/1430279/8c513c4f-19b0-4b63-b61c-e07c187c95b5)
2024-02-26 19:34:40 +08:00
Dunqing
70295a5552
feat(ast): update arrow_expression to arrow_function_expression (#2496) 2024-02-25 14:39:34 +00:00
Boshen
d08abc638e refactor(ast): s/NumberLiteral/NumericLiteral to align with estree 2024-02-21 21:41:08 +08:00
Dunqing
197fa16613
feat(semantic): add check for duplicate class elements in checker (#2455)
1. Remove the check implementation of the parser
2. Implement it to semantic checker
3. Support typescript's check for duplicate class elements

Support checking for duplicate class elements in semantic checker is
easier to support typescript checking rules.
2024-02-21 14:10:19 +08:00
Dunqing
73e116e8a1
fix(parser): incorrect parsing of class accessor property name (#2386) 2024-02-11 22:57:13 +08:00
Boshen
ef336cb66b
feat(parser): recover from async x [newline] => x (#2375)
```javascript
async x
=> x
```

Babel recovers and displays "No line break is allowed before '=>'
2024-02-10 11:19:08 +08:00
overlookmotel
0bdecb5043
refactor(parser): wrapper type for parser (#2339)
Split parser into public interface `Parser` and internal implementation `ParserImpl`.

This involves no changes to public API.

This change is a bit annoying, but justification is that it's required for #2341, which I believe to be very worthwhile.

The `ParserOptions` type also makes it a bit clearer what the defaults for `allow_return_outside_function` and `preserve_parens` are. It came as a surprise to me that `preserve_parens` defaults to `true`, and this refactor makes that a bit more obvious when reading the code.

All the real changes are in [oxc_parser/src/lib.rs](https://github.com/oxc-project/oxc/pull/2339/files#diff-8e59dfd35fc50b6ac9a9ccd991e25c8b5d30826e006d565a2e01f3d15dc5f7cb). The rest of the diff is basically replacing `Parser` with `ParserImpl` everywhere else.
2024-02-07 23:22:08 +08:00
Dunqing
a3570d41f0
feat(semantic): report parameter related errors for setter/getter (#2316) 2024-02-05 17:38:43 +08:00
Boshen
1822cfe18d
refactor(ast): fix BigInt memory leak by removing it (#2293)
relates

We'll need to evaluate the value by other means.
2024-02-04 16:47:00 +08:00
Dunqing
2578bb3d64
feat(ast): remove generator property from ArrowFunction (#2260)
ArrowFunction doesn't support generator.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
2024-02-02 04:01:19 +00:00
Dunqing
165f948227
feat(ast): remove expression property from Function (#2247) 2024-02-01 15:23:27 +08:00
Dunqing
ea8cc98c34
fix(ast): AcessorProperty is missing decorators (#2176) 2024-01-26 15:43:05 +08:00
Dunqing
766ca63aa0
refactor(ast): rename RestElement to BindingRestElement (#2116)
close: #2115
2024-01-22 14:28:35 +08:00
Boshen
aa91fde1d9
refactor(parser): only allocate for escaped template strings (#2005) 2024-01-12 18:56:36 +08:00
overlookmotel
c7316856db
refactor(parser): reduce work parsing regexps (#1999)
#1926 produced a small performance regression because when parsing a
regexp, some work is repeated.
2024-01-12 11:36:30 +08:00
Boshen
4706765d2a
refactor(parser): reduce Token size from 32 to 16 bytes (#1962)
Part of #1880

`Token` size is reduced from 32 to 16 bytes by changing the previous
token value `Option<&'a str>` to a u32 index handle.

It would be nice if this handle is eliminated entirely because
the normal case for a string is always
`&source_text[token.span.start.token.span.end]`

Unfortunately, JavaScript allows escaped characters to appear in
identifiers, strings and templates. These strings need to be unescaped
for equality checks, i.e. `"\a"  === "a"`.

This leads us to adding a `escaped_strings[]` vec for storing these
unescaped and allocated
strings.

Performance regression for adding this vec should be minimal because
escaped strings are rare.

Background Reading:

* https://floooh.github.io/2018/06/17/handles-vs-pointers.html
2024-01-09 15:17:02 +08:00
Boshen
6e0bd52af1
refactor(parser): remove TokenValue::Number from Token (#1945)
This PR is part of #1880.

Token size is reduced from 40 to 32 bytes.
2024-01-08 16:29:03 +08:00
Dunqing
b50c5ec623
fix(parser): unexpected ts type annotation in get/set (#1942)
fix: https://github.com/oxc-project/oxc/issues/1939
2024-01-08 15:07:43 +08:00
Boshen
08438e04ba
refactor(parser): remove TokenValue::RegExp from Token (#1926)
This PR is part of #1880.

`Token` size is reduced from 48 to 40 bytes.

To reconstruct the regex pattern and flags within the parser , the regex
string is
re-parsed from the end by reading all valid flags.

In order to make things work nicely, the lexer will no longer recover
from a invalid regex.
2024-01-08 13:48:52 +08:00
Boshen
7eb2573178
refactor(parser): parse BigInt lazily (#1924)
This PR partially fixes #1803 and is part of #1880.

BigInt is removed from the `Token` value, so that the token size can be
reduced once we removed all the variants.

`Token` is now also `Copy`, which removes all the `clone` and `drop`
calls.

This yields 5% performance improvement for the parser.
2024-01-08 12:37:20 +08:00
overlookmotel
eb2966c512
fix(parser): fix incorrectly identified directives (#1885)
Parser incorrectly identifies string literals as directives if they
follow after `import`s, `export`s, or decorators.

In all of these cases, `'use strict'` produces a directive in the AST,
where it should be parsed as an `ExpressionStatement` containing a
`StringLiteral`:

```js
import x from 'foo';
'use strict';
```

```js
export {x};
'use strict';
```

```js
@foo
'use strict';
```


[Playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC0gICAgICAgIC0G8rnONK89ITJ3zrK%2FUP7OmSZPgHQzStr3yMtwFTU%2BD1WPt09JgqZJLoYooydbGsM5vGcf34BnIA%3D)

This PR should fix that.

I'm not sure about the decorator case, though. I assume it's not a
directive. But is prefixing a string literal with a decorator even legal
syntax anyway?

And a side nit: If I'm reading it right, I don't think the `continue`
statement in the decorator arm of the match does anything. Do I have
that right?

Last question: Where does one go about putting a test? I guess these
silly cases aren't covered by Babel etc's tests.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-01-04 13:39:15 +00:00
Dunqing
c3090c2c70
fix(parser): terminate parsing if an EmptyParenthesizedExpression error occurs (#1874)
close: https://github.com/oxc-project/oxc/issues/1870#issue-2061901976
2024-01-03 11:34:14 +08:00
overlookmotel
1feec95a94
fix(parser) fix typo in expecting_directives variable name (#1801)
Renamves `expecting_diretives ` to `expecting_directives` to fix spelling
2023-12-24 16:51:02 +00:00
magic-akari
5b2696b711
refactor(parser): report this parameter error (#1788)
- follow up: #1728
2023-12-23 22:09:14 +08:00
magic-akari
a2858ed452
refactor(ast): introduce ThisParameter (#1728)
Most TypeScript types can be eliminated during the code generation phase
by not printing the corresponding AST nodes.
The changes in this PR enable applying a similar technique to the `this`
parameter.
2023-12-19 13:20:33 +08:00
Boshen
1554f7c0d2
feat(parsr): parse let.a = 1 with error recovery (#1587) 2023-11-29 23:21:39 +08:00
Boshen
9842be4461
refactor(parser): remove duplicated code 2023-11-29 18:23:32 +08:00
magic-akari
9ff0ffcc6f
feat(ast): implement new proposal-import-attributes (#1476)
- [Import Attributes](https://tc39.es/proposal-import-attributes)
2023-11-25 15:56:09 +08:00
Boshen
567c6ed757
feat(prettier): print directives (#1497) 2023-11-22 19:39:25 +08:00
Boshen
07b010912a
feat(parser): add preserve_parens option (default: true) (#1474)
closes #1461
2023-11-21 11:16:30 +08:00
Jon Surrell
cb804d3cd2
Add base to AST BigintLiteral (#1416) 2023-11-19 11:11:19 +08:00
magic-akari
445352991f
fix(parser): Fix type import (#1291)
- fix: #1288 
- fix: #1289
2023-11-14 15:17:58 +08:00
magic-akari
9c0aafcd1c
fix(parser): Disallow ReservedWord in NamedExports (#1230)
- fix: #1222

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2023-11-12 10:52:02 +00:00