aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-13 07:37:01 +0000
committerGitHub <noreply@github.com>2021-03-13 15:37:01 +0800
commit241113200e8ad3550efc051b2b7f4d7c9a4573e9 (patch)
treea1df05e2910cdef1cbfb4c2d5728cf687c1bcb9b
parent6f3ab09319ec1aa4a8e489f83f6724add46bc19c (diff)
downloadtracifyjs-241113200e8ad3550efc051b2b7f4d7c9a4573e9.tar.gz
tracifyjs-241113200e8ad3550efc051b2b7f4d7c9a4573e9.zip
support shorthand key-symbol output (#4768)
-rw-r--r--lib/output.js40
-rw-r--r--test/compress/exports.js18
-rw-r--r--test/compress/imports.js4
3 files changed, 36 insertions, 26 deletions
diff --git a/lib/output.js b/lib/output.js
index fc71a62b..00037ce8 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -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) {
diff --git a/test/compress/exports.js b/test/compress/exports.js
index 0fdf6412..bef2d85c 100644
--- a/test/compress/exports.js
+++ b/test/compress/exports.js
@@ -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 };
}
}
diff --git a/test/compress/imports.js b/test/compress/imports.js
index 50b3fb69..dbffb161 100644
--- a/test/compress/imports.js
+++ b/test/compress/imports.js
@@ -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: {