Skip to content

Commit 2209e0b

Browse files
authored
fix: correct bracePosition for export default class (#646)
1 parent f23accb commit 2209e0b

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/generation/generate.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ fn gen_class_decl<'a>(node: &ClassDecl<'a>, context: &mut Context<'a>) -> PrintI
807807
node: node.into(),
808808
member_node: node.class.into(),
809809
decorators: node.class.decorators,
810-
is_class_expr: false,
810+
is_class_decl: true,
811811
is_declare: node.declare(),
812812
is_abstract: node.class.is_abstract(),
813813
ident: Some(node.ident.into()),
@@ -826,7 +826,7 @@ struct ClassDeclOrExpr<'a> {
826826
node: Node<'a>,
827827
member_node: Node<'a>,
828828
decorators: &'a [&'a Decorator<'a>],
829-
is_class_expr: bool,
829+
is_class_decl: bool,
830830
is_declare: bool,
831831
is_abstract: bool,
832832
ident: Option<Node<'a>>,
@@ -845,7 +845,8 @@ fn gen_class_decl_or_expr<'a>(node: ClassDeclOrExpr<'a>, context: &mut Context<'
845845
// generate decorators
846846
let parent_kind = node.node.parent().unwrap().kind();
847847
if parent_kind != NodeKind::ExportDecl && parent_kind != NodeKind::ExportDefaultDecl {
848-
items.extend(gen_decorators(node.decorators, node.is_class_expr, context));
848+
let is_inline = !node.is_class_decl;
849+
items.extend(gen_decorators(node.decorators, is_inline, context));
849850
}
850851

851852
// generate header and body
@@ -920,7 +921,9 @@ fn gen_class_decl_or_expr<'a>(node: ClassDeclOrExpr<'a>, context: &mut Context<'
920921
},
921922
));
922923

923-
if node.is_class_expr {
924+
if node.is_class_decl {
925+
items
926+
} else {
924927
let items = items.into_rc_path();
925928
if_true_or(
926929
"classExprConditionalIndent",
@@ -935,8 +938,6 @@ fn gen_class_decl_or_expr<'a>(node: ClassDeclOrExpr<'a>, context: &mut Context<'
935938
items.into(),
936939
)
937940
.into()
938-
} else {
939-
items
940941
}
941942
}
942943

@@ -2266,12 +2267,13 @@ fn gen_import_callee<'a>(node: &Import<'a>, _context: &mut Context<'a>) -> Print
22662267
}
22672268

22682269
fn gen_class_expr<'a>(node: &ClassExpr<'a>, context: &mut Context<'a>) -> PrintItems {
2270+
let is_class_decl = node.parent().kind() == NodeKind::ExportDefaultDecl && node.ident.is_some();
22692271
gen_class_decl_or_expr(
22702272
ClassDeclOrExpr {
22712273
node: node.into(),
22722274
member_node: node.class.into(),
22732275
decorators: node.class.decorators,
2274-
is_class_expr: true,
2276+
is_class_decl,
22752277
is_declare: false,
22762278
is_abstract: node.class.is_abstract(),
22772279
ident: node.ident.map(|x| x.into()),
@@ -2280,7 +2282,11 @@ fn gen_class_expr<'a>(node: &ClassExpr<'a>, context: &mut Context<'a>) -> PrintI
22802282
super_type_params: node.class.super_type_params.map(|x| x.into()),
22812283
implements: node.class.implements.iter().map(|&x| x.into()).collect(),
22822284
members: node.class.body.iter().map(|x| x.into()).collect(),
2283-
brace_position: context.config.class_expression_brace_position,
2285+
brace_position: if is_class_decl {
2286+
context.config.class_declaration_brace_position
2287+
} else {
2288+
context.config.class_expression_brace_position
2289+
},
22842290
},
22852291
context,
22862292
)

tests/specs/declarations/class/ClassDeclaration_BracePosition_NextLine.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ class Test
3232
extends SomethingReallyReallyReallyLong
3333
{
3434
}
35+
36+
== should handle export default with a name ==
37+
export default class Test {
38+
}
39+
40+
[expect]
41+
export default class Test
42+
{
43+
}
44+
45+
== should not consider a class in an export default decl with no name as a declaration ==
46+
export default class {
47+
}
48+
49+
[expect]
50+
export default class {
51+
}

tests/specs/expressions/ClassExpression/ClassExpression_BracePosition_NextLine.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@ const t = class
3434
extends SomethingReallyReallyReallyLong
3535
{
3636
};
37+
38+
== should consider export default with no name as an expr ==
39+
export default class {
40+
}
41+
42+
[expect]
43+
export default class
44+
{
45+
}
46+
47+
== export default class is a declaration ==
48+
export default class Test {
49+
}
50+
51+
[expect]
52+
export default class Test {
53+
}

0 commit comments

Comments
 (0)