forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.dart
More file actions
515 lines (452 loc) · 16.2 KB
/
Copy pathcodegen.dart
File metadata and controls
515 lines (452 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
library angular2.src.transform;
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/java_core.dart';
import 'package:barback/barback.dart' show AssetId, TransformLogger;
import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as path;
import 'annotation_processor.dart';
import 'logging.dart';
/// Base class that maintains codegen state.
class Context {
/// Maps libraries to the import prefixes we will use in the newly
/// generated code.
final Map<LibraryElement, String> _libraryPrefixes = {};
/// Whether to generate constructor stubs for classes annotated
/// with [Component], [Decorator], [Template], and [Inject] (and subtypes).
bool generateCtorStubs = true;
/// Whether to generate setter stubs for classes annotated with
/// [Directive] subtypes. These setters depend on the value passed to the
/// annotation's `bind` value.
bool generateSetterStubs = true;
DirectiveRegistry _directiveRegistry;
/// Generates [registerType] calls for all [register]ed [AnnotationMatch]
/// objects.
DirectiveRegistry get directiveRegistry => _directiveRegistry;
Context() {
_directiveRegistry = new _DirectiveRegistryImpl(this);
}
/// If elements in [lib] should be prefixed in our generated code, returns
/// the appropriate prefix followed by a `.`. Future items from the same
/// library will use the same prefix.
/// If [lib] does not need a prefix, returns the empty string.
String _getPrefixDot(LibraryElement lib) {
if (lib == null || lib.isInSdk) return '';
var prefix =
_libraryPrefixes.putIfAbsent(lib, () => 'i${_libraryPrefixes.length}');
return '${prefix}.';
}
}
/// Object which [register]s [AnnotationMatch] objects for code generation.
abstract class DirectiveRegistry {
// Adds [entry] to the `registerType` calls which will be generated.
void register(AnnotationMatch entry);
}
const setupReflectionMethodName = 'setupReflection';
const _libraryDeclaration = '''
library angular2.src.transform.generated;
''';
const _reflectorImport = '''
import 'package:angular2/src/reflection/reflection.dart' show reflector;
''';
/// Default implementation to map from [LibraryElement] to [AssetId]. This
/// assumes that [el.source] has a getter called [assetId].
AssetId _assetIdFromLibraryElement(LibraryElement el) {
return (el.source as dynamic).assetId;
}
String codegenEntryPoint(Context context, {AssetId newEntryPoint}) {
if (newEntryPoint == null) {
throw new ArgumentError.notNull('newEntryPoint');
}
// TODO(jakemac): copyright and library declaration
var outBuffer = new StringBuffer()
..write(_libraryDeclaration)
..write(_reflectorImport);
_codegenImports(context, newEntryPoint, outBuffer);
outBuffer
.write('${setupReflectionMethodName}() {${context.directiveRegistry}}');
return new DartFormatter().format(outBuffer.toString());
}
void _codegenImports(
Context context, AssetId newEntryPoint, StringBuffer buffer) {
context._libraryPrefixes.forEach((lib, prefix) {
buffer
..write(_codegenImport(
context, _assetIdFromLibraryElement(lib), newEntryPoint))
..writeln('as ${prefix};');
});
}
String _codegenImport(Context context, AssetId libraryId, AssetId entryPoint) {
if (libraryId.path.startsWith('lib/')) {
var packagePath = libraryId.path.replaceFirst('lib/', '');
return "import 'package:${libraryId.package}/${packagePath}'";
} else if (libraryId.package != entryPoint.package) {
logger.error("Can't import `${libraryId}` from `${entryPoint}`");
} else if (path.url.split(libraryId.path)[0] ==
path.url.split(entryPoint.path)[0]) {
var relativePath =
path.relative(libraryId.path, from: path.dirname(entryPoint.path));
return "import '${relativePath}'";
} else {
logger.error("Can't import `${libraryId}` from `${entryPoint}`");
}
}
// TODO(https://github.com/kegluneq/angular/issues/4): Remove calls to
// Element#node.
class _DirectiveRegistryImpl implements DirectiveRegistry {
final Context _context;
final PrintWriter _writer;
final Set<ClassDeclaration> _seen = new Set();
final _AnnotationsTransformVisitor _annotationsVisitor;
final _BindTransformVisitor _bindVisitor;
final _FactoryTransformVisitor _factoryVisitor;
final _ParameterTransformVisitor _parametersVisitor;
_DirectiveRegistryImpl._internal(Context context, PrintWriter writer)
: _writer = writer,
_context = context,
_annotationsVisitor = new _AnnotationsTransformVisitor(writer, context),
_bindVisitor = new _BindTransformVisitor(writer, context),
_factoryVisitor = new _FactoryTransformVisitor(writer, context),
_parametersVisitor = new _ParameterTransformVisitor(writer, context);
factory _DirectiveRegistryImpl(Context context) {
return new _DirectiveRegistryImpl._internal(
context, new PrintStringWriter());
}
@override
String toString() {
return _seen.isEmpty ? '' : 'reflector${_writer};';
}
// Adds [entry] to the `registerType` calls which will be generated.
void register(AnnotationMatch entry) {
if (_seen.contains(entry.node)) return;
_seen.add(entry.node);
if (_context.generateCtorStubs) {
_generateCtorStubs(entry);
}
if (_context.generateSetterStubs) {
_generateSetterStubs(entry);
}
}
void _generateSetterStubs(AnnotationMatch entry) {
// TODO(kegluneq): Remove these requirements for setter stub generation.
if (entry.element is! ClassElement) {
logger.error('Directives can only be applied to classes.');
return;
}
if (entry.node is! ClassDeclaration) {
logger.error('Unsupported annotation type for ctor stub generation. '
'Only class declarations are supported as Directives.');
return;
}
entry.node.accept(_bindVisitor);
}
void _generateCtorStubs(AnnotationMatch entry) {
var element = entry.element;
var annotation = entry.annotation;
// TODO(kegluneq): Remove these requirements for ctor stub generation.
if (annotation.element is! ConstructorElement) {
logger.error('Unsupported annotation type for ctor stub generation. '
'Only constructors are supported as Directives.');
return;
}
if (element is! ClassElement) {
logger.error('Directives can only be applied to classes.');
return;
}
if (entry.node is! ClassDeclaration) {
logger.error('Unsupported annotation type for ctor stub generation. '
'Only class declarations are supported as Directives.');
return;
}
var ctor = element.unnamedConstructor;
if (ctor == null) {
logger.error('No unnamed constructor found for ${element.name}');
return;
}
var ctorNode = ctor.node;
_writer.print('..registerType(');
_codegenClassTypeString(element);
_writer.print(', {"factory": ');
_codegenFactoryProp(ctorNode, element);
_writer.print(', "parameters": ');
_codegenParametersProp(ctorNode);
_writer.print(', "annotations": ');
_codegenAnnotationsProp(entry.node);
_writer.print('})');
}
void _codegenClassTypeString(ClassElement el) {
_writer.print('${_context._getPrefixDot(el.library)}${el.name}');
}
/// Creates the 'annotations' property for the Angular2 [registerType] call
/// for [node].
void _codegenAnnotationsProp(ClassDeclaration node) {
node.accept(_annotationsVisitor);
}
/// Creates the 'factory' property for the Angular2 [registerType] call
/// for [node]. [element] is necessary if [node] is null.
void _codegenFactoryProp(ConstructorDeclaration node, ClassElement element) {
if (node == null) {
// This occurs when the class does not declare a constructor.
var prefix = _context._getPrefixDot(element.library);
_writer.print('() => new ${prefix}${element.displayName}()');
} else {
node.accept(_factoryVisitor);
}
}
/// Creates the 'parameters' property for the Angular2 [registerType] call
/// for [node].
void _codegenParametersProp(ConstructorDeclaration node) {
if (node == null) {
// This occurs when the class does not declare a constructor.
_writer.print('const [const []]');
} else {
node.accept(_parametersVisitor);
}
}
}
/// Visitor providing common methods for concrete implementations.
class _TransformVisitorMixin {
final Context context;
final PrintWriter writer;
/// Safely visit [node].
void _visitNode(AstNode node) {
if (node != null) {
node.accept(this);
}
}
/// If [node] is null does nothing. Otherwise, prints [prefix], then
/// visits [node].
void _visitNodeWithPrefix(String prefix, AstNode node) {
if (node != null) {
writer.print(prefix);
node.accept(this);
}
}
/// If [node] is null does nothing. Otherwise, visits [node], then prints
/// [suffix].
void _visitNodeWithSuffix(AstNode node, String suffix) {
if (node != null) {
node.accept(this);
writer.print(suffix);
}
}
String prefixedSimpleIdentifier(SimpleIdentifier node) {
// Make sure the identifier is prefixed if necessary.
if (node.bestElement is ClassElementImpl ||
node.bestElement is PropertyAccessorElement) {
return context._getPrefixDot(node.bestElement.library) +
node.token.lexeme;
} else {
return node.token.lexeme;
}
}
}
class _TransformVisitor extends ToSourceVisitor with _TransformVisitorMixin {
final Context context;
final PrintWriter writer;
_TransformVisitor(PrintWriter writer, this.context)
: this.writer = writer,
super(writer);
@override
Object visitPrefixedIdentifier(PrefixedIdentifier node) {
// We add our own prefixes in [visitSimpleIdentifier], discard any used in
// the original source.
writer.print(super.prefixedSimpleIdentifier(node.identifier));
return null;
}
@override
Object visitSimpleIdentifier(SimpleIdentifier node) {
writer.print(super.prefixedSimpleIdentifier(node));
return null;
}
}
/// SourceVisitor designed to accept [ConstructorDeclaration] nodes.
class _CtorTransformVisitor extends _TransformVisitor {
bool _withParameterTypes = true;
bool _withParameterNames = true;
_CtorTransformVisitor(PrintWriter writer, Context _context)
: super(writer, _context);
/// If [_withParameterTypes] is true, this method outputs [node]'s type
/// (appropriately prefixed based on [_libraryPrefixes]. If
/// [_withParameterNames] is true, this method outputs [node]'s identifier.
Object _visitNormalFormalParameter(NormalFormalParameter node) {
if (_withParameterTypes) {
var paramType = node.element.type;
var prefix = context._getPrefixDot(paramType.element.library);
writer.print('${prefix}${paramType.displayName}');
if (_withParameterNames) {
_visitNodeWithPrefix(' ', node.identifier);
}
} else if (_withParameterNames) {
_visitNode(node.identifier);
}
return null;
}
@override
Object visitSimpleFormalParameter(SimpleFormalParameter node) {
return _visitNormalFormalParameter(node);
}
@override
Object visitFieldFormalParameter(FieldFormalParameter node) {
if (node.parameters != null) {
logger.error('Parameters in ctor not supported '
'(${super.visitFormalParameterList(node)}');
}
return _visitNormalFormalParameter(node);
}
@override
Object visitDefaultFormalParameter(DefaultFormalParameter node) {
_visitNode(node.parameter);
// Ignore the declared default value.
return null;
}
@override
/// Overridden to avoid outputting grouping operators for default parameters.
Object visitFormalParameterList(FormalParameterList node) {
writer.print('(');
NodeList<FormalParameter> parameters = node.parameters;
int size = parameters.length;
for (int i = 0; i < size; i++) {
if (i > 0) {
writer.print(', ');
}
parameters[i].accept(this);
}
writer.print(')');
return null;
}
}
/// ToSourceVisitor designed to print 'parameters' values for Angular2's
/// [registerType] calls.
class _ParameterTransformVisitor extends _CtorTransformVisitor {
_ParameterTransformVisitor(PrintWriter writer, Context _context)
: super(writer, _context);
@override
Object visitConstructorDeclaration(ConstructorDeclaration node) {
_withParameterNames = false;
_withParameterTypes = true;
writer.print('const [const [');
_visitNode(node.parameters);
writer.print(']]');
return null;
}
@override
Object visitFormalParameterList(FormalParameterList node) {
NodeList<FormalParameter> parameters = node.parameters;
int size = parameters.length;
for (int i = 0; i < size; i++) {
if (i > 0) {
writer.print(', ');
}
parameters[i].accept(this);
}
return null;
}
}
/// ToSourceVisitor designed to print 'factory' values for Angular2's
/// [registerType] calls.
class _FactoryTransformVisitor extends _CtorTransformVisitor {
_FactoryTransformVisitor(PrintWriter writer, Context _context)
: super(writer, _context);
@override
Object visitConstructorDeclaration(ConstructorDeclaration node) {
_withParameterNames = true;
_withParameterTypes = true;
_visitNode(node.parameters);
writer.print(' => new ');
_visitNode(node.returnType);
_visitNodeWithPrefix(".", node.name);
_withParameterTypes = false;
_visitNode(node.parameters);
return null;
}
}
/// ToSourceVisitor designed to print a [ClassDeclaration] node as a
/// 'annotations' value for Angular2's [registerType] calls.
class _AnnotationsTransformVisitor extends _TransformVisitor {
_AnnotationsTransformVisitor(PrintWriter writer, Context _context)
: super(writer, _context);
@override
Object visitClassDeclaration(ClassDeclaration node) {
writer.print('const [');
var size = node.metadata.length;
for (var i = 0; i < size; ++i) {
if (i > 0) {
writer.print(', ');
}
node.metadata[i].accept(this);
}
writer.print(']');
return null;
}
@override
Object visitAnnotation(Annotation node) {
writer.print('const ');
_visitNode(node.name);
// TODO(tjblasi): Do we need to handle named constructors for annotations?
// _visitNodeWithPrefix(".", node.constructorName);
_visitNode(node.arguments);
return null;
}
}
/// Visitor designed to print a [ClassDeclaration] node as a
/// `registerSetters` call for Angular2.
class _BindTransformVisitor extends Object
with SimpleAstVisitor<Object>, _TransformVisitorMixin {
final Context context;
final PrintWriter writer;
final List<String> _bindPieces = [];
SimpleIdentifier _currentName = null;
_BindTransformVisitor(this.writer, this.context);
@override
Object visitClassDeclaration(ClassDeclaration node) {
_currentName = node.name;
node.metadata.forEach((meta) => _visitNode(meta));
if (_bindPieces.isNotEmpty) {
writer.print('..registerSetters({${_bindPieces.join(', ')}})');
}
return null;
}
@override
Object visitAnnotation(Annotation node) {
// TODO(kegluneq): Remove this restriction.
if (node.element is ConstructorElement) {
if (node.element.returnType.element is ClassElement) {
// TODO(kegluneq): Check if this is actually a `directive`.
node.arguments.arguments.forEach((arg) => _visitNode(arg));
}
}
return null;
}
@override
Object visitNamedExpression(NamedExpression node) {
if (node.name.label.toString() == 'bind') {
// TODO(kegluneq): Remove this restriction.
if (node.expression is MapLiteral) {
node.expression.accept(this);
}
}
return null;
}
@override
Object visitMapLiteral(MapLiteral node) {
node.entries.forEach((entry) {
if (entry.key is SimpleStringLiteral) {
_visitNode(entry.key);
} else {
logger.error('`bind` currently only supports string literals');
}
});
return null;
}
@override
Object visitSimpleStringLiteral(SimpleStringLiteral node) {
if (_currentName == null) {
logger.error('Unexpected code path: `currentName` should never be null');
}
_bindPieces.add('"${node.value}": ('
'${super.prefixedSimpleIdentifier(_currentName)} o, String value) => '
'o.${node.value} = value');
return null;
}
}