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 'package:path/path.dart' as path; | 5 import 'package:path/path.dart' as path; |
6 | 6 |
7 import '../js_ast/js_ast.dart'; | 7 import '../js_ast/js_ast.dart'; |
8 import 'js_names.dart'; | 8 import 'js_names.dart'; |
9 | 9 |
10 /// Base class for compiling ES6 modules into various ES5 module patterns. | 10 /// Base class for compiling ES6 modules into various ES5 module patterns. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // This extra level of indirection should be unnecessary. | 143 // This extra level of indirection should be unnecessary. |
144 var block = | 144 var block = |
145 js.statement("(function() { 'use strict'; #; })()", [statements]); | 145 js.statement("(function() { 'use strict'; #; })()", [statements]); |
146 | 146 |
147 return new Program([block]); | 147 return new Program([block]); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 /// Escape [name] to make it into a valid identifier. | 151 /// Escape [name] to make it into a valid identifier. |
152 String pathToJSIdentifier(String name) { | 152 String pathToJSIdentifier(String name) { |
153 name = path.basenameWithoutExtension(name); | 153 return toJSIdentifier(path.basenameWithoutExtension(name)); |
| 154 } |
| 155 |
| 156 /// Escape [name] to make it into a valid identifier. |
| 157 String toJSIdentifier(String name) { |
154 if (name.length == 0) return r'$'; | 158 if (name.length == 0) return r'$'; |
155 | 159 |
156 // Escape any invalid characters | 160 // Escape any invalid characters |
157 StringBuffer buffer = null; | 161 StringBuffer buffer = null; |
158 for (int i = 0; i < name.length; i++) { | 162 for (int i = 0; i < name.length; i++) { |
159 var ch = name[i]; | 163 var ch = name[i]; |
160 var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch); | 164 var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch); |
161 if (needsEscape && buffer == null) { | 165 if (needsEscape && buffer == null) { |
162 buffer = new StringBuffer(name.substring(0, i)); | 166 buffer = new StringBuffer(name.substring(0, i)); |
163 } | 167 } |
164 if (buffer != null) { | 168 if (buffer != null) { |
165 buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch); | 169 buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch); |
166 } | 170 } |
167 } | 171 } |
168 | 172 |
169 var result = buffer != null ? '$buffer' : name; | 173 var result = buffer != null ? '$buffer' : name; |
170 // Ensure the identifier first character is not numeric and that the whole | 174 // Ensure the identifier first character is not numeric and that the whole |
171 // identifier is not a keyword. | 175 // identifier is not a keyword. |
172 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 176 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
173 return '\$$result'; | 177 return '\$$result'; |
174 } | 178 } |
175 return result; | 179 return result; |
176 } | 180 } |
177 | 181 |
178 // Invalid characters for identifiers, which would need to be escaped. | 182 // Invalid characters for identifiers, which would need to be escaped. |
179 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 183 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
OLD | NEW |