Commit graph

7965 commits

Author SHA1 Message Date
翠 / green
6589c3bbb3
feat(mangler): reuse variable names (#8562)
Changed the mangler to reuse variable names where possible.

This will reduce the code size as shorter variable names can be used in
more places. But requires global information and limits parallelism in a
single file and requires more memory.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-25 14:00:24 +08:00
Boshen
32e0e4796c
refactor(minifier): clean up Normalize (#8700) 2025-01-25 13:52:35 +08:00
Boshen
8587965e45
perf(minifier): normalize undefined to void 0 before everything else (#8699)
so subsequent code don't need to lookup `undefined`.
2025-01-25 11:50:08 +08:00
Sysix
34d3d72b64 test(linter): add snapshot tester for cli (#8695)
Our first Snapshot 🥳
2025-01-24 17:19:31 +00:00
Sysix
741fb40a53 refactor(linter): move stdout outside LintRunner (#8694)
This is needed so we can use a custom `Write` implementation (or just `[u8]`) to make snapshots.
In this step I also updated `OutputFormatter::all_rules` so the Formatter does not need to handle the write-error.
2025-01-24 17:05:19 +00:00
Boshen
b7f13e636e feat(ast): implement utf8 to utf16 span converter (#8687)
closes #8629
2025-01-24 16:57:44 +00:00
Sysix
10e59209ef refactor(linter): move finishing default diagnostic message to GraphicalReporter (#8683)
Now every lint output is owned by is right OutputFormatter and his DiagnosticReporter 🥳
Next step is to setup a snapshot Tester, so I can remove the ToDos.

Reorded some lines so the outfor is now for: `cargo run -p oxlint -- test.js --max-warnings=2`
```
Found 4 warnings and 0 errors.
Exceeded maximum number of warnings. Found 4.
Finished in 5ms on 1 file with 97 rules using 24 threads.
```

and for `cargo run -p oxlint -- test.js`

```
Found 4 warnings and 0 errors.
Finished in 5ms on 1 file with 97 rules using 24 threads.
```

The output time and warnings/error count wil be always printed.
2025-01-24 16:39:17 +00:00
oxc-bot
b97767874f
release(oxlint): v0.15.8 (#8689)
## [0.15.8] - 2025-01-24

### Features

- 79ba9b5 linter: Added support to run in Node.JS legacy versions
(#8648) (Luiz Felipe Weber)
- dcaebe6 linter: Add "strict" option to `promise/prefer-await-to-then`
rule (#8674) (Neil Fisher)
- 4ae568e linter: Add DiagnosticResult to the Reporters for receiving a
sub part result (#8666) (Alexander S.)
- 8a0eb2a oxlint: Add stylish formatter (#8607) (Andrew Powell)

### Bug Fixes

- 40316af linter: Fix github `endColumn` output (#8647) (Alexander S.)
- dc912fa linter: Added missing $schema property to default config
(#8625) (Tapan Prakash)

### Refactor

- a3dc4c3 crates: Clean up snapshot files (#8680) (Boshen)
- e66da9f isolated_declarations, linter, minifier, prettier, semantic,
transformer: Remove unnecessary `ref` / `ref mut` syntax (#8643)
(overlookmotel)
- 23b49a6 linter: Use `cow_to_ascii_lowercase` instead
`cow_to_lowercase` (#8678) (Boshen)
- b8d9a51 span: Deal only in owned `Atom`s (#8641) (overlookmotel)
- ac4f98e span: Derive `Copy` on `Atom` (#8596) (branchseer)
- 259a47b vscode: Move commands and `findBinary` to separate files
(#8605) (Alexander S.)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-01-24 18:07:14 +08:00
Boshen
4f6d164f52
ci: fix release napi scripts 2025-01-24 13:37:01 +08:00
oxc-bot
8a72b8ecc7
release(crates): v0.48.0 (#8686)
## [0.48.0] - 2025-01-24

- 54d0fac span: [**BREAKING**] Remove `PartialEq` impl for `&Atom`
(#8642) (overlookmotel)

### Features

- 2a2ad53 allocator: Add `Allocator::capacity` and `used_bytes` methods
(#8621) (overlookmotel)
- 6801c81 allocator: Add `Allocator::new` and `with_capacity` methods
(#8620) (overlookmotel)
- 99607d3 codegen: Print comments in `TSTypeLiteral` (#8679) (Boshen)
- 4ae568e linter: Add DiagnosticResult to the Reporters for receiving a
sub part result (#8666) (Alexander S.)
- 343690e minifier: Replace `Number.*_SAFE_INTEGER`/`Number.EPSILON`
(#8682) (sapphi-red)
- 0c5bb30 minifier: Replace
`Number.POSITIVE_INFINITY`/`Number.NEGATIVE_INFINITY`/`Number.NaN`
(#8681) (sapphi-red)
- 835b258 minifier: Compress `typeof foo === 'object' && foo !== null`
to `typeof foo == 'object' && !!foo` (#8638) (sapphi-red)
- 2bcbed2 minifier: Compress `(a = b) === null || a === undefined` to
`(a = b) == null` (#8637) (sapphi-red)

### Bug Fixes

- 40316af linter: Fix github `endColumn` output (#8647) (Alexander S.)
- 883d25b minifier: Keep esm in dce (#8677) (Boshen)
- 878ce10 minifier: `void 0` equals to `undefined` (#8673) (Boshen)
- ba201a6 minifier: Remove "non esbuild optimizations" which is
incorrect (#8668) (Boshen)
- 8c8b5fa minifier: Avoid minifing `String(a)` into `"" + a` for symbols
(#8612) (翠 / green)
- 4ff6e85 minifier: Remove expression statement `void 0` (#8602)
(Boshen)
- 93d643e minifier: Keep side effects when folding const conditional
exprs (#8591) (camc314)
- 178c232 parser: Parse `intrinsic` TS keyword (#8627) (Kevin Deng 三咲智子)
- 48717ab parser: Parse `true` as `TSLiteralType` (#8626) (Kevin Deng
三咲智子)
- d1c5dc4 semantic: Fix const assertions in `UnresolvedReferencesStack`
(#8653) (overlookmotel)

### Performance

- 787aaad allocator: Make `String` non-drop (#8617) (overlookmotel)
- d966e0a codegen: Do not check for comments if turned off (#8598)
(Boshen)
- 3fa87ff lexer: Peak 2 bytes after `!` (#8662) (Boshen)
- 9953ac7 minifier: Add `LatePeepholeOptimizations` (#8651) (Boshen)
- 00dc63f minifier: Only substitute typed array constructor once (#8649)
(Boshen)
- 3e19e4e minifier: Remove the useless empty statement removal code in
statement fusion (#8646) (Boshen)
- 5b3c412 minifier: Only run optimizations on local changes (#8644)
(Boshen)

### Documentation

- c1d243b allocator: Improve docs for `Allocator` (#8623)
(overlookmotel)
- 01a5e5d allocator: Improve docs for `HashMap` (#8616) (overlookmotel)
- 87568a1 allocator: Reformat docs (#8615) (overlookmotel)
- 3be0392 lexer: Fix doc comment (#8664) (overlookmotel)
- 5029547 semantic: Fix and reformat doc comments (#8652)
(overlookmotel)

### Refactor

- ae8db53 allocator: Move `Allocator` into own module (#8656)
(overlookmotel)
- 0f85bc6 allocator: Reduce repeat code to prevent `Drop` types in arena
(#8655) (overlookmotel)
- de76eb1 allocator: Reorder `Box` methods (#8654) (overlookmotel)
- 997859c ast: Align `#[estree(via)]` behavior (#8599) (sapphi-red)
- db863a3 codegen: Use `Stack` for `binary_expr_stack` (#8663) (Boshen)
- 8cce69a codegen: Remove `match_member_expression` (#8597) (Boshen)
- a3dc4c3 crates: Clean up snapshot files (#8680) (Boshen)
- e66da9f isolated_declarations, linter, minifier, prettier, semantic,
transformer: Remove unnecessary `ref` / `ref mut` syntax (#8643)
(overlookmotel)
- 23b49a6 linter: Use `cow_to_ascii_lowercase` instead
`cow_to_lowercase` (#8678) (Boshen)
- ce2b9da minifier: Remove `wrap_to_avoid_ambiguous_else` (#8676)
(Boshen)
- 75a579b minifier: Clean up
`has_no_side_effect_for_evaluation_same_target` (#8675) (Boshen)
- 1bb2539 minifier: Move more code into `minimize_conditions` local loop
(#8671) (Boshen)
- 13e4a45 minifier: Move conditional assignment to `minimize_conditions`
(#8669) (Boshen)
- ae895d8 minifier: Use `NonEmptyStack` for function stack (#8661)
(Boshen)
- 3802d28 minifier: Clean up `try_minimize_conditional` (#8660) (Boshen)
- dcc1f2b minifier: Rename `ast_passes` to `peephole` (#8635) (Boshen)
- 52458de minifier: Remove unused code and traits (#8632) (Boshen)
- 6f95cd5 minifier: Remove all the unnecessary fake ast passes (#8618)
(Boshen)
- 712cae0 minifier: Run the compressor on all test cases (#8604)
(Boshen)
- 864b8ef parser: Shorten code (#8640) (overlookmotel)
- b8d9a51 span: Deal only in owned `Atom`s (#8641) (overlookmotel)
- 20f52b1 span: Remove unnecessary lifetimes on `Atom` impls (#8639)
(overlookmotel)
- ac4f98e span: Derive `Copy` on `Atom` (#8596) (branchseer)
- a730f99 transformer: Move `create_prototype_member` to utils module
(#8657) (Dunqing)
- 61d96fd transformer/class-properties: Correct comments (#8636)
(overlookmotel)

### Testing

- 39dbd2d codegen: Fix snapshot file (#8685) (Boshen)
- d9f5e7f minifier: Enable passed esbuild tests (Boshen)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-01-24 12:09:37 +08:00
Dunqing
233dc07738 feat(ast): derive Clone for TemplateElement and TemplateElementValue (#8658)
In #8614, we need to duplicate the same template literal, and derive `Copy` and `Clone` to avoid using `CloneIn`.
2025-01-24 03:58:33 +00:00
Boshen
39dbd2d472
test(codegen): fix snapshot file (#8685) 2025-01-24 11:58:02 +08:00
sapphi-red
343690e178
feat(minifier): replace Number.*_SAFE_INTEGER/Number.EPSILON (#8682)
The value of `Number.*_SAFE_INTEGER`, `Number.EPSILON` are constants as they cannot be changed. This PR replaces them with `2**53-1` / `-(2**53-1)` / `2**-52` for ES2016+. For ES2015, `Number.EPSILON` is not changed but `Number.*_SAFE_INTEGER`s are replaced with `9007199254740991` / `-9007199254740991`.

**Reference**
- Spec of [`Number.MAX_SAFE_INTEGER`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.max_safe_integer)
- Spec of [`Number.MIN_SAFE_INTEGER`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.min_safe_integer)
- Spec of [`Number.EPSILON`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.epsilon)

### Additional Information
- [`Number.MIN_VALUE`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.min_value) cannot be replaced as the value depends on the runtime
- [`Number.MAX_VALUE`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.max_value) can be replaced but I didn't come up with a shorter representation that does not lack precision
2025-01-24 03:47:52 +00:00
sapphi-red
0c5bb30859
feat(minifier): replace Number.POSITIVE_INFINITY/Number.NEGATIVE_INFINITY/Number.NaN (#8681)
The value of `Number.POSITIVE_INFINITY`, `Number.NEGATIVE_INFINITY`, `Number.NaN` are constants as they cannot be changed. This PR replaces them with `Infinity`/`-Infinity`/`NaN`.

**Reference**
- Spec of [`Number.POSITIVE_INFINITY`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
- Spec of [`Number.NEGATIVE_INFINITY`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
- Spec of [`Number.NaN`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
2025-01-23 15:26:22 +00:00
Boshen
a3dc4c3cbe refactor(crates): clean up snapshot files (#8680) 2025-01-23 14:43:32 +00:00
Boshen
99607d3f82 feat(codegen): print comments in TSTypeLiteral (#8679)
fixes #8665
2025-01-23 14:31:55 +00:00
Boshen
23b49a665f refactor(linter): use cow_to_ascii_lowercase instead cow_to_lowercase (#8678)
`cow_to_lowercase` is slow as it deals with unicode.

closes #8659
2025-01-23 13:46:52 +00:00
Boshen
883d25b27d fix(minifier): keep esm in dce (#8677)
fixes https://github.com/rolldown/rolldown/issues/3402
2025-01-23 13:30:41 +00:00
Boshen
ce2b9da5c7 refactor(minifier): remove wrap_to_avoid_ambiguous_else (#8676) 2025-01-23 13:20:32 +00:00
Luiz Felipe Weber
79ba9b5970
feat(linter): added support to run in Node.JS legacy versions (#8648)
With a few adjustments It is possible to run _oxlint_ in projects which
use legacy **Node.js** versions, such as **8.x.x**. Basically this is
possible by removing spread operator in 'binpath' environment assemble
and changing a catch command to inform an error variable.

Without this modification, we got always this error:

```
node_modules/oxlint/bin/oxlint:23
  } catch {
          ^

SyntaxError: Unexpected token {
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3
```
2025-01-23 21:17:48 +08:00
Neil Fisher
dcaebe69b1
feat(linter): Add "strict" option to promise/prefer-await-to-then rule (#8674)
Implementation of the strict option [specified in the eslint docs for
`promise/prefer-await-to-then`](https://github.com/eslint-community/eslint-plugin-promise/blob/main/docs/rules/prefer-await-to-then.md)

```js
// Examples of **incorrect** code with `{ strict: true }`:
async function hi() {
  await thing().then(x => {})
}
```
2025-01-23 13:13:50 +00:00
Boshen
75a579b619
refactor(minifier): clean up has_no_side_effect_for_evaluation_same_target (#8675) 2025-01-23 20:55:16 +08:00
Boshen
3fa87ff6a6
perf(lexer): peak 2 bytes after ! (#8662)
`!==` and `!=` is very frequent.

<img width="845" alt="image"
src="https://github.com/user-attachments/assets/91ff20fc-ae1c-4fb9-9444-4eb90d8e95f3"
/>

Picked this up while profiling.
2025-01-23 18:19:03 +08:00
Boshen
878ce10296 fix(minifier): void 0 equals to undefined (#8673) 2025-01-23 08:27:47 +00:00
Boshen
1bb2539d64 refactor(minifier): move more code into minimize_conditions local loop (#8671) 2025-01-23 07:03:42 +00:00
Yuji Sugiura
a529412fe7
refactor(prettier): Verify printing module exports (#8670)
Part of #5068 

- ExportAllDeclaration
- ExportNamedDeclaration
- ExportDefaultDeclaration
- (ExportNamespaceSpecifier = ExportAllDeclaration+exported)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-23 14:54:06 +08:00
Boshen
13e4a4593f refactor(minifier): move conditional assignment to minimize_conditions (#8669) 2025-01-23 04:42:44 +00:00
Boshen
ba201a6511 fix(minifier): remove "non esbuild optimizations" which is incorrect (#8668) 2025-01-23 03:45:42 +00:00
Alexander S.
4ae568e8c9
feat(linter): add DiagnosticResult to the Reporters for receiving a sub part result (#8666)
We are currently outputting only for the default-outputter some extra
information:


3be03926e8/apps/oxlint/src/result.rs (L61-L87)

My goal is that all information will be passed to our new
DiagnosticReporter / OutputFormatter.
This will break the output format in the next PR. **Merging this PR is
the OK for me to make this change** ⚠️
The only breaking point:
`"Found {number_of_warnings} warning{} and {number_of_errors} error{}."`
will still be outputted when `max_warnings_exceeded` is true.

Because this is something the `DiagnosticReporter` should do and not the
`OutputFormatter`.

The end goal is:
- no `println!`, our `OutputFormatter` and his `DiagnosticReporter` will
return `Option<String>` and we output it the our `stdout`
- `LintResult` will only handle `ExitCode` result and nothing more
- `stdout` can be changed from outside (for the next part)
- add snapshots with a custom `stdout`

I do not know if all goals can be done easily. Last two parts should be
a bit tricky for me, never did test setups in rust.
But we do never stop to learn ;)
2025-01-23 10:09:51 +08:00
Boshen
db863a35bc
refactor(codegen): use Stack for binary_expr_stack (#8663)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-01-23 10:08:21 +08:00
Yuji Sugiura
1e9f3857ab
refactor(prettier): Verify printing module imports (#8633)
Part of #5068 

- [x] ImportDeclaration
- [x] ImportSpecifier
- [x] ImportDefaultSpecifier
- [x] ImportNamespaceSpecifier
- [x] WithClause
- [x] ImportAttribute
2025-01-23 09:58:44 +08:00
overlookmotel
3be03926e8 docs(lexer): fix doc comment (#8664)
The comment showing expansion of `ascii_byte_handler!` macro was inaccurate.
2025-01-22 16:22:32 +00:00
Boshen
ae895d8c0e refactor(minifier): use NonEmptyStack for function stack (#8661) 2025-01-22 16:04:36 +00:00
Dunqing
a730f999a8 refactor(transformer): move create_prototype_member to utils module (#8657)
This function can also used in #8614, so move it.
2025-01-22 15:51:28 +00:00
Boshen
3802d28233 refactor(minifier): clean up try_minimize_conditional (#8660) 2025-01-22 15:17:47 +00:00
overlookmotel
ae8db53d65 refactor(allocator): move Allocator into own module (#8656)
Pure refactor. `oxc_allocator` crate is getting quite crowded now. Move `Allocator` into a separate file.
2025-01-22 13:45:32 +00:00
overlookmotel
0f85bc6918 refactor(allocator): reduce repeat code to prevent Drop types in arena (#8655)
Pure refactor. Reduce repeated code.
2025-01-22 13:17:58 +00:00
overlookmotel
d1c5dc4d12 fix(semantic): fix const assertions in UnresolvedReferencesStack (#8653)
The `_SIZE_CHECK` assertion was ignored, because `const`s are only evaluated if they're referenced in a function which is called. Fix that by referencing the const in `UnresolvedReferencesStack::new`.

Also add more assertions to cover all the invariants.
2025-01-22 13:13:15 +00:00
overlookmotel
de76eb1b90 refactor(allocator): reorder Box methods (#8654)
Pure refactor. Move `Box::unbox` down. `Box::new_in` is the most important method, so should be at the top.
2025-01-22 12:29:56 +00:00
overlookmotel
50295474cc docs(semantic): fix and reformat doc comments (#8652)
Update doc comment for `TempUnresolvedReferences` which was out of date. Reformat other comments.
2025-01-22 12:22:27 +00:00
Boshen
9953ac7cad
perf(minifier): add LatePeepholeOptimizations (#8651)
This PR adds a `LatePeepholeOptimizations` pass for minifications that
don't interact with the fixed point loop.

While working on this I found a couple of cases where the previous fixed
point loop is not idempotent.
2025-01-22 16:19:06 +08:00
Boshen
00dc63f6a5 perf(minifier): only substitute typed array constructor once (#8649) 2025-01-22 02:49:52 +00:00
Alexander S.
40316afa7e
fix(linter): fix github endColumn output (#8647)
added `start` and `end` for `Info`, so every reporter can use both if
they want.
Then end calculation is a bit hacky, but i looks like it works.
2025-01-22 09:10:40 +08:00
Tapan Prakash
dc912fa58e
fix(linter): Added missing $schema property to default config (#8625)
The $schema property was not added when the --init command was used to
create the configuration. Now, $schema is added based on the
availability of configuration_schema.json in the current working
directory.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-22 09:09:39 +08:00
Boshen
3e19e4e004
perf(minifier): remove the useless empty statement removal code in statement fusion (#8646) 2025-01-22 09:08:56 +08:00
Boshen
5b3c412e26
perf(minifier): only run optimizations on local changes (#8644)
Previously all code are ran in a fixed point loop when ast changes.

This PR changes running code when a function changes its enclosing ast only.
2025-01-21 23:55:54 +08:00
camc314
b75d4919ee chore(linter): update doc comment to reflect existance of stylish formatter (#8645)
after:
```
Output
    -f, --format=ARG          Use a specific output format (default, json, unix, checkstyle, github,
                              stylish)
```
2025-01-21 15:38:15 +00:00
sapphi-red
835b25889b
feat(minifier): compress typeof foo === 'object' && foo !== null to typeof foo == 'object' && !!foo (#8638)
If `typeof foo == 'object'`, then `foo` is guaranteed to be an object or null. In that case, `foo !== null` can be replaced with `!!foo` because objects return `true` for `!!foo` and null returns `false` for it.

**References**
- [Spec of `typeof`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator)
- [Spec of `!`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-logical-not-operator)
- [Spec of `ToBoolean`](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toboolean)
2025-01-21 14:59:25 +00:00
sapphi-red
2bcbed2d50
feat(minifier): compress (a = b) === null || a === undefined to (a = b) == null (#8637) 2025-01-21 14:59:23 +00:00
overlookmotel
864b8efe1a refactor(parser): shorten code (#8640)
Pure refactor. Shorten code by using `AstBuilder` methods which produce the desired type in a single call.
2025-01-21 14:36:00 +00:00