Skip to content

Commit

Permalink
fix: mark the last statement in do block as completion
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 29, 2022
1 parent 1274a18 commit ba0d01c
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 5 deletions.
Expand Up @@ -41,20 +41,19 @@ export default declare(function ({
Function(path) {
// (b, { #x: x } = I) => body
// transforms to:
// (p1, p2) => { var b = p1, { #x: x } = p2 === undefined ? I : p2; body; }
// (b, p1) => { var { #x: x } = p1 === undefined ? I : p1; body; }
const index = path.node.params.findIndex(param => hasPrivateKeys(param));
if (index === -1) return;
// wrap function body within IIFE if any param is shadowed
convertFunctionParams(path, ignoreFunctionLength, () => false, false);
// invariant: path.body is always a BlockStatement after `convertFunctionParams`
const { node, scope } = path;
const params = node.params;
const paramsAfterIndex = params.splice(index);
const { params: transformedParams, variableDeclaration } =
buildVariableDeclarationFromParams(paramsAfterIndex, scope);

path
.get("body") // invariant: path.body is always a BlockStatement
.unshiftContainer("body", variableDeclaration);
path.get("body").unshiftContainer("body", variableDeclaration);
params.push(...transformedParams);
scope.crawl();
// the pattern will be handled by VariableDeclaration visitor.
Expand Down
@@ -0,0 +1,9 @@
var result;
class C {
static #x;
static {
var x;
result = do { ({#x: x = 2} = C); }
}
}
expect(result).toBe(C);
@@ -0,0 +1,8 @@
var result;
class C {
static #x;
static {
var x;
result = do { ({#x: x = 2} = C); }
}
}
@@ -0,0 +1,10 @@
{
"plugins": [
"proposal-do-expressions",
"proposal-destructuring-private",
"proposal-class-static-block",
"proposal-class-properties",
"proposal-private-methods",
["proposal-object-rest-spread", { "useBuiltIns": true }]
]
}
@@ -0,0 +1,15 @@
var result;

class C {}

var _x = {
writable: true,
value: void 0
};

(() => {
var _m, _m2;

var x;
result = (_m = C, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 2 : _m2, _m);
})();
@@ -0,0 +1,9 @@
var result;
class C {
static #x;
static {
var x;
result = do { ({#x: x = 2} = C); }
}
}
expect(result).toBe(C);
@@ -0,0 +1,8 @@
var result;
class C {
static #x;
static {
var x;
result = do { ({#x: x = 2} = C); }
}
}
@@ -0,0 +1,4 @@
{
"plugins": ["proposal-do-expressions", "proposal-destructuring-private"],
"minNodeVersion": "16.11.0"
}
@@ -0,0 +1,11 @@
var result;

class C {
static #x;
static {
var _m, _m2;

var x;
result = (_m = C, _m2 = _m.#x, x = _m2 === void 0 ? 2 : _m2, _m);
}
}
6 changes: 5 additions & 1 deletion packages/babel-traverse/src/path/introspection.ts
Expand Up @@ -149,7 +149,11 @@ export function isCompletionRecord(
if (Array.isArray(container) && path.key !== container.length - 1) {
return false;
}
} while ((path = path.parentPath) && !path.isProgram());
} while (
(path = path.parentPath) &&
!path.isProgram() &&
!path.isDoExpression()
);

return true;
}
Expand Down

0 comments on commit ba0d01c

Please sign in to comment.