fmt: single line ternary return (#8605)

pull/8634/head
Lukas Neubert 2021-02-08 00:28:46 +01:00 committed by GitHub
parent 118ca1240e
commit 473cd1d416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 49 additions and 79 deletions

View File

@ -353,11 +353,7 @@ fn html_highlight(code string, tb &table.Table) string {
} else {
tok.lit
}
return if typ in [.unone, .name] {
lit
} else {
'<span class="token $typ">$lit</span>'
}
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
}
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut tok := s.scan()

View File

@ -1053,11 +1053,7 @@ pub fn (s string) trim_right(cutset string) string {
for pos >= 0 && s[pos] in cs_arr {
pos--
}
return if pos < 0 {
''
} else {
s[..pos + 1]
}
return if pos < 0 { '' } else { s[..pos + 1] }
}
// trim_prefix strips `str` from the start of the string.

View File

@ -29,31 +29,19 @@ pub fn can_show_color_on_stderr() bool {
// ok_message returns a colored string with green color.
// If colors are not allowed, returns a given string.
pub fn ok_message(s string) string {
return if can_show_color_on_stdout() {
green(' $s ')
} else {
s
}
return if can_show_color_on_stdout() { green(' $s ') } else { s }
}
// fail_message returns a colored string with red color.
// If colors are not allowed, returns a given string.
pub fn fail_message(s string) string {
return if can_show_color_on_stdout() {
inverse(bg_white(bold(red(' $s '))))
} else {
s
}
return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
}
// warn_message returns a colored string with yellow color.
// If colors are not allowed, returns a given string.
pub fn warn_message(s string) string {
return if can_show_color_on_stdout() {
bright_yellow(' $s ')
} else {
s
}
return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
}
// colorize returns a colored string by running the specified `cfn` over
@ -107,11 +95,7 @@ pub fn header(text string, divider string) string {
}
fn imax(x int, y int) int {
return if x > y {
x
} else {
y
}
return if x > y { x } else { y }
}
fn supports_escape_sequences(fd int) bool {

View File

@ -183,11 +183,7 @@ fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.T
return type_hi
} else if idx_lo >= table.i8_type_idx
&& (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
return if idx_lo == table.i64_type_idx {
type_lo
} else {
type_hi
}
return if idx_lo == table.i64_type_idx { type_lo } else { type_hi }
} else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) {
return type_lo // conversion unsigned -> signed if signed type is larger
} else {

View File

@ -1049,11 +1049,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
c.warn('`++` and `--` are statements, not expressions', infix_expr.pos)
}
*/
return if infix_expr.op.is_relational() {
table.bool_type
} else {
return_type
}
return if infix_expr.op.is_relational() { table.bool_type } else { return_type }
}
// returns name and position of variable that needs write lock
@ -4964,11 +4960,7 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool {
// :)
// until `v.eval` is stable, I can't think of a better way to do this
different := expr.str() != cond.right.str()
return if cond.op == .eq {
different
} else {
!different
}
return if cond.op == .eq { different } else { !different }
} else {
c.error('invalid `\$if` condition: ${cond.left.type_name()}1',
cond.pos)

View File

@ -537,11 +537,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
[inline]
fn abs(v int) int {
return if v >= 0 {
v
} else {
-v
}
return if v >= 0 { v } else { -v }
}
const (
@ -1636,15 +1632,15 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, ignore_paren bool) {
}
}
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
dollar := if it.is_comptime { '$' } else { '' }
mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0])
&& branch_is_single_line(it.branches[1])
&& (it.is_expr || f.is_assign || f.single_line_fields)
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
dollar := if node.is_comptime { '$' } else { '' }
mut single_line := node.branches.len == 2 && node.has_else
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
&& (node.is_expr || f.is_assign || f.single_line_fields)
f.single_line_if = single_line
if_start := f.line_len
for {
for i, branch in it.branches {
for i, branch in node.branches {
if i == 0 {
// first `if`
f.comments(branch.comments, {})
@ -1658,7 +1654,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
}
f.write('${dollar}else ')
}
if i < it.branches.len - 1 || !it.has_else {
if i < node.branches.len - 1 || !node.has_else {
f.write('${dollar}if ')
cur_pos := f.out.len
f.expr(branch.cond)
@ -1696,9 +1692,9 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
}
f.write('}')
f.single_line_if = false
if it.post_comments.len > 0 {
if node.post_comments.len > 0 {
f.writeln('')
f.comments(it.post_comments, has_nl: false)
f.comments(node.post_comments, has_nl: false)
}
}

View File

@ -0,0 +1,9 @@
fn non_ternary_return() string {
return if some_cond {
'foo'
} else if false {
'bar'
} else {
'baz'
}
}

View File

@ -0,0 +1,3 @@
fn non_ternary_return() string {
return if some_cond { 'foo' } else if false { 'bar' } else { 'baz' }
}

View File

@ -20,3 +20,15 @@ fn requires_multiple_lines() {
'other str'
}
}
fn return_ternary(cond bool) int {
return if cond { 5 } else { 12 }
}
fn long_return_ternary() string {
return if false {
'spam and eggs'
} else {
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
}
}

View File

@ -249,11 +249,7 @@ fn (mut g Gen) jmp(addr int) {
}
fn abs(a i64) i64 {
return if a < 0 {
-a
} else {
a
}
return if a < 0 { -a } else { a }
}
fn (mut g Gen) jle(addr i64) {

View File

@ -15,6 +15,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
p.inside_ct_if_expr = was_inside_ct_if_expr
}
p.inside_if_expr = true
is_expr := p.prev_tok.kind == .key_return
mut pos := p.tok.position()
if is_comptime {
p.inside_ct_if_expr = true
@ -152,6 +153,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
post_comments: comments
pos: pos
has_else: has_else
is_expr: is_expr
}
}

View File

@ -1182,11 +1182,7 @@ fn (mut s Scanner) ident_char() string {
}
}
// Escapes a `'` character
return if c == "'" {
'\\' + c
} else {
c
}
return if c == "'" { '\\' + c } else { c }
}
[inline]

View File

@ -349,20 +349,12 @@ pub fn skip_bom(file_content string) string {
[inline]
pub fn imin(a int, b int) int {
return if a < b {
a
} else {
b
}
return if a < b { a } else { b }
}
[inline]
pub fn imax(a int, b int) int {
return if a > b {
a
} else {
b
}
return if a > b { a } else { b }
}
pub fn replace_op(s string) string {