Skip to content

Commit

Permalink
refactor(codegen): only print necessary parentheses in TSAsExpression (
Browse files Browse the repository at this point in the history
…#6429)

Part of fixing #6385
  • Loading branch information
Dunqing committed Oct 11, 2024
1 parent 7458089 commit 702b574
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
14 changes: 7 additions & 7 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2092,13 +2092,13 @@ impl<'a> GenExpr for NewExpression<'a> {

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

p.wrap(wrap, |p| {
self.expression.print_expr(p, Precedence::Exponentiation, ctx);
p.print_str(" as ");
self.type_annotation.print(p, ctx);
});
}
}

Expand Down
17 changes: 16 additions & 1 deletion crates/oxc_codegen/tests/integration/snapshots/ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ a = x!;
########## 25
b = (x as y);
----------
b = ((x) as y);
b = x as y;

########## 26
c = foo<string>;
Expand Down Expand Up @@ -210,3 +210,18 @@ export type Component<
E extends EmitsOptions | Record<string, any[]> = {},
S extends Record<string, any> = any
> = 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;
5 changes: 4 additions & 1 deletion crates/oxc_codegen/tests/integration/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export type Component<
> =
| ConcreteComponent<Props, RawBindings, D, C, M, E, S>
| ComponentPublicInstanceConstructor<Props>
"
",
"(a || b) as any",
"(a ** b) as any",
"(function g() {}) as any",
];

snapshot("ts", &cases);
Expand Down

0 comments on commit 702b574

Please sign in to comment.