Skip to content

Commit aee6dfe

Browse files
committed
fix: scope issue
1 parent 957aea8 commit aee6dfe

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-react-label-sugar",
3-
"version": "0.1.0-alpha.2",
3+
"version": "0.1.0-alpha.3",
44
"description": "babel plugin to use label sugar for useState hook",
55
"main": "lib/index.js",
66
"files": [

src/index.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
3737
return {
3838
visitor: {
3939
LabeledStatement: (path, state: ReactRefsState) => {
40-
const { node } = path;
40+
const { node, scope } = path;
4141
const { label, body } = node;
4242
if (label.name !== refLabel) return;
4343

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

5858
const refItem: ReactRefItem = {
5959
identify: left,
60-
modifier: path.scope.generateUidIdentifier(`set${capitalize(left.name)}`),
60+
modifier: scope.generateUidIdentifier(`set${capitalize(left.name)}`),
6161
};
6262

6363
path.replaceWith(
@@ -68,14 +68,14 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
6868
),
6969
])
7070
);
71-
if (!state.reactRefs) state.reactRefs = [];
71+
state.reactRefs ??= [];
7272
state.reactRefs.push(refItem);
7373
},
7474
AssignmentExpression: (path, state: ReactRefsState) => {
7575
if (!state.reactRefs) return;
76-
const { node } = path;
76+
const { node, scope } = path;
7777
const ref = getTargetRef(node.left, state.reactRefs);
78-
if (!ref) return;
78+
if (!ref || scope.bindings[ref.identify.name]) return;
7979
if (isMemberExpression(node.left)) {
8080
!ignoreMemberExpr && handleMemberExpression(node.left, ref, path);
8181
} else {
@@ -88,21 +88,21 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
8888
},
8989
UpdateExpression: (path, state: ReactRefsState) => {
9090
if (!state.reactRefs) return;
91-
const { node } = path;
91+
const { node, scope } = path;
9292
const ref = getTargetRef(node.argument, state.reactRefs);
93-
if (!ref) return;
93+
if (!ref || scope.bindings[ref.identify.name]) return;
9494
if (isMemberExpression(node.argument)) {
9595
!ignoreMemberExpr && handleMemberExpression(node.argument, ref, path);
96-
} else {
97-
path.replaceWith(
98-
callExpression(ref.modifier, [
99-
arrowFunctionExpression(
100-
[ref.identify],
101-
binaryExpression(node.operator === "++" ? "+" : "-", ref.identify, numericLiteral(1))
102-
),
103-
])
104-
);
96+
return;
10597
}
98+
path.replaceWith(
99+
callExpression(ref.modifier, [
100+
arrowFunctionExpression(
101+
[ref.identify],
102+
binaryExpression(node.operator === "++" ? "+" : "-", ref.identify, numericLiteral(1))
103+
),
104+
])
105+
);
106106
},
107107
},
108108
};

tests/index.spec.tsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { minify as _minify } from "terser";
44
import ReactLabelSugar from "../src";
55

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

1010
const plugins = [ReactLabelSugar];
@@ -75,18 +75,25 @@ describe("test react-label-sugar", () => {
7575
const code = `
7676
function App() {
7777
ref: count = 0;
78-
let other = 1;
79-
return () => { count = 1; other = 1; };
78+
useEffect(() => {
79+
let count = 0;
80+
count++;
81+
}, []);
82+
return () => { count = 1; };
8083
}
8184
`;
8285
const actual = transformSync(code, { plugins })?.code;
8386
const expected = await minify(`
8487
function App() {
8588
const [count, setCount] = React.useState(0);
86-
let other = 1;
87-
return () => { setCount((count) => 1); other = 1; };
89+
useEffect(() => {
90+
let count = 0;
91+
count++;
92+
}, []);
93+
return () => { setCount((count) => 1); };
8894
}
8995
`);
96+
9097
expect(await minify(actual)).to.equal(await minify(expected));
9198
});
9299

0 commit comments

Comments
 (0)