| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:collection'; | 5 import 'dart:collection'; |
| 6 | 6 |
| 7 import '../js_ast/js_ast.dart'; | 7 import '../js_ast/js_ast.dart'; |
| 8 | 8 |
| 9 /// The ES6 name for the Dart SDK. All dart:* libraries are in this module. | 9 /// The ES6 name for the Dart SDK. All dart:* libraries are in this module. |
| 10 const String dartSdkModule = 'dart_sdk'; | 10 const String dartSdkModule = 'dart_sdk'; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 void _finishFunctions() { | 171 void _finishFunctions() { |
| 172 scope.functions.forEach((FunctionExpression f, _FunctionScope s) { | 172 scope.functions.forEach((FunctionExpression f, _FunctionScope s) { |
| 173 scope = s; | 173 scope = s; |
| 174 super.visitFunctionExpression(f); | 174 super.visitFunctionExpression(f); |
| 175 _finishFunctions(); | 175 _finishFunctions(); |
| 176 scope = scope.parent; | 176 scope = scope.parent; |
| 177 }); | 177 }); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void _finishNames() { | 180 void _finishNames() { |
| 181 var allNames = new Set<String>(); | |
| 182 pendingRenames.forEach((id, scopes) { | 181 pendingRenames.forEach((id, scopes) { |
| 183 allNames.clear(); | 182 var name = _findName(id, scopes); |
| 184 for (var s in scopes) allNames.addAll(s.used); | |
| 185 | |
| 186 var name = _findName(id, allNames); | |
| 187 for (var s in scopes) { | 183 for (var s in scopes) { |
| 188 s.used.add(name); | 184 s.used.add(name); |
| 189 s.renames[id] = name; | 185 s.renames[id] = name; |
| 190 } | 186 } |
| 191 }); | 187 }); |
| 192 } | 188 } |
| 193 | 189 |
| 194 static String _findName(Object id, Set<String> usedNames) { | 190 static String _findName(Object id, Set<_FunctionScope> scopes) { |
| 195 String name; | 191 String name; |
| 196 bool valid; | 192 bool valid; |
| 197 if (id is TemporaryId) { | 193 if (id is TemporaryId) { |
| 198 name = id.name; | 194 name = id.name; |
| 199 valid = !invalidVariableName(name); | 195 valid = !invalidVariableName(name); |
| 200 } else { | 196 } else { |
| 201 name = id; | 197 name = id; |
| 202 valid = false; | 198 valid = false; |
| 203 } | 199 } |
| 204 | 200 |
| 205 // Try to use the temp's name, otherwise rename. | 201 // Try to use the temp's name, otherwise rename. |
| 206 String candidate; | 202 String candidate; |
| 207 if (valid && !usedNames.contains(name)) { | 203 if (valid && !scopes.any((scope) => scope.used.contains(name))) { |
| 208 candidate = name; | 204 candidate = name; |
| 209 } else { | 205 } else { |
| 210 // This assumes that collisions are rare, hence linear search. | 206 // This assumes that collisions are rare, hence linear search. |
| 211 // If collisions become common we need a better search. | 207 // If collisions become common we need a better search. |
| 212 // TODO(jmesserly): what's the most readable scheme here? Maybe 1-letter | 208 // TODO(jmesserly): what's the most readable scheme here? Maybe 1-letter |
| 213 // names in some cases? | 209 // names in some cases? |
| 214 candidate = name == 'function' ? 'func' : '${name}\$'; | 210 candidate = name == 'function' ? 'func' : '${name}\$'; |
| 215 for (int i = 0; usedNames.contains(candidate); i++) { | 211 for (int i = 0; |
| 212 scopes.any((scope) => scope.used.contains(candidate)); |
| 213 i++) { |
| 216 candidate = '${name}\$$i'; | 214 candidate = '${name}\$$i'; |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 return candidate; | 217 return candidate; |
| 220 } | 218 } |
| 221 } | 219 } |
| 222 | 220 |
| 223 bool needsRename(Identifier node) => | 221 bool needsRename(Identifier node) => |
| 224 node is TemporaryId || node.allowRename && invalidVariableName(node.name); | 222 node is TemporaryId || node.allowRename && invalidVariableName(node.name); |
| 225 | 223 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 bool invalidStaticFieldName(String name) { | 289 bool invalidStaticFieldName(String name) { |
| 292 switch (name) { | 290 switch (name) { |
| 293 case "arguments": | 291 case "arguments": |
| 294 case "caller": | 292 case "caller": |
| 295 case "callee": | 293 case "callee": |
| 296 case "name": | 294 case "name": |
| 297 return true; | 295 return true; |
| 298 } | 296 } |
| 299 return false; | 297 return false; |
| 300 } | 298 } |
| OLD | NEW |