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

Unified Diff: lib/src/utils.dart

Issue 998043002: Fixes in codegen and test script (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: lib/src/utils.dart
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index a30473d5290f12642b1e68ccc6c3b0ba57396915..c276858f9de752f8232769d639399bd96c04691e 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -25,6 +25,8 @@ import 'package:analyzer/analyzer.dart' show parseDirectives;
import 'package:crypto/crypto.dart' show CryptoUtils, MD5;
import 'package:source_span/source_span.dart';
+import 'js/keywords.dart';
+
bool isDartPrivateLibrary(LibraryElement library) {
var uri = library.source.uri;
if (uri.scheme != "dart") return false;
@@ -37,9 +39,39 @@ bool isDartPrivateLibrary(LibraryElement library) {
/// meaningful rules enforced.
String canonicalLibraryName(LibraryElement library) {
var uri = library.source.uri;
- return path.basenameWithoutExtension(uri.pathSegments.last);
+ var name = path.basenameWithoutExtension(uri.pathSegments.last);
+ return _toIdentifier(name);
}
+/// Escape [name] to make it into a valid identifier.
+String _toIdentifier(String name) {
+ if (name.length == 0) return r'$';
+
+ // Escape any invalid characters
+ StringBuffer buffer = null;
+ for (int i = 0; i < name.length; i++) {
+ var ch = name[i];
+ var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch);
+ if (needsEscape && buffer == null) {
+ buffer = new StringBuffer(name.substring(0, i));
+ }
+ if (buffer != null) {
+ buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch);
+ }
+ }
+
+ var result = buffer != null ? '$buffer' : name;
+ // Ensure the idenifier first character is not numeric and that the whole
+ // identifier is not a keyword.
+ if (result.startsWith(new RegExp('[0-9]')) || isJsKeyword(result)) {
+ return '\$$result';
+ }
+ return result;
+}
+
+// Invalid characters for identifiers, which would need to be escaped.
+final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]');
+
/// Returns all libraries transitively imported or exported from [start].
Iterable<LibraryElement> reachableLibraries(LibraryElement start) {
var results = <LibraryElement>[];

Powered by Google App Engine
This is Rietveld 408576698