Skip to content

Commit

Permalink
Fix end location for FunDef (crystal-lang#13789)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and Blacksmoke16 committed Dec 11, 2023
1 parent b34d92c commit 2ca3f58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,34 @@ module Crystal
exps = Parser.parse(code).as(Expressions)
exps.expressions[1].location.not_nil!.line_number.should eq(7)
end

it "sets correct location for fun def" do
source = "lib LibFoo; fun foo(x : Int32); end"
node = Parser.new(source).parse.as(LibDef).body

node_source(source, node).should eq("fun foo(x : Int32)")
end

it "sets correct location for fun def with return type" do
source = "lib LibFoo; fun foo(x : Int32) : Void; end"
node = Parser.new(source).parse.as(LibDef).body

node_source(source, node).should eq("fun foo(x : Int32) : Void")
end

it "sets correct location for fun def on multiple lines" do
source = "lib LibFoo\nfun foo(\n x : Int32\n )\nend"
node = Parser.new(source).parse.as(LibDef).body

node_source(source, node).should eq("fun foo(\n x : Int32\n )")
end

it "sets correct location for fun def with body" do
source = "fun foo(x : Int32) : Void\nend"
node = Parser.new(source).parse.as(FunDef)

node_source(source, node).should eq("fun foo(x : Int32) : Void\nend")
end
end

it "sets correct location of parameter in proc literal" do
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5793,11 +5793,13 @@ module Crystal
break
end
end
end_location = token_end_location
next_token_skip_statement_end
end

if @token.type.op_colon?
next_token_skip_space_or_newline
end_location = token_end_location
return_type = parse_bare_proc_type
end

Expand All @@ -5818,7 +5820,6 @@ module Crystal
@fun_nest -= 1
else
body = nil
end_location = token_end_location
end

fun_def = FunDef.new name, params, return_type, varargs, body, real_name
Expand Down

0 comments on commit 2ca3f58

Please sign in to comment.