Commit graph

158 commits

Author SHA1 Message Date
Boshen
63a830e08c
chore(dprint): format toml files (#5599)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-09-08 14:26:16 +08:00
IWANABETHATGUY
08d2b7d761 feat(oxc_wasm): add controlFlowDotString to oxc_wasm (#5571) 2024-09-07 10:41:00 +00:00
Boshen
b06052501a refactor(semantic)!: remove source_type argument from SemanticBuilder::new (#5553)
Realized we can get the source type from the AST.

The next PR will introduce `unambiguous` to `SourceType` and directly set `Program::source_type` to either `script` or `module`.
2024-09-06 16:40:10 +00:00
Boshen
add146557c
feat(wasm): turn on all transform options 2024-09-01 00:24:10 +08:00
Boshen
cbd9a60194
fix(wasm): do not run symbols and scopes for .d.ts files 2024-08-30 19:57:09 +08:00
Boshen
8ab270b720
feat(wasm): add mangler 2024-08-30 18:45:56 +08:00
Boshen
3ae94b8801
refactor(semantic): change build_module_record to accept &Path instead of PathBuf 2024-08-30 12:24:49 +08:00
Boshen
7fa2fa386c
refactor(wasm): clean up code and add transform (#5299)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-08-28 22:14:35 +08:00
Kevin Deng 三咲智子
681bd8071a
fix(wasm): reference ast from prettier (#5268)
regression by cc3332898c
2024-08-27 22:15:26 +08:00
overlookmotel
c23104b2a2 feat(wasm): output symbol IDs in scope tree (#5266)
`oxc_wasm` include `SymbolId`s in print-out of scopes.
2024-08-27 13:49:44 +00:00
overlookmotel
6a49dc36b2 perf(wasm): generate scope text with visitor (#5264)
Follow-on after #5232. `oxc_wasm` build scopes text with a single AST traversal. Previous implementation was O($n^2$).

If we can assume scopes are listed in traversal order, then we could do it a bit more efficiently just from `ScopeTree`, but this approach of using `Visit` will handle out-of-order scope IDs (which you'd get if printing a post-transform `ScopeTree`).

Also reduce creating and discarding `String`s for indentation - reuse a single string instead.
2024-08-27 13:49:42 +00:00
overlookmotel
17d8a88415
fix(wasm): display correct scope IDs (#5263)
`ScopeId`s were printed incorrectly. `+ 1` used to be correct, but isn't
any more since `ScopeId` became a `NonMaxU32`.
2024-08-27 20:39:07 +08:00
Kevin Deng 三咲智子
cc3332898c
perf(wasm): reuse parse result for prettier (#5254) 2024-08-27 16:56:18 +08:00
Boshen
484f0d7579
fix(wasm): fix broken prettier 2024-08-27 14:25:25 +08:00
Kevin Deng 三咲智子
8b53ef9b27
feat(wasm): add option preserve_parens (#5237) 2024-08-27 12:10:08 +08:00
Boshen
a17cf33dc3 refactor(semantic): remove ScopeTree::child_ids (#5232)
closes https://github.com/oxc-project/oxc/issues/5244
2024-08-27 01:49:47 +00:00
Kevin Deng 三咲智子
1af7f04a36
feat(wasm): output panic info to console (#5238)
Playground Preview:
<img width="1769" alt="image"
src="https://github.com/user-attachments/assets/12143fb5-f6a6-48bc-8dee-cf85b6c52392">
2024-08-27 09:12:15 +08:00
Kevin Deng 三咲智子
30ecbf649b
refactor(wasm): combine into a options (#5235) 2024-08-27 09:11:30 +08:00
Kevin Deng 三咲智子
167f7e452d
fix(wasm): set both scope and symbol (#5236) 2024-08-27 09:10:41 +08:00
Kevin Deng 三咲智子
4641034710
fix(wasm): correct ir type (#5226)
Fixes type error:
https://github.com/oxc-project/playground/actions/runs/10557625323/job/29245568111?pr=22
2024-08-26 21:41:57 +08:00
Kevin Deng 三咲智子
b39c0d6122
refactor(wasm): add source_type for parser, replace class options with plain object (#5217)
- Feat: add `source_type` to `ParserOptions`
- Refactor: use plain objects for options instead of `new
OxcRunOptions()` and `new OxcParserOptions()`, allowing easier
serialization.
2024-08-26 15:08:35 +08:00
Kevin Deng 三咲智子
1cd5401ad7
fix(wasm): respect camelCase (#5213) 2024-08-26 12:51:19 +08:00
Boshen
4b49cf8ce4 feat(transformer): always pass in symbols and scopes (#5087)
We no longer need to build semantic data inside the transformer.

The caller should be responsible for handling semantic data and its
errors.

The best way to achieve this in via `CompilerInterface`.

closes #3565
2024-08-22 16:06:31 +00:00
overlookmotel
f2b8d82499 refactor(semantic)!: ScopeTree::get_child_ids + get_child_ids_mut return value not Option (#5058)
Previously `ScopeTree::get_child_ids` and `ScopeTree::get_child_ids_mut` returned an `Option`. Instead return the unwrapped value, in line with other methods e.g. `get_flags`.
2024-08-22 05:57:55 +00:00
overlookmotel
5f4c9ab38e refactor(semantic)!: rename SymbolTable::get_flag to get_flags (#5030)
Part of #4991.
2024-08-21 00:19:58 +00:00
overlookmotel
ca70cc7c03 refactor(linter, mangler, parser, semantic, transformer, traverse, wasm): rename various flag vars to flags (#5028)
Part of #4991.
2024-08-21 00:19:58 +00:00
Boshen
ce4d4698b4 feat(codegen)!: remove const generic MINIFY (#5001)
This is a premature optimization, makes the code complicated, and bloats the final binary size.

The minify option is moved to `CodegenOptions`
2024-08-20 08:13:27 +00:00
Boshen
b2ff2df5af refactor(parser)!: remove builder pattern from Parser struct (#5000)
part of #4455

use `with_options(ParseOptions { ..ParseOptions::default() })` API instead.
2024-08-20 07:40:25 +00:00
Boshen
e78cba6464 refactor(minifier): ast passes infrastructure (#4625)
After studying google closure compiler, I'm leaning towards a multi-ast-pass infrastructure for the minifier.

This is one of the few places where we are going to trade maintainability over performance, given the goal of the minifier is compression size not performance.

All of the terminologies and separation of concerns are aligned with google closure compiler.

Infrastructure of `terser` and `esbuild` are not suitable for us to study nor pursuit. Their code are so tightly coupled - I failed to comprehend any of them every time I try to walk through a piece of optmization. Google closure compiler despite being written in Java, it's actually the most readable minifier out there.

To improve performance between ast passes, I envision a change detection system over a portion of the code.

The benchmark will demonstrate the performance regression of running 5 ast passes instead of 2.

To complete this PR, I need to figure out "fix-point" and order of these ast passes.
2024-08-04 11:58:39 +00:00
overlookmotel
c99b3eb464 refactor(syntax): give ScopeId a niche (#4468)
Make `ScopeId` a type with a niche, like `SymbolId` and `ReferenceId`. This makes `Option<ScopeId>` 4 bytes instead of 8, and shrinks various AST types e.g. `ArrowFunctionExpression` by 8 bytes, and halves the size of the `Vec` in `ScopeTree::parent_ids`.

The snapshot change on `prefer-hooks-in-order` lint rule appears incidental - it doesn't alter what errors are reported, only the order they're reported in. This appears to be because it changes the order of keys in a hashmap keyed by `ScopeId` that [the rule uses](a49f4915de/crates/oxc_linter/src/rules/jest/prefer_hooks_in_order.rs (L143)).
2024-07-26 00:14:50 +00:00
DonIsaac
7a75e0f8a7 refactor(linter): use diagnostic codes in lint rules (#4349)
> This PR is (unfortunately) quite large, but all changes are needed in tandem for this to work properly.

## What This PR Does

Updates the linter to populate diagnostics reported by rules with error codes statically derived from `RuleMeta` + `RuleEnum`.

Doing so required changing how we handle vitest rules. I know @mysterven was hoping to refactor that part of the code, and I think this approach is an improvement (but could probably be cleaned up further).

## Changes

### 1. Auto-Populate Error Codes
`LintContext` now sets an error code scope + error code number for diagnostics reported by lint rules. `LintContext` will not clobber existing codes set by rules, allowing for rule-specific override behavior (e.g. to use `eslint-plugin-react-hooks` as an error scope).

In order to accomplish this, I had to update every diagnostic factory for every rule. While doing this I found some incorrect error messages, or messages that could be easily improved. This is where a large majority of the snapshot diffs come from. Additionally, I was able to reduce string allocations from `format!` usages in diagnostic factories, especially within jest rules.

### 2. Framework and Library Detection
This PR adds `FrameworkFlags`, which specify what (if any) set of libraries and frameworks are being used by a project and/or file. They are passed in two ways:

1. `LintOptions` can specify a set of `framework_hints` that apply to the entire target codebase. Right now these are always empty, but I'm thinking in the future we could sniff `package.json`. It may be helpful for enabling/disabling default rules.
2. When `Linter` gets run on a file, framework information is sniffed from the `LintContext`. Right now, we are only checking for `vitest` imports in `ModuleRecord` and test path prefixes from `source_path`. It may be useful to do something similar for React/NextJS rules in the future. I know that [next/no-html-link-for-pages](https://nextjs.org/docs/messages/no-html-link-for-pages) could benefit greatly from this.
2024-07-20 03:35:00 +00:00
Boshen
254d5e07e3
fix(wasm): codegen is broken due to preserve_parens(false) 2024-07-11 01:33:10 +08:00
Luca Bruno
5731e3957f
refactor(ast)!: store span details inside comment struct (#4132)
This tweaks `Comment` definition in order to internally store the start
and end position of its span.

Closes: https://github.com/oxc-project/oxc/issues/4069
2024-07-09 23:23:43 +08:00
Boshen
5845057bff refactor(transformer): pass in symbols and scopes (#3978)
This PR adds a new method `build_with_symbols_and_scopes` to make semantic building optional, there may be prior steps that has the semantic data already built.
2024-06-30 06:33:48 +00:00
rzvxa
4d2b7f1227 refactor(linter): LintContext can now only be constructed with a cfg enabled semantic. (#3761)
It has the same spirit as #3747 but with a much simpler approach. I've used the fact that @Boshen mentioned about linter always using CFG so now we assert `semantic.cfg().is_some()` in the `LintContext::new` because of this assertion we can have a `LintContext::cfg` that unwraps unchecked.
Eliminates unnecessary checks in our hot paths.

It has the best of both worlds, No complicated typing yet we still get the CFG as a non-optional value without extra ASM or branching.
2024-06-19 13:01:33 +00:00
Boshen
5c38a0fd69 feat(codegen)!: new code gen API (#3740)
This PR introduces two type alias to avoid the confusing const generic `pub struct Codegen<'a, const MINIFY: bool>`

* CodeGenerator - Code generator without whitespace removal.
* WhitespaceRemover - Code generator with whitespace removal.

Usage is changed to a builder pattern:

```rust
CodeGenerator::new()
  .enable_comment(...)
  .enable_sourcemap(...)
  .build(&program);
```
2024-06-18 15:50:12 +00:00
Wang Wenzhe
4d462636cb
feat(playground): support modify compressor's options (#3734)
![CleanShot 2024-06-18 at
16.18.02@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/nUl2MXIwjUWxxQRgpP2H/37eda403-b5af-44ad-8858-4693e60abbd1.png)
2024-06-18 18:49:16 +08:00
Boshen
534242a729
feat(codegen)!: remove CodegenOptions::enable_typescript (#3674)
The typescript transform pass is now required to strip typescript syntax
for codegen to print things properly.

Codegen will now print whatever is in the AST.
2024-06-14 21:56:00 +08:00
Boshen
b58d8eb88f fix!(codegen): remove the unecessary 4th argument from Codegen::new (#3640) 2024-06-12 07:58:54 +00:00
Boshen
f6752b482f
feat!(ast): make Trivias clonable by adding Arc (#3638)
This makes `Trivias` cloneable and stops us from using `Rc::new` and
`Rc::clone` everywhere.

`Trivias` is rarely cloned so an `Arc` should suffice.
2024-06-12 13:16:10 +08:00
Dunqing
5793ff1986
refactor(transformer): replace &’a Trivias with Rc<Trivias> (#3580)
`Transformer` needs to borrow `Trivias`.
8be1cc8052 (r1630711060)
2024-06-11 13:23:41 +08:00
overlookmotel
fa116448c9 refactor(linter): pass Rc by value (#3587)
Same as #3586.
2024-06-08 11:19:02 +00:00
Boshen
060819894d
chore: crates should only publish src and examples directory 2024-06-08 16:35:16 +08:00
Boshen
9ff4feaa4c
chore: bump oxc-browserslist to 0.17.0 (#3495) 2024-06-01 15:06:34 +08:00
IWANABETHATGUY
0cdb45a1ff
feat(oxc_codegen): preserve annotate comment (#3465)
1. Copy tests from
efa3dd2d8e/internal/bundler_tests/bundler_dce_test.go (L3833-L3971)
2. Add option to preserve annotate comment like `/* #__NO_SIDE_EFFECTS__
*/` and `/* #__PURE__ */`
2024-05-30 15:25:23 +08:00
Boshen
1e84644220
chore: update toml format 2024-05-21 22:15:47 +08:00
Boshen
893af230c0
refactor: clean up more diagnostics usages 2024-05-12 21:46:03 +08:00
Boshen
2064ae9e0a refactor(parser,diagnostic): one diagnostic struct to eliminate monomorphization of generic types (#3214)
part of #3213

We should only have one diagnostic struct instead 353 copies of them, so we don't end up choking LLVM with 50k lines of the same code due to monomorphization.

If the proposed approach is good, then I'll start writing a codemod to turn all the existing structs to plain functions.

---

Background:

Using `--timings`, we see `oxc_linter` is slow on codegen (the purple part).

![image](https://github.com/zkat/miette/assets/1430279/c1df4f7d-90ef-4c0f-9956-2ec3194db7ca)

The crate currently contains 353 miette errors. [cargo-llvm-lines](https://github.com/dtolnay/cargo-llvm-lines) displays

```
cargo llvm-lines -p oxc_linter --lib --release

  Lines                 Copies               Function name
  -----                 ------               -------------
  830350                33438                (TOTAL)
   29252 (3.5%,  3.5%)    808 (2.4%,  2.4%)  <alloc::boxed::Box<T,A> as core::ops::drop::Drop>::drop
   23298 (2.8%,  6.3%)    353 (1.1%,  3.5%)  miette::eyreish::error::object_downcast
   19062 (2.3%,  8.6%)    706 (2.1%,  5.6%)  core::error::Error::type_id
   12610 (1.5%, 10.1%)     65 (0.2%,  5.8%)  alloc::raw_vec::RawVec<T,A>::grow_amortized
   12002 (1.4%, 11.6%)    706 (2.1%,  7.9%)  miette::eyreish::ptr::Own<T>::boxed
    9215 (1.1%, 12.7%)    115 (0.3%,  8.2%)  core::iter::traits::iterator::Iterator::try_fold
    9150 (1.1%, 13.8%)      1 (0.0%,  8.2%)  oxc_linter::rules::RuleEnum::read_json
    8825 (1.1%, 14.9%)    353 (1.1%,  9.3%)  <miette::eyreish::error::ErrorImpl<E> as core::error::Error>::source
    8822 (1.1%, 15.9%)    353 (1.1%, 10.3%)  miette::eyreish::error::<impl miette::eyreish::Report>::construct
    8119 (1.0%, 16.9%)    353 (1.1%, 11.4%)  miette::eyreish::error::object_ref
    8119 (1.0%, 17.9%)    353 (1.1%, 12.5%)  miette::eyreish::error::object_ref_stderr
    7413 (0.9%, 18.8%)    353 (1.1%, 13.5%)  <miette::eyreish::error::ErrorImpl<E> as core::fmt::Display>::fmt
    7413 (0.9%, 19.7%)    353 (1.1%, 14.6%)  miette::eyreish::ptr::Own<T>::new
    6669 (0.8%, 20.5%)     39 (0.1%, 14.7%)  alloc::raw_vec::RawVec<T,A>::try_allocate_in
    6173 (0.7%, 21.2%)    353 (1.1%, 15.7%)  miette::eyreish::error::<impl miette::eyreish::Report>::from_std
    6027 (0.7%, 21.9%)     70 (0.2%, 16.0%)  <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
    6001 (0.7%, 22.7%)    353 (1.1%, 17.0%)  miette::eyreish::error::object_drop
    6001 (0.7%, 23.4%)    353 (1.1%, 18.1%)  miette::eyreish::error::object_drop_front
    5648 (0.7%, 24.1%)    353 (1.1%, 19.1%)  <miette::eyreish::error::ErrorImpl<E> as core::fmt::Debug>::fmt
```

It's totalling more than 50k llvm lines, and is putting pressure on rustc codegen (the purple part on `oxc_linter` in the image above.

---

It's pretty obvious by looking at https://github.com/zkat/miette/blob/main/src/eyreish/error.rs, the generics can expand out to lots of code.
2024-05-11 04:56:22 +00:00
Boshen
a63a45d5b2
refactor(transformer): remove the requirement of Semantic (#3140)
It seems like we need to rebuild the scopes and symbols while
traversing. We can't utilize the scopes and symbols built by semantic
because they are immutable.
2024-04-30 12:48:21 +08:00
Boshen
3419306ac0
feat(transformer): add filename (#2941) 2024-04-11 18:43:51 +08:00