[dartdevc] improve escaping to handle kernel extension symbols

Fixes https://github.com/dart-lang/sdk/issues/38542

Change-Id: I54007cf8ff159634404a22772030d320997aaa6d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/118915
Reviewed-by: Mark Zhou <markzipan@google.com>
Commit-Queue: Vijay Menon <vsm@google.com>
This commit is contained in:
Vijay Menon 2019-09-26 16:58:42 +00:00 committed by commit-bot@chromium.org
parent 44da8af04f
commit 5c5a76078e
3 changed files with 31 additions and 31 deletions

View file

@ -405,3 +405,32 @@ final friendlyNameForDartOperator = {
'[]=': '_set',
'unary-': '_negate',
};
// Invalid characters for identifiers, which would need to be escaped.
final _invalidCharInIdentifier = RegExp(r'[^A-Za-z_$0-9]');
/// Escape [name] to make it into a valid identifier.
String toJSIdentifier(String name) {
if (name.isEmpty) return r'$';
// Escape any invalid characters
StringBuffer buffer;
for (int i = 0; i < name.length; i++) {
var ch = name[i];
var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch);
if (needsEscape && buffer == null) {
buffer = StringBuffer(name.substring(0, i));
}
if (buffer != null) {
buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch);
}
}
var result = buffer != null ? '$buffer' : name;
// Ensure the identifier first character is not numeric and that the whole
// identifier is not a keyword.
if (result.startsWith(RegExp('[0-9]')) || invalidVariableName(result)) {
return '\$$result';
}
return result;
}

View file

@ -359,34 +359,5 @@ String pathToJSIdentifier(String path) {
.replaceAll('-', '_'));
}
/// Escape [name] to make it into a valid identifier.
String toJSIdentifier(String name) {
if (name.isEmpty) return r'$';
// Escape any invalid characters
StringBuffer buffer;
for (int i = 0; i < name.length; i++) {
var ch = name[i];
var needsEscape = ch == r'$' || _invalidCharInIdentifier.hasMatch(ch);
if (needsEscape && buffer == null) {
buffer = StringBuffer(name.substring(0, i));
}
if (buffer != null) {
buffer.write(needsEscape ? '\$${ch.codeUnits.join("")}' : ch);
}
}
var result = buffer != null ? '$buffer' : name;
// Ensure the identifier first character is not numeric and that the whole
// identifier is not a keyword.
if (result.startsWith(RegExp('[0-9]')) || invalidVariableName(result)) {
return '\$$result';
}
return result;
}
// Invalid characters for identifiers, which would need to be escaped.
final _invalidCharInIdentifier = RegExp(r'[^A-Za-z_$0-9]');
// Replacement string for path separators (i.e., '/', '\', '..').
final encodedSeparator = "__";

View file

@ -505,10 +505,10 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
}
static js_ast.Identifier _emitIdentifier(String name) =>
js_ast.Identifier(escapeIdentifier(name));
js_ast.Identifier(js_ast.toJSIdentifier(name));
static js_ast.TemporaryId _emitTemporaryId(String name) =>
js_ast.TemporaryId(escapeIdentifier(name));
js_ast.TemporaryId(js_ast.toJSIdentifier(name));
js_ast.Statement _emitClassDeclaration(Class c) {
// Mixins are unrolled in _defineClass.