chore: Fixing review comments on Dart transformers
See https://codereview.chromium.org/927373004/ Closes #705
This commit is contained in:
parent
fb5b168b19
commit
4d56a1e1af
|
@ -1,3 +1,5 @@
|
|||
library angular2.src.transform;
|
||||
|
||||
import 'dart:collection' show Queue;
|
||||
import 'package:analyzer/src/generated/element.dart';
|
||||
|
||||
|
@ -5,7 +7,7 @@ import 'package:analyzer/src/generated/element.dart';
|
|||
/// [_annotationClass] and reporting the resulting (element, annotation) pairs.
|
||||
class AnnotationMatcher {
|
||||
/// Queue for annotations.
|
||||
final initQueue = new Queue<AnnotationMatch>();
|
||||
final matchQueue = new Queue<AnnotationMatch>();
|
||||
/// All the annotations we have seen for each element
|
||||
final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>();
|
||||
|
||||
|
@ -16,7 +18,7 @@ class AnnotationMatcher {
|
|||
|
||||
/// Records all [_annotationClass] annotations and the [element]s they apply to.
|
||||
/// Returns [true] if 1) [element] is annotated with [_annotationClass] and
|
||||
/// 2) ([element], [_annotationClass]) has been seen previously.
|
||||
/// 2) ([element], [_annotationClass]) has not been seen previously.
|
||||
bool processAnnotations(ClassElement element) {
|
||||
var found = false;
|
||||
element.metadata.where((ElementAnnotation meta) {
|
||||
|
@ -31,7 +33,7 @@ class AnnotationMatcher {
|
|||
.contains(meta);
|
||||
}).forEach((ElementAnnotation meta) {
|
||||
_seenAnnotations[element].add(meta);
|
||||
initQueue.addLast(new AnnotationMatch(element, meta));
|
||||
matchQueue.addLast(new AnnotationMatch(element, meta));
|
||||
found = true;
|
||||
});
|
||||
return found;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library angular2.transformer;
|
||||
library angular2.src.transform;
|
||||
|
||||
import 'package:analyzer/src/generated/ast.dart';
|
||||
import 'package:analyzer/src/generated/element.dart';
|
||||
|
@ -17,6 +17,8 @@ class Context {
|
|||
final Map<LibraryElement, String> _libraryPrefixes;
|
||||
|
||||
DirectiveRegistry _directiveRegistry;
|
||||
/// Generates [registerType] calls for all [register]ed [AnnotationMatch]
|
||||
/// objects.
|
||||
DirectiveRegistry get directiveRegistry => _directiveRegistry;
|
||||
|
||||
Context({TransformLogger logger})
|
||||
|
@ -26,11 +28,8 @@ class Context {
|
|||
}
|
||||
|
||||
void error(String errorString) {
|
||||
if (_logger != null) {
|
||||
if (_logger == null) throw new Error(errorString);
|
||||
_logger.error(errorString);
|
||||
} else {
|
||||
throw new Error(errorString);
|
||||
}
|
||||
}
|
||||
|
||||
/// If elements in [lib] should be prefixed in our generated code, returns
|
||||
|
@ -38,21 +37,22 @@ class Context {
|
|||
/// library will use the same prefix.
|
||||
/// If [lib] does not need a prefix, returns the empty string.
|
||||
String _getPrefixDot(LibraryElement lib) {
|
||||
var prefix = lib != null && !lib.isInSdk
|
||||
? _libraryPrefixes.putIfAbsent(lib, () => 'i${_libraryPrefixes.length}')
|
||||
: null;
|
||||
return prefix == null ? '' : '${prefix}.';
|
||||
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 _reflectorImport =
|
||||
'import \'package:angular2/src/reflection/reflection.dart\' '
|
||||
'show reflector;';
|
||||
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].
|
||||
|
@ -102,6 +102,8 @@ _codegenImport(Context context, AssetId libraryId, AssetId entryPoint) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO(https://github.com/kegluneq/angular/issues/4): Remove calls to
|
||||
// Element#node.
|
||||
class _DirectiveRegistryImpl implements DirectiveRegistry {
|
||||
final Context _context;
|
||||
final StringBuffer _buffer = new StringBuffer();
|
||||
|
@ -197,20 +199,15 @@ abstract class _TransformVisitor extends ToSourceVisitor {
|
|||
: this._writer = writer,
|
||||
super(writer);
|
||||
|
||||
/// Safely visit the given node.
|
||||
/// @param node the node to be visited
|
||||
/// Safely visit [node].
|
||||
void _visitNode(AstNode node) {
|
||||
if (node != null) {
|
||||
node.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely visit the given node, printing the prefix before the node if it is non-`null`.
|
||||
*
|
||||
* @param prefix the prefix to be printed if there is a node to visit
|
||||
* @param node the node to be visited
|
||||
*/
|
||||
/// If [node] is null does nothing. Otherwise, prints [prefix], then
|
||||
/// visits [node].
|
||||
void _visitNodeWithPrefix(String prefix, AstNode node) {
|
||||
if (node != null) {
|
||||
_writer.print(prefix);
|
||||
|
@ -218,12 +215,8 @@ abstract class _TransformVisitor extends ToSourceVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely visit the given node, printing the suffix after the node if it is non-`null`.
|
||||
*
|
||||
* @param suffix the suffix to be printed if there is a node to visit
|
||||
* @param node the node to be visited
|
||||
*/
|
||||
/// If [node] is null does nothing. Otherwise, visits [node], then prints
|
||||
/// [suffix].
|
||||
void _visitNodeWithSuffix(AstNode node, String suffix) {
|
||||
if (node != null) {
|
||||
node.accept(this);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library angular2.transformer;
|
||||
library angular2.src.transform;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:barback/barback.dart';
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library angular2.transformer;
|
||||
library angular2.src.transform;
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
library angular2.src.transform;
|
||||
|
||||
import 'package:code_transformers/resolver.dart';
|
||||
|
||||
Resolvers createResolvers() {
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
library angular2.transformer;
|
||||
library angular2.src.transform;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:barback/barback.dart';
|
||||
|
@ -60,9 +57,12 @@ class AngularTransformer extends Transformer {
|
|||
.error('New entry point file $newEntryPointId already exists.');
|
||||
} else {
|
||||
return _resolvers.get(transform).then((resolver) {
|
||||
try {
|
||||
new _BootstrapFileBuilder(resolver, transform,
|
||||
transform.primaryInput.id, newEntryPointId).run();
|
||||
} finally {
|
||||
resolver.release();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -93,7 +93,7 @@ class _BootstrapFileBuilder {
|
|||
new ImportTraversal(_directiveInfo).traverse(entryLib);
|
||||
|
||||
var context = new codegen.Context(logger: _transform.logger);
|
||||
_directiveInfo.initQueue
|
||||
_directiveInfo.matchQueue
|
||||
.forEach((entry) => context.directiveRegistry.register(entry));
|
||||
|
||||
_transform.addOutput(new Asset.fromString(_newEntryPoint, codegen
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
library angular2.src.transform;
|
||||
|
||||
import 'package:analyzer/src/generated/element.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
library initialize.test.build.common;
|
||||
library angular2.test.transform;
|
||||
|
||||
// TODO(kegluneq): Remove this and use the actual Directive def'n.
|
||||
// Simple mock of Directive.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
library angular2.test;
|
||||
library angular2.test.transform;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:barback/barback.dart';
|
||||
|
|
Loading…
Reference in New Issue