From 5901d2a0f141d6913944fbb4943c77aa96c11b80 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:03:28 +0000 Subject: [PATCH] fix(codegen): various spacing issues (#5820) --- crates/oxc_codegen/src/gen.rs | 61 +++++++++++++------ .../integration/snapshots/pure_comments.snap | 29 ++++++++- .../tests/integration/snapshots/ts.snap | 18 ++++-- crates/oxc_codegen/tests/integration/unit.rs | 4 +- .../tests/snapshots/as-const.snap | 2 +- .../tests/snapshots/mapped-types.snap | 2 +- .../tests/snapshots/readonly.snap | 2 +- tasks/coverage/src/suite.rs | 2 +- 8 files changed, 87 insertions(+), 33 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 47ed1c33ac0ba..8c309e53279bf 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -508,9 +508,6 @@ impl<'a> Gen for TryStatement<'a> { } p.print_soft_space(); p.print_block_statement(&handler.body, ctx); - if self.finalizer.is_some() { - p.print_soft_newline(); - } } if let Some(finalizer) = &self.finalizer { p.print_soft_space(); @@ -1426,7 +1423,7 @@ impl<'a> Gen for ArrayExpressionElement<'a> { self.to_expression().print_expr(p, Precedence::Comma, Context::empty()); } Self::SpreadElement(elem) => elem.print(p, ctx), - Self::Elision(_span) => p.print_comma(), + Self::Elision(_span) => {} } } } @@ -1441,19 +1438,34 @@ impl<'a> Gen for SpreadElement<'a> { impl<'a> Gen for ArrayExpression<'a> { fn gen(&self, p: &mut Codegen, ctx: Context) { + let is_multi_line = self.elements.len() > 2; p.add_source_mapping(self.span.start); p.print_char(b'['); - for (index, item) in self.elements.iter().enumerate() { - item.print(p, ctx); - if index != self.elements.len() - 1 { - if !matches!(item, ArrayExpressionElement::Elision(_)) { - p.print_comma(); - } + if is_multi_line { + p.indent(); + } + for (i, item) in self.elements.iter().enumerate() { + if i != 0 { + p.print_comma(); + } + if is_multi_line { + p.print_soft_newline(); + p.print_indent(); + } else if i != 0 { p.print_soft_space(); } + item.print(p, ctx); + if i == self.elements.len() - 1 && matches!(item, ArrayExpressionElement::Elision(_)) { + p.print_comma(); + } + } + if is_multi_line { + p.print_soft_newline(); + p.dedent(); + p.print_indent(); } - p.print_char(b']'); p.add_source_mapping(self.span.end); + p.print_char(b']'); } } @@ -2629,7 +2641,9 @@ impl<'a> Gen for ObjectPattern<'a> { fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'{'); - p.print_soft_space(); + if !self.is_empty() { + p.print_soft_space(); + } p.print_list(&self.properties, ctx); if let Some(rest) = &self.rest { if !self.properties.is_empty() { @@ -2637,7 +2651,9 @@ impl<'a> Gen for ObjectPattern<'a> { } rest.print(p, ctx); } - p.print_soft_space(); + if !self.is_empty() { + p.print_soft_space(); + } p.print_char(b'}'); p.add_source_mapping(self.span.end); } @@ -2911,19 +2927,19 @@ impl<'a> Gen for TSIndexedAccessType<'a> { impl<'a> Gen for TSMappedType<'a> { fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("{"); + p.print_soft_space(); match self.readonly { TSMappedTypeModifierOperator::True => { - p.print_str("readonly"); + p.print_str("readonly "); } TSMappedTypeModifierOperator::Plus => { - p.print_str("+readonly"); + p.print_str("+readonly "); } TSMappedTypeModifierOperator::Minus => { - p.print_str("-readonly"); + p.print_str("-readonly "); } TSMappedTypeModifierOperator::None => {} } - p.print_hard_space(); p.print_str("["); self.type_parameter.name.print(p, ctx); if let Some(constraint) = &self.type_parameter.constraint { @@ -2957,6 +2973,7 @@ impl<'a> Gen for TSMappedType<'a> { p.print_soft_space(); type_annotation.print(p, ctx); } + p.print_soft_space(); p.print_str("}"); } } @@ -3061,9 +3078,15 @@ impl<'a> Gen for TSTypeLiteral<'a> { let single_line = self.members.len() <= 1; p.print_curly_braces(self.span, single_line, |p| { for item in &self.members { - p.print_indent(); + if single_line { + p.print_soft_space(); + } else { + p.print_indent(); + } item.print(p, ctx); - if !single_line { + if single_line { + p.print_soft_space(); + } else { p.print_semicolon(); p.print_soft_newline(); } diff --git a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap index 0047e85acf0e1..e3c30791ccf0a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap @@ -14,7 +14,16 @@ x([ /* #__NO_SIDE_EFFECTS__ */ async function* y() {}, ]) ---------- -x([/* #__NO_SIDE_EFFECTS__ */ function() {}, /* #__NO_SIDE_EFFECTS__ */ function y() {}, /* #__NO_SIDE_EFFECTS__ */ function* () {}, /* #__NO_SIDE_EFFECTS__ */ function* y() {}, /* #__NO_SIDE_EFFECTS__ */ async function() {}, /* #__NO_SIDE_EFFECTS__ */ async function y() {}, /* #__NO_SIDE_EFFECTS__ */ async function* () {}, /* #__NO_SIDE_EFFECTS__ */ async function* y() {}]); +x([ + /* #__NO_SIDE_EFFECTS__ */ function() {}, + /* #__NO_SIDE_EFFECTS__ */ function y() {}, + /* #__NO_SIDE_EFFECTS__ */ function* () {}, + /* #__NO_SIDE_EFFECTS__ */ function* y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function() {}, + /* #__NO_SIDE_EFFECTS__ */ async function y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function* () {}, + /* #__NO_SIDE_EFFECTS__ */ async function* y() {} +]); ########## 1 @@ -27,7 +36,14 @@ x([ /* #__NO_SIDE_EFFECTS__ */ async (y) => (y), ]) ---------- -x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y]); +x([ + /* #__NO_SIDE_EFFECTS__ */ (y) => y, + /* #__NO_SIDE_EFFECTS__ */ () => {}, + /* #__NO_SIDE_EFFECTS__ */ (y) => y, + /* #__NO_SIDE_EFFECTS__ */ async (y) => y, + /* #__NO_SIDE_EFFECTS__ */ async () => {}, + /* #__NO_SIDE_EFFECTS__ */ async (y) => y +]); ########## 2 @@ -40,7 +56,14 @@ x([ /* #__NO_SIDE_EFFECTS__ */ async (y) => (y), ]) ---------- -x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y]); +x([ + /* #__NO_SIDE_EFFECTS__ */ (y) => y, + /* #__NO_SIDE_EFFECTS__ */ () => {}, + /* #__NO_SIDE_EFFECTS__ */ (y) => y, + /* #__NO_SIDE_EFFECTS__ */ async (y) => y, + /* #__NO_SIDE_EFFECTS__ */ async () => {}, + /* #__NO_SIDE_EFFECTS__ */ async (y) => y +]); ########## 3 diff --git a/crates/oxc_codegen/tests/integration/snapshots/ts.snap b/crates/oxc_codegen/tests/integration/snapshots/ts.snap index 7333861d1a2d5..b443a8333b53a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/ts.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/ts.snap @@ -18,12 +18,20 @@ function foo(x: T, y: string, ...restOfParams: Omit): ########## 2 let x: string[] = ['abc', 'def', 'ghi']; ---------- -let x: string[] = ['abc', 'def', 'ghi']; +let x: string[] = [ + 'abc', + 'def', + 'ghi' +]; ########## 3 let x: Array = ['abc', 'def', 'ghi',]; ---------- -let x: Array = ['abc', 'def', 'ghi']; +let x: Array = [ + 'abc', + 'def', + 'ghi' +]; ########## 4 let x: [string, number] = ['abc', 123]; @@ -91,7 +99,7 @@ export { Foo, type Bar } from 'foo'; ########## 15 type A = { [K in keyof T as K extends string ? B : K ]: T[K] } ---------- -type A = { [K in keyof T as K extends string ? B : K] : T[K]}; +type A = { [K in keyof T as K extends string ? B : K] : T[K] }; ########## 16 class A {readonly type = 'frame'} @@ -103,12 +111,12 @@ class A { ########## 17 let foo: { (t: T): void } ---------- -let foo: {(t: T): void}; +let foo: { (t: T): void }; ########## 18 let foo: { new (t: T): void } ---------- -let foo: {new (t: T): void}; +let foo: { new (t: T): void }; ########## 19 function (){} diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index b0955f2b4123f..3073fdf8b1d13 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -92,8 +92,8 @@ fn regex() { #[test] fn comma() { - test("[1, 2, 3]", "[1, 2, 3];\n"); - test("[1, 2, 3,]", "[1, 2, 3];\n"); + test("[1, 2, 3]", "[\n\t1,\n\t2,\n\t3\n];\n"); + test("[1, 2, 3,]", "[\n\t1,\n\t2,\n\t3\n];\n"); test("[,]", "[,];\n"); test("[,,]", "[, ,];\n"); test("[,1]", "[, 1];\n"); diff --git a/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap b/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap index ee24feeabf582..2f10a1d410c60 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/as-const.snap @@ -18,5 +18,5 @@ declare const F: { readonly a: "a"; readonly b: "b"; }; - readonly array: readonly ["a", undefined, { readonly b: "\n"}]; + readonly array: readonly ["a", undefined, { readonly b: "\n" }]; }; diff --git a/crates/oxc_isolated_declarations/tests/snapshots/mapped-types.snap b/crates/oxc_isolated_declarations/tests/snapshots/mapped-types.snap index e9656e3d14c79..cda301bc6ee25 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/mapped-types.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/mapped-types.snap @@ -7,5 +7,5 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/mapped-types.ts import { K } from "foo"; import { T } from "bar"; export interface I { - prop: { [key in K] : T}; + prop: { [key in K] : T }; } diff --git a/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap b/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap index fb2c57bbe13b7..6f74cb7c2bc61 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/readonly.snap @@ -4,5 +4,5 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/readonly.ts --- ==================== .D.TS ==================== -export declare const EMPTY_OBJ: {readonly [key: string]: any}; +export declare const EMPTY_OBJ: { readonly [key: string]: any }; export declare const EMPTY_ARR: readonly never[]; diff --git a/tasks/coverage/src/suite.rs b/tasks/coverage/src/suite.rs index 76be4730f04f3..33d6ecc1dc95d 100644 --- a/tasks/coverage/src/suite.rs +++ b/tasks/coverage/src/suite.rs @@ -50,7 +50,7 @@ pub trait Suite { self.read_test_cases(name, args); self.get_test_cases_mut().par_iter_mut().for_each(|case| { if args.debug { - println!("{:?}", case.path()); + println!("{}", case.path().to_string_lossy()); } case.run(); });