Skip to content

Commit

Permalink
Merge pull request #2 from InfiniteXyy/fix/deep_value_mutation
Browse files Browse the repository at this point in the history
fix: deep obj mutation issue
  • Loading branch information
InfiniteXyy committed Nov 12, 2020
2 parents 9d1c244 + 25da83d commit 259985d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 128 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Add plugin in .babelrc
(optional) custom options
```json5
{
"refLabel": "$", // default is "ref"
"refFactory": "useState", // default is "React.useState"
"ignoreMemberExpr": false, // default is true, see "with immer" below for more info
"refLabel": "$", // default is "ref"
"refFactory": "useState", // default is "React.useState"
"ignoreMemberExpr": false, // default is true, see "with immer" below for more info
}
```

Expand Down
116 changes: 1 addition & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "babel-plugin-react-label-sugar",
"version": "0.1.0-alpha",
"version": "0.1.0-alpha.1",
"description": "babel plugin to use label sugar for useState hook",
"main": "lib/index.js",
"files": [
"lib",
"README.md"
],
"scripts": {
"test": "ts-mocha tests/*.tsx",
"test": "mocha tests/*.tsx",
"build": "babel --extensions \".ts\" src --out-dir lib",
"prepare": "npm run build",
"watch": "npm run build -- --watch"
Expand Down Expand Up @@ -44,7 +44,12 @@
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"terser": "^5.3.8",
"ts-mocha": "^8.0.0",
"ts-node": "^7.0.1",
"typescript": "^4.0.5"
},
"mocha": {
"require": [
"ts-node/register"
]
}
}
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,12 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
*/
function getTargetRef(leftValue: object | null | undefined, refs: ReactRefs): ReactRefItem | undefined {
if (!isIdentifier(leftValue) && !isMemberExpression(leftValue)) return undefined;
let target: string;
if (isIdentifier(leftValue)) {
target = leftValue.name;
const target = leftValue.name;
return refs.find((ref) => ref.identify.name === target);
} else {
if (!isIdentifier(leftValue.object)) return undefined;
target = leftValue.object.name;
return getTargetRef(leftValue.object, refs);
}
return refs.find((ref) => ref.identify.name === target);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,19 @@ describe("test react-label-sugar", () => {
});

it("should member expression works", async () => {
const code = `$: obj = { count: 0 }; obj.count = 2; obj.count++;`;
const code = `$: obj = { count: 0, foo: { bar: 1 } }; obj.count = 2; obj.count++; obj.foo.bar = 2;`;
const actual = transformSync(code, { plugins: pluginsWithOption })?.code ?? "";
const expected = `
const [obj, _setObj] = useImmer({ count: 0 });
const [obj, _setObj] = useImmer({ count: 0, foo: { bar: 1 } });
_setObj(obj => {
obj.count = 2;
});
_setObj(obj => {
obj.count++;
});
_setObj(obj => {
obj.foo.bar = 2;
});
`;
expect(await minify(actual)).to.equal(await minify(expected));
});
Expand Down

0 comments on commit 259985d

Please sign in to comment.