Skip to content

Commit

Permalink
fix(semantic): implicit return UpdateExpression in `ArrowFunctionEx…
Browse files Browse the repository at this point in the history
…pression` does not as read reference (#5161)

close: #5158
close: #5156
  • Loading branch information
Dunqing committed Aug 26, 2024
1 parent 04c5ca2 commit 293413f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,19 @@ impl<'a> SemanticBuilder<'a> {
for node in self.nodes.iter_parents(self.current_node_id).skip(1) {
return match node.kind() {
AstKind::ParenthesizedExpression(_) => continue,
AstKind::ExpressionStatement(_) => false,
AstKind::ExpressionStatement(_) => {
if self.current_scope_flags().is_arrow() {
if let Some(node) = self.nodes.iter_parents(node.id()).nth(2) {
// (x) => x++
// ^^^ implicit return, we need to treat `x` as a read reference
if matches!(node.kind(), AstKind::ArrowFunctionExpression(arrow) if arrow.expression)
{
return true;
}
}
}
false
}
_ => true,
};
}
Expand Down
54 changes: 54 additions & 0 deletions crates/oxc_semantic/tests/integration/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,57 @@ fn test_ts_interface_heritage() {
.has_number_of_references(1)
.test();
}

#[test]
fn test_arrow_implicit_return() {
SemanticTester::js("let i = 0; const x = () => i")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();

SemanticTester::js("let i = 0; const x = () => ++i")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => { ++i }")
.has_root_symbol("i")
.has_number_of_reads(0)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => (0, ++i)")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => (++i, 0)")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 1; const foo = () => () => { i++ }")
.has_root_symbol("i")
.has_number_of_reads(0)
.has_number_of_writes(1)
.test();
}

#[test]
fn test_arrow_explicit_return() {
SemanticTester::js("let i = 0; const x = () => { return i }")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();

SemanticTester::js("let i = 0; const x = () => { return ++i }")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();
}

0 comments on commit 293413f

Please sign in to comment.