Commit graph

6144 commits

Author SHA1 Message Date
DonIsaac
7c200560c7 perf(regex): reduce string allocations in Display impls (#6528)
There's still room for improvement here.
2024-10-13 19:34:18 +00:00
DonIsaac
2c32dac691 refactor(linter/no-control-regex): remove duplicate code (#6527) 2024-10-13 19:29:39 +00:00
overlookmotel
7cc05f1626 fix(data_structures): fix compilation failure on older Rust versions (#6526)
I thought I was being clever, but too clever = stupid.

The dummy code designed to flag when our MSRV is bumped and supports `NonNull::add` would fail to compile on Rust versions below 1.80.0. Precisely because of the condition this code is testing - it's not supported!

Fix this by skipping compilation unless running clippy.
2024-10-13 17:32:10 +00:00
dalaoshu
97c8a3608f
feat(minifier): implement collapse-variable-declarations (#6464) 2024-10-13 23:01:50 +08:00
camchenry
f960e9ea83 refactor(linter): Add suggested file names for unicorn/filename-case (#6465) 2024-10-13 14:22:15 +00:00
camchenry
d6a0d2e164 fix(linter): fix file name checking behavior of unicorn/filename-case (#6463)
- closes #6459

Currently, the `unicorn/filename-case` doesn't match the behavior of the original rule. There are several issues which this PR fixes:

- The defaults cases were incorrect: only kebab case should be enabled by default, according to the original rule docs.
- Setting a single case or multiple cases did not remove the default cases as it should.
- Leading/trailing underscores were not ignored by default.
- We did not provide a clear diagnostic message indicating which cases are allowed.
- We did not try to parse out multiple file parts (separated by `.`).
  - TODO: We should also support multiple file part checking (which the original rule supports via a config option), for file names such as `someTest.fileName.js`

I have also added many of the original test cases to ensure we are more closely compatible.

This also improves the performance of just running the `unicorn/filename-case` alone by 4% (20ms total) on the `vscode` codebase:

```
Benchmark 1: ./oxlint-main -A all -W unicorn/filename-case --silent vscode
  Time (mean ± σ):     489.4 ms ±  24.0 ms    [User: 2623.8 ms, System: 447.2 ms]
  Range (min … max):   474.7 ms … 622.9 ms    100 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Benchmark 2: ./oxlint-new-filename-case -A all -W unicorn/filename-case --silent vscode
  Time (mean ± σ):     470.8 ms ±  22.9 ms    [User: 2478.8 ms, System: 463.5 ms]
  Range (min … max):   455.6 ms … 599.3 ms    100 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Summary
  ./oxlint-new-filename-case -A all -W unicorn/filename-case --silent vscode ran
    1.04 ± 0.07 times faster than ./oxlint-main -A all -W unicorn/filename-case --silent vscode
```

Perplexingly, it seems like it might actually be even faster on the `vscode` repository, saving ~5% (70ms) with the default ruleset enabled as well. Maybe my laptop was just running a bit faster.

```
Benchmark 1: ./oxlint-main -W unicorn/filename-case --silent vscode
  Time (mean ± σ):      1.402 s ±  0.096 s    [User: 8.863 s, System: 0.505 s]
  Range (min … max):    1.318 s …  1.920 s    100 runs

  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

Benchmark 2: ./oxlint-new-filename-case -W unicorn/filename-case --silent vscode
  Time (mean ± σ):      1.339 s ±  0.042 s    [User: 8.582 s, System: 0.511 s]
  Range (min … max):    1.266 s …  1.506 s    100 runs

Summary
  ./oxlint-new-filename-case -W unicorn/filename-case --silent vscode ran
    1.05 ± 0.08 times faster than ./oxlint-main -W unicorn/filename-case --silent vscode
```
2024-10-13 14:22:14 +00:00
camchenry
725f9f6eb0 perf(linter): get fewer parent nodes in unicorn/prefer-dom-node-text-content (#6467)
Profiling showed that this rule was one of the slower ones, partially due to the fact that it was constantly calling `ctx.nodes()` and `ctx.nodes().parent_node()`. I've tried to fix this by reordering the logic so that we only fetch the parent nodes right before we need it. If we can early return, such as the identifier name not being `innerText`, or the node kind not being right, then we can skip fetching the parent.

Before:

<img width="1147" alt="Screenshot 2024-10-12 at 3 05 05 AM" src="https://github.com/user-attachments/assets/4a551b04-370f-4ed9-b0d5-99d58b03235b">

After (-0.4% reduction in number of samples spent in this rule):

<img width="1146" alt="image" src="https://github.com/user-attachments/assets/340edc36-55cc-4bf4-aa8e-9a600848b798">
2024-10-13 14:17:08 +00:00
Boshen
6d041fb469 refactor(ecmascript): remove NumberValue (#6519) 2024-10-13 14:11:37 +00:00
overlookmotel
51fc63da45 refactor(codegen): rename CodeBuffer::print_bytes_unchecked method (#6517) 2024-10-13 13:14:49 +00:00
overlookmotel
782f0a7a69 refactor(codegen)!: rename print_char method to print_ascii_byte (#6512)
`print_ascii_byte` is more descriptive of what this method does, and makes clear the invariant that `byte` must be ASCII.
2024-10-13 12:51:37 +00:00
overlookmotel
05a2ebd03c refactor(codegen): reorder dependencies in Cargo.toml (#6514) 2024-10-13 12:37:20 +00:00
overlookmotel
235d357600 docs(codegen): improve doc comments for CodeBuffer (#6511) 2024-10-13 12:21:39 +00:00
overlookmotel
e7f3e28076 refactor(codegen): rename var in CodeBuffer (#6510) 2024-10-13 11:58:39 +00:00
Boshen
39c2e66b0b feat(ecmascript): add ToBigInt and StringToBigInt (#6508) 2024-10-13 11:26:07 +00:00
overlookmotel
1bbd383d0f refactor(codegen): rename CodeBuffer::print_ascii_bytes method (#6507)
Pure refactor.
2024-10-13 11:19:09 +00:00
overlookmotel
cd9fe9ec03 refactor(codegen): rename vars in CodeBuffer methods (#6506)
Pure refactor. Consistently use `byte` for bytes, and `ch` for `char`s.
2024-10-13 11:10:10 +00:00
overlookmotel
c8fa2eb17a docs(codegen): correct and reformat doc comments for CodeBuffer (#6504)
Comment about `take_source_text` was erroneous. Clean up and reformat other doc comments.
2024-10-13 11:10:09 +00:00
Boshen
6f2253886d feat(ecmascript): add ToBoolean, ToNumber, ToString (#6502) 2024-10-13 11:03:08 +00:00
overlookmotel
fc536a5648 refactor(codegen): inline CodeBuffer methods (#6501)
These methods are all either trivial, or delegate to another method.
2024-10-13 10:55:07 +00:00
overlookmotel
40d1ee4588 docs(codegen): fix and reformat CodeBuffer examples (#6499)
Fix examples which would not compile.
2024-10-13 10:49:37 +00:00
overlookmotel
c45723be77 refactor(parser): fix typo in var name (#6500) 2024-10-13 10:14:37 +00:00
overlookmotel
74206204dd refactor(codegen): add CodeBuffer::as_bytes method (#6498)
`as_bytes` is clearer than `as_ref` (what kind of ref?)
2024-10-13 10:05:10 +00:00
overlookmotel
d816b0bd0e test(codegen): add test for CodeBuffer::print_byte_unchecked (#6497)
Add test, and rename the rest of the test functions for `CodeBuffer`.
2024-10-13 09:51:50 +00:00
overlookmotel
8ae174bd36 refactor(codegen): rename CodeBuffer::print_byte_unchecked method (#6496)
Pure refactor. `*_unchecked` is the conventional name for unsafe methods, not `*_unsafe`.
2024-10-13 09:51:49 +00:00
overlookmotel
5843e01bed refactor(codegen): shorten CodeBuffer::take_source_text (#6495)
Pure refactor.
2024-10-13 09:51:47 +00:00
overlookmotel
951def6e35 refactor(codegen): clarify safety comments in CodeBuffer (#6494) 2024-10-13 09:36:40 +00:00
overlookmotel
84a51ee576 refactor(codegen): rename vars in CodeBuffer (#6493)
Pure refactor. Just rename vars.
2024-10-13 09:36:39 +00:00
overlookmotel
05bd616d88 refactor(codegen): add line break (#6492) 2024-10-13 09:27:42 +00:00
DonIsaac
204bf5533e refactor(codegen): add CodeBuffer to fix soundness hole (#6148)
# What This PR Does

Adds `CodeBuffer`, a simple wrapper over a `Vec<u8>` with a protective and reduced API for upholding UTF-8 validity guarantees. Closes #6147.

Note that this struct is actually quite small. Most of the added lines are doc comments.
2024-10-13 09:14:48 +00:00
Dunqing
8fe1b0a0f8 feat(transformer): support helper loader (#6162)
Close: #4753
2024-10-13 08:09:08 +00:00
Dunqing
ab51c2ae90 feat(transformer): support DefaultImport in ModuleImports (#6434)
Support import like `import React from 'react';` by `ModuleImports`

The usage is:
```rs
 self.ctx.module_imports.add_import(
     Atom::from("react"),
     ImportKind::new_default(Atom::from("React")),
     symbol_id
)
2024-10-13 08:09:07 +00:00
Dunqing
02bfbfeef5 fix(codegen): preserve parenthesis for ChainExpression (#6430)
close: #6385

The fixing way was referenced from `esbuild`, it is according to these [states](332727499e/internal/js_ast/js_ast.go (L590-L604)
)(determined in the parser) to determine whether to print parenthesis

Due to differences in implementation details, we are unable to record certain information like `esbuild` does in its parser. But fortunately, The `ParenthesisExpression` AST is actually like what `esbuild` stored states.
2024-10-13 08:03:37 +00:00
Boshen
c56343d1f3 feat(linter): promote no_unsafe_optional_chaining to correctness (#6491)
It's in the eslint recommended list https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining
2024-10-13 07:58:01 +00:00
IWANABETHATGUY
1a93033b7b feat(oxc_language_server): add diagnostic code with link (#6487)
1. follow #6479
2. Adding related doc link to lsp diagnostic code.
## Preview
![image](https://github.com/user-attachments/assets/dad5b6a2-787a-43c3-b308-ebe74e38e21c)

**after click**
![image](https://github.com/user-attachments/assets/5b725fcd-cacd-4779-9b2a-15824d7c0cf7)

The UI looks a little wired, due to aba9ea3f1d/crates/oxc_diagnostics/src/lib.rs (L113-L120),

I suggest change the code above to
```rs
format!("{scope}/{number}")
```
2024-10-13 07:07:43 +00:00
camc314
2ade16ece5 fix(codegen): invalid codegen when in inside bin expr in or loop (#6431)
https://github.com/oxc-project/monitor-oxc/actions/runs/11278829870 ??
2024-10-13 06:47:10 +00:00
camc314
6896efc00c fix(codegen): fix in in sequence expr in for loops (#6428)
not sure if this is right, but it doesn't break any existing tests so hopefully?
2024-10-13 06:47:09 +00:00
camc314
096e590483 feat(minifier): implement folding charAt string fns (#6436) 2024-10-13 06:41:05 +00:00
camc314
e5a6f5de6a feat(minifier): implement converting template literals to strings (#6486)
basically:
```
`hello ${"world"}`
```
 now becomes:
```
"hello world"
```
2024-10-13 06:26:32 +00:00
camc314
14d0590b0b feat(minifier): implement folding of simple function calls (Boolean) (#6484)
Basically `Boolean(true)` -> `true` or `Boolean(foo)` -> `!!foo`
2024-10-13 06:26:32 +00:00
camc314
7fbc7b6dae feat(minifier): implement folding of simple function calls (String) (#6483)
basically `String(foo)` -> `foo + ''`
2024-10-13 06:26:31 +00:00
Tapan Prakash
bae5b7da27
chore(editors/vscode): Remove Prettier dependency entirely (#6480)
Previous PR removed the Prettier commands
(https://github.com/oxc-project/oxc/pull/6412), but the prettier
dependency was not fully removed. This PR aims to remove the dependency
from package.json entirely.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-10-13 14:25:49 +08:00
DonIsaac
58467a53a1 feat(parser): better handling of invalid modifiers (#6482)
## What This PR Does

1. Recover on, and provide a better message for, invalid `export` modifier on constructor parameters. Before, an `unexpected token` error would be produced and the parser would panic. Now, the parser recovers and produces a message saying `'export' modifier cannot appear on a parameter.`
  ```ts
class Foo {
    constructor(export x: number) {}
}
  ```

2. Recover on, and provide a better message for, invalid modifiers on index signatures. Same recovery/message characteristics as above.
  ```ts
class Foo {
    public [x: string]: string;
}
```
2024-10-13 03:16:02 +00:00
DonIsaac
8ea6b721b8 feat(parser): better errors for reserved words used as identifier names (#6478)
## What This PR Does
Provide better error messages when a reserved word is used as a `BindingIdentifier`
```ts
var const = 1;
export enum const {}
const if = 1;
```
2024-10-13 03:16:01 +00:00
IWANABETHATGUY
66dccc0584
feat(oxc_language_server): add related code for lsp_types::Diagnostic#source (#6479)
Closed #6477
# Before
# After

![image](https://github.com/user-attachments/assets/20377c2f-ba94-4fb5-a21c-fbd269f40385)

![image](https://github.com/user-attachments/assets/fae901b3-9753-4610-8b36-61974781f770)

Compare to eslint: 

![image](https://github.com/user-attachments/assets/0037b265-0a84-4eae-8bc9-4cbb66197485)

we could polish in the feature and link to
`lsp_types::Diagnostic#source`
2024-10-13 01:53:05 +08:00
dalaoshu
0784e74791
fix(linter): error fixer of switch-case-braces (#6474)
Related to #6466 

This PR does not fundamentally solve the problem, we need to implement
`raw` or `value` for `RegExpLiteral`.


1ba2a247e1/crates/oxc_codegen/src/gen.rs (L1194-L1198)


1ba2a247e1/crates/oxc_ast/src/ast/literal.rs (L94-L101)
2024-10-13 00:15:08 +08:00
Boshen
bbca743689 refactor(minifier): move string methods to oxc_ecmascript (#6472) 2024-10-12 09:40:43 +00:00
Boshen
856cab5000 refactor(ecmascript): move ToInt32 from oxc_syntax to oxc_ecmascript (#6471) 2024-10-12 09:29:46 +00:00
Boshen
1ba2a247e1 refactor(ecmascript): remove HasProto which is not part of the spec (#6470) 2024-10-12 08:48:45 +00:00
Boshen
455dab4900 chore(oxc): rename crate oxc_syntax_operations to oxc_ecmascript (#6469) 2024-10-12 08:34:42 +00:00
7086cmd
702c049ebc refactor(minifier): move compress block to dce (#6468) 2024-10-12 07:59:44 +00:00