2015-03-20 15:37:09 -07:00
|
|
|
library angular2.transform.reflection_remover.codegen;
|
2015-02-27 14:42:21 -08:00
|
|
|
|
|
|
|
|
import 'package:analyzer/src/generated/ast.dart';
|
|
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
|
|
|
|
|
|
import 'package:angular2/src/transform/common/names.dart';
|
|
|
|
|
|
|
|
|
|
class Codegen {
|
|
|
|
|
static const _PREFIX_BASE = 'ngStaticInit';
|
|
|
|
|
|
|
|
|
|
/// The prefix used to import our generated file.
|
|
|
|
|
final String prefix;
|
2015-04-09 17:49:11 -07:00
|
|
|
/// The import uris
|
|
|
|
|
final Iterable<String> importUris;
|
2015-02-27 14:42:21 -08:00
|
|
|
|
2015-04-09 17:49:11 -07:00
|
|
|
Codegen(String reflectionEntryPointPath, Iterable<String> newEntryPointPaths,
|
2015-02-27 14:42:21 -08:00
|
|
|
{String prefix})
|
|
|
|
|
: this.prefix = prefix == null ? _PREFIX_BASE : prefix,
|
2015-07-01 13:20:12 -07:00
|
|
|
importUris = newEntryPointPaths.map((p) => path
|
|
|
|
|
.relative(p, from: path.dirname(reflectionEntryPointPath))
|
|
|
|
|
.replaceAll('\\', '/')) {
|
2015-02-27 14:42:21 -08:00
|
|
|
if (this.prefix.isEmpty) throw new ArgumentError.value('(empty)', 'prefix');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generates code to import the library containing the method which sets up
|
|
|
|
|
/// Angular2 reflection statically.
|
|
|
|
|
///
|
|
|
|
|
/// The code generated here should follow the example of code generated for
|
2015-04-17 13:01:07 -07:00
|
|
|
/// an {@link ImportDirective} node.
|
2015-02-27 14:42:21 -08:00
|
|
|
String codegenImport() {
|
2015-04-09 17:49:11 -07:00
|
|
|
var count = 0;
|
|
|
|
|
return importUris
|
|
|
|
|
.map((importUri) => 'import \'${importUri}\' as ${prefix}${count++};')
|
|
|
|
|
.join('');
|
2015-02-27 14:42:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generates code to call the method which sets up Angular2 reflection
|
|
|
|
|
/// statically.
|
|
|
|
|
///
|
2015-04-17 13:01:07 -07:00
|
|
|
/// If `reflectorAssignment` is provided, it is expected to be the node
|
|
|
|
|
/// representing the {@link ReflectionCapabilities} assignment, and we will
|
|
|
|
|
/// attempt to parse the access of `reflector` from it so that `reflector` is
|
2015-02-27 14:42:21 -08:00
|
|
|
/// properly prefixed if necessary.
|
|
|
|
|
String codegenSetupReflectionCall(
|
|
|
|
|
{AssignmentExpression reflectorAssignment}) {
|
|
|
|
|
var reflectorExpression = null;
|
|
|
|
|
if (reflectorAssignment != null) {
|
|
|
|
|
reflectorExpression = reflectorAssignment.accept(new _ReflectorVisitor());
|
|
|
|
|
}
|
|
|
|
|
if (reflectorExpression == null) {
|
|
|
|
|
reflectorExpression = 'reflector';
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 17:49:11 -07:00
|
|
|
var count = 0;
|
|
|
|
|
return importUris
|
2015-07-15 23:15:44 -07:00
|
|
|
.map((_) => '${prefix}${count++}.${SETUP_METHOD_NAME}();')
|
2015-04-09 17:49:11 -07:00
|
|
|
.join('');
|
2015-02-27 14:42:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-17 13:01:07 -07:00
|
|
|
/// A visitor whose job it is to find the access of `reflector`.
|
2015-02-27 14:42:21 -08:00
|
|
|
class _ReflectorVisitor extends Object with SimpleAstVisitor<Expression> {
|
|
|
|
|
@override
|
|
|
|
|
Expression visitAssignmentExpression(AssignmentExpression node) {
|
|
|
|
|
if (node == null || node.leftHandSide == null) return null;
|
|
|
|
|
return node.leftHandSide.accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Expression visitPropertyAccess(PropertyAccess node) {
|
2015-04-01 13:47:42 -07:00
|
|
|
if (node == null || node.target == null) return null;
|
2015-02-27 14:42:21 -08:00
|
|
|
return node.target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Expression visitPrefixedIdentifier(PrefixedIdentifier node) {
|
|
|
|
|
if (node == null || node.prefix == null) return null;
|
|
|
|
|
return node.prefix;
|
|
|
|
|
}
|
|
|
|
|
}
|