Skip to content

Commit a2e7ae5

Browse files
committed
feat(transformers): update transformers to handle components without @view
1 parent bd31b01 commit a2e7ae5

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ class _DirectiveMetadataVisitor extends Object
141141
List<String> _outputs;
142142
Map<String, String> _host;
143143
List<LifecycleHooks> _lifecycleHooks;
144-
CompileTemplateMetadata _template;
144+
CompileTemplateMetadata _cmpTemplate;
145+
CompileTemplateMetadata _viewTemplate;
145146

146147
void reset(AssetId assetId) {
147148
_lifecycleVisitor.reset(assetId);
@@ -157,23 +158,29 @@ class _DirectiveMetadataVisitor extends Object
157158
_outputs = <String>[];
158159
_host = <String, String>{};
159160
_lifecycleHooks = null;
160-
_template = null;
161+
_cmpTemplate = null;
162+
_viewTemplate = null;
161163
}
162164

163165
bool get hasMetadata => _hasMetadata;
164166

165-
CompileDirectiveMetadata createMetadata() => CompileDirectiveMetadata.create(
166-
type: _type,
167-
isComponent: _isComponent,
168-
dynamicLoadable: true, // NOTE(kegluneq): For future optimization.
169-
selector: _selector,
170-
exportAs: _exportAs,
171-
changeDetection: _changeDetection,
172-
inputs: _inputs,
173-
outputs: _outputs,
174-
host: _host,
175-
lifecycleHooks: _lifecycleHooks,
176-
template: _template);
167+
get _template => _viewTemplate != null ? _viewTemplate : _cmpTemplate;
168+
169+
CompileDirectiveMetadata createMetadata() {
170+
return CompileDirectiveMetadata.create(
171+
type: _type,
172+
isComponent: _isComponent,
173+
dynamicLoadable: true,
174+
// NOTE(kegluneq): For future optimization.
175+
selector: _selector,
176+
exportAs: _exportAs,
177+
changeDetection: _changeDetection,
178+
inputs: _inputs,
179+
outputs: _outputs,
180+
host: _host,
181+
lifecycleHooks: _lifecycleHooks,
182+
template: _template);
183+
}
177184

178185
@override
179186
Object visitAnnotation(Annotation node) {
@@ -183,20 +190,26 @@ class _DirectiveMetadataVisitor extends Object
183190
if (_hasMetadata) {
184191
throw new FormatException(
185192
'Only one Directive is allowed per class. '
186-
'Found unexpected "$node".',
193+
'Found unexpected "$node".',
187194
'$node' /* source */);
188195
}
189196
_isComponent = isComponent;
190197
_hasMetadata = true;
198+
if (isComponent) {
199+
var t = new _CompileTemplateMetadataVisitor().visitAnnotation(node);
200+
if (t.template != null || t.templateUrl != null) {
201+
_cmpTemplate = t;
202+
}
203+
}
191204
super.visitAnnotation(node);
192205
} else if (_annotationMatcher.isView(node, _assetId)) {
193-
if (_template != null) {
206+
if (_viewTemplate!= null) {
194207
throw new FormatException(
195208
'Only one View is allowed per class. '
196209
'Found unexpected "$node".',
197210
'$node' /* source */);
198211
}
199-
_template = new _CompileTemplateMetadataVisitor().visitAnnotation(node);
212+
_viewTemplate = new _CompileTemplateMetadataVisitor().visitAnnotation(node);
200213
}
201214

202215
// Annotation we do not recognize - no need to visit.

modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import 'package:code_transformers/assets.dart';
1919
///
2020
/// The returned value wraps the [NgDeps] at `entryPoint` as well as these
2121
/// created objects.
22-
Future<CompileDataResults> createCompileData(
23-
AssetReader reader, AssetId entryPoint) async {
22+
Future<CompileDataResults> createCompileData(AssetReader reader,
23+
AssetId entryPoint) async {
2424
return new _CompileDataCreator(reader, entryPoint).createCompileData();
2525
}
2626

@@ -43,6 +43,14 @@ bool _isViewAnnotation(InstanceCreationExpression node) {
4343
return constructorName.name == 'View';
4444
}
4545

46+
bool _isComponentAnnotation(InstanceCreationExpression node) {
47+
var constructorName = node.constructorName.type.name;
48+
if (constructorName is PrefixedIdentifier) {
49+
constructorName = constructorName.identifier;
50+
}
51+
return constructorName.name == 'Component';
52+
}
53+
4654
/// Creates [ViewDefinition] objects for all `View` `Directive`s defined in
4755
/// `entryPoint`.
4856
class _CompileDataCreator {
@@ -68,6 +76,7 @@ class _CompileDataCreator {
6876
// Note: we use '' because the current file maps to the default prefix.
6977
var ngMeta = visitor._metadataMap[''];
7078
var typeName = '${rType.typeName}';
79+
7180
if (ngMeta.types.containsKey(typeName)) {
7281
visitor.compileData.component = ngMeta.types[typeName];
7382
} else {
@@ -161,7 +170,6 @@ class _CompileDataCreator {
161170
return retVal;
162171
}
163172
}
164-
165173
/// Visitor responsible for processing the `annotations` property of a
166174
/// [RegisterType] object, extracting the `directives` dependencies, and adding
167175
/// their associated [CompileDirectiveMetadata] to the `directives` of a
@@ -187,7 +195,8 @@ class _DirectiveDependenciesVisitor extends Object
187195
/// reflector.
188196
@override
189197
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
190-
if (_isViewAnnotation(node)) {
198+
// if (_isViewAnnotation(node)) {
199+
if (_isViewAnnotation(node) || _isComponentAnnotation(node)) {
191200
compileData = new NormalizedComponentWithViewDirectives(
192201
null, <CompileDirectiveMetadata>[]);
193202
node.visitChildren(this);
@@ -202,7 +211,7 @@ class _DirectiveDependenciesVisitor extends Object
202211
if (node.name is! Label || node.name.label is! SimpleIdentifier) {
203212
logger.error(
204213
'Angular 2 currently only supports simple identifiers in directives.'
205-
' Source: ${node}');
214+
' Source: ${node}');
206215
return null;
207216
}
208217
if ('${node.name.label}' == 'directives') {
@@ -234,7 +243,7 @@ class _DirectiveDependenciesVisitor extends Object
234243
} else {
235244
logger.error(
236245
'Angular 2 currently only supports simple and prefixed identifiers '
237-
'as values for "directives". Source: $node');
246+
'as values for "directives". Source: $node');
238247
return;
239248
}
240249
if (ngMeta.types.containsKey(name)) {

modules_dart/transform/test/transform/integration/simple_annotation_files/bar.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ library bar;
22

33
import 'package:angular2/src/core/metadata.dart';
44

5-
@Component(selector: '[soup]')
6-
@View(template: '')
5+
@Component(selector: '[soup]', template: 'aa')
76
class MyComponent {
87
MyComponent();
98
}

modules_dart/transform/test/transform/integration/simple_annotation_files/expected/bar.ng_deps.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ void initReflector() {
1616
..registerType(
1717
MyComponent,
1818
new _ngRef.ReflectionInfo(const [
19-
const Component(selector: '[soup]'),
20-
const View(template: ''),
19+
const Component(selector: '[soup]', template: 'aa'),
2120
_templates.HostMyComponentTemplate
2221
], const [], () => new MyComponent()));
2322
i0.initReflector();

0 commit comments

Comments
 (0)