Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Report deprecation of child queries inside component constructors.
Browse files Browse the repository at this point in the history
Example:
Queries inside constructors are deprecated, please replace with query
annotations on fields in class FrequencyCapComponent.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140380047
  • Loading branch information
ferhatb authored and matanlurey committed Nov 28, 2016
1 parent ef06b11 commit 7b2953f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/src/transform/common/type_metadata_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:angular2/src/transform/common/annotation_matcher.dart';
import 'package:angular2/src/transform/common/interface_matcher.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:barback/barback.dart' show AssetId;
import 'package:logging/logging.dart';

import 'naive_eval.dart';
import 'url_resolver.dart';
Expand Down Expand Up @@ -225,7 +226,8 @@ class _CompileTypeMetadataVisitor extends Object
final fieldTypes = _readFields(node);
diDeps = constructor == null
? []
: _getCompileDiDependencyMetadata(constructor.parameters, fieldTypes);
: _getCompileDiDependencyMetadata(
node, constructor.parameters, fieldTypes);
_type = new CompileTypeMetadata(
moduleUrl: toAssetUri(_assetId),
name: node.name.toString(),
Expand Down Expand Up @@ -301,7 +303,7 @@ class _CompileFactoryMetadataVisitor extends Object
moduleUrl: toAssetUri(_assetId),
name: node.name.toString(),
diDeps: _getCompileDiDependencyMetadata(
node.functionExpression.parameters, {}),
null, node.functionExpression.parameters, {}),
runtime: null // Intentionally `null`, cannot be provided here.
);
}
Expand Down Expand Up @@ -1218,7 +1220,9 @@ CompileQueryMetadata _createQueryMetadata(Annotation a, String propertyName,
}

List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata(
FormalParameterList params, Map<String, TypeName> fieldTypes) {
ClassDeclaration classDecl,
FormalParameterList params,
Map<String, TypeName> fieldTypes) {
return params.parameters.map((dynamic p) {
if (p is DefaultFormalParameter) {
p = p.parameter;
Expand Down Expand Up @@ -1247,20 +1251,24 @@ List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata(
if (_hasAnnotation(p, "Query")) {
query =
_createQueryMetadata(_getAnnotation(p, "Query"), null, first: false);
_reportDeprecation(classDecl, query);
}
if (_hasAnnotation(p, "ContentChildren")) {
query = _createQueryMetadata(_getAnnotation(p, "ContentChildren"), null,
defaultDescendantsValue: true, first: false);
_reportDeprecation(classDecl, query);
}

var viewQuery;
if (_hasAnnotation(p, "ViewQuery")) {
viewQuery = _createQueryMetadata(_getAnnotation(p, "ViewQuery"), null,
first: false);
_reportDeprecation(classDecl, viewQuery);
}
if (_hasAnnotation(p, "ViewChildren")) {
viewQuery = _createQueryMetadata(_getAnnotation(p, "ViewChildren"), null,
defaultDescendantsValue: true, first: false);
_reportDeprecation(classDecl, viewQuery);
}
if (token == null) {
throw new ArgumentError(
Expand All @@ -1278,6 +1286,16 @@ List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata(
}).toList();
}

final Logger _deprecationLogger = new Logger('angular2.transformer');
void _reportDeprecation(
ClassDeclaration classDecl, CompileQueryMetadata query) {
String source = classDecl == null
? '${query?.propertyName}'
: 'in class ${classDecl.name.name}';
_deprecationLogger.warning('Queries inside constructors are deprecated, '
'please replace with query annotations on fields $source');
}

Annotation _getAnnotation(p, String attrName) =>
p.metadata.where((m) => m.name.toString() == attrName).first;

Expand Down

0 comments on commit 7b2953f

Please sign in to comment.