Skip to content

Commit

Permalink
Fix regression for AssignmentExpression support of memo
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Sep 17, 2021
1 parent ab4db67 commit 29a3f22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
29 changes: 17 additions & 12 deletions packages/babel-plugin-solid-reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ function memoSingleExpression(
path: NodePath<t.LabeledStatement>,
memoIdentifier: t.Identifier,
stateIdentifier: t.Expression,
replace = false,
): void {
const readIdentifier = path.scope.generateUidIdentifier(memoIdentifier.name);
const expr = t.variableDeclaration(
Expand All @@ -324,7 +325,11 @@ function memoSingleExpression(
)],
);

path.insertAfter(expr);
if (replace) {
path.replaceWith(expr);
} else {
path.insertAfter(expr);
}

const parent = path.scope.path;
if (parent) {
Expand Down Expand Up @@ -435,19 +440,19 @@ function memoExpression(
body: t.Statement,
): void {
if (t.isExpressionStatement(body)) {
if (!t.isAssignmentExpression(body.expression)) {
if (t.isAssignmentExpression(body.expression)) {
if (body.expression.operator !== '=') {
throw new Error('Invalid assignment expression operator');
}
const leftExpr = body.expression.left;
const rightExpr = body.expression.right;
if (!t.isIdentifier(leftExpr)) {
throw new Error('Expected identifier');
}
memoSingleExpression(hooks, path, leftExpr, rightExpr, true);
} else {
throw new Error('Expected assignment expression');
}
if (body.expression.operator !== '=') {
throw new Error('Invalid assignment expression operator');
}
const leftExpr = body.expression.left;
const rightExpr = body.expression.right;
if (!t.isIdentifier(leftExpr)) {
throw new Error('Expected identifier');
}
memoSingleExpression(hooks, path, leftExpr, rightExpr);
path.remove();
} else if (t.isVariableDeclaration(body)) {
for (let i = body.declarations.length - 1; i >= 0; i -= 1) {
const declarator = body.declarations[i];
Expand Down
11 changes: 1 addition & 10 deletions packages/babel-plugin-solid-reactivity/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ const babel = require('@babel/core');
const plugin = require('../dist/cjs');

const code = `
function Example() {
signal: var x = 0, y = 1;
while (true) {
let y = x;
}
x += y = 2;
x++;
}
memo: var x = 0, y = 0;
`;
babel.transformAsync(code, {
plugins: [
Expand Down

0 comments on commit 29a3f22

Please sign in to comment.