Skip to content

Conversation

@Malian
Copy link
Collaborator

@Malian Malian commented May 7, 2021

Related to #340

My first attempt was to add handle this in the parser but I believe it is not the role of the parser and it. I was able to move this warning to the TypeHandler or directly to the Compiler. From my point of view, it makes more sense to handle it in the compiler, so I decided to catch it in the attr_value function when the attr_value is not a string. If it is not a string, even if the type is :string, it means that the value as been converted before to something else, boolean or integer.

Ok, this will teach me not to run all the tests before pushing...

@Malian
Copy link
Collaborator Author

Malian commented May 7, 2021

Mhhh these tests should not pass. We should force warning_as_error=true in 0.5

@Malian
Copy link
Collaborator Author

Malian commented May 7, 2021

@msaraiva & @lnr0626 due to the parser we will have to raise a warning from the parser, or modify the way it handles <div selected>.

Because of this piece of code in the parser:

defp translate_attr({name, nil, meta}) do
{name, true, to_meta(meta)}
end

There is no way to differentiate <div selected> from <div selected=true> in the compiler.

We could emit a warning from the parser, but we do not have access to a caller for IOHelper.warn. We could also modify the translate_attr signature to differentiate both cases.

For example, we could replace the above function with:

  defp translate_attr({name, nil, meta}) do
    {name, {:attribute_expr, "true", to_meta(meta)}, to_meta(meta)}
  end

and keep the current implementation. It means that <div selected> === <div selected={true}> !== <div selected=true>. Then in the compiler we emit a warning when a :string type with a boolean value or an integer value are provided.

WDYT? Do you have any alternatives?

Copy link
Member

@msaraiva msaraiva left a comment

Choose a reason for hiding this comment

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

@Malian what about passing an extra field in the attribute's metadata to differentiate them? Since the attribute's metadata is available in attr_value, you could check that info.

@Malian Malian force-pushed the deprecated-unquoted-string branch from 1cd48f4 to 9f1db64 Compare May 8, 2021 04:58
@Malian
Copy link
Collaborator Author

Malian commented May 8, 2021

what about passing an extra field in the attribute's metadata to differentiate them? Since the attribute's metadata is available in attr_value, you could check that info.

@msaraiva It is a solution, but it implies that we have to modify the AST.Meta struct to accept extra metadata about the node. We call everywhere in the code Helpers.to_meta that returns an AST.Meta. Adding an attribute that is only used for deprecated code and that we will remove in the future seems weird.

def to_meta(tree_meta, %CompileMeta{caller: caller, checks: checks}) do
%AST.Meta{
line: tree_meta.line,
column: tree_meta.column,
file: tree_meta.file,
caller: caller,
checks: checks
}
end
def to_meta(tree_meta, %AST.Meta{} = parent_meta) do
%{parent_meta | line: tree_meta.line, column: tree_meta.column}
end

We could add a field that handles all extra meta, but It is maybe an overkill addition at this stage, isn't it?

An alternative could be to catch this extra metadata one function above in the process_attributes but I have the feeling that it makes the code more spaghetti. As I said, we will remove this code in the future, so maybe we should not care too much about it?

defp process_attributes(mod, [{name, value, attr_meta} | attrs], meta, acc) do
name = String.to_atom(name)
attr_meta = Helpers.to_meta(attr_meta, meta)
{type, type_opts} = Surface.TypeHandler.attribute_type_and_opts(mod, name, attr_meta)

@msaraiva
Copy link
Member

msaraiva commented May 8, 2021

we will remove this code in the future, so maybe we should not care too much about it?

Agreed. We shouldn't care. Those deprecations are temporary so if they work as expected, it's fine. 👍

@Malian Malian requested a review from msaraiva May 8, 2021 13:16
@Malian Malian merged commit 8f39301 into surface-ui:surface-next May 8, 2021
lnr0626 added a commit to lnr0626/surface that referenced this pull request May 11, 2021
* surface-next:
  Remove `phx_` prefix from props (surface-ui#384)
  Remove code for embedded interpolation inside strings
  Introduce <#unless> (surface-ui#376)
  Introduce the concept of root properties (surface-ui#382)
  Warn on invalid prop/data defaults (surface-ui#374)
  Deprecated unquoted strings (surface-ui#379)
  Convert embedded interpolation (surface-ui#380)
  Introduce <#raw> and deprecate <#Raw> (surface-ui#377)
@msaraiva msaraiva mentioned this pull request May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants