support shorthand key-symbol output (#4768)

This commit is contained in:
Alex Lam S.L
2021-03-13 07:37:01 +00:00
committed by GitHub
parent 6f3ab09319
commit 241113200e
3 changed files with 36 additions and 26 deletions

View File

@@ -1607,7 +1607,14 @@ function OutputStream(options) {
output.space(); output.space();
} : noop); } : 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) { DEFPRINT(AST_DestructuredObject, function(output) {
var props = this.properties, len = props.length, rest = this.rest; var props = this.properties, len = props.length, rest = this.rest;
if (len || rest) output.with_block(function() { if (len || rest) output.with_block(function() {
@@ -1670,20 +1677,19 @@ function OutputStream(options) {
output.print_string(key, quote); output.print_string(key, quote);
} else { } else {
output.print_name(key); output.print_name(key);
return key;
} }
} else { } else {
output.print_string(key, quote); output.print_string(key, quote);
} }
} }
} }
DEFPRINT(AST_ObjectKeyVal, function(output) {
function print_key_value(output) {
var self = this; var self = this;
print_property_key(self, output); print_property_key(self, output);
output.colon(); output.colon();
self.value.print(output); self.value.print(output);
} });
DEFPRINT(AST_ObjectKeyVal, print_key_value);
DEFPRINT(AST_ObjectMethod, function(output) { DEFPRINT(AST_ObjectMethod, function(output) {
print_method(this, output); print_method(this, output);
}); });
@@ -1702,32 +1708,36 @@ function OutputStream(options) {
} }
DEFPRINT(AST_ObjectGetter, print_accessor("get")); DEFPRINT(AST_ObjectGetter, print_accessor("get"));
DEFPRINT(AST_ObjectSetter, print_accessor("set")); DEFPRINT(AST_ObjectSetter, print_accessor("set"));
function print_symbol(self, output) { function get_symbol_name(sym) {
var def = self.definition(); var def = sym.definition();
output.print_name(def && def.mangled_name || self.name); return def && def.mangled_name || sym.name;
} }
DEFPRINT(AST_Symbol, function(output) { DEFPRINT(AST_Symbol, function(output) {
print_symbol(this, output); output.print_name(get_symbol_name(this));
}); });
DEFPRINT(AST_SymbolExport, function(output) { DEFPRINT(AST_SymbolExport, function(output) {
var self = this; var self = this;
print_symbol(self, output); var name = get_symbol_name(self);
if (self.alias) { output.print_name(name);
var alias = self.alias;
if (alias != name) {
output.space(); output.space();
output.print("as"); output.print("as");
output.space(); output.space();
output.print_name(self.alias); output.print_name(alias);
} }
}); });
DEFPRINT(AST_SymbolImport, function(output) { DEFPRINT(AST_SymbolImport, function(output) {
var self = this; var self = this;
if (self.key) { var name = get_symbol_name(self);
output.print_name(self.key); var key = self.key;
if (key && key != name) {
output.print_name(key);
output.space(); output.space();
output.print("as"); output.print("as");
output.space(); output.space();
} }
print_symbol(self, output); output.print_name(name);
}); });
DEFPRINT(AST_Hole, noop); DEFPRINT(AST_Hole, noop);
DEFPRINT(AST_Template, function(output) { DEFPRINT(AST_Template, function(output) {

View File

@@ -3,7 +3,7 @@ refs: {
export {}; export {};
export { a, b as B, c as case, d as default }; 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: { var_defs: {
@@ -12,7 +12,7 @@ var_defs: {
export let b = 2, c = 3; export let b = 2, c = 3;
export var { d, e: [] } = f; 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: { defuns: {
@@ -35,7 +35,7 @@ defaults: {
export default function*(a, b) {}; export default function*(a, b) {};
export default async function f({ c }, ...[ d ]) {}; 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: { defaults_parentheses_1: {
@@ -242,15 +242,15 @@ hoist_exports_2: {
} }
} }
expect: { expect: {
let f, { foo: o } = 42; let e, { foo: a } = 42;
function c(t, { [f]: a }) { function f(t, { [e]: o }) {
t(a, c); t(o, f);
} }
export default 42; export default 42;
export default async function e(t, ...{ [o]: a }) { export default async function n(t, ...{ [a]: o }) {
(await t)(e, a); (await t)(n, o);
}; };
export { f as bbb, o as ccc, c as fff }; export { e as bbb, a as ccc, f as fff };
} }
} }

View File

@@ -23,7 +23,7 @@ keys_only: {
input: { input: {
import { as as foo, bar, delete as baz } from "moo"; 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: { default_all: {
@@ -37,7 +37,7 @@ default_keys: {
input: { input: {
import foo, { bar } from "baz"; import foo, { bar } from "baz";
} }
expect_exact: 'import foo,{bar as bar}from"baz";' expect_exact: 'import foo,{bar}from"baz";'
} }
dynamic: { dynamic: {