Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 22 additions & 6 deletions lib/elixir/src/elixir_tokenizer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,15 @@ tokenize([T | Rest], Line, Column, Scope, Tokens) when ?pipe_op(T) ->
% Non-operator Atoms

tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H) ->
case elixir_interpolation:extract(Line, Column + 2, Scope, true, T, H) of
OuterScope = case H == $' of
true ->
prepend_warning(Line, Column, "single quotes around atoms are deprecated. Use double quotes instead.", Scope);

false ->
Scope
end,

case elixir_interpolation:extract(Line, Column + 2, OuterScope, true, T, H) of
{NewLine, NewColumn, Parts, Rest, InterScope} ->
NewScope = case is_unnecessary_quote(Parts, InterScope) of
true ->
Expand All @@ -508,7 +516,7 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)

case unescape_tokens(Parts, Line, Column, NewScope) of
{ok, [Part]} when is_binary(Part) ->
case unsafe_to_atom(Part, Line, Column, Scope) of
case unsafe_to_atom(Part, Line, Column, OuterScope) of
{ok, Atom} ->
Token = {atom_quoted, {Line, Column, H}, Atom},
tokenize(Rest, NewLine, NewColumn, NewScope, [Token | Tokens]);
Expand All @@ -518,7 +526,7 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)
end;

{ok, Unescaped} ->
Key = case Scope#elixir_tokenizer.existing_atoms_only of
Key = case OuterScope#elixir_tokenizer.existing_atoms_only of
true -> atom_safe;
false -> atom_unsafe
end,
Expand All @@ -531,7 +539,7 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)

{error, Reason} ->
Message = " (for atom starting at line ~B)",
interpolation_error(Reason, Original, Scope, Tokens, Message, [Line], Line, Column + 1, [H], [H])
interpolation_error(Reason, Original, OuterScope, Tokens, Message, [Line], Line, Column + 1, [H], [H])
end;

tokenize([$: | String] = Original, Line, Column, Scope, Tokens) ->
Expand Down Expand Up @@ -897,7 +905,15 @@ handle_dot([$., $( | Rest], Line, Column, DotInfo, Scope, Tokens) ->
tokenize([$( | Rest], Line, Column, Scope, TokensSoFar);

handle_dot([$., H | T] = Original, Line, Column, DotInfo, Scope, Tokens) when ?is_quote(H) ->
case elixir_interpolation:extract(Line, Column + 1, Scope, true, T, H) of
OuterScope = case H == $' of
true ->
prepend_warning(Line, Column, "single quotes around calls are deprecated. Use double quotes instead.", Scope);

false ->
Scope
end,

case elixir_interpolation:extract(Line, Column + 1, OuterScope, true, T, H) of
{NewLine, NewColumn, [Part], Rest, InterScope} when is_list(Part) ->
NewScope = case is_unnecessary_quote([Part], InterScope) of
true ->
Expand Down Expand Up @@ -928,7 +944,7 @@ handle_dot([$., H | T] = Original, Line, Column, DotInfo, Scope, Tokens) when ?i
Message = "interpolation is not allowed when calling function/macro. Found interpolation in a call starting with: ",
error({?LOC(Line, Column), Message, [H]}, Rest, NewScope, Tokens);
{error, Reason} ->
interpolation_error(Reason, Original, Scope, Tokens, " (for function name starting at line ~B)", [Line], Line, Column, [H], [H])
interpolation_error(Reason, Original, OuterScope, Tokens, " (for function name starting at line ~B)", [Line], Line, Column, [H], [H])
end;

handle_dot([$. | Rest], Line, Column, DotInfo, Scope, Tokens) ->
Expand Down
36 changes: 36 additions & 0 deletions lib/elixir/test/elixir/kernel/warning_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,42 @@ defmodule Kernel.WarningTest do
end
end

describe "deprecated single quotes in atoms" do
test "warns for single quotes in atoms" do
assert_warn_eval(
[
"nofile:1:1",
"single quotes around atoms are deprecated. Use double quotes instead"
],
~s/:'a+b'/
)
end

test "warns twice for single and unnecessary atom quotes" do
assert_warn_eval(
[
"nofile:1:1",
"single quotes around atoms are deprecated. Use double quotes instead",
"nofile:1:1",
"found quoted atom \"ab\" but the quotes are not required"
],
~s/:'ab'/
)
end

test "warns twice for single and unnecessary call quotes" do
assert_warn_eval(
[
"nofile:1:9",
"single quotes around calls are deprecated. Use double quotes instead",
"nofile:1:9",
"found quoted call \"length\" but the quotes are not required"
],
~s/[Kernel.'length'([])]/
)
end
end

test "warns on :: as atom" do
assert_warn_eval(
[
Expand Down