Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ const _COMPONENTS = const [
superClass: 'Directive'),
];

const _PIPES = const [
const ClassDescriptor(
'Pipe', 'package:angular2/src/core/metadata/directive.dart',
superClass: 'Injectable'),
const ClassDescriptor('Pipe', 'package:angular2/src/core/metadata.dart',
superClass: 'Injectable'),
const ClassDescriptor('Pipe', 'package:angular2/angular2.dart',
superClass: 'Injectable'),
const ClassDescriptor('Pipe', 'package:angular2/core.dart',
superClass: 'Injectable'),
const ClassDescriptor('Pipe', 'package:angular2/web_worker/worker.dart',
superClass: 'Injectable'),
];

const _VIEWS = const [
const ClassDescriptor('View', 'package:angular2/angular2.dart'),
const ClassDescriptor('View', 'package:angular2/web_worker/worker.dart'),
Expand Down Expand Up @@ -79,6 +93,7 @@ class AnnotationMatcher extends ClassMatcherBase {
return new AnnotationMatcher._([]
..addAll(_COMPONENTS)
..addAll(_DIRECTIVES)
..addAll(_PIPES)
..addAll(_INJECTABLES)
..addAll(_VIEWS)
..addAll(_ENTRYPOINTS));
Expand Down Expand Up @@ -109,6 +124,10 @@ class AnnotationMatcher extends ClassMatcherBase {
bool isView(Annotation annotation, AssetId assetId) =>
_implementsWithWarning(annotation, assetId, _VIEWS);

/// Checks if an [Annotation] node implements [Pipe].
bool isPipe(Annotation annotation, AssetId assetId) =>
_implementsWithWarning(annotation, assetId, _PIPES);

/// Checks if an [Annotation] node implements [AngularEntrypoint]
bool isEntrypoint(Annotation annotation, AssetId assetId) =>
_implementsWithWarning(annotation, assetId, _ENTRYPOINTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,34 @@ class ReflectionInfoVisitor extends RecursiveAstVisitor<ReflectionInfoModel> {
}

if (node.metadata != null) {
var componentDirectives, viewDirectives;
var componentDirectives = [];
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since _extractReferencedType returns Iterable, consider using new Iterable.empty() here rather than a list literal to maintain consistent type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

var componentPipes = [];
var viewDirectives, viewPipes;
node.metadata.forEach((node) {
if (_annotationMatcher.isComponent(node, assetId)) {
componentDirectives = _extractDirectives(node);
componentDirectives = _extractReferencedTypes(node, 'directives');
componentPipes = _extractReferencedTypes(node, 'pipes');
} else if (_annotationMatcher.isView(node, assetId)) {
viewDirectives = _extractDirectives(node);
viewDirectives = _extractReferencedTypes(node, 'directives');
viewPipes = _extractReferencedTypes(node, 'pipes');
}
model.annotations.add(_annotationVisitor.visitAnnotation(node));
});
if (componentDirectives != null && componentDirectives.isNotEmpty) {
if (viewDirectives != null) {
log.warning(
'Cannot specify view parameters on @Component when a @View '
'is present. Component name: ${model.name}',
asset: assetId);
}
model.directives.addAll(componentDirectives);
} else if (viewDirectives != null) {
if ((componentDirectives.isNotEmpty || componentPipes.isNotEmpty) &&
(viewDirectives != null || viewPipes != null)) {
log.warning(
'Cannot specify view parameters on @Component when a @View '
'is present. Component name: ${model.name}',
asset: assetId);
}
model.directives.addAll(componentDirectives);
model.pipes.addAll(componentPipes);
if (viewDirectives != null) {
model.directives.addAll(viewDirectives);
}
if (viewPipes != null) {
model.pipes.addAll(viewPipes);
}
}
if (ctor != null &&
ctor.parameters != null &&
Expand Down Expand Up @@ -151,43 +159,43 @@ class ReflectionInfoVisitor extends RecursiveAstVisitor<ReflectionInfoModel> {
}
}

/// Returns [PrefixedDirective] values parsed from the value of the
/// `directives` parameter of the provided `node`.
/// Returns [PrefixedType] values parsed from the value of the
/// `directives`/`pipes` parameter of the provided `node`.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This method will parse from any provided fieldName, not just directives/pipes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// This will always return a non-null value, so if there are no `directives`
/// specified on `node`, it will return an empty iterable.
Iterable<PrefixedDirective> _extractDirectives(Annotation node) {
/// nor `pipes` specified on `node`, it will return an empty iterable.
Iterable<PrefixedType> _extractReferencedTypes(Annotation node, String fieldName) {
assert(_annotationMatcher.isComponent(node, assetId) ||
_annotationMatcher.isView(node, assetId));

if (node.arguments == null && node.arguments.arguments == null) {
return const [];
}
final directivesNode = node.arguments.arguments.firstWhere((arg) {
return arg is NamedExpression && '${arg.name.label}' == 'directives';
final typesNode = node.arguments.arguments.firstWhere((arg) {
return arg is NamedExpression && '${arg.name.label}' == fieldName;
}, orElse: () => null);
if (directivesNode == null) return const [];
if (typesNode == null) return const [];

if (directivesNode.expression is! ListLiteral) {
if (typesNode.expression is! ListLiteral) {
log.warning(
'Angular 2 expects a list literal for `directives` '
'but found a ${directivesNode.expression.runtimeType}',
'Angular 2 expects a list literal for `${fieldName}` '
'but found a ${typesNode.expression.runtimeType}',
asset: assetId);
return const [];
}
final directives = <PrefixedDirective>[];
for (var dep in (directivesNode.expression as ListLiteral).elements) {
final types = <PrefixedType>[];
for (var dep in (typesNode.expression as ListLiteral).elements) {
if (dep is PrefixedIdentifier) {
directives.add(new PrefixedDirective()
types.add(new PrefixedType()
..prefix = '${dep.prefix}'
..name = '${dep.identifier}');
} else if (dep is Identifier) {
directives.add(new PrefixedDirective()..name = '${dep}');
types.add(new PrefixedType()..name = '${dep}');
} else {
log.warning('Found unexpected value $dep in `directives`.',
log.warning('Found unexpected value $dep in `types`.',
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/types/$fieldName/

Also, would you change "Found" to "Ignoring" in this message?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

asset: assetId);
}
}
return directives;
return types;
}

@override
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ message PropertyMetadataModel {
repeated AnnotationModel annotations = 2;
}

message PrefixedDirective {
// The prefix used to reference this Directive, if any.
message PrefixedType {
// The prefix used to reference this Type, if any.
optional string prefix = 1;

// The name of the Directive or directive alias.
// See https://goo.gl/d8XPt0 for info on directive aliases.
// The name of the Type or type alias.
// See https://goo.gl/d8XPt0 for info on type aliases.
optional string name = 2;
}

Expand All @@ -44,5 +44,8 @@ message ReflectionInfoModel {

// Directive dependencies parsed from the @View or @Component `directives`
// parameter.
repeated PrefixedDirective directives = 8;
repeated PrefixedType directives = 8;

// Pipe dependencies parsed from the @View or @Component `pipes` parameter.
repeated PrefixedType pipes = 9;
}
11 changes: 8 additions & 3 deletions modules_dart/transform/lib/src/transform/common/ng_compiler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library angular2.transform.template_compiler.ng_compiler;

import 'package:angular2/src/compiler/command_compiler.dart';
import 'package:angular2/src/compiler/view_compiler.dart';
import 'package:angular2/src/compiler/html_parser.dart';
import 'package:angular2/src/compiler/style_compiler.dart';
import 'package:angular2/src/compiler/template_compiler.dart';
Expand All @@ -13,6 +13,7 @@ import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/core/change_detection/interfaces.dart';
import 'package:angular2/src/compiler/change_detector_compiler.dart';
import 'package:angular2/router/router_link_dsl.dart';
import 'package:angular2/src/compiler/proto_view_compiler.dart';

import 'xhr_impl.dart';
import 'url_resolver.dart';
Expand All @@ -37,6 +38,10 @@ TemplateCompiler createTemplateCompiler(AssetReader reader,
new TemplateNormalizer(_xhr, _urlResolver, _htmlParser),
templateParser,
new StyleCompiler(_xhr, _urlResolver),
new CommandCompiler(),
cdCompiler);
cdCompiler,
new ProtoViewCompiler(),
new ViewCompiler(),
null /* ResolvedMetadataCache */,
changeDetectionConfig
);
}
10 changes: 5 additions & 5 deletions modules_dart/transform/lib/src/transform/common/ng_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NgMeta {
static const _VALUE_KEY = 'value';

/// Directive metadata for each type annotated as a directive.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this comment & explain the expected value types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

final Map<String, CompileDirectiveMetadata> types;
final Map<String, dynamic> types;

/// List of other types and names associated with a given name.
final Map<String, List<String>> aliases;
Expand All @@ -43,7 +43,7 @@ class NgMeta {
final NgDepsModel ngDeps;

NgMeta(
{Map<String, CompileDirectiveMetadata> types,
{Map<String, dynamic> types,
Map<String, List<String>> aliases,
this.ngDeps: null})
: this.types = types != null ? types : {},
Expand Down Expand Up @@ -91,7 +91,7 @@ class NgMeta {
continue;
}
if (entry[_KIND_KEY] == _TYPE_VALUE) {
types[key] = CompileDirectiveMetadata.fromJson(entry[_VALUE_KEY]);
types[key] = CompileMetadataWithType.fromJson(entry[_VALUE_KEY]);
} else if (entry[_KIND_KEY] == _ALIAS_VALUE) {
aliases[key] = entry[_VALUE_KEY];
}
Expand Down Expand Up @@ -123,8 +123,8 @@ class NgMeta {
}

/// Returns the metadata for every type associated with the given [alias].
List<CompileDirectiveMetadata> flatten(String alias) {
var result = <CompileDirectiveMetadata>[];
List<dynamic> flatten(String alias) {
var result = [];
var seen = new Set();
helper(name) {
if (!seen.add(name)) {
Expand Down
8 changes: 8 additions & 0 deletions modules_dart/transform/lib/src/transform/common/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ENTRY_POINT_PARAM = 'entry_points';
const FORMAT_CODE_PARAM = 'format_code';
const REFLECT_PROPERTIES_AS_ATTRIBUTES = 'reflect_properties_as_attributes';
const PLATFORM_DIRECTIVES = 'platform_directives';
const PLATFORM_PIPES = 'platform_pipes';
const INIT_REFLECTOR_PARAM = 'init_reflector';
const INLINE_VIEWS_PARAM = 'inline_views';
const MIRROR_MODE_PARAM = 'mirror_mode';
Expand Down Expand Up @@ -47,6 +48,10 @@ class TransformerOptions {
/// Format of an item in the list: angular2/lib/src/common/directives.dart#CORE_DIRECTIVES
final List<String> platformDirectives;

/// A set of pipes that will be automatically passed-in to the template compiler
/// Format of an item in the list: angular2/lib/src/common/directives.dart#CORE_DIRECTIVES
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/CORE_DIRECTIVES/CORE_PIPES/ ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

final List<String> platformPipes;

/// Whether to format generated code.
/// Code that is only modified will never be formatted because doing so may
/// invalidate the source maps generated by `dart2js` and/or other tools.
Expand Down Expand Up @@ -76,6 +81,7 @@ class TransformerOptions {
{this.genChangeDetectionDebugInfo,
this.reflectPropertiesAsAttributes,
this.platformDirectives,
this.platformPipes,
this.inlineViews,
this.lazyTransformers,
this.formatCode});
Expand All @@ -89,6 +95,7 @@ class TransformerOptions {
bool genChangeDetectionDebugInfo: false,
bool reflectPropertiesAsAttributes: false,
List<String> platformDirectives,
List<String> platformPipes,
bool lazyTransformers: false,
bool formatCode: false}) {
var annotationMatcher = new AnnotationMatcher()
Expand All @@ -101,6 +108,7 @@ class TransformerOptions {
genChangeDetectionDebugInfo: genChangeDetectionDebugInfo,
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
inlineViews: inlineViews,
lazyTransformers: lazyTransformers,
formatCode: formatCode);
Expand Down
Loading