| 
 | 1 | +library angular2.src.transform;  | 
 | 2 | + | 
 | 3 | +import 'package:analyzer/src/generated/ast.dart';  | 
 | 4 | +import 'package:analyzer/src/generated/element.dart';  | 
 | 5 | +import 'package:analyzer/src/generated/java_core.dart';  | 
 | 6 | +import 'package:barback/barback.dart';  | 
 | 7 | +import 'package:code_transformers/resolver.dart';  | 
 | 8 | +import 'package:dart_style/dart_style.dart';  | 
 | 9 | +import 'package:path/path.dart' as path;  | 
 | 10 | + | 
 | 11 | +import 'codegen.dart';  | 
 | 12 | +import 'logging.dart';  | 
 | 13 | +import 'resolvers.dart';  | 
 | 14 | + | 
 | 15 | +/// Finds all calls to the Angular2 [ReflectionCapabilities] constructor  | 
 | 16 | +/// defined in [library].  | 
 | 17 | +/// This only searches the code defined in the file  | 
 | 18 | +// represented by [library], not `part`s, `import`s, `export`s, etc.  | 
 | 19 | +String findReflectionCapabilities(  | 
 | 20 | + Resolver resolver, AssetId reflectionEntryPoint, AssetId newEntryPoint) {  | 
 | 21 | + var types = new Angular2Types(resolver);  | 
 | 22 | + if (types.reflectionCapabilities == null) {  | 
 | 23 | + throw new ArgumentError(  | 
 | 24 | + 'Could not find class for ${reflectionCapabilitiesTypeName}.');  | 
 | 25 | + }  | 
 | 26 | + | 
 | 27 | + var codegen = new _SetupReflectionCodegen(  | 
 | 28 | + resolver, reflectionEntryPoint, newEntryPoint);  | 
 | 29 | + | 
 | 30 | + var writer = new PrintStringWriter();  | 
 | 31 | + var visitor = new _RewriteReflectionEntryPointVisitor(  | 
 | 32 | + writer, types.reflectionCapabilities, codegen);  | 
 | 33 | + | 
 | 34 | + // TODO(kegluneq): Determine how to get nodes without querying Element#node.  | 
 | 35 | + // Root of file defining that library (main part).  | 
 | 36 | + resolver.getLibrary(reflectionEntryPoint).definingCompilationUnit.node  | 
 | 37 | + .accept(visitor);  | 
 | 38 | + | 
 | 39 | + return new DartFormatter().format(writer.toString());  | 
 | 40 | +}  | 
 | 41 | + | 
 | 42 | +class _SetupReflectionCodegen {  | 
 | 43 | + static const _prefixBase = 'ngStaticInit';  | 
 | 44 | + | 
 | 45 | + final String prefix;  | 
 | 46 | + final String importUri;  | 
 | 47 | + | 
 | 48 | + _SetupReflectionCodegen._internal(this.prefix, this.importUri);  | 
 | 49 | + | 
 | 50 | + factory _SetupReflectionCodegen(  | 
 | 51 | + Resolver resolver, AssetId reflectionEntryPoint, AssetId newEntryPoint) {  | 
 | 52 | + var lib = resolver.getLibrary(reflectionEntryPoint);  | 
 | 53 | + var prefix = _prefixBase;  | 
 | 54 | + var idx = 0;  | 
 | 55 | + while (lib.imports.any((import) {  | 
 | 56 | + return import.prefix != null && import.prefix == prefix;  | 
 | 57 | + })) {  | 
 | 58 | + prefix = '${_prefixBase}${idx++}';  | 
 | 59 | + }  | 
 | 60 | + | 
 | 61 | + var importPath = path.relative(newEntryPoint.path,  | 
 | 62 | + from: path.dirname(reflectionEntryPoint.path));  | 
 | 63 | + return new _SetupReflectionCodegen._internal(prefix, importPath);  | 
 | 64 | + }  | 
 | 65 | + | 
 | 66 | + /// Generates code to import the library containing the method which sets up  | 
 | 67 | + /// Angular2 reflection statically.  | 
 | 68 | + ///  | 
 | 69 | + /// The code generated here should follow the example of code generated for  | 
 | 70 | + /// an [ImportDirective] node.  | 
 | 71 | + String codegenImport() {  | 
 | 72 | + return 'import \'${importUri}\' as ${prefix};';  | 
 | 73 | + }  | 
 | 74 | + | 
 | 75 | + /// Generates code to call the method which sets up Angular2 reflection  | 
 | 76 | + /// statically.  | 
 | 77 | + ///  | 
 | 78 | + /// The code generated here should follow the example of code generated for  | 
 | 79 | + /// a [MethodInvocation] node, that is, it should be prefixed as necessary  | 
 | 80 | + /// and not be followed by a ';'.  | 
 | 81 | + String codegenSetupReflectionCall() {  | 
 | 82 | + return '${prefix}.${setupReflectionMethodName}()';  | 
 | 83 | + }  | 
 | 84 | +}  | 
 | 85 | + | 
 | 86 | +class _RewriteReflectionEntryPointVisitor extends ToSourceVisitor {  | 
 | 87 | + final PrintWriter _writer;  | 
 | 88 | + final ClassElement _forbiddenClass;  | 
 | 89 | + final _SetupReflectionCodegen _codegen;  | 
 | 90 | + | 
 | 91 | + _RewriteReflectionEntryPointVisitor(  | 
 | 92 | + PrintWriter writer, this._forbiddenClass, this._codegen)  | 
 | 93 | + : _writer = writer,  | 
 | 94 | + super(writer);  | 
 | 95 | + | 
 | 96 | + bool _isNewReflectionCapabilities(InstanceCreationExpression node) {  | 
 | 97 | + var typeElement = node.constructorName.type.name.bestElement;  | 
 | 98 | + return typeElement != null && typeElement == _forbiddenClass;  | 
 | 99 | + }  | 
 | 100 | + | 
 | 101 | + bool _isReflectionCapabilitiesImport(ImportDirective node) {  | 
 | 102 | + return node.uriElement == _forbiddenClass.library;  | 
 | 103 | + }  | 
 | 104 | + | 
 | 105 | + @override  | 
 | 106 | + Object visitImportDirective(ImportDirective node) {  | 
 | 107 | + if (_isReflectionCapabilitiesImport(node)) {  | 
 | 108 | + // TODO(kegluneq): Remove newlines once dart_style bug is fixed.  | 
 | 109 | + // https://github.com/dart-lang/dart_style/issues/178  | 
 | 110 | + // _writer.print('\n/* ReflectionCapabilities import removed */\n');  | 
 | 111 | + _writer.print(_codegen.codegenImport());  | 
 | 112 | + // TODO(kegluneq): Remove once we generate all needed code.  | 
 | 113 | + {  | 
 | 114 | + super.visitImportDirective(node);  | 
 | 115 | + }  | 
 | 116 | + return null;  | 
 | 117 | + }  | 
 | 118 | + return super.visitImportDirective(node);  | 
 | 119 | + }  | 
 | 120 | + | 
 | 121 | + @override  | 
 | 122 | + Object visitAssignmentExpression(AssignmentExpression node) {  | 
 | 123 | + if (node.rightHandSide is InstanceCreationExpression &&  | 
 | 124 | + _isNewReflectionCapabilities(node.rightHandSide)) {  | 
 | 125 | + // TODO(kegluneq): Remove newlines once dart_style bug is fixed.  | 
 | 126 | + // https://github.com/dart-lang/dart_style/issues/178  | 
 | 127 | + // _writer.print('/* Creation of ReflectionCapabilities removed */\n');  | 
 | 128 | + _writer.print(_codegen.codegenSetupReflectionCall());  | 
 | 129 | + | 
 | 130 | + // TODO(kegluneq): Remove once we generate all needed code.  | 
 | 131 | + {  | 
 | 132 | + _writer.print(';');  | 
 | 133 | + node.leftHandSide.accept(this);  | 
 | 134 | + _writer.print(' ${node.operator.lexeme} ');  | 
 | 135 | + super.visitInstanceCreationExpression(node.rightHandSide);  | 
 | 136 | + }  | 
 | 137 | + return null;  | 
 | 138 | + }  | 
 | 139 | + return super.visitAssignmentExpression(node);  | 
 | 140 | + }  | 
 | 141 | + | 
 | 142 | + @override  | 
 | 143 | + Object visitInstanceCreationExpression(InstanceCreationExpression node) {  | 
 | 144 | + if (_isNewReflectionCapabilities(node)) {  | 
 | 145 | + logger.error('Unexpected format in creation of '  | 
 | 146 | + '${reflectionCapabilitiesTypeName}');  | 
 | 147 | + } else {  | 
 | 148 | + return super.visitInstanceCreationExpression(node);  | 
 | 149 | + }  | 
 | 150 | + return null;  | 
 | 151 | + }  | 
 | 152 | +}  | 
0 commit comments