support shorthand key-symbol output (#4768)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 13 Mar 2021 07:37:01 +0000 (07:37 +0000)
committerGitHub <noreply@github.com>
Sat, 13 Mar 2021 07:37:01 +0000 (15:37 +0800)
lib/output.js
test/compress/exports.js
test/compress/imports.js

index fc71a62..00037ce 100644 (file)
@@ -1607,7 +1607,14 @@ function OutputStream(options) {
             output.space();
         } : noop);
     });
-    DEFPRINT(AST_DestructuredKeyVal, print_key_value);
+    DEFPRINT(AST_DestructuredKeyVal, function(output) {
+        var self = this;
+        var key = print_property_key(self, output);
+        var value = self.value;
+        if (key && value instanceof AST_SymbolDeclaration && key == get_symbol_name(value)) return;
+        output.colon();
+        value.print(output);
+    });
     DEFPRINT(AST_DestructuredObject, function(output) {
         var props = this.properties, len = props.length, rest = this.rest;
         if (len || rest) output.with_block(function() {
@@ -1670,20 +1677,19 @@ function OutputStream(options) {
                     output.print_string(key, quote);
                 } else {
                     output.print_name(key);
+                    return key;
                 }
             } else {
                 output.print_string(key, quote);
             }
         }
     }
-
-    function print_key_value(output) {
+    DEFPRINT(AST_ObjectKeyVal, function(output) {
         var self = this;
         print_property_key(self, output);
         output.colon();
         self.value.print(output);
-    }
-    DEFPRINT(AST_ObjectKeyVal, print_key_value);
+    });
     DEFPRINT(AST_ObjectMethod, function(output) {
         print_method(this, output);
     });
@@ -1702,32 +1708,36 @@ function OutputStream(options) {
     }
     DEFPRINT(AST_ObjectGetter, print_accessor("get"));
     DEFPRINT(AST_ObjectSetter, print_accessor("set"));
-    function print_symbol(self, output) {
-        var def = self.definition();
-        output.print_name(def && def.mangled_name || self.name);
+    function get_symbol_name(sym) {
+        var def = sym.definition();
+        return def && def.mangled_name || sym.name;
     }
     DEFPRINT(AST_Symbol, function(output) {
-        print_symbol(this, output);
+        output.print_name(get_symbol_name(this));
     });
     DEFPRINT(AST_SymbolExport, function(output) {
         var self = this;
-        print_symbol(self, output);
-        if (self.alias) {
+        var name = get_symbol_name(self);
+        output.print_name(name);
+        var alias = self.alias;
+        if (alias != name) {
             output.space();
             output.print("as");
             output.space();
-            output.print_name(self.alias);
+            output.print_name(alias);
         }
     });
     DEFPRINT(AST_SymbolImport, function(output) {
         var self = this;
-        if (self.key) {
-            output.print_name(self.key);
+        var name = get_symbol_name(self);
+        var key = self.key;
+        if (key && key != name) {
+            output.print_name(key);
             output.space();
             output.print("as");
             output.space();
         }
-        print_symbol(self, output);
+        output.print_name(name);
     });
     DEFPRINT(AST_Hole, noop);
     DEFPRINT(AST_Template, function(output) {
index 0fdf641..bef2d85 100644 (file)
@@ -3,7 +3,7 @@ refs: {
         export {};
         export { a, b as B, c as case, d as default };
     }
-    expect_exact: "export{};export{a as a,b as B,c as case,d as default};"
+    expect_exact: "export{};export{a,b as B,c as case,d as default};"
 }
 
 var_defs: {
@@ -12,7 +12,7 @@ var_defs: {
         export let b = 2, c = 3;
         export var { d, e: [] } = f;
     }
-    expect_exact: "export const a=1;export let b=2,c=3;export var{d:d,e:[]}=f;"
+    expect_exact: "export const a=1;export let b=2,c=3;export var{d,e:[]}=f;"
 }
 
 defuns: {
@@ -35,7 +35,7 @@ defaults: {
         export default function*(a, b) {};
         export default async function f({ c }, ...[ d ]) {};
     }
-    expect_exact: "export default 42;export default async;export default(x,y)=>x*x;export default class{}export default function*(a,b){}export default async function f({c:c},...[d]){}"
+    expect_exact: "export default 42;export default async;export default(x,y)=>x*x;export default class{}export default function*(a,b){}export default async function f({c},...[d]){}"
 }
 
 defaults_parentheses_1: {
@@ -242,15 +242,15 @@ hoist_exports_2: {
         }
     }
     expect: {
-        let f, { foo: o } = 42;
-        function c(t, { [f]: a }) {
-            t(a, c);
+        let e, { foo: a } = 42;
+        function f(t, { [e]: o }) {
+            t(o, f);
         }
         export default 42;
-        export default async function e(t, ...{ [o]: a }) {
-            (await t)(e, a);
+        export default async function n(t, ...{ [a]: o }) {
+            (await t)(n, o);
         };
-        export { f as bbb, o as ccc, c as fff };
+        export { e as bbb, a as ccc, f as fff };
     }
 }
 
index 50b3fb6..dbffb16 100644 (file)
@@ -23,7 +23,7 @@ keys_only: {
     input: {
         import { as as foo, bar, delete as baz } from "moo";
     }
-    expect_exact: 'import{as as foo,bar as bar,delete as baz}from"moo";'
+    expect_exact: 'import{as as foo,bar,delete as baz}from"moo";'
 }
 
 default_all: {
@@ -37,7 +37,7 @@ default_keys: {
     input: {
         import foo, { bar } from "baz";
     }
-    expect_exact: 'import foo,{bar as bar}from"baz";'
+    expect_exact: 'import foo,{bar}from"baz";'
 }
 
 dynamic: {