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 library dev_compiler.src.codegen.js_names; | 5 library dev_compiler.src.codegen.js_names; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'package:dev_compiler/src/js/js_ast.dart'; | 8 import 'package:dev_compiler/src/js/js_ast.dart'; |
9 | 9 |
10 /// Unique instance for temporary variables. Will be renamed consistently | 10 /// Unique instance for temporary variables. Will be renamed consistently |
11 /// across the entire file. Different instances will be named differently | 11 /// across the entire file. Different instances will be named differently |
12 /// even if they have the same name, this makes it safe to use in code | 12 /// even if they have the same name, this makes it safe to use in code |
13 /// generation without needing global knowledge. See [JSNamer]. | 13 /// generation without needing global knowledge. See [JSNamer]. |
14 /// | 14 /// |
15 // TODO(jmesserly): move into js_ast? add a boolean to Identifier? | 15 // TODO(jmesserly): move into js_ast? add a boolean to Identifier? |
16 class JSTemporary extends Identifier { | 16 class TemporaryId extends Identifier { |
17 JSTemporary(String name) : super(name); | 17 TemporaryId(String name) : super(name); |
18 } | 18 } |
19 | 19 |
20 /// This class has two purposes: | 20 /// This class has two purposes: |
21 /// | 21 /// |
22 /// * rename JS identifiers to avoid keywords. | 22 /// * rename JS identifiers to avoid keywords. |
23 /// * rename temporary variables to avoid colliding with user-specified names, | 23 /// * rename temporary variables to avoid colliding with user-specified names, |
24 /// or other temporaries | 24 /// or other temporaries |
25 /// | 25 /// |
26 /// Each instance of [JSTemporary] is treated as a unique variable, with its | 26 /// Each instance of [JSTemporary] is treated as a unique variable, with its |
27 /// `name` field simply the suggestion of what name to use. By contrast | 27 /// `name` field simply the suggestion of what name to use. By contrast |
28 /// [Identifiers] are never renamed unless they are an invalid identifier, like | 28 /// [Identifiers] are never renamed unless they are an invalid identifier, like |
29 /// `function` or `instanceof`, and their `name` field controls whether they | 29 /// `function` or `instanceof`, and their `name` field controls whether they |
30 /// refer to the same variable. | 30 /// refer to the same variable. |
31 class JSNamer extends LocalNamer { | 31 class TemporaryNamer extends LocalNamer { |
32 final Map<Object, String> renames; | 32 final Map<Object, String> renames; |
33 | 33 |
34 JSNamer(Node node) : renames = new _RenameVisitor.build(node).renames; | 34 TemporaryNamer(Node node) : renames = new _RenameVisitor.build(node).renames; |
35 | 35 |
36 String getName(Identifier node) { | 36 String getName(Identifier node) { |
37 var rename = renames[identifierKey(node)]; | 37 var rename = renames[identifierKey(node)]; |
38 if (rename != null) return rename; | 38 if (rename != null) return rename; |
39 | 39 |
40 assert(!needsRename(node)); | 40 assert(!needsRename(node)); |
41 return node.name; | 41 return node.name; |
42 } | 42 } |
43 | 43 |
44 void enterScope(FunctionExpression node) {} | 44 void enterScope(FunctionExpression node) {} |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 var name = _findName(id, allNames); | 145 var name = _findName(id, allNames); |
146 renames[id] = name; | 146 renames[id] = name; |
147 | 147 |
148 for (var s in scopes) s.used.add(name); | 148 for (var s in scopes) s.used.add(name); |
149 }); | 149 }); |
150 } | 150 } |
151 | 151 |
152 static String _findName(Object id, Set<String> usedNames) { | 152 static String _findName(Object id, Set<String> usedNames) { |
153 String name; | 153 String name; |
154 bool valid; | 154 bool valid; |
155 if (id is JSTemporary) { | 155 if (id is TemporaryId) { |
156 name = id.name; | 156 name = id.name; |
157 valid = !invalidJSVariableName(name); | 157 valid = !invalidVariableName(name); |
158 } else { | 158 } else { |
159 name = id; | 159 name = id; |
160 valid = false; | 160 valid = false; |
161 } | 161 } |
162 | 162 |
163 // Try to use the temp's name, otherwise rename. | 163 // Try to use the temp's name, otherwise rename. |
164 String candidate; | 164 String candidate; |
165 if (valid && !usedNames.contains(name)) { | 165 if (valid && !usedNames.contains(name)) { |
166 candidate = name; | 166 candidate = name; |
167 } else { | 167 } else { |
168 // This assumes that collisions are rare, hence linear search. | 168 // This assumes that collisions are rare, hence linear search. |
169 // If collisions become common we need a better search. | 169 // If collisions become common we need a better search. |
170 // TODO(jmesserly): what's the most readable scheme here? Maybe 1-letter | 170 // TODO(jmesserly): what's the most readable scheme here? Maybe 1-letter |
171 // names in some cases? | 171 // names in some cases? |
172 candidate = name == 'function' ? 'func' : '${name}\$'; | 172 candidate = name == 'function' ? 'func' : '${name}\$'; |
173 for (int i = 0; usedNames.contains(candidate); i++) { | 173 for (int i = 0; usedNames.contains(candidate); i++) { |
174 candidate = '${name}\$$i'; | 174 candidate = '${name}\$$i'; |
175 } | 175 } |
176 } | 176 } |
177 return candidate; | 177 return candidate; |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 bool needsRename(Identifier node) => | 181 bool needsRename(Identifier node) => |
182 node is JSTemporary || node.allowRename && invalidJSVariableName(node.name); | 182 node is TemporaryId || node.allowRename && invalidVariableName(node.name); |
183 | 183 |
184 Object /*String|JSTemporary*/ identifierKey(Identifier node) => | 184 Object /*String|JSTemporary*/ identifierKey(Identifier node) => |
185 node is JSTemporary ? node : node.name; | 185 node is TemporaryId ? node : node.name; |
186 | 186 |
187 /// Returns true for invalid JS variable names, such as keywords. | 187 /// Returns true for invalid JS variable names, such as keywords. |
188 /// Also handles invalid variable names in strict mode, like "arguments". | 188 /// Also handles invalid variable names in strict mode, like "arguments". |
189 bool invalidJSVariableName(String keyword, {bool strictMode: true}) { | 189 bool invalidVariableName(String keyword, {bool strictMode: true}) { |
190 switch (keyword) { | 190 switch (keyword) { |
191 case "break": | 191 case "break": |
192 case "case": | 192 case "case": |
193 case "catch": | 193 case "catch": |
194 case "class": | 194 case "class": |
195 case "const": | 195 case "const": |
196 case "continue": | 196 case "continue": |
197 case "debugger": | 197 case "debugger": |
198 case "default": | 198 case "default": |
199 case "delete": | 199 case "delete": |
(...skipping 26 matching lines...) Expand all Loading... |
226 return true; | 226 return true; |
227 case "arguments": | 227 case "arguments": |
228 case "eval": | 228 case "eval": |
229 return strictMode; | 229 return strictMode; |
230 } | 230 } |
231 return false; | 231 return false; |
232 } | 232 } |
233 | 233 |
234 /// Returns true for invalid static method names in strict mode. | 234 /// Returns true for invalid static method names in strict mode. |
235 /// In particular, "caller" "callee" and "arguments" cannot be used. | 235 /// In particular, "caller" "callee" and "arguments" cannot be used. |
236 bool invalidJSStaticMethodName(String name) { | 236 bool invalidStaticMethodName(String name) { |
237 switch (name) { | 237 switch (name) { |
238 case "arguments": | 238 case "arguments": |
239 case "caller": | 239 case "caller": |
240 case "callee": | 240 case "callee": |
241 return true; | 241 return true; |
242 } | 242 } |
243 return false; | 243 return false; |
244 } | 244 } |
OLD | NEW |