Index: lib/src/js_ast/nodes.dart |
diff --git a/lib/src/js/nodes.dart b/lib/src/js_ast/nodes.dart |
similarity index 97% |
rename from lib/src/js/nodes.dart |
rename to lib/src/js_ast/nodes.dart |
index cd0e807a9542a3cf4c5cb13da93722b2f49473a4..be1e764806eee0f05a38179aba939dda738b9d91 100644 |
--- a/lib/src/js/nodes.dart |
+++ b/lib/src/js_ast/nodes.dart |
@@ -311,7 +311,12 @@ class Program extends Node { |
/// Top-level statements in the program. |
final List<ModuleItem> body; |
- Program(this.body, {this.scriptTag}); |
+ /// The module's own name. |
+ /// |
+ /// This is not used in ES6, but is provided to allow module lowering. |
+ final String name; |
+ |
+ Program(this.body, {this.scriptTag, this.name}); |
accept(NodeVisitor visitor) => visitor.visitProgram(this); |
void visitChildren(NodeVisitor visitor) { |
@@ -741,7 +746,10 @@ class LiteralExpression extends Expression { |
* AST. |
*/ |
class VariableDeclarationList extends Expression { |
- /** The `var` or `let` keyword used for this variable declaration list. */ |
+ /** |
+ * The `var` or `let` or `const` keyword used for this variable declaration |
+ * list. |
+ */ |
final String keyword; |
final List<VariableInitialization> declarations; |
@@ -1733,15 +1741,17 @@ class ImportDeclaration extends ModuleItem { |
final LiteralString from; |
- ImportDeclaration({this.defaultBinding, this.namedImports, this.from}); |
+ ImportDeclaration({this.defaultBinding, this.namedImports, this.from}) { |
+ assert(from != null); |
+ } |
/** The `import "name.js"` form of import */ |
ImportDeclaration.all(LiteralString module) : this(from: module); |
/** If this import has `* as name` returns the name, otherwise null. */ |
- String get importStarAs { |
+ Identifier get importStarAs { |
if (namedImports != null && namedImports.length == 1 && |
- namedImports[0].name == '*') { |
+ namedImports[0].isStar) { |
return namedImports[0].asName; |
} |
return null; |
@@ -1780,6 +1790,26 @@ class ExportDeclaration extends ModuleItem { |
exported is ExportClause); |
} |
+ /// Gets the list of names exported by this export declaration, or `null` |
+ /// if this is an `export *`. |
+ /// |
+ /// This can be useful for lowering to other module formats. |
+ List<Identifier> get exportedNames { |
+ if (isDefault) return [new Identifier('default')]; |
+ |
+ var exported = this.exported; |
+ if (exported is ClassDeclaration) return [exported.classExpr.name]; |
+ if (exported is FunctionDeclaration) return [exported.name]; |
+ if (exported is VariableDeclarationList) { |
+ return exported.declarations.map((i) => i.declaration).toList(); |
+ } |
+ if (exported is ExportClause) { |
+ if (exported.exportStar) return null; |
+ return exported.exports.map((e) => e.name).toList(); |
+ } |
+ throw new StateError('invalid export declaration'); |
+ } |
+ |
accept(NodeVisitor visitor) => visitor.visitExportDeclaration(this); |
visitChildren(NodeVisitor visitor) => exported.accept(visitor); |
ExportDeclaration _clone() => |
@@ -1794,10 +1824,10 @@ class ExportClause extends Node { |
/** The `export * from 'name.js'` form. */ |
ExportClause.star(LiteralString from) |
- : this([new NameSpecifier('*')], from: from); |
+ : this([new NameSpecifier.star()], from: from); |
/** True if this is an `export *`. */ |
- bool get exportStar => exports.length == 1 && exports[0].name == '*'; |
+ bool get exportStar => exports.length == 1 && exports[0].isStar; |
accept(NodeVisitor visitor) => visitor.visitExportClause(this); |
void visitChildren(NodeVisitor visitor) { |
@@ -1809,11 +1839,14 @@ class ExportClause extends Node { |
/** An import or export specifier. */ |
class NameSpecifier extends Node { |
- // TODO(jmesserly): should we wrap this in a node of some sort? |
- final String name; |
- final String asName; // Can be null. |
+ final Identifier name; |
+ final Identifier asName; // Can be null. |
NameSpecifier(this.name, {this.asName}); |
+ NameSpecifier.star() : this(null); |
+ |
+ /** True if this is a `* as someName` specifier. */ |
+ bool get isStar => name == null; |
accept(NodeVisitor visitor) => visitor.visitNameSpecifier(this); |
void visitChildren(NodeVisitor visitor) {} |