[Proposal] Warn when using quoted atoms with trailing commas in maps

59 views
Skip to first unread message

Allen Madsen

unread,
Sep 13, 2017, 6:16:52 PM9/13/17
to elixir-lang-core
At work we deal with a lot of JSON and so it's common to have the equivalent of JSON in Elixir. A common problem people on my team have when they write out the Elixir structure is that they miss converting trailing commas into hash rockets.

They have this:

%{"x": 1}

Instead of:

%{"x" => 1}

Because both look like a string, it's very easy to miss. I'm suggesting warning about the latter syntax as it can be a source of errors.

My goal is that it become more obvious that you've made this type of error. If people don't like this suggestion, I'd love to hear other ideas about ways this problem could be addressed.

Allen Madsen

unread,
Sep 13, 2017, 8:02:41 PM9/13/17
to elixir-l...@googlegroups.com
I've thought about this and rather than warning when using trailing colons on quoted atoms. It could make sense to warn when using quoted atoms when the characters are valid for a non-quoted atom.

%{"this": "warns"}
%{"this one": "doesn't"}

I'm also exploring adding this to credo, in case it doesn't make sense for Elixir core.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/14344c22-06a8-42c2-a827-29359a826ad0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Valim

unread,
Sep 14, 2017, 5:46:38 AM9/14/17
to elixir-l...@googlegroups.com
Those are both valid syntaxes. I don't think Elixir nor Credo should warn. If another serialization becomes wide-spread in the future in a way it also causes confusion with Elixir syntax, should we also deprecate that other part of the Elixir syntax? The rationale of why it works in Elixir is because we allow :"such an atom" and keywords keep the same syntax except ":" is used at the end.

If you are writing a lot of JSON, maybe one idea is to use a sigil so you can literally write JSON?

~j"""
{
  json: "this is literally json"
}
"""

The json can be converted to a data structure at compile-time if you don't have an interpolation. And if you have interpolation, you can probably change each interpolated argument to encode to json. So you can even do things such as:

sub = ~j"""
{foo: "bar"}
"""

~j"""
{total: #{sub}}
"""






José Valim
Skype: jv.ptec
Founder and Director of R&D

Allen Madsen

unread,
Sep 14, 2017, 9:07:31 PM9/14/17
to elixir-l...@googlegroups.com
The first suggestion I made was a bad one. I like the suggestion of the json sigil, except that you loose things like syntax highlighting and bracket matching in editors. I do still think that it is reasonable for something like credo to suggest writing `%{foo: 1}` instead of `%{"foo": 1}` in situations where the quotes are unnecessary. I mentioned the problem in the context of JSON, but it's more generally a problem of it being easy to confuse quoted atoms as strings.

José Valim

unread,
Sep 15, 2017, 3:01:41 AM9/15/17
to elixir-l...@googlegroups.com
> I do still think that it is reasonable for something like credo to suggest writing `%{foo: 1}` instead of `%{"foo": 1}` in situations where the quotes are unnecessary.

I see, I got those reversed, thanks for clarifying. I don't think Elixir should warn on those cases as that could get in the way of more important warnings shown by the language. So Credo sounds like the way to go.

> I like the suggestion of the json sigil, except that you loose things like syntax highlighting and bracket matching in editors. 

FWIW, this should be a solvable problem. You can change your editor syntax highlighting so that everything inside ~j"""...""" is treated as JSON. I know it is possible on Sublime/TextMate, not sure about the rest. I remember when I used to write things such as <<-SQL in Ruby's heredocs to get highlighting inside.



José Valim
Skype: jv.ptec
Founder and Director of R&D

OvermindDL1

unread,
Sep 15, 2017, 12:07:48 PM9/15/17
to elixir-lang-core
On Friday, September 15, 2017 at 1:01:41 AM UTC-6, José Valim wrote:
FWIW, this should be a solvable problem. You can change your editor syntax highlighting so that everything inside ~j"""...""" is treated as JSON. I know it is possible on Sublime/TextMate, not sure about the rest. I remember when I used to write things such as <<-SQL in Ruby's heredocs to get highlighting inside.

Yeah this is trivial to do in Atom and EMacs as well via their sub-modes.  That is how, for example, elixir appears inside html in `.html.eex` files.  ^.^ 
Reply all
Reply to author
Forward
0 new messages