Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions JuliaSyntax/src/julia/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2200,10 +2200,11 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
_parsed_call = was_eventually_call(ps)
_maybe_grouping_parens = !had_commas && !had_splat && num_semis == 0 && num_subexprs == 1
# Skip intervening newlines only when the parentheses hold a single
# expression, which is the ambiguous case between a name like (::T)
# and an anonymous function parameter list.
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens)
# Check if there's a newline right after the closing paren
has_newline_after = peek(ps) == K"NewlineWs"
# Skip newlines when looking ahead only if there's no newline immediately after )
# This prevents treating `function (x)\n(body)` as a named function
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens && !has_newline_after)
_needs_parse_call = next_kind ∈ KSet"( ."
_is_anon_func = (!_needs_parse_call && !_parsed_call) || had_commas
return (needs_parameters = _is_anon_func,
Expand Down
1 change: 1 addition & 0 deletions JuliaSyntax/test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ tests = [
"macro \$f() end" => "(macro (call (\$ f)) (block))"
"macro (\$f)() end" => "(macro (call (parens (\$ f))) (block))"
"function (x) body end"=> "(function (tuple-p x) (block body))"
"function (x)\n body\nend"=> "(function (tuple-p x) (block body))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also insert a involving explicitly a tuple () as the body?

E.g. near line 622-628, where in JuliaLang/JuliaSyntax.jl#580 this test was inserted:

        "function (\n        f\n        )() end" =>  "(function (call (parens f)) (block))"

well now we want to test something like this (not sure if I got it quite right)

        "function (\n        x\n        )\n() end" =>  "(function (tuple-p x) (block (tuple-p)))"

Because for me, on master, this parses:

function(y)
  body
end

but this does not:

function(y)
  ()
end

"function (x,y) end" => "(function (tuple-p x y) (block))"
"function (x,y,) end" => "(function (tuple-p-, x y) (block))"
"function (x=1) end" => "(function (tuple-p (= x 1)) (block))"
Expand Down