Chromium Code Reviews| Index: lib/src/codegen/js_names.dart |
| diff --git a/lib/src/codegen/js_names.dart b/lib/src/codegen/js_names.dart |
| index 00ee437a6231bef9120e9a0ab4c880c9ac3e9e93..1a4a868b2703213f6dc70d21e32df8b7609d6b71 100644 |
| --- a/lib/src/codegen/js_names.dart |
| +++ b/lib/src/codegen/js_names.dart |
| @@ -14,7 +14,41 @@ import 'package:dev_compiler/src/js/js_ast.dart'; |
| /// |
| // TODO(jmesserly): move into js_ast? add a boolean to Identifier? |
| class TemporaryId extends Identifier { |
| + static int __id = 0; |
|
Jennifer Messerly
2015/05/12 16:23:46
this helped for debugging, I could remove it thoug
|
| + final int _id = ++__id; |
| TemporaryId(String name) : super(name); |
| + toString() => super.toString() + ' $_id'; |
| +} |
| + |
| +/// Creates a qualified identifier, without determining for sure if it needs to |
| +/// be qualified until [setQualified] is called. |
| +/// |
| +/// This expression is transparent to visiting after [setQualified]. |
| +class MaybeQualifiedId extends Expression { |
| + Expression _expr; |
| + |
| + final Identifier qualifier; |
| + final Expression name; |
| + |
| + MaybeQualifiedId(this.qualifier, this.name) { |
| + _expr = new PropertyAccess(qualifier, name); |
| + } |
| + |
| + /// Helper to create an [Identifier] from something that starts as a property. |
| + static identifier(LiteralString propertyName) => |
| + new Identifier(propertyName.valueWithoutQuotes); |
| + |
| + void setQualified(bool qualified) { |
| + if (!qualified && name is LiteralString) { |
| + _expr = identifier(name); |
| + } |
| + } |
| + |
| + int get precedenceLevel => _expr.precedenceLevel; |
| + |
| + accept(NodeVisitor visitor) => _expr.accept(visitor); |
| + |
| + void visitChildren(NodeVisitor visitor) => _expr.visitChildren(visitor); |
| } |
| /// This class has two purposes: |
| @@ -23,7 +57,7 @@ class TemporaryId extends Identifier { |
| /// * rename temporary variables to avoid colliding with user-specified names, |
| /// or other temporaries |
| /// |
| -/// Each instance of [JSTemporary] is treated as a unique variable, with its |
| +/// Each instance of [TemporaryId] is treated as a unique variable, with its |
| /// `name` field simply the suggestion of what name to use. By contrast |
| /// [Identifiers] are never renamed unless they are an invalid identifier, like |
| /// `function` or `instanceof`, and their `name` field controls whether they |
| @@ -70,8 +104,8 @@ class _FunctionScope { |
| /// Collects all names used in the visited tree. |
| class _RenameVisitor extends VariableDeclarationVisitor { |
| - final pendingRenames = new Map<Object, Set<_FunctionScope>>(); |
| - final renames = new HashMap<Object, String>(); |
| + final pendingRenames = new Map<Object, Set<_FunctionScope>>.identity(); |
| + final renames = new HashMap<Object, String>.identity(); |
| final _FunctionScope rootScope = new _FunctionScope(null); |
| _FunctionScope scope; |
| @@ -115,7 +149,6 @@ class _RenameVisitor extends VariableDeclarationVisitor { |
| if (needsRename(node)) { |
| usedIn = pendingRenames.putIfAbsent(id, () => new HashSet()); |
| } |
| - |
| for (var s = scope, end = declScope.parent; s != end; s = s.parent) { |
| if (usedIn != null) { |
| usedIn.add(s); |
| @@ -184,7 +217,7 @@ class _RenameVisitor extends VariableDeclarationVisitor { |
| bool needsRename(Identifier node) => |
| node is TemporaryId || node.allowRename && invalidVariableName(node.name); |
| -Object /*String|JSTemporary*/ identifierKey(Identifier node) => |
| +Object /*String|TemporaryId*/ identifierKey(Identifier node) => |
| node is TemporaryId ? node : node.name; |
| /// Returns true for invalid JS variable names, such as keywords. |