From 5276a4a873d174ae594198045a15cb821cca29e7 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 7 Nov 2012 12:43:27 +0200 Subject: [PATCH] add AST_Accessor and AST_SymbolAccessor node types 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 | 8 ++++++++ lib/parse.js | 6 ++++-- lib/scope.js | 12 +++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index 32ec5380..d55ec223 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -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: { diff --git a/lib/parse.js b/lib/parse.js index ea9eba50..ffdd7a53 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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; diff --git a/lib/scope.js b/lib/scope.js index 27cd5259..f8a1cb8b 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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; -- 2.34.1