Commit graph

316 commits

Author SHA1 Message Date
Boshen
93ce5a919a
chore: fix internal doc warnings 2024-04-13 15:59:24 +08:00
Boshen
504698ab4a
chore: guard against unsafe code as much as possible. 2024-04-03 19:35:07 +08:00
Ali Rezvani
b76b02d019
fix(parser): add support for empty module declaration (#2834)
Should be merged after #2829, Tried a few times to get it done with
graphite stacking but found no success. I guess it either doesn't work
with forks or It is just a skill issue since I'm not familiar with it.

closes: #2829
closes: #2830

---------

Co-authored-by: Dmytro Maretskyi <maretskii@gmail.com>
2024-03-27 13:48:03 +08:00
overlookmotel
e793063f75
perf(parser): faster lexing JSX identifiers (#2557)
Speed up lexing JSX identifier continuations (i.e. after `-`), by
searching for end of identifier byte-by-byte.

Change does not register on benchmarks, only because benchmarks don't
contain any `<Foo-Bar />` identifiers, so don't exercise this code path.
2024-03-18 12:06:27 +00:00
Boshen
798a1fde09
fix(parser): fix failed to parse JSXChild after JSXEmptyExpression (#2726)
fixes #2723
2024-03-15 13:39:20 +08:00
Boshen
0f86333437
refactor(ast): refactor Trivias API - have less noise around it (#2692) 2024-03-12 20:16:36 +08:00
Boshen
86ee074678
fix(parser): remove all duplicated comments in trivia builder (#2689) 2024-03-12 17:51:22 +08:00
Boshen
cda9c93436
fix(parser): improve lexing of jsx identifier to fix duplicated comments after jsx name (#2687) 2024-03-12 15:51:51 +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é
b378e7ecc9
fix(parser): fix span for JSXEmptyExpression with comment (#2673)
[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAICVgICAgICAgICejwtjmCpbllbPawdM2eEFKwhGb62iFlQWu39yrLCA)

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-03-11 10:50:33 +00:00
Arnaud Barré
82260318a9
fix(parser): fix span start for return type in function type (#2660)
This matter for code like. This matches the behavior of both Babel and
TSESLint.

```ts
export type Plugin = (
  a: string
) => // Comment
number
```


[TSESLint](https://ast.sxzz.moe/#eNo9jMEKwjAMQH8l5KQw2X0wL/6AB/HUS61hVNq0pKkoY/9u52C35L3kzRhwwJd92+LEZ8UOcwP6zbSBE5XgeeWucfrkJAqrhmuok2cY4WAYwA5QVDxPho8wnqHv4ZJiJFbDXOODpCVSS8zrtcGSqji6tZDBoe0xPWtoc7dpctHeSYpPvPlglYruXixP/0+VSoYXXH53+0Kk)

[OXC](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC5gICAgICAgICyHorESipoTp3admelrvvzLVu5WllVkMM9n7p1s27YYhddDchOGSC6foF%2BGw%2B1Mfo7DYhiNueGpuc27%2F3gf2tToIA%3D)
2024-03-10 13:32:25 +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é
b453a072cc
fix(parser): parse named rest element in type tuple (#2655)
This is fixing the parser for `type X = [...args: string[]];`

In TSESLint TSNamedTupleMember in part of the TSType union, so I did the
same.
2024-03-10 13:25:15 +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
Boshen
9cc960e591
fix(parser): fix duplicated comments during parser rewind (#2600)
closes #2592
2024-03-04 14:07:33 +08:00
Arnaud Barré
24d46bccb5
fix(parser): fix span start for TSModuleDeclaration (#2593)
[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAICZgICAgICAgICymcgR7351T2PErukD7UXVyom%2F90VSbC7jSaCgoj9%2BqCyA)

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-03-04 04:29:32 +00:00
Arnaud Barré
f66059e91b
fix(ast)!: align TSImportType with ESTree (#2578)
Implements
https://github.com/typescript-eslint/typescript-eslint/issues/2998

The copy of props feels wrong, but could not get it working otherwise
with the box and borrow things 😅

Also I found that TSImportType was missing some entries for visitors and
codegen.

In the case of codegen I'm not really understand the need as all the
types seems to be dismissed?
2024-03-04 12:28:18 +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
Boshen
9479865d9b
feat(napi/parser): expose preserveParans option (#2582)
closes #2576
2024-03-03 15:18:47 +08:00
Arnaud Barré
637cd1dea4
fix(ast): support TSIndexSignature.readonly (#2579)
[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIDKgICAgICAgIC0GwpuZs97oWDqPM4xvCuoRB73mPOSrYb%2BTQEZf3b8RF0G%2B60jF5tYXUE9Me2%2FmMqVEwVy%2FiBIlyIMX6PqBpqsSmIXTJcsRqi4f3%2Bj6ICA)
2024-03-03 14:58:57 +08: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
Arnaud Barré
78f30bc2db
fix(ast): change TSMappedType.type_annotation from TSTypeAnnotation to TSType (#2571)
Is ESTree, in that special case, there is no TSTypeAnnotation wrapper:

(See `type X` in each)

- [oxc
playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIAWgICAgICAgICyHorESipoToAAwTlix58geR2%2Beeu9rZHQZOqK%2B%2BX85ZQ9ldchOoVw2oAm2qi9okF3bJ9o4l78ENP3f%2Bc%2B8cIK6Itp%2B3SIInU72Vk0%2FSqawy1VNV5zTgBr7gOpGtUZsvkc12Yp8MC2shel9fbpgDySpYsWdgDhf3jVlIA%3D)
- [astexplorer TSESLint
parser](9fc767f3a5)
- [astexplorer Babel
parser](9a4b02fae1)

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-03-03 14:38:45 +08:00
Arnaud Barré
32028eb1c5
fix(parser): TSConditionalType span start (#2570)
Span start should be the checkType.start

(as all my PR, I try to make it work, don't hesitate to close and to it
in a better way)


[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIDFgICAgICAgIC6nsrEgtelB%2FCnUFVHa8WBImPvKP4Ye3U5jBKASUfm8OtkXZASTLptdPlvM%2Fult4BgRbjIq3Yts9L2pZ%2FhVs8hMF%2Bwpqd%2FfdHggA%3D%3D)
2024-03-03 06:25:55 +00:00
Arnaud Barré
670081050f
fix(parser): set span end for TSEnumDeclaration (#2573)
[playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIDHgICAgICAgICyHorESipoTXPdvBaE9wxyPnC9nb7Q6xEpIf3AzkuhOU2arZOLF1u08q1G2hs5klxiUYA6%2BBkL693d0iAZC%2BUFyne3yIKPv32k8IA%3D)

(Tell me if you prefer that I group this kind of small fixes together)
2024-03-03 13:54:43 +08:00
Arnaud Barré
8a81851bf3
fix(parser): don't parse null as a literal type (#2572)
See playgrounds:

-
[oxc](https://oxc-project.github.io/oxc/playground/?code=3YCAAIDbgICAgICAgIC6nsrEgteLFrCnQnPuEizmC%2BDQ8C8bP9fXPj%2B7%2FjjmRZPvpAH3N7PfIPDu7RDOlrl79cHiork8WA08r39%2FqpCAgA%3D%3D)
-
[Babel](3a263be55b)
2024-03-03 13:54:16 +08:00
overlookmotel
78f8c2ce7f
perf(parser): lex JSXText with memchr (#2558)
Lexing JSXText only requires searching for 2 possible characters (`<`
and `{`), so can use `memchr`.
2024-03-01 22:26:53 +08:00
overlookmotel
dd31c6453a
refactor(parser): byte_search macro evaluate to matched byte (#2555)
Change behavior of `byte_search!` macro, to make it easier to understand and use:

1. `handle_match` removed. Macro instead evaluates to the first matching byte.
2. `handle_eof` does not return from enclosing function.
3. Alter syntax to make clear that `continue_if` and `handle_eof` are not closures, so can use `return` statements in them.

These changes enabled by #2552.
2024-03-01 21:28:39 +08:00
overlookmotel
c579620701
refactor(parser): small efficiencies in byte_search macro usage (#2554)
A few small efficiencies in usage of `byte_search` macro for lexing comments.
2024-03-01 21:23:34 +08:00
overlookmotel
18cff6aab8
refactor(parser): remove start params for byte_search macro arms (#2553)
Simplify `byte_search` macro a bit more.
2024-03-01 21:15:27 +08:00
overlookmotel
34ecdd58d8
refactor(parser): simplify byte_search macro (#2552)
This PR greatly simplifies the `byte_search!` macro.

Mainly removing `cold_branch()` from the "not enough bytes remaining for a batch" branch, which allows refactoring so that `handle_match` and `continue_if` don't need to be repeated twice.

Result for performance is inconsistent - a little better on some benchmarks, a little worse on others. But not by significant amounts either way. In my view, the benefit of making the macro simpler outweighs a small speed loss anyway.
2024-03-01 21:07:39 +08:00
overlookmotel
ddccaa1af9
refactor(parser): remove unsafe code in lexer (#2549)
Same as #2527. Just remove some unnecessary unsafe code, no substantive
changes.
2024-02-29 15:00:08 +00:00
overlookmotel
5a13714a18
perf(parser): faster lexing template strings (#2541)
Speed up lexing template strings.

This was the last use of `AutoCow` remaining in the lexer, and it's now removed.

Implementation is quite complex, to avoid repeatedly branching on whether an unescaped string is required or not (the way `AutoCow` did). I tried to simplify it down to a single function, but this hurt performance significantly.

Benchmarks do not show much movement, but I believe that's because there aren't many template strings in the benchmarks. Where there are template strings, I believe this speeds up lexing them significantly.
2024-02-29 13:28:30 +08:00
overlookmotel
9d7ea6b3f0
refactor(parser): single function for all string slicing (#2540)
Pure refactor. Move all string-slicing in `lexer::Source` into a single function.
2024-02-29 13:22:55 +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
overlookmotel
24ded3cb15
perf(parser): lex JSX strings with memchr (#2528)
Simplify lexing JSX string attributes. As the search is purely for 1
byte value (the closing quote), and so doesn't require a byte table, use
`memchr`.

This change doesn't really register on benchmarks, but it's one step
closer to removing `AutoCow`, and transitioning all the searches in the
lexer to byte-by-byte.
2024-02-28 14:39:23 +08:00
overlookmotel
0ddfc856d2
refactor(parser): remove unsafe code (#2527)
Remove some unnecessary unsafe code.
2024-02-27 20:28:21 +08:00
Boshen
46e779194a
chore: fix clippy warnings (#2519) 2024-02-26 23:55:18 +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
7a796c4b5f
feat(ast): add TSModuleDeclaration.kind (#2487)
closes #2395
2024-02-24 17:09:31 +08:00