UNPKG

2.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.babelRemoveFunction = void 0;
4function babelRemoveFunction(options) {
5 if (!options || typeof options.name !== 'string') {
6 throw new Error('Please provide a function name in the options.');
7 }
8 return function babelRemoveFunctionInstance({ types: t }) {
9 return {
10 visitor: {
11 // Remove any definition of the function
12 Function(path) {
13 if (path.node.id && path.node.id.name === 'debugClassName') {
14 path.remove();
15 }
16 },
17 // Remove any import of the function
18 ImportDeclaration(path) {
19 const hasDebugName = path.node.specifiers.findIndex((s) => { var _a; return ((_a = s.imported) === null || _a === void 0 ? void 0 : _a.name) === 'debugClassName'; });
20 if (hasDebugName >= 0) {
21 if (path.node.specifiers.length === 1) {
22 // If debugName is the only imported, just remove the statement completely
23 path.remove();
24 }
25 else {
26 // Just remove the debugName import
27 path.node.specifiers.splice(hasDebugName, 1);
28 }
29 }
30 },
31 // Remove any call to the function. If used inside style, we drop the call entirely, otherwise we replace with a empty string
32 CallExpression(path) {
33 if (path.node.callee.name === 'debugClassName') {
34 if (t.isCallExpression(path.parent) && path.parent.callee.name === 'style') {
35 path.parent.arguments.shift();
36 }
37 else {
38 path.replaceWith(t.StringLiteral(''));
39 }
40 }
41 }
42 }
43 };
44 };
45}
46exports.babelRemoveFunction = babelRemoveFunction;