Skip to content

Commit

Permalink
Add separate metadata entry for every block parens (#13996)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Nov 15, 2024
1 parent 9c7604c commit 3b01b2a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
12 changes: 2 additions & 10 deletions lib/elixir/src/elixir_parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -791,15 +791,7 @@ build_block([{Op, ExprMeta, Args}], {Before, After}) ->
case ?token_metadata() of
true ->
ParensEntry = meta_from_token(Before) ++ [{closing, meta_from_token(After)}],
case ExprMeta of
% If there are multiple parens, those will result in subsequent
% build_block/2 calls, so we can assume parens entry is first
[{parens, Parens} | Meta] ->
[{parens, [ParensEntry | Parens]} | Meta];

Meta ->
[{parens, [ParensEntry]} | Meta]
end;
[{parens, ParensEntry} | ExprMeta];
false ->
ExprMeta
end,
Expand Down Expand Up @@ -1147,7 +1139,7 @@ parens_meta({Open, Close}) ->
case ?token_metadata() of
true ->
ParensEntry = meta_from_token(Open) ++ [{closing, meta_from_token(Close)}],
[{parens, [ParensEntry]}];
[{parens, ParensEntry}];
false ->
[]
end;
Expand Down
44 changes: 29 additions & 15 deletions lib/elixir/test/elixir/kernel/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ defmodule Kernel.ParserTest do
1,
{:+,
[
parens: [[line: 1, column: 5, closing: [line: 1, column: 11]]],
parens: [line: 1, column: 5, closing: [line: 1, column: 11]],
line: 1,
column: 8
], [2, 3]}
Expand All @@ -671,10 +671,8 @@ defmodule Kernel.ParserTest do
1,
{:+,
[
parens: [
[line: 1, column: 5, closing: [line: 1, column: 13]],
[line: 1, column: 6, closing: [line: 1, column: 12]]
],
parens: [line: 1, column: 5, closing: [line: 1, column: 13]],
parens: [line: 1, column: 6, closing: [line: 1, column: 12]],
line: 1,
column: 9
], [2, 3]}
Expand All @@ -688,17 +686,33 @@ defmodule Kernel.ParserTest do
file = "()"

assert string_to_quoted.(file) ==
{:__block__, [parens: [[line: 1, column: 1, closing: [line: 1, column: 2]]]], []}
{:__block__, [parens: [line: 1, column: 1, closing: [line: 1, column: 2]]], []}

file = "(())"

assert string_to_quoted.(file) ==
{:__block__,
[
parens: [
[line: 1, column: 1, closing: [line: 1, column: 4]],
[line: 1, column: 2, closing: [line: 1, column: 3]]
]
parens: [line: 1, column: 1, closing: [line: 1, column: 4]],
parens: [line: 1, column: 2, closing: [line: 1, column: 3]]
], []}

file = """
(
# Foo
(
# Bar
)
)
"""

assert string_to_quoted.(file) ==
{:__block__,
[
end_of_expression: [newlines: 1, line: 6, column: 2],
parens: [line: 1, column: 1, closing: [line: 6, column: 1]],
end_of_expression: [newlines: 1, line: 5, column: 4],
parens: [line: 3, column: 3, closing: [line: 5, column: 3]]
], []}
end

Expand All @@ -710,7 +724,7 @@ defmodule Kernel.ParserTest do
[
{:->,
[
parens: [[line: 1, column: 4, closing: [line: 1, column: 5]]],
parens: [line: 1, column: 4, closing: [line: 1, column: 5]],
line: 1,
column: 7
], [[], {:x, [line: 1, column: 10], nil}]}
Expand All @@ -725,7 +739,7 @@ defmodule Kernel.ParserTest do
[
{:->,
[
parens: [[line: 1, column: 4, closing: [line: 1, column: 9]]],
parens: [line: 1, column: 4, closing: [line: 1, column: 9]],
line: 1,
column: 11
],
Expand Down Expand Up @@ -753,7 +767,7 @@ defmodule Kernel.ParserTest do
do: [
{:->,
[
parens: [[line: 1, column: 12, closing: [line: 1, column: 17]]],
parens: [line: 1, column: 12, closing: [line: 1, column: 17]],
line: 1,
column: 19
],
Expand Down Expand Up @@ -807,7 +821,7 @@ defmodule Kernel.ParserTest do
[
{:__block__,
[
parens: [[line: 1, closing: [line: 1]]],
parens: [line: 1, closing: [line: 1]],
token: "1",
line: 1
], [1]}
Expand All @@ -817,7 +831,7 @@ defmodule Kernel.ParserTest do
]}

assert string_to_quoted.("(1)") ==
{:__block__, [parens: [[line: 1, closing: [line: 1]]], token: "1", line: 1], [1]}
{:__block__, [parens: [line: 1, closing: [line: 1]], token: "1", line: 1], [1]}
end

test "adds identifier_location for qualified identifiers" do
Expand Down

0 comments on commit 3b01b2a

Please sign in to comment.