source: imaps-frontend/node_modules/babel-plugin-transform-remove-console/lib/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[79a0317]1"use strict";
2
3module.exports = function ({
4 types: t
5}) {
6 return {
7 name: "transform-remove-console",
8 visitor: {
9 CallExpression(path, state) {
10 const callee = path.get("callee");
11 if (!callee.isMemberExpression()) return;
12
13 if (isIncludedConsole(callee, state.opts.exclude)) {
14 // console.log()
15 if (path.parentPath.isExpressionStatement()) {
16 path.remove();
17 } else {
18 path.replaceWith(createVoid0());
19 }
20 } else if (isIncludedConsoleBind(callee, state.opts.exclude)) {
21 // console.log.bind()
22 path.replaceWith(createNoop());
23 }
24 },
25
26 MemberExpression: {
27 exit(path, state) {
28 if (isIncludedConsole(path, state.opts.exclude) && !path.parentPath.isMemberExpression()) {
29 if (path.parentPath.isAssignmentExpression() && path.parentKey === "left") {
30 path.parentPath.get("right").replaceWith(createNoop());
31 } else {
32 path.replaceWith(createNoop());
33 }
34 }
35 }
36
37 }
38 }
39 };
40
41 function isGlobalConsoleId(id) {
42 const name = "console";
43 return id.isIdentifier({
44 name
45 }) && !id.scope.getBinding(name) && id.scope.hasGlobal(name);
46 }
47
48 function isExcluded(property, excludeArray) {
49 return excludeArray && excludeArray.some(name => property.isIdentifier({
50 name
51 }));
52 }
53
54 function isIncludedConsole(memberExpr, excludeArray) {
55 const object = memberExpr.get("object");
56 const property = memberExpr.get("property");
57 if (isExcluded(property, excludeArray)) return false;
58 if (isGlobalConsoleId(object)) return true;
59 return isGlobalConsoleId(object.get("object")) && (property.isIdentifier({
60 name: "call"
61 }) || property.isIdentifier({
62 name: "apply"
63 }));
64 }
65
66 function isIncludedConsoleBind(memberExpr, excludeArray) {
67 const object = memberExpr.get("object");
68 if (!object.isMemberExpression()) return false;
69 if (isExcluded(object.get("property"), excludeArray)) return false;
70 return isGlobalConsoleId(object.get("object")) && memberExpr.get("property").isIdentifier({
71 name: "bind"
72 });
73 }
74
75 function createNoop() {
76 return t.functionExpression(null, [], t.blockStatement([]));
77 }
78
79 function createVoid0() {
80 return t.unaryExpression("void", t.numericLiteral(0));
81 }
82};
Note: See TracBrowser for help on using the repository browser.