dart-lang / dart-lang/source_gen

How to get a reference of a type from my element attribute to create an import statement?

Open
#502 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
494
Forks
115
Avg merge
12h 13m
Merged PRs (30d)
3

Description

I am generating code from a function

```
@mycodegen
List getCats() {
return cats;
}
```

I need now to get an import statement when I reproduce the call method when generating code in my library builder, otherwise a reference to ```Cat``` will be missing. I can do this in the generator:

`callMethod.type.returnType.element.location`

which produces something like

`asset:example/test/Cat.dart;asset:example/test/Cat.dart;Cat`

I would then have to do some string manipulation to get the bits I want to create an import statment. Is this best practice or is there an easier way?

Contributor guide

Open the contributing guide

Research direction

Start with the generator entry point shown in the issue and trace callMethod.type.returnType.element.location for the Cat type. Determine whether source_gen exposes a supported way to obtain the needed import reference without string manipulation, and document the recommended approach and what generated output should contain.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
build-system
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.