rrousselGit / functional_widget

A code generator to write widgets as function without loosing the benefits of classes.
597 stars 46 forks source link

Error with @required arguments with not included types #31

Closed aloisdeniel closed 5 years ago

aloisdeniel commented 5 years ago

When using a parameter both @required and with a type not included in the Dart SDK (Color for example), the generation fails.

Example :

@widget 
Widget example(BuildContext context, {@required Color splashColor}) 

Produces :

line 17, column 7: Expected an identifier.
   ╷
17 │ final @ splashColor;
   │       ^
   ╵
line 17, column 7: Expected to find ';'.
   ╷
17 │ final @ splashColor;
   │       ^
   ╵
line 17, column 20: Expected a class member.
   ╷
17 │ final @ splashColor;
   │                    ^

This is due of the access of the first token of the computedNode.

rrousselGit commented 5 years ago

I can't do much about it. It's a blocker on the generator side

aloisdeniel commented 5 years ago

You may can check what is the first token in the sequence... but since computedNode is deprecated it is not a long term solution infortunately.

rrousselGit commented 5 years ago

That computeNode usage do not aim at being a long-term solution, so I'm fine with a deprecated function that barely fixes the problem.

It just handles the most common usage.

aloisdeniel commented 5 years ago

A potential solution could be to check if the node is an AnnotatedNode and if so, use visitChildren to find the declared identifier.

rrousselGit commented 5 years ago

How so? We're using computeNode when the analyzer is unable to resolve nodes.

aloisdeniel commented 5 years ago

Hum, only when the element's type is undefined, not the element itself.

Reference _parameterToReference(ParameterElement element) {
  if (element.type.isUndefined) {
    return refer(element.computeNode().beginToken.toString());
  }

We may here try to get the annotation associated with element before accessing its first token.

I will try some experiments as soon as I get the time to.

rrousselGit commented 5 years ago

Closing this, since I cannot do anything.

As a workaround you may use a custom object:

class MyColor {
  MyColor(this.color);
  final Color color;
}

and send it to the generator:

@widget Widget foo({@required MyColor color}) {}

This will correctly build.