chore: Fixing review comments on Dart transformers

See https://codereview.chromium.org/927373004/

Closes #705
This commit is contained in:
Tim Blasi 2015-02-18 12:51:12 -08:00 committed by Misko Hevery
parent fb5b168b19
commit 4d56a1e1af
9 changed files with 41 additions and 42 deletions

View File

@ -1,3 +1,5 @@
library angular2.src.transform;
import 'dart:collection' show Queue; import 'dart:collection' show Queue;
import 'package:analyzer/src/generated/element.dart'; 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. /// [_annotationClass] and reporting the resulting (element, annotation) pairs.
class AnnotationMatcher { class AnnotationMatcher {
/// Queue for annotations. /// Queue for annotations.
final initQueue = new Queue<AnnotationMatch>(); final matchQueue = new Queue<AnnotationMatch>();
/// All the annotations we have seen for each element /// All the annotations we have seen for each element
final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>(); final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>();
@ -16,7 +18,7 @@ class AnnotationMatcher {
/// Records all [_annotationClass] annotations and the [element]s they apply to. /// Records all [_annotationClass] annotations and the [element]s they apply to.
/// Returns [true] if 1) [element] is annotated with [_annotationClass] and /// 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) { bool processAnnotations(ClassElement element) {
var found = false; var found = false;
element.metadata.where((ElementAnnotation meta) { element.metadata.where((ElementAnnotation meta) {
@ -31,7 +33,7 @@ class AnnotationMatcher {
.contains(meta); .contains(meta);
}).forEach((ElementAnnotation meta) { }).forEach((ElementAnnotation meta) {
_seenAnnotations[element].add(meta); _seenAnnotations[element].add(meta);
initQueue.addLast(new AnnotationMatch(element, meta)); matchQueue.addLast(new AnnotationMatch(element, meta));
found = true; found = true;
}); });
return found; return found;

View File

@ -1,4 +1,4 @@
library angular2.transformer; library angular2.src.transform;
import 'package:analyzer/src/generated/ast.dart'; import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart'; import 'package:analyzer/src/generated/element.dart';
@ -17,6 +17,8 @@ class Context {
final Map<LibraryElement, String> _libraryPrefixes; final Map<LibraryElement, String> _libraryPrefixes;
DirectiveRegistry _directiveRegistry; DirectiveRegistry _directiveRegistry;
/// Generates [registerType] calls for all [register]ed [AnnotationMatch]
/// objects.
DirectiveRegistry get directiveRegistry => _directiveRegistry; DirectiveRegistry get directiveRegistry => _directiveRegistry;
Context({TransformLogger logger}) Context({TransformLogger logger})
@ -26,11 +28,8 @@ class Context {
} }
void error(String errorString) { void error(String errorString) {
if (_logger != null) { if (_logger == null) throw new Error(errorString);
_logger.error(errorString); _logger.error(errorString);
} else {
throw new Error(errorString);
}
} }
/// If elements in [lib] should be prefixed in our generated code, returns /// If elements in [lib] should be prefixed in our generated code, returns
@ -38,21 +37,22 @@ class Context {
/// library will use the same prefix. /// library will use the same prefix.
/// If [lib] does not need a prefix, returns the empty string. /// If [lib] does not need a prefix, returns the empty string.
String _getPrefixDot(LibraryElement lib) { String _getPrefixDot(LibraryElement lib) {
var prefix = lib != null && !lib.isInSdk if (lib == null || lib.isInSdk) return '';
? _libraryPrefixes.putIfAbsent(lib, () => 'i${_libraryPrefixes.length}') var prefix =
: null; _libraryPrefixes.putIfAbsent(lib, () => 'i${_libraryPrefixes.length}');
return prefix == null ? '' : '${prefix}.'; return '${prefix}.';
} }
} }
/// Object which [register]s [AnnotationMatch] objects for code generation.
abstract class DirectiveRegistry { abstract class DirectiveRegistry {
// Adds [entry] to the `registerType` calls which will be generated. // Adds [entry] to the `registerType` calls which will be generated.
void register(AnnotationMatch entry); void register(AnnotationMatch entry);
} }
const _reflectorImport = const _reflectorImport = '''
'import \'package:angular2/src/reflection/reflection.dart\' ' import 'package:angular2/src/reflection/reflection.dart' show reflector;
'show reflector;'; ''';
/// Default implementation to map from [LibraryElement] to [AssetId]. This /// Default implementation to map from [LibraryElement] to [AssetId]. This
/// assumes that [el.source] has a getter called [assetId]. /// 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 { class _DirectiveRegistryImpl implements DirectiveRegistry {
final Context _context; final Context _context;
final StringBuffer _buffer = new StringBuffer(); final StringBuffer _buffer = new StringBuffer();
@ -197,20 +199,15 @@ abstract class _TransformVisitor extends ToSourceVisitor {
: this._writer = writer, : this._writer = writer,
super(writer); super(writer);
/// Safely visit the given node. /// Safely visit [node].
/// @param node the node to be visited
void _visitNode(AstNode node) { void _visitNode(AstNode node) {
if (node != null) { if (node != null) {
node.accept(this); node.accept(this);
} }
} }
/** /// If [node] is null does nothing. Otherwise, prints [prefix], then
* Safely visit the given node, printing the prefix before the node if it is non-`null`. /// visits [node].
*
* @param prefix the prefix to be printed if there is a node to visit
* @param node the node to be visited
*/
void _visitNodeWithPrefix(String prefix, AstNode node) { void _visitNodeWithPrefix(String prefix, AstNode node) {
if (node != null) { if (node != null) {
_writer.print(prefix); _writer.print(prefix);
@ -218,12 +215,8 @@ abstract class _TransformVisitor extends ToSourceVisitor {
} }
} }
/** /// If [node] is null does nothing. Otherwise, visits [node], then prints
* Safely visit the given node, printing the suffix after the node if it is non-`null`. /// [suffix].
*
* @param suffix the suffix to be printed if there is a node to visit
* @param node the node to be visited
*/
void _visitNodeWithSuffix(AstNode node, String suffix) { void _visitNodeWithSuffix(AstNode node, String suffix) {
if (node != null) { if (node != null) {
node.accept(this); node.accept(this);

View File

@ -1,4 +1,4 @@
library angular2.transformer; library angular2.src.transform;
import 'dart:async'; import 'dart:async';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';

View File

@ -1,4 +1,4 @@
library angular2.transformer; library angular2.src.transform;
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;

View File

@ -1,3 +1,5 @@
library angular2.src.transform;
import 'package:code_transformers/resolver.dart'; import 'package:code_transformers/resolver.dart';
Resolvers createResolvers() { Resolvers createResolvers() {

View File

@ -1,7 +1,4 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file library angular2.src.transform;
// 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;
import 'dart:async'; import 'dart:async';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
@ -60,9 +57,12 @@ class AngularTransformer extends Transformer {
.error('New entry point file $newEntryPointId already exists.'); .error('New entry point file $newEntryPointId already exists.');
} else { } else {
return _resolvers.get(transform).then((resolver) { return _resolvers.get(transform).then((resolver) {
new _BootstrapFileBuilder(resolver, transform, try {
transform.primaryInput.id, newEntryPointId).run(); new _BootstrapFileBuilder(resolver, transform,
resolver.release(); transform.primaryInput.id, newEntryPointId).run();
} finally {
resolver.release();
}
}); });
} }
}); });
@ -93,7 +93,7 @@ class _BootstrapFileBuilder {
new ImportTraversal(_directiveInfo).traverse(entryLib); new ImportTraversal(_directiveInfo).traverse(entryLib);
var context = new codegen.Context(logger: _transform.logger); var context = new codegen.Context(logger: _transform.logger);
_directiveInfo.initQueue _directiveInfo.matchQueue
.forEach((entry) => context.directiveRegistry.register(entry)); .forEach((entry) => context.directiveRegistry.register(entry));
_transform.addOutput(new Asset.fromString(_newEntryPoint, codegen _transform.addOutput(new Asset.fromString(_newEntryPoint, codegen

View File

@ -1,3 +1,5 @@
library angular2.src.transform;
import 'package:analyzer/src/generated/element.dart'; import 'package:analyzer/src/generated/element.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;

View File

@ -1,7 +1,7 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file // 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 // 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. // 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. // TODO(kegluneq): Remove this and use the actual Directive def'n.
// Simple mock of Directive. // Simple mock of Directive.

View File

@ -1,4 +1,4 @@
library angular2.test; library angular2.test.transform;
import 'dart:io'; import 'dart:io';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';