Skip to content

Commit

Permalink
fix: support using decl in for of stmt (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 1, 2024
1 parent 9a734c2 commit 8ba0b68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ fn gen_using_decl<'a>(node: &UsingDecl<'a>, context: &mut Context<'a>) -> PrintI

items.extend(gen_var_declarators(node.into(), node.decls, context));

if context.config.semi_colons.is_true() {
if requires_semi_colon_for_var_or_using_decl(node.into(), context) {
items.push_sc(sc!(";"));
}

Expand Down Expand Up @@ -5343,22 +5343,22 @@ fn gen_var_decl<'a>(node: &VarDecl<'a>, context: &mut Context<'a>) -> PrintItems

items.extend(gen_var_declarators(node.into(), node.decls, context));

if requires_semi_colon(node, context) {
if requires_semi_colon_for_var_or_using_decl(node.into(), context) {
items.push_sc(sc!(";"));
}

return items;
items
}

fn requires_semi_colon(node: &VarDecl, context: &mut Context) -> bool {
let use_semi_colons = context.config.semi_colons.is_true();
use_semi_colons
&& match node.parent() {
Node::ForInStmt(node) => node.start() >= node.body.start(),
Node::ForOfStmt(node) => node.start() >= node.body.start(),
Node::ForStmt(node) => node.start() >= node.body.start(),
_ => use_semi_colons,
}
}
fn requires_semi_colon_for_var_or_using_decl(node: Node, context: &mut Context) -> bool {
let use_semi_colons = context.config.semi_colons.is_true();
use_semi_colons
&& match node.parent() {
Some(Node::ForInStmt(node)) => node.start() >= node.body.start(),
Some(Node::ForOfStmt(node)) => node.start() >= node.body.start(),
Some(Node::ForStmt(node)) => node.start() >= node.body.start(),
_ => use_semi_colons,
}
}

fn gen_var_declarators<'a>(parent: Node<'a>, decls: &[&'a VarDeclarator<'a>], context: &mut Context<'a>) -> PrintItems {
Expand Down
10 changes: 10 additions & 0 deletions tests/specs/declarations/using/Using_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ function test() {
using f = 2, g = 3, testingTheLimitOut = 4;
}

for (using test of something) {
}
for (using test in something) {
}

[expect]
using a!: string;
using b = 5;
Expand All @@ -25,6 +30,11 @@ function test() {
testingTheLimitOut = 4;
}

for (using test of something) {
}
for (using test in something) {
}

== should not wrap within a variable declaration ==
using t: Testing, testingThis: outALittleBit;

Expand Down

0 comments on commit 8ba0b68

Please sign in to comment.