Commit Graph

49 Commits

Author SHA1 Message Date
Boshen 030857b509 refactor(diagnostics): update oxc-miette to v4 (#25395)
Remove the miette `Report` wrapper and update oxc-miette to v4's narrowed diagnostic and source protocol.

AI-assisted by OpenAI Codex.
2026-08-10 07:48:02 +00:00
camc314 87841eebfd chore(all): fix clippy::useless_borrows_in_formatting (#24347) 2026-07-10 09:01:12 +00:00
camc314 260425f706 feat(semantic/examples): include unresolved references (#24214)
We now include unresolved references in the semantic output

Example after output:
```
  ⚠ References for symbol `I`
   ╭─[1:11]
 1 │ interface I {
   ·           ┬
   ·           ╰── declared here
 2 │     [Symbol.iterator]: number;
   ╰────
  note: This symbol has no references.

  ⚠ Unresolved ident `Symbol`
   ╭─[2:6]
 1 │ interface I {
 2 │     [Symbol.iterator]: number;
   ·      ───┬──
   ·         ╰── referenced here: (ReferenceFlags(ValueAsType))
 3 │     [Symbol.iterator]?: number;
   ·      ───┬──
   ·         ╰── referenced here: (ReferenceFlags(ValueAsType))
 4 │ }
   ╰────
```
2026-07-06 08:43:13 +00:00
Boshen 777f02ae10 refactor(diagnostics)!: return a Diagnostics type instead of Vec<OxcDiagnostic> (#23217)
## Summary

Introduces `oxc_diagnostics::Diagnostics` — a `Vec<OxcDiagnostic>` newtype that knows the difference between errors and warnings — and makes it the standard diagnostics return value across oxc.

- **New type**: `Diagnostics` with `has_errors()`, `has_warnings()`, `errors()`, `warnings()`, plus `Vec`-like ergonomics (Deref, `IntoIterator`, `Extend`, `From`/`Into<Vec>`) so most call sites are unchanged.
- **Return types**: `ParserReturn`, `SemanticBuilderReturn`, `IsolatedDeclarationsReturn`, the React Compiler `TransformResult`/`LintResult`, and `TransformerReturn` now expose `diagnostics: Diagnostics` instead of `errors: Vec<OxcDiagnostic>` (the React Compiler's separate `errors`/`warnings` are merged into one severity-tagged list).
- **Behaviour**: a React Compiler **error** now stops the transform (no output), while **warnings** flow through and the transform continues. `compile()` aborts codegen only when `diagnostics.has_errors()` — warnings alone no longer abort (previously any diagnostic did).

## Breaking change

Consumers reading `*.errors` on these return values must read `*.diagnostics`. React Compiler consumers that read `.errors`/`.warnings` separately should use `diagnostics.has_errors()` / `diagnostics.errors()` / `diagnostics.warnings()`.

Parser/semantic/isolated-declarations fatality is otherwise unchanged. Builds on the React Compiler transform feature (#23201).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-10 14:46:52 +00:00
Boshen 1260f03b4f refactor(semantic): don't build the AstNodes store by default (#23005)
## What

Make the full `AstNodes` store **opt-in**, so the compiler pipeline (transformer / codegen / minifier) can run semantic analysis without building it at all.

Previously every `SemanticBuilder` run allocated and populated the full `AstNodes` store, even for consumers that only need scoping + the syntax checker. This separates the node-related builder state into three independent pieces:

- a standalone `NodeId` counter — the source of truth for node ids, valid in either mode;
- an **ancestry stack** — an upward-walk-only stack of the current node's ancestors, the only ancestry the syntax checker and binder actually read;
- the full **`AstNodes`** store — random access to every node, needed by the linter and mangler.

## How

- A new `AstNodeStore` (in `crates/oxc_semantic/src/node/store.rs`) bundles the node-id counter, the current-node cursor/flags, and the storage. The storage is an enum (`AstNodeStoreKind`) with two variants — `Full(AstNodes)` and `Ancestry(AncestryStack)` — so the full store and the ancestry stack are **mutually exclusive by construction**; they can never both be maintained at once.
- The checker and binder read ancestry through an `Ancestry` view (`node/ancestry.rs`) that dispatches between the full store (linter / mangler) and the stack (compiler pipeline). They no longer do random `NodeId` access into the store — all ancestry access is upward-walk-only.
- `SemanticBuilder` gains `with_build_nodes(bool)`. The default is **off** (ancestry stack only). `new_linter()` turns it on (the linter needs the full store); `new_compiler()` leaves it off.
- `CompilerInterface::build_semantic_nodes()` lets `oxc::Compiler` consumers opt back in if their `after_semantic` hook reads `Semantic::nodes`.

## Notes

- Behaviour-neutral: semantic conformance snapshots are unchanged.
- Allocation snapshots updated — the default (ancestry) path allocates slightly less (the growing `Stack` trades a couple of fixed allocations for a few reallocs).
- Builds on the already-merged `SymbolFlags`-based redeclaration checks (#23060) and the `new_linter`/`new_compiler` preset adoption (#23057), which removed the last random node reads from the redeclaration path so the checker works without the store.
2026-06-08 16:13:56 +00:00
Boshen 336413bf5d refactor(semantic): use new_linter/new_compiler presets at callsites (#23037)
Replaces ad-hoc `SemanticBuilder::new().with_*()` chains with the `new_linter()` / `new_compiler()` presets where the preset already expands to the exact same option set, so the config lives in one place.

- linter example + linter benchmark → `new_linter()` (cfg + class table + syntax)
- semantic benchmark, cfg example, integration test util → `new_compiler()` (syntax)

Behavior-neutral — each preset matches the options the callsite set by hand. Carved out of the larger store-optional refactor so it can land independently on `main` first.
2026-06-06 19:09:57 +00:00
Boshen 324c8ae137 refactor(semantic): add SemanticBuilder::new_compiler / new_linter presets (#23019)
## Summary

Adds two named constructor presets to `SemanticBuilder` that bake in the options each use case needs, so the configuration lives in one place instead of being repeated at call sites:

- **`SemanticBuilder::new_compiler()`** — compiler pipeline. Enables syntax-error checking; leaves linter-only analyses (control flow graph, class table) off.
- **`SemanticBuilder::new_linter()`** — linter. Enables syntax-error checking, the control flow graph, and the class table.

The presets are applied at the real pipeline entry points:
- `new_linter()` → the linter service build.
- `new_compiler()` → `oxc::Compiler`, and the napi parser/transform bindings.

Everywhere else keeps `SemanticBuilder::new()` and turns on only the features it needs (mangler, minifier, formatter, transformer plugins, examples, tests, ...). Individual options can still be overridden after a preset (e.g. the linter service overrides `check_syntax_error` with its runtime flag).

Behavior is preserved at every migrated site. Node-store access is intentionally untouched in this PR.

## Verified

- Builds across the affected crates + napi; clippy clean; `oxc_linter` tests pass; full `cargo coverage` with no snapshot changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-06 15:44:18 +00:00
renovate 611b3b69ef chore(deps): update dependency rust to v1.95.0 (#21509)
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [rust](https://redirect.github.com/rust-lang/rust) | minor | `1.94.1` → `1.95.0` |

---

### Release Notes

<details>
<summary>rust-lang/rust (rust)</summary>

### [`v1.95.0`](https://redirect.github.com/rust-lang/rust/compare/1.94.1...1.95.0)

[Compare Source](https://redirect.github.com/rust-lang/rust/compare/1.94.1...1.95.0)

</details>

---

### Configuration

📅 **Schedule**: (in timezone Asia/Shanghai)

- 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:eyJjcmVhdGVkSW5WZXIiOiI0My4xMjMuOCIsInVwZGF0ZWRJblZlciI6IjQzLjEyMy44IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
2026-04-17 14:43:16 +00:00
camc314 8d0b4f3e91 refactor(examples/semantic): simplify symbol span retrieval in semantic analysis (#19754)
This makes it clear exactly where the declaration is, instead of it being ambiguous.

It also reduces the changes of spans being overlapping ect.

Before:

![Screenshot 2026-02-26 at 12.12.14.png](https://app.graphite.com/user-attachments/assets/cfe5fce1-c488-4d70-9f4e-d3270f651cba.png)

After:

![Screenshot 2026-02-26 at 12.12.03.png](https://app.graphite.com/user-attachments/assets/fc7181b1-24ab-44d4-8753-01e4a7974245.png)
2026-02-26 12:18:08 +00:00
camc314 6b29bd19e7 feat(semantic/example): add detailed references info (#17938)
![Screenshot 2026-01-12 at 18.52.14.png](https://app.graphite.com/user-attachments/assets/f51bc810-1ac3-4561-afac-624dc1b92f1c.png)

While possible with the current example infra, it's hard to understand what exactly points where, by reading the ids, and comparing it vs the ast.
This change makes it incredibly easy to identify what symbol a refernce points to.
2026-01-13 02:59:13 +00:00
camc314 aec04f20ad fix(semantic/cfg): update example docs + code (#14809) 2025-10-20 11:43:23 +00:00
Boshen 497236ebe4 refactor(semantic): move AstNode::cfg_id to struct of arrays in AstNodes (#14137)
part of #13646

Reduces AstNode from 32 to 24 bytes in the linter pipeline.
2025-09-26 04:26:11 +00:00
Boshen 08c05df60c perf(semantic): make CFG construction a compile-time feature (#13678)
part of #13646

## Summary

This PR converts the Control Flow Graph (CFG) functionality in `oxc_semantic` from runtime checks to a compile-time cargo feature, eliminating runtime overhead when CFG is not needed.

## Changes

- Added `cfg` cargo feature to `oxc_semantic/Cargo.toml` with `oxc_cfg` as optional dependency
- Feature-gated all CFG-related code using `#[cfg(feature = "cfg")]`
- Updated `oxc_linter` to enable the `cfg` feature (since it requires CFG)
- Made the `control_flow!` macro work correctly with and without the feature

## Benefits

- **Performance**: Eliminates runtime `Option<CFG>` checks when feature is disabled
- **Binary size**: Smaller binaries for crates that don't need CFG
- **Cleaner API**: CFG methods only available when feature is enabled

## Test Plan

- [x] All existing tests pass with `cargo test -p oxc_semantic --features cfg`
- [x] `oxc_linter` builds and works correctly (it enables the feature)
- [x] Feature is backward compatible - existing code continues to work

🤖 Generated with [Claude Code](https://claude.ai/code)
2025-09-11 12:44:19 +00:00
Copilot 09ae2a936c perf(linter): Eliminate unnecessary Iterator::collect() allocations (#12776) 2025-08-03 18:01:33 +08:00
Copilot 514322c4c3 docs(rust): Add minimal documentation to example files in crates directory (#12731) 2025-08-02 22:02:20 +08:00
overlookmotel 65847c0ff5 style(semantic): reorder imports (#12671)
Re-order imports in `oxc_semantic` in order of furthest away (`std`) to closest (`mod`).
2025-07-31 12:36:29 +00:00
camchenry c9b65137b4 feat(semantic/example): add ability to print symbols and references in example (#12017)
This was helpful for debugging some issues in semantic. It roughly matches what we show in the playground on the website as well.
2025-07-02 05:58:59 +00:00
overlookmotel 5983760ae0 docs(semantic): fix instructions for running semantic example (#10016) 2025-03-24 11:57:40 +00:00
Boshen 1d81adac48 Rust Edition 2024 (#9289)
closes #9271

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-02-22 11:56:25 +08:00
overlookmotel a4a8e7deda style(all): replace #[allow] with #[expect] (#8930)
Replace `#[allow]` with `#[expect]` through the whole codebase.

This also surfaced many places where the `#[allow]` attributes were outdated/pointless and could be removed entirely.

Only places `#[allow]` remains are:

1. In generated code and macros, where lint errors may or may not be triggered depending on the generated code.
2. A few places where clippy malfunctions. These use e.g. `#[allow(unused, clippy::allow_attributes)]`.
2025-02-07 14:50:02 +00:00
Boshen 8a788b8f4b feat(parser)!: Build ModuleRecord directly in parser (#7546)
This has the benefit of:

* expose dynamic import / import meta info from parser
* 1 less ast shallow in semantic builder
* no ast walk in oxc's module lexer
* some more benefits coming soon
2024-11-29 14:50:42 +00:00
Boshen 4740642926 chore(semantic): rename examples/simple.rs to examples/semantic.rs 2024-11-12 11:55:58 +08:00
Boshen 435a89c6e4 refactor(oxc): remove useless allocator.alloc(program) calls (#6571) 2024-10-15 02:21:20 +00:00
Boshen 520096030a refactor(oxc)!: remove passing Trivias around (#6446)
part of #6426
2024-10-11 06:09:25 +00:00
Boshen 2b7be08af4 feat(ast)! add source_text to Program (#6444) 2024-10-11 04:13:41 +00:00
DonIsaac 40932f79b1 refactor(cfg): use IndexVec for storing basic blocks (#6323)
Use an `IndexVec` when storing basic blocks. This makes the link between nodes in `.graph` and elements of `.basic_blocks` more clear. I had to rename `BasicBlockId` to `BlockNodeId` to avoid a name collision. I wasn't sure what else to name the `Idx` type for the basic blocks vec.
2024-10-07 19:28:40 -04:00
DonIsaac 95ca01ccc1 refactor(cfg)!: make BasicBlock::unreachable private (#6321)
Protect `unreachable` property of basic blocks in preparation of upcoming
refactors. This is technically a breaking change.
2024-10-07 15:13:56 +00:00
camchenry 2b17003e0b perf(linter, prettier, diagnostics): use FxHashMap instead of std::collections::HashMap (#5993)
Using `FxHashMap` is faster than `HashMap` in many cases, especially for hashing-heavy workloads. This change improves the performance of the linter, prettier, and diagnostics crates by using `FxHashMap` instead of `std::collections::HashMap`.
2024-09-23 16:29:05 +00:00
DonIsaac 3d13c6d1f9 refactor(semantic): impl IntoIterator for &AstNodes (#5873)
Simple quality-of-life change.
2024-09-19 03:22:29 +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 3ae94b8801 refactor(semantic): change build_module_record to accept &Path instead of PathBuf 2024-08-30 12:24:49 +08:00
Boshen 7cc2bbd293 chore(semantic): print errors from examples/simple.rs 2024-08-23 22:57:26 +08:00
DonIsaac 0a01a4729a docs(semantic): improve documentation (#4850) 2024-08-13 02:14:07 +00:00
overlookmotel d79b60afc4 example(semantic): examples print parser errors (#4405)
Examples for `Semantic` print parser errors if parsing fails.
2024-07-22 11:05:03 +00:00
rzvxa d8ad321687 refactor(semantic): make control flow generation optional. (#3737)
For maximum backward compatibility, we generate CFG by default.

Note: It can't be done with a simple method since lifetimes make it impossible(at least without unsafe trickery) I've tried to do it without a macro but it was just unintuitive.
2024-06-18 15:59:38 +00:00
rzvxa 0537d298db refactor(cfg)!: move control flow to its own crate. (#3728) 2024-06-18 15:59:29 +00:00
Boshen 982e6f08df chore: make println and eprintln opt-in (#3712)
I noticed accidental `println` can be merged, which isn't really nice.
2024-06-17 10:40:34 +00:00
rzvxa 9c31ed9178 feat(semantic/cfg): propagate unreachable edges through subgraphs. (#3648)
From this:

![Screenshot 2024-06-12 230152](https://github.com/oxc-project/oxc/assets/3788964/b489cee8-62ec-4483-9c9b-6e3d9858fec2)

To this:

![Screenshot 2024-06-12 230031](https://github.com/oxc-project/oxc/assets/3788964/674a6e42-5311-4ca6-940b-65d8e3ccc0f4)
2024-06-13 08:34:12 +00:00
rzvxa 9b3097147e improvement(semantic/cfg): rework error path. (#3519)
This PR aims to provide a more accurate error/finalization flow, I've nuked the old error path approach and rewrote it with more versatility in mind.

We used to visit the finalizer block twice and create 2 sets of AstNodes/Basic Blocks for them, This was there to differentiate between the error path finalizer and success path one. There is no longer a need for having 2 separate sets of nodes to do this differentiation.

As for the error path now we have 2 kinds of them, Everything is attached to an error block - even if it is not in a try-catch statement - this results in a lot of extra edges to keep track of these "Implicit" error blocks but I believe in future it can help us to track cross block error paths, For example, we can dynamically attach and cache the implicit error block of a function to its call site error path (either implicit or explicit).
2024-06-13 07:36:16 +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`.
https://github.com/rolldown/rolldown/pull/1364/files/8be1cc80520e1a466cd7b2d3fb8b637db9e529a9#r1630711060
2024-06-11 13:23:41 +08:00
rzvxa 61bae74f76 improvement(semantic/cfg): better CFG API (#3472) 2024-06-06 07:55:37 +00:00
rzvxa 209a99d49f improvement(semantic/cfg): rework basic blocks. (#3381)
I've replaced the `BasicBlockElement` with an `Instruction` type which would keep both the instruction kind and its associated AstNodeId.
I also removed the register scheme in the control flow in favor of a simpler approach using explicit enums.

https://github.com/oxc-project/oxc/pull/3381#issuecomment-2126622774
2024-06-06 05:41:01 +00:00
rzvxa 78e6326e48 refactor(semantic/cfg): alias petgraph's NodeIndex as BasicBlockId. (#3380)
Hides petgraph's general `NodeIndex` type behind `BasicBlockId`.
2024-05-22 03:09:38 +00:00
Boshen a2c173de57 refactor: remove panic! from examples (#2454)
relates #2308
2024-02-20 16:18:39 +08:00
Tzvi Melamed 27681951e1 feat(oxc_semantic): Improve sample visualization (#2251)
1. add a `test.js` file to the project root:

```js
class A extends B {
  constructor() {
    try {
      super();
    } finally {
      this.a;
    }
  }
}
```

2. run:

```bash
$ cargo run -p oxc_semantic --example simple
   Compiling oxc_semantic v0.5.0 (/home/tzvipm/src/github.com/tzvipm/oxc/crates/oxc_semantic)
    Finished dev [unoptimized + debuginfo] target(s) in 32.07s
     Running `target/debug/examples/simple`
Wrote AST to: test.ast.txt
Wrote CFG blocks to: test.cfg.txt
Wrote CFG dot diagram to: test.dot
```

3. resulting graph from .dot file:


![image](https://github.com/TzviPM/oxc/assets/1950680/7163deaa-ab75-4bed-a093-946e2d6d2206)
2024-02-01 12:55:56 +00:00
Tzvi Melamed e561457683 feat(semantic): track cfg index per ast node (#2210)
This allows looking up a cfg index from an ast node in a semantics
return. This allows later passes to better make use of the cfg.
2024-02-01 13:27:20 +08:00
Dunqing 972be831e9 fix(semantic): fix incorrect semantic example (#2198) 2024-01-29 06:48:47 +00:00
Boshen 889837704c feat(semantic): cfg prototype (#2019)
Co-authored-by: U9G <git@u9g.dev>
2024-01-25 20:19:35 +08:00