polish: skip creating extra reference for safely re-used node (#10720)

* polish: skip creating extra reference for safely re-used node

* reimplement using scope.maybeGenerateMemoised
This commit is contained in:
Huáng Jùnliàng 2019-11-15 19:11:03 -05:00 committed by Nicolò Ribaudo
parent d56911b88d
commit 6c7f8291d4
9 changed files with 26 additions and 11 deletions

View File

@ -16,14 +16,15 @@ export default declare((api, { loose = false }) => {
return;
}
const ref = scope.generateUidIdentifierBasedOnNode(node.left);
scope.push({ id: ref });
const assignment = t.assignmentExpression(
"=",
t.cloneNode(ref),
node.left,
);
let ref = scope.maybeGenerateMemoised(node.left);
let assignment;
// skip creating extra reference when `left` is static
if (ref === null) {
ref = node.left;
assignment = t.cloneNode(node.left);
} else {
assignment = t.assignmentExpression("=", ref, node.left);
}
path.replaceWith(
t.conditionalExpression(

View File

@ -1 +1 @@
function foo(foo, bar = foo ?? "bar") {}
function foo(foo, qux = foo.bar ?? "qux") {}

View File

@ -1,3 +1,3 @@
function foo(foo, bar = (_foo = foo) !== null && _foo !== void 0 ? _foo : "bar") {
var _foo;
function foo(foo, qux = (_foo$bar = foo.bar) !== null && _foo$bar !== void 0 ? _foo$bar : "qux") {
var _foo$bar;
}

View File

@ -0,0 +1 @@
function foo(foo, bar = foo ?? "bar") {}

View File

@ -0,0 +1,3 @@
{
"plugins": ["proposal-nullish-coalescing-operator"]
}

View File

@ -0,0 +1 @@
function foo(foo, bar = foo !== null && foo !== void 0 ? foo : "bar") {}

View File

@ -0,0 +1,3 @@
function foo() {
var foo = this ?? {};
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["proposal-nullish-coalescing-operator"]
}

View File

@ -0,0 +1,3 @@
function foo() {
var foo = this !== null && this !== void 0 ? this : {};
}