Skip to content

Commit

Permalink
fix: always call setState with arrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteXyy committed Nov 12, 2020
1 parent 259985d commit 957aea8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-react-label-sugar",
"version": "0.1.0-alpha.1",
"version": "0.1.0-alpha.2",
"description": "babel plugin to use label sugar for useState hook",
"main": "lib/index.js",
"files": [
Expand Down
17 changes: 5 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,11 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
if (isMemberExpression(node.left)) {
!ignoreMemberExpr && handleMemberExpression(node.left, ref, path);
} else {
if (node.operator === "=") {
path.replaceWith(callExpression(ref.modifier, [node.right]));
} else {
path.replaceWith(
callExpression(ref.modifier, [
arrowFunctionExpression(
[ref.identify],
binaryExpression(node.operator.slice(0, -1) as any, ref.identify, node.right)
),
])
);
}
const arrowReturn =
node.operator === "="
? node.right
: binaryExpression(node.operator.slice(0, -1) as any, ref.identify, node.right);
path.replaceWith(callExpression(ref.modifier, [arrowFunctionExpression([ref.identify], arrowReturn)]));
}
},
UpdateExpression: (path, state: ReactRefsState) => {
Expand Down
16 changes: 8 additions & 8 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ describe("test react-label-sugar", () => {
const code = `
function App() {
ref: count = 20 * 3;
return React.createElement("button", { onClick: () => count = count + 1 }, count)
return () => count = count + 1
}
`;
const actual = transformSync(code, { plugins })?.code;
const expected = `
function App() {
const [count, setCount] = React.useState(20 * 3);
return React.createElement("button", { onClick: () => setCount(count + 1) }, count)
return () => setCount(count => count + 1)
}
`;
expect(await minify(actual)).to.equal(await minify(expected));
Expand All @@ -54,7 +54,7 @@ describe("test react-label-sugar", () => {
const _setCount2 = () => {};
ref: count = 0;
ref: count2 = 1;
return React.createElement("button", { onClick: () => { count = 1; count2 = 2; } });
return () => { count = 1; count2 = 2; };
}
`;
const actual = transformSync(code, { plugins })?.code;
Expand All @@ -65,7 +65,7 @@ describe("test react-label-sugar", () => {
const _setCount2 = () => {};
const [count, _setCount] = React.useState(0);
const [count2, _setCount3] = React.useState(1);
return React.createElement("button", { onClick: () => { _setCount(1); _setCount3(2); } });
return () => { _setCount((count) => 1); _setCount3((count) => 2); };
}
`;
expect(await minify(actual)).to.equal(await minify(expected));
Expand All @@ -76,15 +76,15 @@ describe("test react-label-sugar", () => {
function App() {
ref: count = 0;
let other = 1;
return React.createElement("button", { onClick: () => { count = 1; other = 1; } });
return () => { count = 1; other = 1; };
}
`;
const actual = transformSync(code, { plugins })?.code;
const expected = await minify(`
function App() {
const [count, setCount] = React.useState(0);
let other = 1;
return React.createElement("button", { onClick: () => { setCount(1); other = 1; } });
return () => { setCount((count) => 1); other = 1; };
}
`);
expect(await minify(actual)).to.equal(await minify(expected));
Expand All @@ -95,15 +95,15 @@ describe("test react-label-sugar", () => {
function App() {
ref: count = 0;
ref: count2 = 0;
return React.createElement("button", { onClick: () => { count *= 2; count2++; } });
return () => { count *= 2; count2++; };
}
`;
const actual = transformSync(code, { plugins })?.code;
const expected = `
function App() {
const [count, setCount] = React.useState(0);
const [count2, setCount2] = React.useState(0);
return React.createElement("button", { onClick: () => { setCount(count => count * 2); setCount2(count => count + 1) } });
return () => { setCount(count => count * 2); setCount2(count => count + 1) };
}
`;
expect(await minify(actual)).to.equal(await minify(expected));
Expand Down

0 comments on commit 957aea8

Please sign in to comment.