Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: lib/src/compiler/module_builder.dart

Issue 1979013003: fix #569, cache constants defined in methods (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: merged Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/compiler/element_helpers.dart ('k') | test/browser/language_tests.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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]');
OLDNEW
« no previous file with comments | « lib/src/compiler/element_helpers.dart ('k') | test/browser/language_tests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698