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:
parent
d56911b88d
commit
6c7f8291d4
@ -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(
|
||||
|
||||
@ -1 +1 @@
|
||||
function foo(foo, bar = foo ?? "bar") {}
|
||||
function foo(foo, qux = foo.bar ?? "qux") {}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
function foo(foo, bar = foo ?? "bar") {}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["proposal-nullish-coalescing-operator"]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
function foo(foo, bar = foo !== null && foo !== void 0 ? foo : "bar") {}
|
||||
@ -0,0 +1,3 @@
|
||||
function foo() {
|
||||
var foo = this ?? {};
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["proposal-nullish-coalescing-operator"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function foo() {
|
||||
var foo = this !== null && this !== void 0 ? this : {};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user