Description
Context: Code-generators often want to copy-paste constants from a constructor/function into the generated source.
This cannot be done by pasting the raw unedited source, as the location of said default may be different from the original one. As such, import prefixes may change, and static variables may need to be used explicitly with their class name (Class.foo
instead of foo
when inside Class
).
Usually, this is done by manipulating DartObject
, obtained with computeConstantValue
. For example, source_gen provides ConstantReader.revive()
to "restore" a constant.
This is not without limitation though, and can often fail. In particular, restoring a complex object such as const Foo.namedCtor(variable)
can be challenging, as we need to reuse the same expression.
In theory, the information is available by obtaining the AST. One challenge though is that said AST is generally not available within code-generators, because source_gen doesn't expose it.
Would it maybe be possible to have DartObject
the missing information to support complex objects?
It should be enough to expose:
- The class name used (with its Element)
- The constructor name
- A DartObject for all the arguments, and whether the arguments are named or not.
This would avoid having to re-parse the AST of a file; search for the corresponding AstNode of an element, and decode it.