feature(dart/transform): Inject initReflector
at @AngularEntrypoint
Detect the `@AngularEntrypoint` annotations on methods and/or functions and add a call to `initReflector` there. See #4865
This commit is contained in:
@ -22,7 +22,7 @@ const _INJECTABLES = const [
|
||||
|
||||
const _DIRECTIVES = const [
|
||||
const ClassDescriptor(
|
||||
'Directive', 'package:angular2/src/core/metadatada/directive.dart',
|
||||
'Directive', 'package:angular2/src/core/metadata/directive.dart',
|
||||
superClass: 'Injectable'),
|
||||
const ClassDescriptor('Directive', 'package:angular2/src/core/metadata.dart',
|
||||
superClass: 'Injectable'),
|
||||
@ -57,6 +57,19 @@ const _VIEWS = const [
|
||||
const ClassDescriptor('View', 'package:angular2/src/core/metadata.dart'),
|
||||
];
|
||||
|
||||
const _ENTRYPOINTS = const [
|
||||
const ClassDescriptor('AngularEntrypoint', 'package:angular2/angular2.dart'),
|
||||
const ClassDescriptor('AngularEntrypoint', 'package:angular2/bootstrap.dart'),
|
||||
const ClassDescriptor(
|
||||
'AngularEntrypoint', 'package:angular2/bootstrap_static.dart'),
|
||||
const ClassDescriptor(
|
||||
'AngularEntrypoint', 'package:angular2/platform/browser.dart'),
|
||||
const ClassDescriptor(
|
||||
'AngularEntrypoint', 'package:angular2/platform/browser_static.dart'),
|
||||
const ClassDescriptor(
|
||||
'AngularEntrypoint', 'package:angular2/src/core/angular_entrypoint.dart'),
|
||||
];
|
||||
|
||||
/// Checks if a given [Annotation] matches any of the given
|
||||
/// [ClassDescriptors].
|
||||
class AnnotationMatcher extends ClassMatcherBase {
|
||||
@ -67,7 +80,8 @@ class AnnotationMatcher extends ClassMatcherBase {
|
||||
..addAll(_COMPONENTS)
|
||||
..addAll(_DIRECTIVES)
|
||||
..addAll(_INJECTABLES)
|
||||
..addAll(_VIEWS));
|
||||
..addAll(_VIEWS)
|
||||
..addAll(_ENTRYPOINTS));
|
||||
}
|
||||
|
||||
bool _implementsWithWarning(Annotation annotation, AssetId assetId,
|
||||
@ -94,4 +108,8 @@ class AnnotationMatcher extends ClassMatcherBase {
|
||||
/// Checks if an [Annotation] node implements [View].
|
||||
bool isView(Annotation annotation, AssetId assetId) =>
|
||||
_implementsWithWarning(annotation, assetId, _VIEWS);
|
||||
|
||||
/// Checks if an [Annotation] node implements [AngularEntrypoint]
|
||||
bool isEntrypoint(Annotation annotation, AssetId assetId) =>
|
||||
_implementsWithWarning(annotation, assetId, _ENTRYPOINTS);
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
library angular2.transform.reflection_remover.entrypoint_matcher;
|
||||
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
import 'package:barback/barback.dart';
|
||||
|
||||
import 'package:angular2/src/transform/common/annotation_matcher.dart';
|
||||
import 'package:angular2/src/transform/common/naive_eval.dart';
|
||||
|
||||
/// Determines if a [FunctionDeclaration] or [MethodDeclaration] is an
|
||||
/// `AngularEntrypoint`.
|
||||
class EntrypointMatcher {
|
||||
final AssetId _assetId;
|
||||
final AnnotationMatcher _annotationMatcher;
|
||||
|
||||
EntrypointMatcher(this._assetId, this._annotationMatcher) {
|
||||
if (_assetId == null) {
|
||||
throw new ArgumentError.notNull('AssetId');
|
||||
}
|
||||
if (_annotationMatcher == null) {
|
||||
throw new ArgumentError.notNull('AnnotationMatcher');
|
||||
}
|
||||
}
|
||||
|
||||
bool isEntrypoint(AnnotatedNode node) {
|
||||
if (node == null ||
|
||||
(node is! FunctionDeclaration && node is! MethodDeclaration)) {
|
||||
return false;
|
||||
}
|
||||
return node.metadata
|
||||
.any((a) => _annotationMatcher.isEntrypoint(a, _assetId));
|
||||
}
|
||||
|
||||
/// Gets the name assigned to the `AngularEntrypoint`.
|
||||
///
|
||||
/// This method assumes the name is the first argument to `AngularEntrypoint`;
|
||||
String getName(AnnotatedNode node) {
|
||||
final annotation = node.metadata.firstWhere(
|
||||
(a) => _annotationMatcher.isEntrypoint(a, _assetId),
|
||||
orElse: () => null);
|
||||
if (annotation == null) return null;
|
||||
if (annotation.arguments == null ||
|
||||
annotation.arguments.arguments == null ||
|
||||
annotation.arguments.arguments.isEmpty) {
|
||||
return _defaultEntrypointName;
|
||||
}
|
||||
final entryPointName = naiveEval(annotation.arguments.arguments.first);
|
||||
if (entryPointName == NOT_A_CONSTANT) {
|
||||
throw new ArgumentError(
|
||||
'Could not evaluate "${node}" as parameter to @AngularEntrypoint');
|
||||
}
|
||||
if (entryPointName is! String) {
|
||||
throw new ArgumentError('Unexpected type "${entryPointName.runtimeType}" '
|
||||
'as first parameter to @AngularEntrypoint');
|
||||
}
|
||||
return entryPointName;
|
||||
}
|
||||
}
|
||||
|
||||
const _defaultEntrypointName = "(no name provided)";
|
@ -2,11 +2,14 @@ library angular2.transform.reflection_remover.remove_reflection_capabilities;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
import 'package:angular2/src/transform/common/asset_reader.dart';
|
||||
import 'package:angular2/src/transform/common/mirror_mode.dart';
|
||||
import 'package:barback/barback.dart';
|
||||
|
||||
import 'package:angular2/src/transform/common/annotation_matcher.dart';
|
||||
import 'package:angular2/src/transform/common/asset_reader.dart';
|
||||
import 'package:angular2/src/transform/common/mirror_mode.dart';
|
||||
|
||||
import 'codegen.dart';
|
||||
import 'entrypoint_matcher.dart';
|
||||
import 'rewriter.dart';
|
||||
|
||||
/// Finds the call to the Angular2 `ReflectionCapabilities` constructor
|
||||
@ -15,14 +18,15 @@ import 'rewriter.dart';
|
||||
///
|
||||
/// This only searches the code in `reflectionEntryPoint`, not `part`s,
|
||||
/// `import`s, `export`s, etc.
|
||||
Future<String> removeReflectionCapabilities(
|
||||
AssetReader reader, AssetId reflectionEntryPoint,
|
||||
Future<String> removeReflectionCapabilities(AssetReader reader,
|
||||
AssetId reflectionEntryPoint, AnnotationMatcher annotationMatcher,
|
||||
{MirrorMode mirrorMode: MirrorMode.none,
|
||||
bool writeStaticInit: true}) async {
|
||||
var code = await reader.readAsString(reflectionEntryPoint);
|
||||
|
||||
var codegen = new Codegen(reflectionEntryPoint);
|
||||
return new Rewriter(code, codegen,
|
||||
new EntrypointMatcher(reflectionEntryPoint, annotationMatcher),
|
||||
mirrorMode: mirrorMode, writeStaticInit: writeStaticInit)
|
||||
.rewrite(parseCompilationUnit(code, name: reflectionEntryPoint.path));
|
||||
}
|
||||
|
@ -1,31 +1,43 @@
|
||||
library angular2.transform.reflection_remover.rewriter;
|
||||
|
||||
import 'package:analyzer/src/generated/ast.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'package:angular2/src/transform/common/logging.dart';
|
||||
import 'package:angular2/src/transform/common/mirror_matcher.dart';
|
||||
import 'package:angular2/src/transform/common/mirror_mode.dart';
|
||||
import 'package:angular2/src/transform/common/names.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'codegen.dart';
|
||||
import 'entrypoint_matcher.dart';
|
||||
|
||||
class Rewriter {
|
||||
final String _code;
|
||||
final Codegen _codegen;
|
||||
final EntrypointMatcher _entrypointMatcher;
|
||||
final MirrorMatcher _mirrorMatcher;
|
||||
final MirrorMode _mirrorMode;
|
||||
final bool _writeStaticInit;
|
||||
|
||||
Rewriter(this._code, this._codegen,
|
||||
Rewriter(this._code, this._codegen, this._entrypointMatcher,
|
||||
{MirrorMatcher mirrorMatcher,
|
||||
MirrorMode mirrorMode: MirrorMode.none,
|
||||
bool writeStaticInit: true})
|
||||
: _mirrorMode = mirrorMode,
|
||||
_writeStaticInit = writeStaticInit,
|
||||
_mirrorMatcher =
|
||||
mirrorMatcher == null ? const MirrorMatcher() : mirrorMatcher;
|
||||
mirrorMatcher == null ? const MirrorMatcher() : mirrorMatcher {
|
||||
if (_codegen == null) {
|
||||
throw new ArgumentError.notNull('Codegen');
|
||||
}
|
||||
if (_entrypointMatcher == null) {
|
||||
throw new ArgumentError.notNull('EntrypointMatcher');
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites the provided code removing imports of the
|
||||
/// Rewrites the provided code to remove dart:mirrors.
|
||||
///
|
||||
/// Specifically, removes imports of the
|
||||
/// {@link ReflectionCapabilities} library and instantiations of
|
||||
/// {@link ReflectionCapabilities}, as detected by the (potentially) provided
|
||||
/// {@link MirrorMatcher}.
|
||||
@ -51,7 +63,7 @@ class Rewriter {
|
||||
class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
|
||||
final Rewriter _rewriter;
|
||||
final buf = new StringBuffer();
|
||||
final reflectionCapabilityAssignments = [];
|
||||
final reflectionCapabilityAssignments = <AssignmentExpression>[];
|
||||
|
||||
int _currentIndex = 0;
|
||||
bool _setupAdded = false;
|
||||
@ -105,6 +117,45 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
|
||||
return super.visitMethodInvocation(node);
|
||||
}
|
||||
|
||||
@override
|
||||
Object visitMethodDeclaration(MethodDeclaration node) {
|
||||
if (_rewriter._entrypointMatcher.isEntrypoint(node)) {
|
||||
if (_rewriter._writeStaticInit) {
|
||||
_rewriteEntrypointFunctionBody(node.body);
|
||||
}
|
||||
}
|
||||
return super.visitMethodDeclaration(node);
|
||||
}
|
||||
|
||||
@override
|
||||
Object visitFunctionDeclaration(FunctionDeclaration node) {
|
||||
if (_rewriter._entrypointMatcher.isEntrypoint(node)) {
|
||||
if (_rewriter._writeStaticInit) {
|
||||
_rewriteEntrypointFunctionBody(node.functionExpression.body);
|
||||
}
|
||||
}
|
||||
return super.visitFunctionDeclaration(node);
|
||||
}
|
||||
|
||||
void _rewriteEntrypointFunctionBody(FunctionBody node) {
|
||||
if (node is BlockFunctionBody) {
|
||||
final insertOffset = node.block.leftBracket.end;
|
||||
buf.write(_rewriter._code.substring(_currentIndex, insertOffset));
|
||||
buf.write(_getStaticReflectorInitBlock());
|
||||
_currentIndex = insertOffset;
|
||||
} else if (node is ExpressionFunctionBody) {
|
||||
// TODO(kegluneq): Add support, see issue #5474.
|
||||
throw new ArgumentError(
|
||||
'Arrow syntax is not currently supported as `@AngularEntrypoint`s');
|
||||
} else if (node is NativeFunctionBody) {
|
||||
throw new ArgumentError('Native functions and methods are not supported '
|
||||
'as `@AngularEntrypoint`s');
|
||||
} else if (node is EmptyFunctionBody) {
|
||||
throw new ArgumentError('Empty functions and methods are not supported '
|
||||
'as `@AngularEntrypoint`s');
|
||||
}
|
||||
}
|
||||
|
||||
String outputRewrittenCode() {
|
||||
if (_currentIndex < _rewriter._code.length) {
|
||||
buf.write(_rewriter._code.substring(_currentIndex));
|
||||
|
@ -52,8 +52,11 @@ class ReflectionRemover extends Transformer implements LazyTransformer {
|
||||
}
|
||||
|
||||
var transformedCode = await removeReflectionCapabilities(
|
||||
new AssetReader.fromTransform(transform), primaryId,
|
||||
mirrorMode: mirrorMode, writeStaticInit: writeStaticInit);
|
||||
new AssetReader.fromTransform(transform),
|
||||
primaryId,
|
||||
options.annotationMatcher,
|
||||
mirrorMode: mirrorMode,
|
||||
writeStaticInit: writeStaticInit);
|
||||
transform.addOutput(new Asset.fromString(primaryId, transformedCode));
|
||||
}, log: transform.logger);
|
||||
}
|
||||
|
Reference in New Issue
Block a user