Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 16x 71x 71x 13x 58x 58x 58x 58x 10x 48x 48x 2x 46x 39x 39x 39x 39x 4x 4x 35x 4x 4x 31x 31x 31x 4x 1x 4x 27x 12x 46x 46x 6x 6x 40x 5x 5x 35x 27x 27x 8x 40x 40x 4x 36x 27x 27x 27x 27x 27x 8x 8x 19x 19x 15x 4x 1x 1x 3x 85x 85x 94x 70x 94x |
export default function ({ types: t }) {
return {
name: 'transform-remove-imports',
visitor: {
// https://babeljs.io/docs/en/babel-types#callexpression
CallExpression(path, state) {
const { node } = path;
if (node.callee.name !== 'require') {
return;
}
const argument = node.arguments[0];
const moduleId = argument.value;
const options = state.opts;
if (options.test && !testMatches(moduleId, options.test)) {
return;
}
const parentType = path.parentPath.node.type;
// In remove effects mode we should delete only requires that are
// simple expression statements
if (
options.remove === 'effects' &&
parentType !== 'ExpressionStatement'
) {
return;
}
removeRequire(path, t);
},
// https://babeljs.io/docs/en/babel-types#importdeclaration
ImportDeclaration(path, state) {
const { node } = path;
const { source } = node;
const { opts } = state;
if (opts.removeAll) {
path.remove();
return;
}
if (!opts.test) {
console.warn('transform-remove-imports: "test" option should be specified');
return;
}
/** @var {string} importName */
const importName = (source && source.value ? source.value : undefined);
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();
}
},
}
};
}
/**
* Removes a matched `require()` without leaving a syntax error behind.
* Bindings that cannot exist without an initializer are removed. A call in
* any other expression position is replaced with `undefined`.
*
* @param {import('@babel/traverse').NodePath} path
* @param {import('@babel/types')} t
*/
function removeRequire(path, t) {
const parent = path.parentPath;
if (parent.isExpressionStatement()) {
parent.remove();
return;
}
if (isRemovableListEntry(path)) {
path.remove();
return;
}
if (parent.isVariableDeclarator() && parent.node.init === path.node) {
removeRequireBinding(parent);
return;
}
path.replaceWith(t.identifier('undefined'));
}
/**
* Argument lists and array elements can shrink and still parse.
* `require("x")()` is the callee, not an argument, so it is not handled here.
*
* @param {import('@babel/traverse').NodePath} path
* @returns {Boolean}
*/
function isRemovableListEntry(path) {
const parent = path.parentPath;
if (path.listKey === 'arguments') {
return parent.isCallExpression() || parent.isNewExpression();
}
return path.listKey === 'elements' && parent.isArrayExpression();
}
/**
* @param {import('@babel/traverse').NodePath} declaratorPath
*/
function removeRequireBinding(declaratorPath) {
const declarationPath = declaratorPath.parentPath;
const id = declaratorPath.node.id;
const kind = declarationPath.isVariableDeclaration()
? declarationPath.node.kind
: null;
// `const`, `using`, and destructuring patterns are syntax errors without an
// initializer. A `var` or `let` identifier can stay uninitialized.
const needsInitializer = kind === 'const'
|| kind === 'using'
|| kind === 'await using'
|| id.type !== 'Identifier';
if (!needsInitializer) {
declaratorPath.get('init').remove();
return;
}
declaratorPath.remove();
// Babel itself drops a declaration that lost its only declarator, and an
// `export` wrapped around that declaration. Cover the case where it did not.
if (!declarationPath.node || declarationPath.node.declarations.length > 0) {
return;
}
if (declarationPath.parentPath.isExportNamedDeclaration()) {
declarationPath.parentPath.remove();
return;
}
declarationPath.remove();
}
/**
* Determines if the import matches the specified tests.
*
* @param {string} importName
* @param {RegExp|RegExp[]|string|string[]} test
* @returns {Boolean}
*/
function testMatches(importName, test) {
// Normalizing tests
const tests = Array.isArray(test) ? test : [test];
// Finding out if at least one test matches
return tests.some(regex => {
if (typeof regex === 'string') {
regex = new RegExp(regex);
}
return regex.test(importName || '');
});
}
|