From c3eb61af4935f872b88bd815ec0158753b779c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sun, 11 Oct 2015 18:22:07 +0100 Subject: [PATCH 1/5] start concise methods --- lib/ast.js | 4 ++++ lib/output.js | 3 +++ lib/parse.js | 10 ++++++++++ test/compress/harmony.js | 16 ++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/lib/ast.js b/lib/ast.js index d9afcff4..32d8486f 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -473,6 +473,10 @@ var AST_Arrow = DEFNODE("Arrow", null, { $documentation: "An ES6 Arrow function ((a) => b)" }, AST_Lambda); +var AST_ConciseMethod = DEFNODE("ConciseMethod", null, { + $documentation: "An ES6 concise method inside an object or class" +}, AST_Lambda); + var AST_Defun = DEFNODE("Defun", null, { $documentation: "A function definition" }, AST_Lambda); diff --git a/lib/output.js b/lib/output.js index 177a772c..490466f4 100644 --- a/lib/output.js +++ b/lib/output.js @@ -832,6 +832,9 @@ function OutputStream(options) { } if (needs_parens) { output.print(")") } }); + DEFPRINT(AST_ConciseMethod, function(self, output){ + self._do_print(output, true /* do not print "function" */); + }); /* -----[ exits ]----- */ AST_Exit.DEFMETHOD("_do_print", function(output, kind){ diff --git a/lib/parse.js b/lib/parse.js index c0065217..12c7acaa 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1446,6 +1446,16 @@ function parse($TEXT, options) { var type = start.type; var name = as_property_name(); if (type == "name" && !is("punc", ":")) { + if (is("punc", "(")) { + a.push(new AST_ConciseMethod({ + start : start, + name : new AST_Symbol({ name: name }), // TODO what symbol is this really? + argnames : params_or_seq_().as_params(croak), + body : _function_body(true), + end : prev() + })) + continue; + } if (name == "get") { a.push(new AST_ObjectGetter({ start : start, diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 86c2d33a..1659e89b 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -132,6 +132,22 @@ destructuring_arguments: { } } +concise_methods: { + input: { + x = { + foo(a, b) { + return x; + } + } + y = { + foo([{a}]) { + return a; + } + } + } + expect_exact: "x={foo(a,b){return x}};y={foo([{a}]){return a}};" +} + number_literals: { input: { 0b1001; From 7514ec93e107b9fd2f6bb321dd2df50c04ee1f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Fri, 23 Oct 2015 18:59:07 +0100 Subject: [PATCH 2/5] Just making sure that concise methods are separated by commas. When classes come, they won't be necessary. --- test/compress/harmony.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 1659e89b..4746a78e 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -142,10 +142,11 @@ concise_methods: { y = { foo([{a}]) { return a; - } + }, + bar(){} } } - expect_exact: "x={foo(a,b){return x}};y={foo([{a}]){return a}};" + expect_exact: "x={foo(a,b){return x}};y={foo([{a}]){return a},bar(){}};" } number_literals: { From a75785d7256c298010a7d36a6c9eb2a870915da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Mon, 26 Oct 2015 22:14:55 +0000 Subject: [PATCH 3/5] Create a new symbol for methods' names --- lib/ast.js | 4 ++++ lib/parse.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ast.js b/lib/ast.js index 32d8486f..8dadf21c 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -991,6 +991,10 @@ var AST_SymbolDefun = DEFNODE("SymbolDefun", null, { $documentation: "Symbol defining a function", }, AST_SymbolDeclaration); +var AST_SymbolMethod = DEFNODE("SymbolMethod", null, { + $documentation: "Symbol in an object defining a method", +}, AST_Symbol); + var AST_SymbolLambda = DEFNODE("SymbolLambda", null, { $documentation: "Symbol naming a function expression", }, AST_SymbolDeclaration); diff --git a/lib/parse.js b/lib/parse.js index 12c7acaa..5384f4f6 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1449,7 +1449,7 @@ function parse($TEXT, options) { if (is("punc", "(")) { a.push(new AST_ConciseMethod({ start : start, - name : new AST_Symbol({ name: name }), // TODO what symbol is this really? + name : new AST_SymbolMethod({ name: name }), argnames : params_or_seq_().as_params(croak), body : _function_body(true), end : prev() From 963a0b896a296b813de18de438d7bd0fa8e96562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Mon, 26 Oct 2015 22:15:21 +0000 Subject: [PATCH 4/5] Make concise methods work with propmangle --- lib/propmangle.js | 8 ++++++++ test/compress/harmony.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/propmangle.js b/lib/propmangle.js index ff782b57..86da5de9 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -107,6 +107,9 @@ function mangle_properties(ast, options) { addStrings(node.property); } } + else if (node instanceof AST_ConciseMethod) { + add(node.name.name); + } })); // step 2: transform the tree, renaming properties @@ -129,6 +132,11 @@ function mangle_properties(ast, options) { else if (node instanceof AST_Sub) { node.property = mangleStrings(node.property); } + else if (node instanceof AST_ConciseMethod) { + if (should_mangle(node.name.name)) { + node.name.name = mangle(node.name.name); + } + } // else if (node instanceof AST_String) { // if (should_mangle(node.value)) { // AST_Node.warn( diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 4746a78e..272014c6 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -149,6 +149,26 @@ concise_methods: { expect_exact: "x={foo(a,b){return x}};y={foo([{a}]){return a},bar(){}};" } +concise_methods_and_mangle_props: { + mangle_props = { + regex: /_/ + }; + input: { + function x() { + obj = { + _foo() { return 1; } + } + } + } + expect: { + function x() { + obj = { + a() { return 1; } + } + } + } +} + number_literals: { input: { 0b1001; From 07e2b18991ecbf08836e30d2376f6d8a413658e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Mon, 26 Oct 2015 23:24:04 +0000 Subject: [PATCH 5/5] Accept keyword names as concise method names --- lib/parse.js | 2 +- test/compress/harmony.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/parse.js b/lib/parse.js index 5384f4f6..7fd15a6d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1445,7 +1445,7 @@ function parse($TEXT, options) { var start = S.token; var type = start.type; var name = as_property_name(); - if (type == "name" && !is("punc", ":")) { + if (type != "string" && type != "num" && !is("punc", ":")) { if (is("punc", "(")) { a.push(new AST_ConciseMethod({ start : start, diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 272014c6..87f2a31f 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -169,6 +169,18 @@ concise_methods_and_mangle_props: { } } +concise_methods_and_keyword_names: { + input: { + x = { + catch() {}, + throw() {} + } + } + expect: { + x={catch(){},throw(){}}; + } +} + number_literals: { input: { 0b1001;