v.fmt, v.gen.js: use 'if foo in [TypeA, TypeB]' in fmt.v and js.v (#11524)
parent
bd65ceb463
commit
44ec8a8cd4
|
@ -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]
|
line_break := f.array_init_break[f.array_init_depth - 1]
|
||||||
mut penalty := if line_break { 0 } else { 4 }
|
mut penalty := if line_break { 0 } else { 4 }
|
||||||
if penalty > 0 {
|
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--
|
penalty--
|
||||||
}
|
}
|
||||||
if should_decrease_arr_penalty(expr) {
|
if expr in [ast.ArrayInit, ast.StructInit, ast.MapInit, ast.CallExpr] {
|
||||||
penalty--
|
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) {
|
pub fn (mut f Fmt) as_cast(node ast.AsCast) {
|
||||||
f.mark_types_import_as_used(node.typ)
|
f.mark_types_import_as_used(node.typ)
|
||||||
type_str := f.table.type_to_str_using_aliases(node.typ, f.mod2alias)
|
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
|
cur_pos := f.out.len
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
cond_len := f.out.len - cur_pos
|
cond_len := f.out.len - cur_pos
|
||||||
is_cond_wrapped := cond_len > 0
|
is_cond_wrapped := cond_len > 0 && branch.cond in [ast.IfGuardExpr, ast.CallExpr]
|
||||||
&& (branch.cond is ast.IfGuardExpr || branch.cond is ast.CallExpr)
|
|
||||||
&& f.out.last_n(cond_len).contains('\n')
|
&& f.out.last_n(cond_len).contains('\n')
|
||||||
if is_cond_wrapped {
|
if is_cond_wrapped {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
|
|
@ -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.len == 1 {
|
||||||
if branch.stmts[0] is ast.ExprStmt {
|
if branch.stmts[0] is ast.ExprStmt {
|
||||||
stmt := branch.stmts[0] as ast.ExprStmt
|
stmt := branch.stmts[0] as ast.ExprStmt
|
||||||
if stmt.expr is ast.CallExpr || stmt.expr is ast.IfExpr
|
if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr]
|
||||||
|| stmt.expr is ast.MatchExpr || (stmt.expr is ast.IndexExpr
|
|| (stmt.expr is ast.IndexExpr
|
||||||
&& (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) {
|
&& (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -1867,9 +1867,8 @@ fn (mut g JsGen) match_expr(node ast.MatchExpr) {
|
||||||
g.inside_ternary = true
|
g.inside_ternary = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.cond is ast.Ident || node.cond is ast.SelectorExpr || node.cond is ast.IntegerLiteral
|
if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral,
|
||||||
|| node.cond is ast.StringLiteral || node.cond is ast.FloatLiteral
|
ast.FloatLiteral, ast.CallExpr] {
|
||||||
|| node.cond is ast.CallExpr {
|
|
||||||
cond_var = CondExpr{node.cond}
|
cond_var = CondExpr{node.cond}
|
||||||
} else {
|
} else {
|
||||||
s := g.new_tmp_var()
|
s := g.new_tmp_var()
|
||||||
|
|
Loading…
Reference in New Issue