refactor(codegen): only print necessary parentheses in TSAsExpression (#6429)

Part of fixing #6385
This commit is contained in:
Dunqing 2024-10-11 02:04:27 +00:00
parent 7458089e3c
commit 702b574afb
3 changed files with 27 additions and 9 deletions

View file

@ -2092,13 +2092,13 @@ impl<'a> GenExpr for NewExpression<'a> {
impl<'a> GenExpr for TSAsExpression<'a> { impl<'a> GenExpr for TSAsExpression<'a> {
fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) {
p.print_char(b'('); let wrap = precedence >= Precedence::Shift;
p.print_char(b'(');
self.expression.print_expr(p, precedence, Context::default()); p.wrap(wrap, |p| {
p.print_char(b')'); self.expression.print_expr(p, Precedence::Exponentiation, ctx);
p.print_str(" as "); p.print_str(" as ");
self.type_annotation.print(p, ctx); self.type_annotation.print(p, ctx);
p.print_char(b')'); });
} }
} }

View file

@ -159,7 +159,7 @@ a = x!;
########## 25 ########## 25
b = (x as y); b = (x as y);
---------- ----------
b = ((x) as y); b = x as y;
########## 26 ########## 26
c = foo<string>; c = foo<string>;
@ -210,3 +210,18 @@ export type Component<
E extends EmitsOptions | Record<string, any[]> = {}, E extends EmitsOptions | Record<string, any[]> = {},
S extends Record<string, any> = any S extends Record<string, any> = any
> = ConcreteComponent<Props, RawBindings, D, C, M, E, S> | ComponentPublicInstanceConstructor<Props>; > = ConcreteComponent<Props, RawBindings, D, C, M, E, S> | ComponentPublicInstanceConstructor<Props>;
########## 32
(a || b) as any
----------
(a || b) as any;
########## 33
(a ** b) as any
----------
(a ** b) as any;
########## 34
(function g() {}) as any
----------
(function g() {}) as any;

View file

@ -50,7 +50,10 @@ export type Component<
> = > =
| ConcreteComponent<Props, RawBindings, D, C, M, E, S> | ConcreteComponent<Props, RawBindings, D, C, M, E, S>
| ComponentPublicInstanceConstructor<Props> | ComponentPublicInstanceConstructor<Props>
" ",
"(a || b) as any",
"(a ** b) as any",
"(function g() {}) as any",
]; ];
snapshot("ts", &cases); snapshot("ts", &cases);