scanner: warn about interpolation for `$f(expr)` (#6179)

pull/6192/head
Nick Treleaven 2020-08-22 04:48:06 +01:00 committed by GitHub
parent f320be690c
commit 98c39a37c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -725,7 +725,11 @@ fn (mut s Scanner) text_scan() token.Token {
} }
// end of `$expr` // end of `$expr`
// allow `'$a.b'` and `'$a.c()'` // allow `'$a.b'` and `'$a.c()'`
if s.is_inter_start && next_char != `.` && next_char != `(` { if s.is_inter_start && next_char == `(` {
if s.look_ahead(2) != `)` {
s.warn('use e.g. `\${f(expr)}` or `\$name\\(` instead of `\$f(expr)`')
}
} else if s.is_inter_start && next_char != `.` {
s.is_inter_end = true s.is_inter_end = true
s.is_inter_start = false s.is_inter_start = false
} }
@ -1220,14 +1224,14 @@ fn (mut s Scanner) ident_string() string {
} }
} }
// ${var} (ignore in vfmt mode) // ${var} (ignore in vfmt mode)
if c == `{` && prevc == `$` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { if prevc == `$` && c == `{` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
s.is_inside_string = true s.is_inside_string = true
// so that s.pos points to $ at the next step // so that s.pos points to $ at the next step
s.pos -= 2 s.pos -= 2
break break
} }
// $var // $var
if util.is_name_char(c) && prevc == `$` && !is_raw && if prevc == `$` && util.is_name_char(c) && !is_raw &&
s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
s.is_inside_string = true s.is_inside_string = true
s.is_inter_start = true s.is_inter_start = true
@ -1372,6 +1376,14 @@ fn (mut s Scanner) inc_line_number() {
} }
} }
pub fn (s &Scanner) warn(msg string) {
pos := token.Position{
line_nr: s.line_nr
pos: s.pos
}
eprintln(util.formatted_error('warning:', msg, s.file_path, pos))
}
pub fn (s &Scanner) error(msg string) { pub fn (s &Scanner) error(msg string) {
pos := token.Position{ pos := token.Position{
line_nr: s.line_nr line_nr: s.line_nr

View File

@ -182,3 +182,12 @@ fn test_method_interpolation() {
assert '>${y.f().a}<' == '>2<' assert '>${y.f().a}<' == '>2<'
assert '>$y.f().a<' == '>2<' assert '>$y.f().a<' == '>2<'
} }
fn f(i int) int {
return i
}
fn test_call() {
s := '${f(4)}'
assert s == '4'
}