Skip to content

Commit

Permalink
feat(codegen): improve codegen formatting (#3731)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 18, 2024
1 parent ee627c3 commit bf9b38a
Show file tree
Hide file tree
Showing 5 changed files with 744 additions and 620 deletions.
45 changes: 27 additions & 18 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,18 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Statement<'a> {
decl.gen(p, ctx);
p.print_soft_newline();
}
Self::ClassDeclaration(decl) => decl.gen(p, ctx),
Self::UsingDeclaration(declaration) => declaration.gen(p, ctx),
Self::ClassDeclaration(decl) => {
p.print_indent();
decl.gen(p, ctx);
p.print_soft_newline();
}
Self::UsingDeclaration(declaration) => {
p.print_indent();
declaration.gen(p, ctx);
p.print_semicolon_after_statement();
}
Self::TSModuleDeclaration(decl) => {
p.print_indent();
decl.gen(p, ctx);
p.print_soft_newline();
}
Expand All @@ -133,8 +142,16 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Statement<'a> {
decl.gen(p, ctx);
p.print_semicolon_after_statement();
}
Self::TSInterfaceDeclaration(decl) => decl.gen(p, ctx),
Self::TSEnumDeclaration(decl) => decl.gen(p, ctx),
Self::TSInterfaceDeclaration(decl) => {
p.print_indent();
decl.gen(p, ctx);
p.print_soft_newline();
}
Self::TSEnumDeclaration(decl) => {
p.print_indent();
decl.gen(p, ctx);
p.print_soft_newline();
}
Self::TSImportEqualsDeclaration(decl) => {
p.print_indent();
decl.gen(p, ctx);
Expand Down Expand Up @@ -565,7 +582,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for UsingDeclaration<'a> {
p.print_str(b"using");
p.print_soft_space();
p.print_list(&self.declarations, ctx);
p.needs_semicolon = true;
}
}

Expand Down Expand Up @@ -858,6 +874,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {
if matches!(
decl,
Declaration::VariableDeclaration(_)
| Declaration::UsingDeclaration(_)
| Declaration::TSTypeAliasDeclaration(_)
| Declaration::TSImportEqualsDeclaration(_)
) {
Expand Down Expand Up @@ -1460,14 +1477,17 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ObjectExpression<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) {
let n = p.code_len();
p.wrap(p.start_of_stmt == n || p.start_of_arrow_expr == n, |p| {
p.print_curly_braces(self.span, self.properties.is_empty(), |p| {
let single_line = self.properties.is_empty();
p.print_curly_braces(self.span, single_line, |p| {
for (index, item) in self.properties.iter().enumerate() {
if index != 0 {
p.print_comma();
p.print_soft_space();
p.print_soft_newline();
}
p.print_indent();
item.gen(p, ctx);
}
if !single_line {
p.print_soft_newline();
}
});
Expand Down Expand Up @@ -3155,7 +3175,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSNamedTupleMember<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.print_indent();
if self.modifiers.contains(ModifierKind::Export) {
p.print_str(b"export ");
}
Expand Down Expand Up @@ -3255,11 +3274,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSInterfaceDeclaration<'a> {
p.print_soft_newline();
}
});
if MINIFY {
p.print_hard_space();
}
p.print_soft_newline();
p.needs_semicolon = false;
}
}

Expand Down Expand Up @@ -3296,11 +3310,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSEnumDeclaration<'a> {
p.print_soft_newline();
}
});
if MINIFY {
p.print_hard_space();
}
p.print_soft_newline();
p.needs_semicolon = false;
}
}

Expand Down
87 changes: 3 additions & 84 deletions tasks/coverage/codegen_runtime_test262.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
commit: 17ba9aea
commit: a1587416

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

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

Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-accessors-computed-property-name-from-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/class/cpn-class-expr-computed-property-name-from-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-computed-property-name-from-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/class/cpn-class-expr-fields-methods-computed-property-name-from-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/expressions/compound-assignment/compound-assignment-operator-calls-putvalue-lref--v--1.js"
But got a runtime error: Test262Error: Expected a ReferenceError but got a TypeError

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

Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/object/cpn-obj-lit-computed-property-name-from-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/expressions/postfix-decrement/operator-x-postfix-decrement-calls-putvalue-lhs-newvalue--1.js"
But got a runtime error: Test262Error: Expected a ReferenceError but got a TypeError

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

Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-accessors-computed-property-name-from-function-expression.js"
But got a runtime error: Test262Error: Expected SameValueundefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/cpn-class-decl-computed-property-name-from-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-computed-property-name-from-function-expression.js"
But got a runtime error: Test262Error: Expected SameValue(«undefined», «1») to be true

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-async-arrow-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/cpn-class-decl-fields-methods-computed-property-name-from-function-expression.js"
But got a runtime error: TypeError: c[(intermediate value)] is not a function

Expect to run correctly: "language/statements/class/subclass/default-constructor-spread-override.js"
But got a runtime error: Test262Error: @@iterator invoked

Expand Down
Loading

0 comments on commit bf9b38a

Please sign in to comment.