Skip to content

Commit

Permalink
fix: scope issue
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteXyy committed Nov 12, 2020
1 parent 957aea8 commit aee6dfe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 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.2",
"version": "0.1.0-alpha.3",
"description": "babel plugin to use label sugar for useState hook",
"main": "lib/index.js",
"files": [
Expand Down
32 changes: 16 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
return {
visitor: {
LabeledStatement: (path, state: ReactRefsState) => {
const { node } = path;
const { node, scope } = path;
const { label, body } = node;
if (label.name !== refLabel) return;

Expand All @@ -57,7 +57,7 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {

const refItem: ReactRefItem = {
identify: left,
modifier: path.scope.generateUidIdentifier(`set${capitalize(left.name)}`),
modifier: scope.generateUidIdentifier(`set${capitalize(left.name)}`),
};

path.replaceWith(
Expand All @@ -68,14 +68,14 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
),
])
);
if (!state.reactRefs) state.reactRefs = [];
state.reactRefs ??= [];
state.reactRefs.push(refItem);
},
AssignmentExpression: (path, state: ReactRefsState) => {
if (!state.reactRefs) return;
const { node } = path;
const { node, scope } = path;
const ref = getTargetRef(node.left, state.reactRefs);
if (!ref) return;
if (!ref || scope.bindings[ref.identify.name]) return;
if (isMemberExpression(node.left)) {
!ignoreMemberExpr && handleMemberExpression(node.left, ref, path);
} else {
Expand All @@ -88,21 +88,21 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
},
UpdateExpression: (path, state: ReactRefsState) => {
if (!state.reactRefs) return;
const { node } = path;
const { node, scope } = path;
const ref = getTargetRef(node.argument, state.reactRefs);
if (!ref) return;
if (!ref || scope.bindings[ref.identify.name]) return;
if (isMemberExpression(node.argument)) {
!ignoreMemberExpr && handleMemberExpression(node.argument, ref, path);
} else {
path.replaceWith(
callExpression(ref.modifier, [
arrowFunctionExpression(
[ref.identify],
binaryExpression(node.operator === "++" ? "+" : "-", ref.identify, numericLiteral(1))
),
])
);
return;
}
path.replaceWith(
callExpression(ref.modifier, [
arrowFunctionExpression(
[ref.identify],
binaryExpression(node.operator === "++" ? "+" : "-", ref.identify, numericLiteral(1))
),
])
);
},
},
};
Expand Down
17 changes: 12 additions & 5 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { minify as _minify } from "terser";
import ReactLabelSugar from "../src";

const minify = async (code?: string | null): Promise<string> => {
return (await _minify(code ?? "")).code ?? "";
return (await _minify(code ?? "", { format: { beautify: true }, compress: false })).code ?? "";
};

const plugins = [ReactLabelSugar];
Expand Down Expand Up @@ -75,18 +75,25 @@ describe("test react-label-sugar", () => {
const code = `
function App() {
ref: count = 0;
let other = 1;
return () => { count = 1; other = 1; };
useEffect(() => {
let count = 0;
count++;
}, []);
return () => { count = 1; };
}
`;
const actual = transformSync(code, { plugins })?.code;
const expected = await minify(`
function App() {
const [count, setCount] = React.useState(0);
let other = 1;
return () => { setCount((count) => 1); other = 1; };
useEffect(() => {
let count = 0;
count++;
}, []);
return () => { setCount((count) => 1); };
}
`);

expect(await minify(actual)).to.equal(await minify(expected));
});

Expand Down

0 comments on commit aee6dfe

Please sign in to comment.