From 1fa4a660c76bb08f33292905ef567de9c9de7a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Javier=20Cravero?= Date: Thu, 4 Feb 2016 21:54:21 +0000 Subject: [PATCH] fix: don't fail if definition is undefined Running `uglifyjs --verbose --compress --mangle --screw-ie8 class.js` with `class.js`: ``` class Foo { bar() { } } ``` Fails with: ``` undefined:4041 return this.definition().unmangleable(options); TypeError: Cannot read property 'unmangleable' of undefined at AST_Node.eval [as unmangleable] (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :4041:27) at Object.eval [as visit] (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :4214:53) at Object.TreeWalker._visit (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :1493:24) at AST_Node.DEFNODE._walk (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :415:24) at AST_Node.eval (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :775:38) at Object.TreeWalker._visit (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :1497:21) at AST_Node.DEFNODE._walk (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :774:24) at eval (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :1303:22) at Array.forEach (native) at AST_Node.eval (eval at (/Users/craverod/opensource/panels/panels/node_modules/uglify-js/tools/node.js:22:1), :1302:29) ``` This PR sorts it out (it gives me: `class Foo{bar(){}}`) but it feels like the wrong hack. I don't know what's best and whether we can even try to mangle the method or not, mainly because it's the classes' external API and doing so would prevent others from using it outside the minified source. Thoughts? --- lib/scope.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/scope.js b/lib/scope.js index ac58ad80..371afb2b 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -350,7 +350,8 @@ AST_Scope.DEFMETHOD("references", function(sym){ }); AST_Symbol.DEFMETHOD("unmangleable", function(options){ - return this.definition().unmangleable(options); + var def = this.definition(); + return def && def.unmangleable(options); }); // property accessors are not mangleable