From abbfc9888667c96ee411dc29399701f14bacdcc8 Mon Sep 17 00:00:00 2001 From: hint Date: Tue, 1 Jun 2021 14:11:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix=20=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=84?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=97=B6=E4=B8=8E=20@babel/plugin-transform-?= =?UTF-8?q?spread=20=E5=AD=98=E5=9C=A8=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- src/index.js | 56 ++++++++++++------- test/fixtures/assignment-statement/actual.js | 4 ++ .../fixtures/assignment-statement/expected.js | 9 +++ 4 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 test/fixtures/assignment-statement/actual.js create mode 100644 test/fixtures/assignment-statement/expected.js diff --git a/README.md b/README.md index 865759f..9074100 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,8 @@ import _get from 'lodash/get' _get() ``` -For more examples, please see the catalog test \ No newline at end of file +For more examples, please see the catalog test + +## Limitations +- Chain sequences aren’t supported. +- Destructuring assignment aren’t supported. \ No newline at end of file diff --git a/src/index.js b/src/index.js index 50b4365..52434fb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,46 @@ -import {addDefault} from '@babel/helper-module-imports'; +import {addDefault} from '@babel/helper-module-imports' const CHAIN_ERROR = 'Lodash chain sequences are not supported by babel-plugin-elfin.' +function transformMemberExp(rootPath, path, t) { + const {node, scope} = path + const objectPath = path.get('object') + if (objectPath.isMemberExpression()) { + transformMemberExp(rootPath, objectPath, t) + return + } + if (!t.isIdentifier(node.object, {name: 'glodash'})) return + const property = node.property + // 是否自定义了 glodash 变量 + if (scope.getBinding('glodash') || !property) return + // module标示 + const propertyName = property.name + // 不支持chain的链式调用 + if (propertyName === 'chain') { + throw path.buildCodeFrameError(CHAIN_ERROR) + } + // 处理参数同名的情况 + const {name} = addDefault(rootPath, `lodash/${propertyName}`, {nameHint: propertyName}) + path.replaceWith(t.identifier(name)) +} + export default function (babel) { const {types: t} = babel; return { name: 'elfin-glodash', visitor: { - MemberExpression: { - exit(path) { - if (!path.hub) return - const {file} = path.hub - const {node} = path - // 是以属性调用的形式使用 glodash.cloneDeep() - if (!t.isIdentifier(path.node.object, {name: 'glodash'})) return - const property = node.property - // 是否自定义了 glodash 变量 - if (file.scope.getBinding('glodash') || !property) return - // module标示 - const propertyName = property.name - // 不支持chain的链式调用 - if (propertyName === 'chain') { - throw path.buildCodeFrameError(CHAIN_ERROR) - } - // 处理参数同名的情况 - const {name} = addDefault(path, `lodash/${propertyName}`, {nameHint: propertyName}) - path.replaceWith(t.identifier(name)) - }, + CallExpression(path) { + const calleePath = path.get('callee') + if (calleePath.isMemberExpression()) { + transformMemberExp(path, calleePath, t) + } + }, + VariableDeclarator(path) { + const initPath = path.get('init') + if (initPath.isMemberExpression()) { + transformMemberExp(path, initPath, t) + } + // TODO: Destructuring assignment support }, } }; diff --git a/test/fixtures/assignment-statement/actual.js b/test/fixtures/assignment-statement/actual.js new file mode 100644 index 0000000..0ba616d --- /dev/null +++ b/test/fixtures/assignment-statement/actual.js @@ -0,0 +1,4 @@ +const a = {} + +const get = glodash.get +get(a, 'a') \ No newline at end of file diff --git a/test/fixtures/assignment-statement/expected.js b/test/fixtures/assignment-statement/expected.js new file mode 100644 index 0000000..f4a3f4c --- /dev/null +++ b/test/fixtures/assignment-statement/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _get2 = _interopRequireDefault(require("lodash/get")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } + +var a = {}; +var get = _get2["default"]; +get(a, 'a'); \ No newline at end of file From 8390f528aec8300ff0e13fc013f565f9041803af Mon Sep 17 00:00:00 2001 From: hint Date: Tue, 1 Jun 2021 14:15:15 +0800 Subject: [PATCH 2/3] Bump to v1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 97b293e..3f09a25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@elfin-fe/babel-plugin-glodash", - "version": "1.0.7", + "version": "1.1.0", "description": "elfin babel plugin", "repository": { "type": "git", From ab2df8f8f33dfcf8735a550495f28080ef9da51a Mon Sep 17 00:00:00 2001 From: hint Date: Tue, 1 Jun 2021 18:33:28 +0800 Subject: [PATCH 3/3] add arguments-destructuring unit test --- test/fixtures/arguments-destructuring/actual.js | 2 ++ test/fixtures/arguments-destructuring/expected.js | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 test/fixtures/arguments-destructuring/actual.js create mode 100644 test/fixtures/arguments-destructuring/expected.js diff --git a/test/fixtures/arguments-destructuring/actual.js b/test/fixtures/arguments-destructuring/actual.js new file mode 100644 index 0000000..f5cc6af --- /dev/null +++ b/test/fixtures/arguments-destructuring/actual.js @@ -0,0 +1,2 @@ +const a = [[1], [1, 2]] +glodash.union(...a) \ No newline at end of file diff --git a/test/fixtures/arguments-destructuring/expected.js b/test/fixtures/arguments-destructuring/expected.js new file mode 100644 index 0000000..9da2475 --- /dev/null +++ b/test/fixtures/arguments-destructuring/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _union2 = _interopRequireDefault(require("lodash/union")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } + +var a = [[1], [1, 2]]; + +_union2["default"].apply(void 0, a); \ No newline at end of file