Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ fn needs_temporary_in_safe_access(expr: &IrExpression<'_>) -> bool {
needs_temporary_in_safe_access(&keyed.receiver)
|| needs_temporary_in_safe_access(&keyed.key)
}
// Binary operators need to check both operands
IrExpression::Binary(bin) => {
needs_temporary_in_safe_access(&bin.lhs) || needs_temporary_in_safe_access(&bin.rhs)
}
// Ternary needs to check all branches
IrExpression::Ternary(ternary) => {
needs_temporary_in_safe_access(&ternary.condition)
|| needs_temporary_in_safe_access(&ternary.true_expr)
|| needs_temporary_in_safe_access(&ternary.false_expr)
}
// Not expression needs to check operand
IrExpression::Not(not) => needs_temporary_in_safe_access(&not.expr),
// Unary operator needs to check operand
IrExpression::Unary(unary) => needs_temporary_in_safe_access(&unary.expr),
// Check AST expressions for function calls
IrExpression::Ast(ast_expr) => needs_temporary_in_ast_expression(ast_expr),
// Parenthesized expressions need to check their inner expression
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_angular_compiler/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,20 @@ fn test_safe_call_in_listener_inside_conditional() {
insta::assert_snapshot!("safe_call_in_listener_inside_conditional", js);
}

#[test]
fn test_pipe_in_binary_with_safe_property_read() {
// Pattern from EmailCommentComponent: (comment$ | async || comment)?.new_mentioned_thread_count
// When a pipe binding is inside a binary expression that is the receiver of a safe property read,
// the compiler must generate a temporary variable to avoid evaluating the pipe twice.
// TypeScript Angular compiler produces: (tmp = pipeBind(...) || fallback) == null ? null : tmp.prop
// Without the fix, OXC duplicates the pipe call in both the guard and the access expression.
let js = compile_template_to_js(
r#"<div>{{ ((data$ | async) || fallback)?.name }}</div>"#,
"TestComponent",
);
insta::assert_snapshot!("pipe_in_binary_with_safe_property_read", js);
}

// ============================================================================
// Event Modifier Tests
// ============================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/oxc_angular_compiler/tests/integration_test.rs
assertion_line: 1170
expression: js
---
function TestComponent_Template(rf,ctx) {
if ((rf & 1)) {
i0.ɵɵelementStart(0,"div");
i0.ɵɵtext(1);
i0.ɵɵpipe(2,"async");
i0.ɵɵelementEnd();
}
if ((rf & 2)) {
let tmp_0_0;
i0.ɵɵadvance();
i0.ɵɵtextInterpolate((((tmp_0_0 = (i0.ɵɵpipeBind1(2,1,ctx.data$) || ctx.fallback)) == null)? null: tmp_0_0.name));
}
}
Loading