Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser, fmt: parse methods on JS interfaces, write JS method bodies #21088

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ fn (mut f Fmt) fn_body(node ast.FnDecl) {
defer {
f.fn_scope = prev_fn_scope
}
if node.language == .v {
if node.language == .v || (node.is_method && node.language == .js) {
if !node.no_body {
f.write(' {')
pre_comments := node.comments.filter(it.pos.pos < node.name_pos.pos)
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/fmt/tests/interface_js_interop_method_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub interface JS.CanvasRenderingContext2D {
setLineDash(JS.Array)
}

pub fn (ctx JS.CanvasRenderingContext2D) set_line_dash(arr []f64) {
tmp := JS.Array.prototype.constructor()
for x in arr {
tmp.push(JS.Number(x))
}
ctx.setLineDash(tmp)
}
2 changes: 1 addition & 1 deletion vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ run them via `v file.v` instead',
mut stmts := []ast.Stmt{}
body_start_pos := p.tok.pos()
if p.tok.kind == .lcbr {
if language != .v {
if language != .v && !(language == .js && type_sym.info is ast.Interface) {
p.error_with_pos('interop functions cannot have a body', body_start_pos)
}
p.inside_fn = true
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/interop_js_func_body_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/parser/tests/interop_js_func_body_err.vv:1:43: error: interop functions cannot have a body
1 | fn JS.state_is_ready_on_leave(retval int) {}
| ^
1 change: 1 addition & 0 deletions vlib/v/parser/tests/interop_js_func_body_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn JS.state_is_ready_on_leave(retval int) {}