Skip to content

Commit

Permalink
fix(codegen): various spacing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 17, 2024
1 parent 18e4ac2 commit 475d5e3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 33 deletions.
61 changes: 42 additions & 19 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) => {}
}
}
}
Expand All @@ -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']');
}
}

Expand Down Expand Up @@ -2629,15 +2641,19 @@ 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() {
p.print_comma();
}
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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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("}");
}
}
Expand Down Expand Up @@ -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();
}
Expand Down
29 changes: 26 additions & 3 deletions crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
18 changes: 13 additions & 5 deletions crates/oxc_codegen/tests/integration/snapshots/ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ function foo<T extends string>(x: T, y: string, ...restOfParams: Omit<T, 'x'>):
########## 2
let x: string[] = ['abc', 'def', 'ghi'];
----------
let x: string[] = ['abc', 'def', 'ghi'];
let x: string[] = [
'abc',
'def',
'ghi'
];

########## 3
let x: Array<string> = ['abc', 'def', 'ghi',];
----------
let x: Array<string> = ['abc', 'def', 'ghi'];
let x: Array<string> = [
'abc',
'def',
'ghi'
];

########## 4
let x: [string, number] = ['abc', 123];
Expand Down Expand Up @@ -91,7 +99,7 @@ export { Foo, type Bar } from 'foo';
########## 15
type A<T> = { [K in keyof T as K extends string ? B<K> : K ]: T[K] }
----------
type A<T> = { [K in keyof T as K extends string ? B<K> : K] : T[K]};
type A<T> = { [K in keyof T as K extends string ? B<K> : K] : T[K] };

########## 16
class A {readonly type = 'frame'}
Expand All @@ -103,12 +111,12 @@ class A {
########## 17
let foo: { <T>(t: T): void }
----------
let foo: {<T>(t: T): void};
let foo: { <T>(t: T): void };

########## 18
let foo: { new <T>(t: T): void }
----------
let foo: {new <T>(t: T): void};
let foo: { new <T>(t: T): void };

########## 19
function <const T>(){}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }];
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
2 changes: 1 addition & 1 deletion tasks/coverage/src/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait Suite<T: Case> {
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();
});
Expand Down

0 comments on commit 475d5e3

Please sign in to comment.