Skip to content

Commit 52236bd

Browse files
Tim Blasitbosch
authored andcommitted
refactor(transformer): use the new compiler
Part of angular#3605
1 parent a562230 commit 52236bd

File tree

159 files changed

+2745
-2137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+2745
-2137
lines changed

modules_dart/transform/lib/src/transform/bind_generator/generator.dart

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class _ExtractQueryFieldsFromAnnotation extends Object
3434
}
3535
}
3636

37-
3837
class _ExtractQueryFieldsFromPropMetadata extends Object
3938
with RecursiveAstVisitor<Object> {
4039
final ConstantEvaluator _evaluator = new ConstantEvaluator();
@@ -52,7 +51,10 @@ class _ExtractQueryFieldsFromPropMetadata extends Object
5251
var res = false;
5352
list.elements.forEach((item) {
5453
var n = item.constructorName.toString();
55-
if(n == "ContentChild" || n == "ViewChild" || n == "ContentChildren" || n == "ViewChildren") {
54+
if (n == "ContentChild" ||
55+
n == "ViewChild" ||
56+
n == "ContentChildren" ||
57+
n == "ViewChildren") {
5658
res = true;
5759
}
5860
});
@@ -65,14 +67,12 @@ class _ExtractQueryFieldsFromPropMetadata extends Object
6567
}
6668
}
6769

68-
6970
Future<String> createNgSettersAndGetters(
7071
AssetReader reader, AssetId entryPoint) async {
7172
NgDeps ngDeps = await NgDeps.parse(reader, entryPoint);
7273

7374
String code = ngDeps.code;
7475
var setters = _generateSetters(_createPropertiesMap(ngDeps));
75-
var getters = _generateGetters(_createEventPropertiesList(ngDeps));
7676

7777
ngDeps.registeredTypes.forEach((t) {
7878
final fromAnnotation = new _ExtractQueryFieldsFromAnnotation();
@@ -86,16 +86,13 @@ Future<String> createNgSettersAndGetters(
8686
setters.addAll(_generateSetters(fromPropMetadata.asMap()));
8787
});
8888

89-
if (setters.isEmpty && getters.isEmpty) return code;
89+
if (setters.isEmpty) return code;
9090
var out = new StringBuffer();
9191
var codeInjectIdx = ngDeps.registeredTypes.last.registerMethod.end;
9292
out.write(code.substring(0, codeInjectIdx));
9393
if (setters.isNotEmpty) {
9494
out.write('..registerSetters({${setters.join(', ')}})');
9595
}
96-
if (getters.isNotEmpty) {
97-
out.write('..registerGetters({${getters.join(', ')}})');
98-
}
9996
out.write(code.substring(codeInjectIdx));
10097
return '$out';
10198
}
@@ -123,7 +120,7 @@ List<String> _generateSetters(Map<String, String> bindMap) {
123120
/// the bind properties and the values are either the one and only type
124121
/// binding to that property or the empty string.
125122
Map<String, String> _createPropertiesMap(NgDeps ngDeps) {
126-
var visitor = new ExtractNamedExpressionVisitor('inputs');
123+
var visitor = new ExtractNamedExpressionVisitor('properties');
127124
var bindMap = {};
128125
ngDeps.registeredTypes.forEach((RegisteredType t) {
129126
visitor.bindConfig.clear();
@@ -147,37 +144,3 @@ Map<String, String> _createPropertiesMap(NgDeps ngDeps) {
147144
});
148145
return bindMap;
149146
}
150-
151-
/// Consumes the list generated by {@link _createEventPropertiesList} to codegen
152-
/// getters.
153-
List<String> _generateGetters(List<String> eventProperties) {
154-
var getters = [];
155-
// TODO(kegluneq): Include types for receivers. See #886.
156-
for (var property in eventProperties) {
157-
if (!prop.isValid(property)) {
158-
// TODO(kegluenq): Eagerly throw here once #1295 is addressed.
159-
getters.add(prop.lazyInvalidGetter(property));
160-
} else {
161-
getters.add(''' '${prop.sanitize(property)}': (o) => o.$property''');
162-
}
163-
}
164-
return getters;
165-
}
166-
167-
/// Collapses all `events` in {@link ngDeps} into a list of corresponding
168-
/// property names.
169-
List<String> _createEventPropertiesList(NgDeps ngDeps) {
170-
var visitor = new ExtractNamedExpressionVisitor('outputs');
171-
var propertyNames = [];
172-
ngDeps.registeredTypes.forEach((RegisteredType t) {
173-
visitor.bindConfig.clear();
174-
t.annotations.accept(visitor);
175-
visitor.bindConfig.forEach((String config) {
176-
// See comments for `Directive` in annotations_impl/annotations.ts for
177-
// details on how `events` is specified. We are pulling out the property
178-
// name only (everything before the first `:`).
179-
propertyNames.add(config.split(':').first.trim());
180-
});
181-
});
182-
return propertyNames;
183-
}

modules_dart/transform/lib/src/transform/common/code/ng_deps_code.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,9 @@ class NgDepsVisitor extends RecursiveAstVisitor<Object> {
3838
}
3939

4040
void _createModel(String libraryUri) {
41-
_model = new NgDepsModel()..libraryUri = libraryUri;
42-
43-
// We need to import & export the original file.
44-
var origDartFile = path.basename(processedFile.path);
45-
_model.imports.add(new ImportModel()..uri = origDartFile);
46-
_model.exports.add(new ExportModel()..uri = origDartFile);
47-
48-
// Used to register reflective information.
49-
_model.imports.add(new ImportModel()
50-
..uri = REFLECTOR_IMPORT
51-
..prefix = REFLECTOR_PREFIX);
41+
_model = new NgDepsModel()
42+
..libraryUri = libraryUri
43+
..sourceFile = path.basename(processedFile.path);
5244
}
5345

5446
@override
@@ -139,6 +131,14 @@ abstract class NgDepsWriterMixin
139131
buffer.writeln('library ${model.libraryUri}${DEPS_EXTENSION};\n');
140132
}
141133

134+
// We need to import & export the source file.
135+
writeImportModel(new ImportModel()..uri = model.sourceFile);
136+
137+
// Used to register reflective information.
138+
writeImportModel(new ImportModel()
139+
..uri = REFLECTOR_IMPORT
140+
..prefix = REFLECTOR_PREFIX);
141+
142142
// We do not support `partUris`, so skip outputting them.
143143
for (var importModel in model.imports) {
144144
// Ignore deferred imports here so as to not load the deferred libraries
@@ -149,6 +149,7 @@ abstract class NgDepsWriterMixin
149149

150150
writeImportModel(importModel);
151151
}
152+
writeExportModel(new ExportModel()..uri = model.sourceFile);
152153
model.exports.forEach(writeExportModel);
153154

154155
buffer
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
library angular2.transform.common.code.reflection_info_code;
2+
3+
import 'package:angular2/src/compiler/source_module.dart';
4+
5+
import 'uri.dart';
6+
7+
/// Writes the full Dart code for the provided [SourceModule].
8+
///
9+
/// If `libraryName` is provided, the generated source will be generated with
10+
/// the approprate "library" directive.
11+
String writeSourceModule(SourceModule sourceModule, {String libraryName}) {
12+
if (sourceModule == null) return null;
13+
var buf = new StringBuffer();
14+
var sourceWithImports = sourceModule.getSourceWithImports();
15+
16+
if (libraryName != null && libraryName.isNotEmpty) {
17+
buf..writeln('library $libraryName;')..writeln();
18+
}
19+
sourceWithImports.imports.forEach((import) {
20+
// Format for importLine := [uri, prefix]
21+
if (import.length != 2) {
22+
throw new FormatException(
23+
'Unexpected import format! '
24+
'Angular 2 compiler returned imports in an unexpected format. '
25+
'Expected [<import_uri>, <prefix>].',
26+
import.join(', '));
27+
}
28+
buf.writeln(writeImportUri(import[0],
29+
prefix: import[1], fromAbsolute: sourceModule.moduleUrl));
30+
});
31+
buf..writeln()..writeln(sourceWithImports.source);
32+
33+
return buf.toString();
34+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
library angular2.transform.common.code.reflection_info_code;
2+
3+
import 'package:angular2/src/transform/common/url_resolver.dart';
4+
import 'package:path/path.dart' as path;
5+
6+
/// Generates an `import` statement for the file specified by `importPath`.
7+
///
8+
/// If `fromAbsolute` is specified, `importPath` may be a relative path,
9+
/// otherwise it is expected to be absolute.
10+
String writeImportUri(String importPath, {String prefix, String fromAbsolute}) {
11+
var codegenImportPath;
12+
13+
var resolver = const TransformerUrlResolver();
14+
var importUri = resolver.toAssetScheme(Uri.parse(importPath));
15+
if (_canPackageImport(importUri) ||
16+
fromAbsolute == null ||
17+
fromAbsolute.isEmpty) {
18+
codegenImportPath = _toPackageImport(importUri);
19+
} else {
20+
var moduleUri = resolver.toAssetScheme(Uri.parse(fromAbsolute));
21+
if (_canImportRelative(importUri, from: moduleUri)) {
22+
codegenImportPath = path.url.relative(importUri.toString(),
23+
from: path.dirname(moduleUri.toString()));
24+
} else {
25+
var errMsg;
26+
if (fromAbsolute == null || fromAbsolute.isEmpty) {
27+
errMsg = 'Can only import $importPath using a relative uri';
28+
} else {
29+
errMsg = 'Cannot import $importPath from $fromAbsolute';
30+
}
31+
throw new FormatException(errMsg, importPath);
32+
}
33+
}
34+
35+
if (prefix != null && prefix.isNotEmpty) {
36+
prefix = ' as $prefix';
37+
}
38+
return 'import \'$codegenImportPath\'$prefix;';
39+
}
40+
41+
// For a relative import, the scheme, first (package) and second (lib|test|web)
42+
// path segments must be equal.
43+
bool _canImportRelative(Uri importUri, {Uri from}) {
44+
if (importUri == null) throw new ArgumentError.notNull('importUri');
45+
if (from == null) throw new ArgumentError.notNull('from');
46+
assert(importUri.scheme == 'asset');
47+
assert(importUri.pathSegments.length >= 2);
48+
assert(from.scheme == 'asset');
49+
assert(from.pathSegments.length >= 2);
50+
return importUri.pathSegments.first == from.pathSegments.first &&
51+
importUri.pathSegments[1] == from.pathSegments[1];
52+
}
53+
54+
/// Pub's package scheme assumes that an asset lives under the lib/ directory,
55+
/// so an asset: Uri is package-importable if its second path segment is lib/.
56+
///
57+
/// For a file located at angular2/lib/src/file.dart:
58+
/// - Asset scheme => asset:angular2/lib/src/file.dart
59+
/// - Package scheme => package:angular2/src/file.dart
60+
bool _canPackageImport(Uri assetImport) {
61+
if (assetImport == null) throw new ArgumentError.notNull('assetImport');
62+
if (!assetImport.isAbsolute || assetImport.scheme != 'asset') {
63+
throw new ArgumentError.value(assetImport, 'assetImport',
64+
'Must be an absolute uri using the asset: scheme');
65+
}
66+
return assetImport.pathSegments.length >= 2 &&
67+
assetImport.pathSegments[1] == 'lib';
68+
}
69+
70+
String _toPackageImport(Uri assetImport) {
71+
assert(_canPackageImport(assetImport));
72+
var subPath = assetImport.pathSegments
73+
.getRange(2, assetImport.pathSegments.length)
74+
.join('/');
75+
return 'package:${assetImport.pathSegments.first}/$subPath';
76+
}

0 commit comments

Comments
 (0)