Repository navigation
fix(compiler): keep parentheses that JavaScript requires (arrow bodies, unary ** bases) - #557
Merged
Merged
Conversation
The emitter dropped every OutputExpression::Parenthesized, so
`(v) => ({value: v}).value` emitted as `(v) => {value:v}.value` — invalid
JS. ngtsc emits such expressions verbatim via WrappedNodeExpr; we rebuild
the AST, so parens that survive the strip phase must print.
- emitter.rs: `Parenthesized` keeps its parens unless the inner
expression already self-parenthesizes (BinaryOperator, Conditional,
Comma, UnaryOperator with `parens`). Arrow expression bodies use a new
`emits_leading_brace` helper that walks the leftmost token through
member access, calls and tagged templates, replacing the whole-body
LiteralMap check.
- strip_nonrequired_parentheses.rs: handle `IrExpression::Parenthesized`
(our pipe-aware ingest produces them; upstream's single-tree strip
covers them) and mark required parens on IR `Binary` operands for
`**`/`??`/`&&`/`||`. The unary `**`-base check covers `!`/`typeof`/`void`
too — upstream only needs `UnaryOperatorExpr` because the TS printer
re-adds parens; we emit raw JS. Nested `((-1)) ** 2` marks the outer
wrapper so `(-1) ** 2` survives.
Fixes #510
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #510 — the emitter dropped source parentheses, so an arrow body whose leftmost token is
{printed invalid JS:Upstream
ngtscnever hits this: it emits source expressions verbatim throughWrappedNodeExpr. Our converter rebuilds the AST (Expression::ParenthesizedExpression→OutputExpression::Parenthesized), and the emitter dropped the parens unconditionally. That also masked other required parens —(-1) ** 2,(1).toString(),(obj?.m)(),(() => f)()all lost theirs.Changes
src/output/emitter.rs:Parenthesizednow keeps its parens — they're source parens ngtsc would emit verbatim, or required parens the strip phase kept. Skipped only when the inner expression already self-parenthesizes (BinaryOperator,Conditional,Comma,UnaryOperatorwithparens), so((a + b))sources don't emit double parens.emits_leading_bracehelper walks the leftmost emitted token throughReadProp/ReadKeyreceivers,InvokeFunctioncallees andTaggedTemplateLiteraltags; theArrowFunctionbody check uses it, replacing theLiteralMap/raw-source whole-body check.() => ({...})still emits exactly once.src/pipeline/phases/strip_nonrequired_parentheses.rs:IrExpression::Parenthesized— our pipe-aware ingest produces IR-level parens (e.g.(a | async) || b) that were invisible to it before; the emitter's drop-parens behavior masked that. Non-required IR parens are stripped like upstream's single-tree pass.check_ir_expression_for_required_parensmarksParenthesizedoperands of IRBinary(**,??,&&/||) as required, mirroring upstreamcheckExponentiationParens/checkNullishCoalescingParens/checkAndOrParens.**-base check covers!/typeof/voidunary bases too, and classifiers look through nestedParenthesized(((-1)) ** 2keeps the required wrapper). Upstream needs onlyUnaryOperatorExprbecause its emit goes through the TypeScript printer, which re-adds parens — we emit raw JS.Tests
New
tests/arrow_paren_emit_test.rs(9 tests): both issue repros in@Input transformanduseFactorymetadata (full + partial mode),() => ({...})(issue #43), call/member chains on parenthesized objects, verbatim source parens,(-1) ** 2/(1).toString()/(o?.m)()in metadata, and template-pipeline coverage for all four unary**bases, nested parens, paren stripping, and??/&&/?:mixes.Verification
cargo test --all-features: 3077 pass — including thepipe_in_binary_with_safe_property_readsnapshot, which now round-trips through the corrected strip phasecargo check+--all-features,cargo fmt --check: cleancargo run -p oxc_angular_conformance: 1264/1264