dart-lang / code_builder

A fluent API for generating valid Dart source code
https://pub.dev/packages/code_builder
BSD 3-Clause "New" or "Revised" License
427 stars 66 forks source link

Cannot call toString() on a list of non-literal expressions #340

Open m0nac0 opened 3 years ago

m0nac0 commented 3 years ago

Whenever I try to use literalList() and pass non-literal expressions, I get the error: Not a supported literal type

According to #225 this should be supported, though.

srawlins commented 3 years ago

Please provide a minimal reproduction case. What code do you try to run which produces this error?

m0nac0 commented 3 years ago

When I tried to provide the minimal reproduction case, I noticed that the same code I couldn't get to work yesterday seems to work fine today. So it seems to have been some other issue on my side. Thank you nonetheless for responding!

leonsenft commented 2 years ago

I ran into this issue and I think the root cause is LiteralListExpression.toString()'s implementation:

https://github.com/dart-lang/code_builder/blob/743cb0a9f40079ff76fa45a6c2cb7d59a7ec4ad6/lib/src/specs/expression/literal.dart#L137

It passes each value to literal() which is what throws the reported error:

https://github.com/dart-lang/code_builder/blob/743cb0a9f40079ff76fa45a6c2cb7d59a7ec4ad6/lib/src/specs/expression/literal.dart#L35

I encountered this issue rather indirectly via the equalsDart matcher, because it invokes toString() in order to describe a mismatch.

I'm not very familiar with this code base, but I wonder if simply calling toString(), instead of literal(), on each value would be sufficient?