mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
777f02ae10
## 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)
61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
//! # TypeScript JSX Parsing Example
|
|
//!
|
|
//! This example demonstrates parsing TypeScript files with JSX syntax.
|
|
//! It shows how to parse a simple React component and validate the parsing results.
|
|
//!
|
|
//! ## Usage
|
|
//!
|
|
//! ```bash
|
|
//! cargo run -p oxc_parser --example parser_tsx
|
|
//! ```
|
|
|
|
use oxc_allocator::Allocator;
|
|
use oxc_parser::{Parser, ParserReturn};
|
|
use oxc_span::SourceType;
|
|
|
|
/// Parse a TypeScript JSX file and validate the results
|
|
fn main() -> Result<(), String> {
|
|
let source_text = r"
|
|
import React from 'react';
|
|
|
|
/**
|
|
* A simple counter component
|
|
*/
|
|
export const Counter: React.FC = () => {
|
|
const [count, setCount] = React.useState(0);
|
|
|
|
return (
|
|
<div>
|
|
<p>Count: {count}</p>
|
|
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
<button onClick={() => setCount(count - 1)}>Decrement</button>
|
|
</div>
|
|
)
|
|
}";
|
|
|
|
// Memory arena where AST nodes get stored
|
|
let allocator = Allocator::default();
|
|
// Infers TypeScript + JSX + ESM modules
|
|
let source_type = SourceType::from_path("Counter.tsx").unwrap();
|
|
|
|
let ParserReturn {
|
|
program, // AST
|
|
diagnostics, // Syntax errors
|
|
panicked, // Parser encountered an error it couldn't recover from
|
|
..
|
|
} = Parser::new(&allocator, source_text, source_type).parse();
|
|
|
|
if panicked {
|
|
return Err("Parser panicked".to_string());
|
|
}
|
|
|
|
if !diagnostics.is_empty() {
|
|
return Err(format!("Parsing errors: {}", diagnostics.len()));
|
|
}
|
|
|
|
assert!(!program.body.is_empty());
|
|
assert_eq!(program.comments.len(), 1);
|
|
|
|
Ok(())
|
|
}
|