Skip to content

Commit bf9b38a

Browse files
committed
feat(codegen): improve codegen formatting (#3731)
1 parent ee627c3 commit bf9b38a

File tree

5 files changed

+744
-620
lines changed

5 files changed

+744
-620
lines changed

crates/oxc_codegen/src/gen.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,18 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Statement<'a> {
122122
decl.gen(p, ctx);
123123
p.print_soft_newline();
124124
}
125-
Self::ClassDeclaration(decl) => decl.gen(p, ctx),
126-
Self::UsingDeclaration(declaration) => declaration.gen(p, ctx),
125+
Self::ClassDeclaration(decl) => {
126+
p.print_indent();
127+
decl.gen(p, ctx);
128+
p.print_soft_newline();
129+
}
130+
Self::UsingDeclaration(declaration) => {
131+
p.print_indent();
132+
declaration.gen(p, ctx);
133+
p.print_semicolon_after_statement();
134+
}
127135
Self::TSModuleDeclaration(decl) => {
136+
p.print_indent();
128137
decl.gen(p, ctx);
129138
p.print_soft_newline();
130139
}
@@ -133,8 +142,16 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Statement<'a> {
133142
decl.gen(p, ctx);
134143
p.print_semicolon_after_statement();
135144
}
136-
Self::TSInterfaceDeclaration(decl) => decl.gen(p, ctx),
137-
Self::TSEnumDeclaration(decl) => decl.gen(p, ctx),
145+
Self::TSInterfaceDeclaration(decl) => {
146+
p.print_indent();
147+
decl.gen(p, ctx);
148+
p.print_soft_newline();
149+
}
150+
Self::TSEnumDeclaration(decl) => {
151+
p.print_indent();
152+
decl.gen(p, ctx);
153+
p.print_soft_newline();
154+
}
138155
Self::TSImportEqualsDeclaration(decl) => {
139156
p.print_indent();
140157
decl.gen(p, ctx);
@@ -565,7 +582,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for UsingDeclaration<'a> {
565582
p.print_str(b"using");
566583
p.print_soft_space();
567584
p.print_list(&self.declarations, ctx);
568-
p.needs_semicolon = true;
569585
}
570586
}
571587

@@ -858,6 +874,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {
858874
if matches!(
859875
decl,
860876
Declaration::VariableDeclaration(_)
877+
| Declaration::UsingDeclaration(_)
861878
| Declaration::TSTypeAliasDeclaration(_)
862879
| Declaration::TSImportEqualsDeclaration(_)
863880
) {
@@ -1460,14 +1477,17 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ObjectExpression<'a> {
14601477
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) {
14611478
let n = p.code_len();
14621479
p.wrap(p.start_of_stmt == n || p.start_of_arrow_expr == n, |p| {
1463-
p.print_curly_braces(self.span, self.properties.is_empty(), |p| {
1480+
let single_line = self.properties.is_empty();
1481+
p.print_curly_braces(self.span, single_line, |p| {
14641482
for (index, item) in self.properties.iter().enumerate() {
14651483
if index != 0 {
14661484
p.print_comma();
1467-
p.print_soft_space();
1485+
p.print_soft_newline();
14681486
}
14691487
p.print_indent();
14701488
item.gen(p, ctx);
1489+
}
1490+
if !single_line {
14711491
p.print_soft_newline();
14721492
}
14731493
});
@@ -3155,7 +3175,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSNamedTupleMember<'a> {
31553175

31563176
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
31573177
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
3158-
p.print_indent();
31593178
if self.modifiers.contains(ModifierKind::Export) {
31603179
p.print_str(b"export ");
31613180
}
@@ -3255,11 +3274,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSInterfaceDeclaration<'a> {
32553274
p.print_soft_newline();
32563275
}
32573276
});
3258-
if MINIFY {
3259-
p.print_hard_space();
3260-
}
3261-
p.print_soft_newline();
3262-
p.needs_semicolon = false;
32633277
}
32643278
}
32653279

@@ -3296,11 +3310,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSEnumDeclaration<'a> {
32963310
p.print_soft_newline();
32973311
}
32983312
});
3299-
if MINIFY {
3300-
p.print_hard_space();
3301-
}
3302-
p.print_soft_newline();
3303-
p.needs_semicolon = false;
33043313
}
33053314
}
33063315

tasks/coverage/codegen_runtime_test262.snap

+3-84
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
commit: 17ba9aea
1+
commit: a1587416
22

33
codegen_runtime_test262 Summary:
4-
AST Parsed : 18685/18685 (100.00%)
5-
Positive Passed: 18586/18685 (99.47%)
4+
AST Parsed : 18687/18687 (100.00%)
5+
Positive Passed: 18615/18687 (99.61%)
66
Expect to run correctly: "annexB/built-ins/String/prototype/substr/surrogate-pairs.js"
77
But got a runtime error: Test262Error: start: 1 Expected SameValue(«�», «\udf06») to be true
88

@@ -33,42 +33,6 @@ But got a runtime error: Test262Error: descriptor should be enumerable
3333
Expect to run correctly: "language/expressions/assignment/fn-name-lhs-cover.js"
3434
But got a runtime error: Test262Error: descriptor value should be ; object value should be
3535

36-
Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-arrow-function-expression.js"
37-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
38-
39-
Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-async-arrow-function-expression.js"
40-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
41-
42-
Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-function-expression.js"
43-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
44-
45-
Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-arrow-function-expression.js"
46-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
47-
48-
Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-async-arrow-function-expression.js"
49-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
50-
51-
Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-function-expression.js"
52-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
53-
54-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-arrow-function-expression.js"
55-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
56-
57-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-async-arrow-function-expression.js"
58-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
59-
60-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-function-expression.js"
61-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
62-
63-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-arrow-function-expression.js"
64-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
65-
66-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-async-arrow-function-expression.js"
67-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
68-
69-
Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-function-expression.js"
70-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
71-
7236
Expect to run correctly: "language/expressions/compound-assignment/compound-assignment-operator-calls-putvalue-lref--v--1.js"
7337
But got a runtime error: Test262Error: Expected a ReferenceError but got a TypeError
7438

@@ -144,15 +108,6 @@ But got a runtime error: Test262Error: constructor is %Promise% Expected SameVal
144108
Expect to run correctly: "language/expressions/object/__proto__-permitted-dup-shorthand.js"
145109
But got a runtime error: Test262Error: Expected SameValue(«[object Object]», «2») to be true
146110

147-
Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-arrow-function-expression.js"
148-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
149-
150-
Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-async-arrow-function-expression.js"
151-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
152-
153-
Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-function-expression.js"
154-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
155-
156111
Expect to run correctly: "language/expressions/postfix-decrement/operator-x-postfix-decrement-calls-putvalue-lhs-newvalue--1.js"
157112
But got a runtime error: Test262Error: Expected a ReferenceError but got a TypeError
158113

@@ -243,42 +198,6 @@ But got a runtime error: Test262Error: Expected a ReferenceError to be thrown bu
243198
Expect to run correctly: "language/module-code/verify-dfs.js"
244199
But got a runtime error: Test262Error: Expected SameValueB», «A») to be true
245200

246-
Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-arrow-function-expression.js"
247-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
248-
249-
Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-async-arrow-function-expression.js"
250-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
251-
252-
Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-function-expression.js"
253-
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true
254-
255-
Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-arrow-function-expression.js"
256-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
257-
258-
Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-async-arrow-function-expression.js"
259-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
260-
261-
Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-function-expression.js"
262-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
263-
264-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-arrow-function-expression.js"
265-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
266-
267-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-async-arrow-function-expression.js"
268-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
269-
270-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-function-expression.js"
271-
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true
272-
273-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-arrow-function-expression.js"
274-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
275-
276-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-async-arrow-function-expression.js"
277-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
278-
279-
Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-function-expression.js"
280-
But got a runtime error: TypeError: c[(intermediate value)] is not a function
281-
282201
Expect to run correctly: "language/statements/class/subclass/default-constructor-spread-override.js"
283202
But got a runtime error: Test262Error: @@iterator invoked
284203

0 commit comments

Comments
 (0)