From 32b762c006dea5d2fd3cdbf8b2ec0a70d24a23ad Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 20 Jun 2020 10:17:21 +0800 Subject: [PATCH] feat: Use the `remove: effect` option with the `test` options. #3 --- src/index.js | 22 +++++++++++++--------- test/index.test.js | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 18cb6a12..3909c97c 100644 --- a/src/index.js +++ b/src/index.js @@ -10,13 +10,6 @@ export default function () { const { source } = node; const { opts = {} } = state; - // https://github.com/uiwjs/babel-plugin-transform-remove-imports/issues/3 - if (opts.remove === 'effects') { - if (node.specifiers && node.specifiers.length === 0) { - path.remove(); - } - } - if (opts.removeAll) { path.remove(); return; @@ -29,7 +22,17 @@ export default function () { /** @var {string} importName */ const importName = (source && source.value ? source.value : undefined); - if (importName && testMatches(importName, opts.test)) { + const isMatch = testMatches(importName, opts.test); + + // https://github.com/uiwjs/babel-plugin-transform-remove-imports/issues/3 + if (opts.remove === 'effects') { + if (node.specifiers && node.specifiers.length === 0 && importName && isMatch) { + path.remove(); + } + return; + } + + if (importName && isMatch) { path.remove(); } @@ -44,6 +47,7 @@ export default function () { * * @param {string} importName * @param {RegExp|RegExp[]|string|string[]} test + * @returns {Boolean} */ function testMatches(importName, test) { @@ -55,7 +59,7 @@ function testMatches(importName, test) { if (typeof regex === "string") { regex = new RegExp(regex); } - return regex.test(importName); + return regex.test(importName || ''); }); } diff --git a/test/index.test.js b/test/index.test.js index 1e52eccc..ab4ae53c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -28,7 +28,7 @@ fixtures.map((caseName) => { } else if (caseName === 'remove-effects-import') { pluginBaseOpts.presets = [["@babel/preset-env", { "modules": false }]]; pluginBaseOpts.plugins = [ - [plugin, { remove: 'effects' }] + [plugin, { remove: 'effects', test: /^uiw/ }] ] } else if (caseName === 'options-empty') { pluginBaseOpts.presets = [["@babel/preset-env", { "modules": false }]];