Skip to content

Commit

Permalink
922da62 chore(browser_tree): fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbcross committed Dec 1, 2015
1 parent e70c4f3 commit 760fc7a
Show file tree
Hide file tree
Showing 22 changed files with 770 additions and 452 deletions.
4 changes: 2 additions & 2 deletions BUILD_INFO
@@ -1,2 +1,2 @@
Tue Dec 1 21:54:43 UTC 2015
3e364b0d41154e637cbcea12f929e578781a61af
Tue Dec 1 21:58:14 UTC 2015
922da62720599f003f4b04edeeca478429407b01
836 changes: 419 additions & 417 deletions _analyzer.dart

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/angular2.dart
Expand Up @@ -9,6 +9,8 @@ export 'package:angular2/core.dart'
hide forwardRef, resolveForwardRef, ForwardRefFn;
export 'package:angular2/common.dart';
export 'package:angular2/instrumentation.dart';
export 'package:angular2/src/core/angular_entrypoint.dart'
show AngularEntrypoint;
export 'package:angular2/src/core/application_tokens.dart'
hide APP_COMPONENT_REF_PROMISE, APP_ID_RANDOM_PROVIDER;
export 'package:angular2/src/platform/dom/dom_tokens.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/bootstrap.dart
Expand Up @@ -5,3 +5,5 @@
library angular2.bootstrap;

export "package:angular2/platform/browser.dart" show bootstrap;
export "package:angular2/src/core/angular_entrypoint.dart"
show AngularEntrypoint;
2 changes: 2 additions & 0 deletions lib/bootstrap_static.dart
Expand Up @@ -5,3 +5,5 @@
library angular2.bootstrap_static;

export "package:angular2/platform/browser_static.dart" show bootstrapStatic;
export "package:angular2/src/core/angular_entrypoint.dart"
show AngularEntrypoint;
2 changes: 2 additions & 0 deletions lib/platform/browser.dart
@@ -1,5 +1,7 @@
library angular2.platform.browser;

export "package:angular2/src/core/angular_entrypoint.dart"
show AngularEntrypoint;
export "package:angular2/src/platform/browser_common.dart"
show
BROWSER_PROVIDERS,
Expand Down
4 changes: 3 additions & 1 deletion lib/platform/browser_static.dart
@@ -1,5 +1,7 @@
library angular2.platform.browser_static;

export "package:angular2/src/core/angular_entrypoint.dart"
show AngularEntrypoint;
export "package:angular2/src/platform/browser_common.dart"
show
BROWSER_PROVIDERS,
Expand All @@ -15,7 +17,7 @@ import "package:angular2/src/facade/lang.dart" show Type, isPresent;
import "package:angular2/src/facade/promise.dart" show Future;
import "package:angular2/src/platform/browser_common.dart"
show BROWSER_PROVIDERS, BROWSER_APP_COMMON_PROVIDERS;
import "package:angular2/core.dart" show ComponentRef, platform, reflector;
import "package:angular2/core.dart" show ComponentRef, platform;

/**
* An array of providers that should be passed into `application()` when bootstrapping a component
Expand Down
22 changes: 22 additions & 0 deletions lib/src/core/angular_entrypoint.dart
@@ -0,0 +1,22 @@
library angular2.src.core.angular_entrypoint;

/**
* Marks a function or method as an Angular 2 entrypoint. Only necessary in Dart code.
*
* The optional `name` parameter will be reflected in logs when the entry point is processed.
*
* See [the wiki][] for detailed documentation.
* [the wiki]: https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer#entry_points
*
* ## Example
*
* ```
* @AngularEntrypoint("name-for-debug")
* void main() {
* bootstrap(MyComponent);
* }
*/
class AngularEntrypoint {
final String name;
const AngularEntrypoint([this.name]);
}
22 changes: 20 additions & 2 deletions lib/src/transform/common/annotation_matcher.dart
Expand Up @@ -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'),
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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);
}
59 changes: 59 additions & 0 deletions lib/src/transform/reflection_remover/entrypoint_matcher.dart
@@ -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)";
Expand Up @@ -2,11 +2,14 @@ library angular2.transform.reflection_remover.remove_reflection_capabilities;

import 'dart:async';
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/asset_reader.dart';
import 'package:angular2/src/transform/common/mirror_mode.dart';
import 'package:barback/barback.dart';

import 'codegen.dart';
import 'entrypoint_matcher.dart';
import 'rewriter.dart';

/// Finds the call to the Angular2 `ReflectionCapabilities` constructor
Expand All @@ -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));
}
61 changes: 56 additions & 5 deletions lib/src/transform/reflection_remover/rewriter.dart
@@ -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}.
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 5 additions & 2 deletions lib/src/transform/reflection_remover/transformer.dart
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions test/public_api_spec.dart
Expand Up @@ -84,6 +84,8 @@ var NG_ALL = [
"AbstractControlDirective.valid",
"AbstractControlDirective.value",
"AbstractControlDirective.path",
"AngularEntrypoint",
"AngularEntrypoint.name",
"AppRootUrl",
"AppRootUrl.value",
"AppRootUrl.value=",
Expand Down
@@ -0,0 +1,8 @@
library web_foo;

import 'package:angular2/bootstrap.dart';

abstract class TestBootstrapper {
@AngularEntrypoint()
void testBootstrap();
}

0 comments on commit 760fc7a

Please sign in to comment.