v.fmt, v.gen.js: use 'if foo in [TypeA, TypeB]' in fmt.v and js.v (#11524)

pull/11529/head
yuyi 2021-09-18 02:59:16 +08:00 committed by GitHub
parent bd65ceb463
commit 44ec8a8cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 16 deletions

View File

@ -1382,10 +1382,11 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
line_break := f.array_init_break[f.array_init_depth - 1]
mut penalty := if line_break { 0 } else { 4 }
if penalty > 0 {
if i == 0 || should_decrease_arr_penalty(node.exprs[i - 1]) {
if i == 0
|| node.exprs[i - 1] in [ast.ArrayInit, ast.StructInit, ast.MapInit, ast.CallExpr] {
penalty--
}
if should_decrease_arr_penalty(expr) {
if expr in [ast.ArrayInit, ast.StructInit, ast.MapInit, ast.CallExpr] {
penalty--
}
}
@ -1504,13 +1505,6 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
}
}
fn should_decrease_arr_penalty(e ast.Expr) bool {
if e is ast.ArrayInit || e is ast.StructInit || e is ast.MapInit || e is ast.CallExpr {
return true
}
return false
}
pub fn (mut f Fmt) as_cast(node ast.AsCast) {
f.mark_types_import_as_used(node.typ)
type_str := f.table.type_to_str_using_aliases(node.typ, f.mod2alias)
@ -1794,8 +1788,7 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
cur_pos := f.out.len
f.expr(branch.cond)
cond_len := f.out.len - cur_pos
is_cond_wrapped := cond_len > 0
&& (branch.cond is ast.IfGuardExpr || branch.cond is ast.CallExpr)
is_cond_wrapped := cond_len > 0 && branch.cond in [ast.IfGuardExpr, ast.CallExpr]
&& f.out.last_n(cond_len).contains('\n')
if is_cond_wrapped {
f.writeln('')

View File

@ -1702,8 +1702,8 @@ fn (mut g JsGen) need_tmp_var_in_match(node ast.MatchExpr) bool {
if branch.stmts.len == 1 {
if branch.stmts[0] is ast.ExprStmt {
stmt := branch.stmts[0] as ast.ExprStmt
if stmt.expr is ast.CallExpr || stmt.expr is ast.IfExpr
|| stmt.expr is ast.MatchExpr || (stmt.expr is ast.IndexExpr
if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr]
|| (stmt.expr is ast.IndexExpr
&& (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) {
return true
}
@ -1867,9 +1867,8 @@ fn (mut g JsGen) match_expr(node ast.MatchExpr) {
g.inside_ternary = true
}
if node.cond is ast.Ident || node.cond is ast.SelectorExpr || node.cond is ast.IntegerLiteral
|| node.cond is ast.StringLiteral || node.cond is ast.FloatLiteral
|| node.cond is ast.CallExpr {
if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral,
ast.FloatLiteral, ast.CallExpr] {
cond_var = CondExpr{node.cond}
} else {
s := g.new_tmp_var()