Commit graph

5933 commits

Author SHA1 Message Date
Yuji Sugiura
7e4fa337d9
chore(just): Fix typo to make just r works (#6264)
Apparently this is only happening on my end, but a typo is still a typo.
🙄
2024-10-03 11:41:52 +08:00
Boshen
7bb745b085
ci: add trigger monitor oxc in prepare_release_crates
closes https://github.com/oxc-project/monitor-oxc/issues/44
2024-10-03 11:19:53 +08:00
7086cmd
e29c0676d6 fix(minifier): handle exceeded shifts. (#6237)
Related: #6161, the last situation.

```js
// https://github.com/tc39/test262/blob/main/test/language/expressions/unsigned-right-shift/S9.6_A2.2.js
test((-2147483649 >>> 0) !== 2147483647)
```
2024-10-03 02:52:05 +00:00
leaysgur
5a73a663dc refactor(regular_expression)!: Simplify public APIs (#6262)
This PR makes 2 changes to improve the existing API that are not very useful.

- Remove `(Literal)Parser` and `FlagsParser` and their ASTs
- Add `with_flags(flags_text)` helper to `ParserOptions`

Here are the details.

> Remove `(Literal)Parser` and `FlagsParser` and their ASTs

Previously, the `oxc_regular_expression` crate exposed 3 parsers.

- `(Literal)Parser`: assumes `/pattern/flags` format
- `PatternParser`: assumes `pattern` part only
- `FlagsParser`: assumes `flags` part only

However, it turns out that in actual usecases, only the `PatternParser` is actually sufficient, as the pattern and flags are validated and sliced in advance on the `oxc_parser` side.

The current usecase for `(Literal)Parser` is mostly for internal testing.

There were also some misuses of `(Literal)Parser` that restore `format!("/{pattern}/{flags}")` back and use `(Literal)Parser`.

Therefore, only `PatternParser` is now published, and unnecessary ASTs have been removed.
(This also obsoletes #5592 .)

> Added `with_flags(flags_text)` helper to `ParserOptions`

Strictly speaking, there was a subtle difference between the "flag" strings that users were aware of and the "mode" recognised by the parser.

Therefore, it was a common mistake to forget to enable `unicode_mode` when using the `v` flag.

With this helper, crate users no longer need to distinguish between flags and modes.
2024-10-03 02:47:08 +00:00
camchenry
5957214f4c feat(linter): allow fixing in files with source offsets (#6197)
- fixes https://github.com/oxc-project/oxc/issues/5913

This PR fixes the calculation of spans when dealing with files that have multiple sources and non-zero source start offsets. This is almost always the case for `.vue`, `.astro`, and `.svelte` files. This enables us to correctly apply fixes for these file types both in the CLI with `oxlint` and also in editors like VS Code.

https://github.com/user-attachments/assets/2836c8bd-09be-4e59-801d-7c95f8c2491f

I'm open to ideas on how to improve testing in this area, as I don't think that we currently have any tests for fixing files end-to-end (beyond what the linter rules check). I did run this locally on the gitlab repository (which is written in Vue) and all of the fixes appeared to be applied correctly.
2024-10-03 00:53:30 +00:00
Boshen
294da86283
fix(napi/transform): fix index.d.ts 2024-10-03 08:52:10 +08:00
Boshen
099ff3a033 refactor(napi/transform): remove "Binding" from types; fix type error (#6260)
closes #6254
closes #6255
2024-10-03 00:44:07 +00:00
overlookmotel
32d972e2a4 refactor(parser)!: treat unambiguous files containing TS export assignments as modules (#6253)
An "unambiguous" TS source which contains an `export = <value>;` statement should be interpreted as an ES module.

This is in line with [this Babel test case](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous) which has [input option of `sourceType: "unambiguous"`](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/options.json (L2)), and [outputs an AST with `sourceType: "module"`](94e166782a/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/output.json (L7)).
2024-10-03 00:28:00 +00:00
overlookmotel
4f6bc79734 refactor(transformer)!: remove source_type param from Transformer::new (#6251)
Closes #6248.
2024-10-03 00:21:01 +00:00
7086cmd
115ccc941e feat(minifier): bitwise not in exceeded value. (#6235)
```rs
        test("x = ~2147483658.0", "x = 2147483637");
        test("x = ~-2147483658", "x = -2147483639");
```

Used `wrapping_neg`, and maybe it is a great solution for #6161.
2024-10-03 00:16:05 +00:00
Boshen
f98e12c13a feat(napi/transform): add inject plugin (#6250) 2024-10-02 15:15:51 +00:00
Boshen
d085c2cd25
chore: update license copyright holder 2024-10-02 22:37:53 +08:00
overlookmotel
bc757c89b4 refactor(transformer): move functionality of common transforms into stores (#6243)
In common transforms, move all functionality into the `*Store` structs. The `Traverse` impls become just thin wrappers which call into methods on the `*Store` structs.

I think this is clearer because now reading these files from top-to-bottom, you have the code that inserts into the stores before the code that reads from the stores and acts on it.
2024-10-02 14:13:48 +00:00
7086cmd
ee6c85003d feat(minifier): scaffold peephole replace known methods. (#6245) 2024-10-02 14:06:05 +00:00
7086cmd
c32af57e13 feat(minifier): fold demical bitwise not for bigint. (#6233)
Only demical first, because the rest bases are not natively supported by transforming from raw str.
2024-10-02 13:44:20 +00:00
7086cmd
23b646484c feat(minifier): fold true / false comparison. (#6225)
Input:
```js
a == false
```
Previous:
```js
a == !1
```
Current:
```js
a == 0
```

Only handle it when it is non-plus, non-relation binary expressions. Align with [Closure Compiler](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250Ax%2520%253C%253C%2520true%253B%250A%250Ax%2520%252B%2520true%253B%250A%250Ax%2520-%2520true%253B%250A%250Ax%2520%257C%2520true%253B%250A%250Ax%2520%2525%2520true%253B%250A%250Ay%2520!%253D%2520false%253B%250A%250Af()%2520%253D%253D%2520false%253B%250A%250Ax%2520instanceof%2520true%250A%250Ax%2520**%2520true%250A%250Ax%2520%2526%2520true%250A%250Ax%2520%255E%2520false%250A%250Ax%2520%253D%253D%2520(x%2520instanceof%2520false)%250A%250Ax%2520instanceof%2520(x%2520%253C%253C%2520true)%250A%250Ax%2520%253D%253D%2520fake(false)).
2024-10-02 13:37:17 +00:00
leaysgur
acab777c0a refactor(regular_expression): Misc fixes (#6234)
Preparation for #6141

- Keep `enum` size + add size asserts tests
- Arrange AST related directories
- Renaming
2024-10-02 13:32:29 +00:00
overlookmotel
1c31932f03 refactor(transformer): rename var in VarDeclarations common transform (#6242)
Pure refactor. Just rename a variable for clarity.
2024-10-02 11:38:27 +00:00
7086cmd
585ccdad8c feat(minifier): support subtraction assignment. (#6214)
Due to the potential for string concatenation when using the `+=` operator, we should only handle the scenario when using the `-=` operator.
2024-10-02 01:42:56 +00:00
dalaoshu
ea28ee9eda
fix(linter): improve the fixer of prefer-namespace-keyword (#6230)
closes #6204
2024-10-02 09:41:22 +08:00
yoho
a089e19cf9
feat(linter): eslint/no-else-return (#4305) 2024-10-01 19:09:58 -04:00
overlookmotel
0400ff9ea5 refactor(transformer): VarDeclarations common transform: check if at top level with ctx.parent() (#6231)
Micro-optimization. The stack of ancestors is now a `NonEmptyStack`, so `ctx.parent()` is now cheaper than `ctx.ancestors_depth()`.
2024-10-01 22:44:51 +00:00
DonIsaac
a949ecb052 docs(linter): improve docs for eslint/getter-return (#6229) 2024-10-01 19:38:24 +00:00
DonIsaac
d883562741 test(linter): invalid eslint/no-unused-vars options (#6228)
No behavior changes. Just test cases for invalid config objects.
2024-10-01 18:30:01 +00:00
overlookmotel
911e4e58ab ci(minifier): minifier benchmark measure only the minifier itself (#6222)
Minifier benchmark currently includes the time to run parser and semantic as well as the minifier itself. Similar to transformer benchmark, only measure the minifier itself in the benchmark.

This will give us a clearer picture of performance changes, and should also reduce variance in the benchmarks.
2024-10-01 17:43:39 +00:00
overlookmotel
4b42047fef fix(transformer): fix memory leak in ReplaceGlobalDefines (#6224)
`ReplaceGlobalDefines` was putting a `String` into the arena. `String` allocates on the heap, so this was a memory leak because it didn't get dropped when the allocator is dropped. This was caught by Miri.

Allocate a string slice instead.
2024-10-01 15:10:05 +00:00
overlookmotel
cc57541281 refactor(data_structures): NonEmptyStack::len hint that len is never 0 (#6220)
Tiny optimization. Make sure compiler knows that `NonEmptyStack::len` can never return 0.
2024-10-01 14:58:36 +00:00
overlookmotel
147a5d50fd refactor(data_structures): remove is_empty methods for non-empty stacks (#6219)
Remove `SparseStack::is_empty` method. It's pointless as the stack is never empty.

Add a dummy `NonEmptyStack::is_empty` method that always returns `false`. This is also pointless, but it overrides `slice::is_empty` which is otherwise accessible via `Deref`.
2024-10-01 14:58:35 +00:00
overlookmotel
3da0334396
ci(miri): run Miri on changes to more crates (#6223)
Run Miri when changes are made to crates which contain a lot of unsafe
code.
2024-10-01 22:56:05 +08:00
Boshen
291891e71a feat(napi/transform): add define option (#6212)
part of #6156
2024-10-01 12:57:29 +00:00
overlookmotel
61805fd3f2 refactor(data_structures): add debug assertion to SparseStack (#6218)
Just add an additional debug assert.
2024-10-01 12:26:38 +00:00
overlookmotel
adc538141c refactor(traverse): TraverseAncestry use NonEmptyStack (#6217)
#6206 moved stack types into a shared crate. So now we can use it for the stack in `TraverseAncestry`.
2024-10-01 12:26:37 +00:00
overlookmotel
7566c2d66f feat(data_structures): add as_slice + as_mut_slice methods to stacks (#6216)
Add `as_slice` and `as_mut_slice` methods to `Stack` and `NonEmptyStack`.
2024-10-01 12:26:36 +00:00
overlookmotel
dbfa0bc01f refactor(data_structures): add len method to StackCommon trait (#6215)
`len` method is in common between all stack types. Move it into `StackCommon` trait.
2024-10-01 12:26:35 +00:00
overlookmotel
f7d113625e refactor(allocator): remove unnecessary Vec impl (#6213)
`impl<'alloc, T> ops::Index<usize> for &'alloc Vec<'alloc, T>` is unnecessary, as we already have `impl<'alloc, T> ops::Index<usize> for Vec<'alloc, T>`, whose `index` method takes a `&self`.
2024-10-01 10:54:47 +00:00
Boshen
afc3ccbece feat(napi/transform)!: rename TransformOptions::react to jsx. (#6211) 2024-10-01 10:25:21 +00:00
7086cmd
cca0034e8b feat(minifier): handle positive NaN and Infinity. (#6207)
`+NaN` -> `NaN`, `+Infinity` -> `Infinity`.
2024-10-01 10:12:19 +00:00
Boshen
c3c3447ac3 feat(data_structures): add oxc_data_structures crate; add stack (#6206) 2024-10-01 10:02:45 +00:00
overlookmotel
788e444c1f perf(transformer): parse options from comments only once (#6152)
Both React and TypeScript transforms were repeating the same work - iterating through comments to find `@jsx` pragmas.

Instead, perform this search only once, using the optimized pragma search introduced in #6151.
2024-10-01 09:57:46 +00:00
Boshen
54c1c53e69 refactor(napi/transform): remove a call on TransformOptions::clone (#6210) 2024-10-01 09:52:39 +00:00
overlookmotel
235cdba927 refactor(transformer): use AstBuilder instance from TraverseCtx (#6209)
A small modification of #6173. Closes #6172.
2024-10-01 09:27:37 +00:00
overlookmotel
da2b2a4060 perf(transformer): look up SymbolId for require only once (#6192)
When inserting `var x = require('y');` statements in `ModuleImports`, look up the binding for `require` only once, rather than for each statement.
2024-10-01 08:41:45 +00:00
overlookmotel
a7ed29e67a refactor(transformer): insert import statement or require depending on source type (#6191)
We don't need to store `ImportKind` for every import in `ModuleImports`. Whether we generate `import`s or `require`s depends on the source type (module or script), which is a global setting.
2024-10-01 08:20:57 +00:00
overlookmotel
4c63f0eb11 refactor(transformer): rename methods (#6190)
Pure refactor. Rename a few methods when had `file_name` in their name. Convention throughout codebase appears to be `filename` (without the `_`).
2024-10-01 08:10:31 +00:00
overlookmotel
a28926f316 fix(transformer): fix inserting require with front option (#6188)
Fix a tiny bug in `add_require`. If `front` option is `true` but there's already an entry for the key, it may not be the last entry in the index map, so `move_index(len, 0)` could swap a *different* `require` to be first.

This will not make a difference in practice right now as we don't add enough `requires` at present for it to matter. But it might do once we add imports of helpers etc.
2024-10-01 08:02:54 +00:00
Dunqing
e9eeae0f4a fix(isolated-declarations): false positive for function with a type asserted parameters (#6181)
close: #6179
2024-10-01 07:57:49 +00:00
overlookmotel
900cb46f6c refactor(transformer): convert ModuleImports into common transform (#6186)
An alternative version of #6177.

Convert `ModuleImports` into a common transform. Works much as before, but it inserts `import` / `require` statements by passing them to `TopLevelStatements` common transform, so they get inserted in one go with any other inserted top-level statements. This avoids shuffling up the `Vec<Statement>` multiple times, which can be slow with large files.

`VarDeclarations` also inserts any declarations via `TopLevelStatements` but runs after `ModuleImports`, so can control whether a `var` statement is inserted before or after `import` statements by inserting it via `VarDeclarations` (to appear after `import` statements) or directly into `TopLevelStatements` (to appear before `import` statements). Insertion order is not actually important, but allows us to match Babel's output and pass its tests.
2024-10-01 07:40:17 +00:00
overlookmotel
00e28029ae refactor(transformer): introduce TopLevelStatements common transform (#6185)
Introduce `TopLevelStatements` common transform. It holds a `Vec` of statements to be inserted at the top of the program, and other transforms can push statements to it.

All statements will be inserted in one go at the end of traversal, to avoid shuffling up the `Vec<Statement>` multiple times, which can be slow with large files.
2024-10-01 07:40:17 +00:00
7086cmd
dac8f09232 feat(minifier): minify unary plus negation. (#6203)
resolves #6201.
It is uncertain whether this applies to other situations, so let us eliminate it in negation first, and after I have conducted a comprehensive investigation of the Closure Compiler, we can make a definitive decision.
2024-10-01 07:13:39 +00:00
renovate[bot]
7a381ee8f0
chore(deps): update crate-ci/typos action to v1.25.0 (#6200)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [crate-ci/typos](https://redirect.github.com/crate-ci/typos) | action
| minor | `v1.24.5` -> `v1.25.0` |

---

### Release Notes

<details>
<summary>crate-ci/typos (crate-ci/typos)</summary>

###
[`v1.25.0`](https://redirect.github.com/crate-ci/typos/releases/tag/v1.25.0)

[Compare
Source](https://redirect.github.com/crate-ci/typos/compare/v1.24.6...v1.25.0)

#### \[1.25.0] - 2024-10-01

##### Fixes

- Updated the dictionary with the [September
2024](https://redirect.github.com/crate-ci/typos/issues/1107) changes

###
[`v1.24.6`](https://redirect.github.com/crate-ci/typos/releases/tag/v1.24.6)

[Compare
Source](https://redirect.github.com/crate-ci/typos/compare/v1.24.5...v1.24.6)

#### \[1.24.6] - 2024-09-16

##### Fixes

-   Respect negation (`!`) in `extend-exclude`

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/oxc-project/oxc).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC45Ny4wIiwidXBkYXRlZEluVmVyIjoiMzguOTcuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Boshen <boshenc@gmail.com>
2024-10-01 12:42:33 +08:00