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 { } else {
tok.lit tok.lit
} }
return if typ in [.unone, .name] { return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
lit
} else {
'<span class="token $typ">$lit</span>'
}
} }
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{}) mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut tok := s.scan() 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 { for pos >= 0 && s[pos] in cs_arr {
pos-- pos--
} }
return if pos < 0 { return if pos < 0 { '' } else { s[..pos + 1] }
''
} else {
s[..pos + 1]
}
} }
// trim_prefix strips `str` from the start of the string. // 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. // ok_message returns a colored string with green color.
// If colors are not allowed, returns a given string. // If colors are not allowed, returns a given string.
pub fn ok_message(s string) string { pub fn ok_message(s string) string {
return if can_show_color_on_stdout() { return if can_show_color_on_stdout() { green(' $s ') } else { s }
green(' $s ')
} else {
s
}
} }
// fail_message returns a colored string with red color. // fail_message returns a colored string with red color.
// If colors are not allowed, returns a given string. // If colors are not allowed, returns a given string.
pub fn fail_message(s string) string { pub fn fail_message(s string) string {
return if can_show_color_on_stdout() { return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
inverse(bg_white(bold(red(' $s '))))
} else {
s
}
} }
// warn_message returns a colored string with yellow color. // warn_message returns a colored string with yellow color.
// If colors are not allowed, returns a given string. // If colors are not allowed, returns a given string.
pub fn warn_message(s string) string { pub fn warn_message(s string) string {
return if can_show_color_on_stdout() { return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
bright_yellow(' $s ')
} else {
s
}
} }
// colorize returns a colored string by running the specified `cfn` over // 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 { fn imax(x int, y int) int {
return if x > y { return if x > y { x } else { y }
x
} else {
y
}
} }
fn supports_escape_sequences(fd int) bool { 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 return type_hi
} else if idx_lo >= table.i8_type_idx } else if idx_lo >= table.i8_type_idx
&& (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed && (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
return if idx_lo == table.i64_type_idx { return if idx_lo == table.i64_type_idx { type_lo } else { type_hi }
type_lo
} else {
type_hi
}
} else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) { } 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 return type_lo // conversion unsigned -> signed if signed type is larger
} else { } 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) c.warn('`++` and `--` are statements, not expressions', infix_expr.pos)
} }
*/ */
return if infix_expr.op.is_relational() { return if infix_expr.op.is_relational() { table.bool_type } else { return_type }
table.bool_type
} else {
return_type
}
} }
// returns name and position of variable that needs write lock // 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 // until `v.eval` is stable, I can't think of a better way to do this
different := expr.str() != cond.right.str() different := expr.str() != cond.right.str()
return if cond.op == .eq { return if cond.op == .eq { different } else { !different }
different
} else {
!different
}
} else { } else {
c.error('invalid `\$if` condition: ${cond.left.type_name()}1', c.error('invalid `\$if` condition: ${cond.left.type_name()}1',
cond.pos) cond.pos)

View File

@ -537,11 +537,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
[inline] [inline]
fn abs(v int) int { fn abs(v int) int {
return if v >= 0 { return if v >= 0 { v } else { -v }
v
} else {
-v
}
} }
const ( 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) { pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
dollar := if it.is_comptime { '$' } else { '' } dollar := if node.is_comptime { '$' } else { '' }
mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0]) mut single_line := node.branches.len == 2 && node.has_else
&& branch_is_single_line(it.branches[1]) && branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
&& (it.is_expr || f.is_assign || f.single_line_fields) && (node.is_expr || f.is_assign || f.single_line_fields)
f.single_line_if = single_line f.single_line_if = single_line
if_start := f.line_len if_start := f.line_len
for { for {
for i, branch in it.branches { for i, branch in node.branches {
if i == 0 { if i == 0 {
// first `if` // first `if`
f.comments(branch.comments, {}) f.comments(branch.comments, {})
@ -1658,7 +1654,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
} }
f.write('${dollar}else ') 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 ') f.write('${dollar}if ')
cur_pos := f.out.len cur_pos := f.out.len
f.expr(branch.cond) f.expr(branch.cond)
@ -1696,9 +1692,9 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
} }
f.write('}') f.write('}')
f.single_line_if = false f.single_line_if = false
if it.post_comments.len > 0 { if node.post_comments.len > 0 {
f.writeln('') 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' '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 { fn abs(a i64) i64 {
return if a < 0 { return if a < 0 { -a } else { a }
-a
} else {
a
}
} }
fn (mut g Gen) jle(addr i64) { 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_ct_if_expr = was_inside_ct_if_expr
} }
p.inside_if_expr = true p.inside_if_expr = true
is_expr := p.prev_tok.kind == .key_return
mut pos := p.tok.position() mut pos := p.tok.position()
if is_comptime { if is_comptime {
p.inside_ct_if_expr = true p.inside_ct_if_expr = true
@ -152,6 +153,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
post_comments: comments post_comments: comments
pos: pos pos: pos
has_else: has_else has_else: has_else
is_expr: is_expr
} }
} }

View File

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

View File

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