Skip to content

Commit

Permalink
feat: Use the remove: effect option with the test options. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 20, 2020
1 parent 9a7d357 commit 32b762c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -44,6 +47,7 @@ export default function () {
*
* @param {string} importName
* @param {RegExp|RegExp[]|string|string[]} test
* @returns {Boolean}
*/
function testMatches(importName, test) {

Expand All @@ -55,7 +59,7 @@ function testMatches(importName, test) {
if (typeof regex === "string") {
regex = new RegExp(regex);
}
return regex.test(importName);
return regex.test(importName || '');
});

}
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]];
Expand Down

0 comments on commit 32b762c

Please sign in to comment.