mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(codegen): various spacing issues (#5820)
This commit is contained in:
parent
18e4ac22ad
commit
5901d2a0f1
8 changed files with 87 additions and 33 deletions
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
@ -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'}
|
||||
|
|
@ -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>(){}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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" }];
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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[];
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue