From 25da83d6dc92b2da1d1fcea4184f09be61ac575a Mon Sep 17 00:00:00 2001 From: InfiniteXyy Date: Thu, 12 Nov 2020 18:44:22 +0800 Subject: [PATCH] fix: deep obj mutation issue --- README.md | 6 +-- package-lock.json | 116 +------------------------------------------ package.json | 11 ++-- src/index.ts | 8 ++- tests/index.spec.tsx | 7 ++- 5 files changed, 20 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index 4d50f31..b66dfe0 100644 --- a/README.md +++ b/README.md @@ -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 } ``` diff --git a/package-lock.json b/package-lock.json index 2d066fc..e8544c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "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" }, "peerDependencies": { @@ -1686,13 +1686,6 @@ "integrity": "sha512-G+ITQPXkwTrslfG5L/BksmbLUA0M1iybEsmCWPqzSxsRRhJZimBKJkoMi8fr/CPygPTj4zO5pJH7I2/cm9M7SQ==", "dev": true }, - "node_modules/@types/json5": { - "version": "0.0.29", - "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", - "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", - "dev": true, - "optional": true - }, "node_modules/@types/mocha": { "version": "8.0.4", "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.4.tgz", @@ -5829,28 +5822,6 @@ "node": ">=8.0" } }, - "node_modules/ts-mocha": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/ts-mocha/-/ts-mocha-8.0.0.tgz", - "integrity": "sha512-Kou1yxTlubLnD5C3unlCVO7nh0HERTezjoVhVw/M5S1SqoUec0WgllQvPk3vzPMc6by8m6xD1uR1yRf8lnVUbA==", - "dev": true, - "dependencies": { - "ts-node": "7.0.1", - "tsconfig-paths": "^3.5.0" - }, - "bin": { - "ts-mocha": "bin/ts-mocha" - }, - "engines": { - "node": ">= 6.X.X" - }, - "optionalDependencies": { - "tsconfig-paths": "^3.5.0" - }, - "peerDependencies": { - "mocha": "^3.X.X || ^4.X.X || ^5.X.X || ^6.X.X || ^7.X.X || ^8.X.X" - } - }, "node_modules/ts-node": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-7.0.1.tgz", @@ -5882,42 +5853,6 @@ "node": ">=0.3.1" } }, - "node_modules/tsconfig-paths": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz", - "integrity": "sha512-dRcuzokWhajtZWkQsDVKbWyY+jgcLC5sqJhg2PSgf4ZkH2aHPvaOY8YWGhmjb68b5qqTfasSsDO9k7RUiEmZAw==", - "dev": true, - "optional": true, - "dependencies": { - "@types/json5": "^0.0.29", - "json5": "^1.0.1", - "minimist": "^1.2.0", - "strip-bom": "^3.0.0" - } - }, - "node_modules/tsconfig-paths/node_modules/json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", - "dev": true, - "optional": true, - "dependencies": { - "minimist": "^1.2.0" - }, - "bin": { - "json5": "lib/cli.js" - } - }, - "node_modules/tsconfig-paths/node_modules/strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", - "dev": true, - "optional": true, - "engines": { - "node": ">=4" - } - }, "node_modules/type-detect": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", @@ -7774,13 +7709,6 @@ "integrity": "sha512-G+ITQPXkwTrslfG5L/BksmbLUA0M1iybEsmCWPqzSxsRRhJZimBKJkoMi8fr/CPygPTj4zO5pJH7I2/cm9M7SQ==", "dev": true }, - "@types/json5": { - "version": "0.0.29", - "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", - "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", - "dev": true, - "optional": true - }, "@types/mocha": { "version": "8.0.4", "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.4.tgz", @@ -11016,16 +10944,6 @@ "is-number": "^7.0.0" } }, - "ts-mocha": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/ts-mocha/-/ts-mocha-8.0.0.tgz", - "integrity": "sha512-Kou1yxTlubLnD5C3unlCVO7nh0HERTezjoVhVw/M5S1SqoUec0WgllQvPk3vzPMc6by8m6xD1uR1yRf8lnVUbA==", - "dev": true, - "requires": { - "ts-node": "7.0.1", - "tsconfig-paths": "^3.5.0" - } - }, "ts-node": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-7.0.1.tgz", @@ -11050,38 +10968,6 @@ } } }, - "tsconfig-paths": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz", - "integrity": "sha512-dRcuzokWhajtZWkQsDVKbWyY+jgcLC5sqJhg2PSgf4ZkH2aHPvaOY8YWGhmjb68b5qqTfasSsDO9k7RUiEmZAw==", - "dev": true, - "optional": true, - "requires": { - "@types/json5": "^0.0.29", - "json5": "^1.0.1", - "minimist": "^1.2.0", - "strip-bom": "^3.0.0" - }, - "dependencies": { - "json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", - "dev": true, - "optional": true, - "requires": { - "minimist": "^1.2.0" - } - }, - "strip-bom": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", - "dev": true, - "optional": true - } - } - }, "type-detect": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", diff --git a/package.json b/package.json index ec32030..317d6a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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": [ @@ -8,7 +8,7 @@ "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" @@ -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" + ] } } diff --git a/src/index.ts b/src/index.ts index 29d186e..9fda635 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); } /** diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index f857b12..97c422f 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -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)); });