Skip to content

Commit

Permalink
scanner: disallow a shebang line, that is not at the top of a file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 15, 2024
1 parent 84a52ae commit c024721
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/v/scanner/scanner.v
Expand Up @@ -959,6 +959,16 @@ pub fn (mut s Scanner) text_scan() token.Token {
if nextc == `!` {
// treat shebang line (#!) as a comment
comment := s.text[start - 1..s.pos].trim_space()
if s.line_nr != 1 {
comment_pos := token.Pos{
line_nr: s.line_nr - 1
len: comment.len
pos: start
col: s.current_column() - comment.len
}
s.error_with_pos('a shebang is only valid at the top of the file',
comment_pos)
}
// s.fgenln('// shebang line "$s.line_comment"')
return s.new_token(.comment, comment, comment.len + 2)
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/scanner/tests/shebang_outside_vsh_err.out
@@ -0,0 +1,7 @@
vlib/v/scanner/tests/shebang_outside_vsh_err.vv:3:1: error: a shebang is only valid at the top of the file
1 | module foo
2 |
3 | #!/usr/bin/env -S v
| ^
4 |
5 | import os
5 changes: 5 additions & 0 deletions vlib/v/scanner/tests/shebang_outside_vsh_err.vv
@@ -0,0 +1,5 @@
module foo

#!/usr/bin/env -S v

import os

0 comments on commit c024721

Please sign in to comment.