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

Unified Diff: lib/src/js/printer.dart

Issue 1030063004: more care around generated names, fixes #60 #82 and #97 (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/js/printer.dart
diff --git a/lib/src/js/printer.dart b/lib/src/js/printer.dart
index 0e92ed5e224edfd40be6a88b6e69a0d30a119163..90cba8e472065aaea33bde68b488a0b0c1510976 100644
--- a/lib/src/js/printer.dart
+++ b/lib/src/js/printer.dart
@@ -9,13 +9,17 @@ class JavaScriptPrintingOptions {
final bool shouldCompressOutput;
final bool minifyLocalVariables;
final bool preferSemicolonToNewlineInMinifiedOutput;
- final bool avoidKeywordsInIdentifiers;
+
+
+ /// True to allow keywords in properties, such as `obj.var` or `obj.function`
+ /// Modern JS engines support this.
+ final bool allowKeywordsInProperties;
JavaScriptPrintingOptions(
{this.shouldCompressOutput: false,
this.minifyLocalVariables: false,
this.preferSemicolonToNewlineInMinifiedOutput: false,
- this.avoidKeywordsInIdentifiers: false});
+ this.allowKeywordsInProperties: false});
}
@@ -68,17 +72,18 @@ class Printer implements NodeVisitor {
static final expressionContinuationRegExp = new RegExp(r'^[-+([]');
Printer(JavaScriptPrintingOptions options,
- JavaScriptPrintingContext context)
+ JavaScriptPrintingContext context,
+ {LocalNamer localNamer})
: options = options,
context = context,
shouldCompressOutput = options.shouldCompressOutput,
danglingElseVisitor = new DanglingElseVisitor(context),
- localNamer = determineRenamer(options.shouldCompressOutput,
- options.minifyLocalVariables);
+ localNamer = determineRenamer(localNamer, options);
- static LocalNamer determineRenamer(bool shouldCompressOutput,
- bool allowVariableMinification) {
- return (shouldCompressOutput && allowVariableMinification)
+ static LocalNamer determineRenamer(LocalNamer localNamer,
+ JavaScriptPrintingOptions options) {
+ if (localNamer != null) return localNamer;
+ return (options.shouldCompressOutput && options.minifyLocalVariables)
? new MinifyRenamer() : new IdentityNamer();
}
@@ -778,8 +783,8 @@ class Printer implements NodeVisitor {
out("super");
}
- visitIdentifier(Identifier param) {
- out(localNamer.getName(param.name));
+ visitIdentifier(Identifier node) {
+ out(localNamer.getName(node));
}
bool isDigit(int charCode) {
@@ -801,14 +806,10 @@ class Printer implements NodeVisitor {
}
}
- if (options.avoidKeywordsInIdentifiers) {
- return !isJsKeyword(field.substring(1, field.length - 1));
- } else {
- // TODO(floitsch): normally we should also check that the field is not a
- // reserved word. We don't generate fields with reserved word names except
- // for 'super'.
- return field != '"super"';
- }
+ // TODO(floitsch): normally we should also check that the field is not a
+ // reserved word. We don't generate fields with reserved word names except
+ // for 'super'.
+ return options.allowKeywordsInProperties || field != '"super"';
}
visitAccess(PropertyAccess access) {
@@ -1241,14 +1242,14 @@ class DanglingElseVisitor extends BaseVisitor<bool> {
abstract class LocalNamer {
- String getName(String oldName);
+ String getName(Identifier node);
void enterScope(Node node);
void leaveScope();
}
class IdentityNamer implements LocalNamer {
- String getName(String oldName) => oldName;
+ String getName(Identifier node) => node.name;
void enterScope(Node node) {}
void leaveScope() {}
}
@@ -1277,7 +1278,8 @@ class MinifyRenamer implements LocalNamer {
parameterNumber = parameterNumberStack.removeLast();
}
- String getName(String oldName) {
+ String getName(Identifier node) {
+ String oldName = node.name;
// Go from inner scope to outer looking for mapping of name.
for (int i = maps.length - 1; i >= 0; i--) {
var map = maps[i];

Powered by Google App Engine
This is Rietveld 408576698