add AST_Accessor and AST_SymbolAccessor node types
authorMihai Bazon <mihai@bazon.net>
Wed, 7 Nov 2012 10:43:27 +0000 (12:43 +0200)
committerMihai Bazon <mihai@bazon.net>
Wed, 7 Nov 2012 10:43:27 +0000 (12:43 +0200)
AST_Accessor will represent the function for a setter or getter.  Since they
are not mangleable, and they should not introduce a name in scope, we have a
new node for their name (AST_SymbolAccessor) which doesn't inherit from
AST_SymbolDeclaration.

fix #37

lib/ast.js
lib/parse.js
lib/scope.js

index 32ec538..d55ec22 100644 (file)
@@ -345,6 +345,10 @@ var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
     }
 }, AST_Scope);
 
+var AST_Accessor = DEFNODE("Accessor", null, {
+    $documentation: "A setter/getter function"
+}, AST_Lambda);
+
 var AST_Function = DEFNODE("Function", null, {
     $documentation: "A function expression"
 }, AST_Lambda);
@@ -758,6 +762,10 @@ var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
     $documentation: "Base class for all symbols",
 });
 
+var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
+    $documentation: "The name of a property accessor (setter/getter function)"
+}, AST_Symbol);
+
 var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
     $documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
     $propdoc: {
index ea9eba5..ffdd7a5 100644 (file)
@@ -883,6 +883,8 @@ function parse($TEXT, options) {
     var function_ = function(in_statement, ctor) {
         var name = is("name") ? as_symbol(in_statement
                                           ? AST_SymbolDefun
+                                          : ctor === AST_Accessor
+                                          ? AST_SymbolAccessor
                                           : AST_SymbolLambda) : null;
         if (in_statement && !name)
             unexpected();
@@ -1158,7 +1160,7 @@ function parse($TEXT, options) {
                     a.push(new AST_ObjectGetter({
                         start : start,
                         key   : name,
-                        value : function_(false, AST_Lambda),
+                        value : function_(false, AST_Accessor),
                         end   : prev()
                     }));
                     continue;
@@ -1167,7 +1169,7 @@ function parse($TEXT, options) {
                     a.push(new AST_ObjectSetter({
                         start : start,
                         key   : name,
-                        value : function_(false, AST_Lambda),
+                        value : function_(false, AST_Accessor),
                         end   : prev()
                     }));
                     continue;
index 27cd525..f8a1cb8 100644 (file)
@@ -312,6 +312,11 @@ AST_Symbol.DEFMETHOD("unmangleable", function(options){
     return this.definition().unmangleable(options);
 });
 
+// property accessors are not mangleable
+AST_SymbolAccessor.DEFMETHOD("unmangleable", function(){
+    return true;
+});
+
 // labels are always mangleable
 AST_Label.DEFMETHOD("unmangleable", function(){
     return false;
@@ -363,12 +368,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
         }
         if (node instanceof AST_Scope) {
             var p = tw.parent();
-            var is_setget = p instanceof AST_ObjectSetter || p instanceof AST_ObjectGetter;
             node.variables.each(function(symbol){
-                if (!(is_setget && symbol instanceof AST_SymbolLambda)) {
-                    if (options.except.indexOf(symbol.name) < 0) {
-                        to_mangle.push(symbol);
-                    }
+                if (options.except.indexOf(symbol.name) < 0) {
+                    to_mangle.push(symbol);
                 }
             });
             return;