mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
b20fc1950c
The parser currently accepts mixed nullish and logical expressions when parentheses cover only the first operand of the logical expression: ```js (a) || b ?? c; a ?? (b) || c; (a) && b ?? c; a ?? (b) && c; ``` These expressions require a syntax error because `??` cannot be mixed directly with `||` or `&&`. Parentheses around `a` or `b` do not group the complete logical expression. This change reports the existing mixed-coalesce diagnostic for all four forms. Fully grouped operands remain valid: ```js (a || b) ?? c; a ?? (b || c); (a && b) ?? c; a ?? (b && c); ``` ### Why the existing check misses these expressions `parse_binary_expression_rest` used `lhs_parenthesized` and `rhs_parenthesized` flags captured before parsing an operand. Those flags describe whether the initial token is `(`, but the parser can subsequently accumulate a larger logical expression outside those parentheses. Applying the initial flag to that complete expression incorrectly suppresses the diagnostic. ### Complete-operand checks Check both operands independently for an `And` or `Or` logical expression whose span starts at the operand's starting position. A logical expression accumulated outside initial parentheses starts at that position and must be rejected. Checking both operands also prevents a grouped right operand from hiding an invalid left operand. This works with either `preserve_parens` setting. When parentheses are preserved, a fully grouped operand is a `ParenthesizedExpression`. When they are omitted, its inner logical expression retains a span beginning after the opening `(`, so its start differs from the operand start and it remains allowed. For `(a) || b`, the complete logical expression instead starts at the opening `(` and is correctly rejected.
Oxc Parser
High-performance JavaScript and TypeScript parser with comprehensive language support.
Overview
This crate provides a fast, spec-compliant parser for JavaScript and TypeScript that produces a complete Abstract Syntax Tree (AST). It supports all modern language features including JSX, TSX, and the latest ECMAScript proposals.
Key Features
- Complete language support: JavaScript, TypeScript, JSX, and TSX
- Latest ECMAScript: All stable ECMAScript features plus stage 3+ proposals
- High performance: Significantly faster than traditional parsers
- Error recovery: Continues parsing after errors to provide complete AST
- Comprehensive AST: Detailed node information with accurate source positions
Architecture
Parser Design
- Recursive descent: Traditional recursive descent parser architecture
- Error recovery: Sophisticated error recovery for IDE-friendly parsing
- Memory efficient: Uses arena allocation for optimal performance
- Streaming: Processes source text in a single pass
Language Support
- JavaScript: Full ECMAScript 2024+ support
- TypeScript: Complete TypeScript syntax including decorators
- JSX/TSX: React JSX and TypeScript JSX syntax
- Proposals: Stage 3 decorators and other advancing proposals
AST Structure
The parser produces an AST that closely follows ECMAScript specifications:
- Accurate positions: Every node has precise source location information
- Complete information: Preserves all syntactic details including trivia
- Type-safe: Leverages Rust's type system for correctness
- Visitor-friendly: Designed for easy traversal and transformation
The parser is designed as the foundation for all other oxc tools, providing the high-quality AST needed for analysis, transformation, and code generation.