I'd like to derive exactly that subset of the sources of a dart comiler (dart2js or dartdevc or other) or of a dart analyser that can 1. transform a string of dart code (or better a list of strings each representing a compilation unit) into a typed syntax tree, 2. be translated into js, 3. be run in the browser. Is there a marked subset that fulfills these requirements, which is it, and how can I find it, in general.
1 Answers
Accomplishing #1 is fairly simple using package:analyzer
, which is the same static analyzer used to provide IDE hinting and autocomplete, etc. The Dart Team is currently working on unifying their compiler frontends behind on main API, but for now, analyzer
should definitely take care of most of what you need.
Here's an example of getting a syntax tree and running analysis on it: https://github.com/thosakwe/analyzer_examples/blob/master/analyze_a_file/analyze_a_file.dart
As for #2, you'll likely have to fork the dart-lang/sdk
repo and make your own adjustments to dart2js
. It's not published as a standalone package. Otherwise, you can write your own compiler, which is probably not going be fun.
- I suppose you'd have to figure out how to get #2 up and running, but hypothetically, if you could compile a JavaScript source, you could just
eval
it after compilation.
To answer your final question, no, AFAIK, there is no subset of dart2js
available that lets you create your own Dart-to-JavaScript compiler.

- 729
- 7
- 18
-
The answer describes how to get an ast and run the typer, but it doesnt describe the 3rd step: how can I get the typed syntax tree? – Malelago Aug 10 '17 at 16:23
-
The CompilationUnit returned by the function in the example is the syntax tree you're looking for. – Tobe Osakwe Aug 10 '17 at 16:25
-
How can I run that example "analyze_a_file.dart"? How can I run that using pub? It's not a package?! – Malelago Aug 12 '17 at 11:53
-
You should clone the repo, and then you can run the examples. – Tobe Osakwe Aug 12 '17 at 13:15