-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-plugin-unused-dependencies.js
170 lines (148 loc) · 5.68 KB
/
babel-plugin-unused-dependencies.js
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
// https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#toc-basics
//
// https://github.com/babel/babel/search?utf8=%E2%9C%93&q=codeFrame&type=Code
var util = require('util');
var codeFrame = require('babel-code-frame');
module.exports = function(babel) {
var t = babel.types; // docs: https://github.com/babel/babel/tree/master/packages/babel-types#babel-types
var get_service_expr_names = "sap.bi.framework.getService".split(".");
var jQuery_sap_require_expr_names = "jQuery.sap.require".split(".");
var $_sap_require_expr_names = "$.sap.require".split("."); // TODO: unifiy
return {
pre: function(state) {
this.allUi5Requires = [];
},
post: function(state) {
for (var i = 0, len = this.allUi5Requires.length; i < len; i++) {
var ui5Require = this.allUi5Requires[i];
if (!this.allUi5Requires[i].referenced) {
printCodeFrame("Warning! Unused dependency:", ui5Require.path);
}
}
},
visitor: {
VariableDeclarator: function(path) {
var node = path.node;
if (t.isCallExpression(node.init)) {
var callee = node.init.callee;
if (t.isIdentifier(callee, {name: "require"}) ||
isDotMemberExpr(callee, get_service_expr_names)) {
// console.log(path.node.id.name, "\n\n", util.inspect(path.scope.getBinding(path.node.id.name), false, 2, true));
if (!path.scope.getBinding(node.id.name).referenced) {
// throw path.buildCodeFrameError("unused dependency!");
printCodeFrame("Warning! Unused dependency:", path);
}
}
}
},
CallExpression: function(path) {
var node = path.node;
if (!isDotMemberExpr(node.callee, jQuery_sap_require_expr_names) &&
!isDotMemberExpr(node.callee, $_sap_require_expr_names)) {
return;
}
var scope = path.scope;
scope.ui5Requires = scope.ui5Requires || [];
var ui5Require = {pkg: node.arguments[0].value.split("."), referenced: false, path: path};
scope.ui5Requires.push(ui5Require);
this.allUi5Requires.push(ui5Require);
//console.log(util.inspect(path, false, 1, true));
},
MemberExpression: function(path) {
if (t.isMemberExpression(path.parent)) {
return;
}
var pkg = memberExprToTokenArr(path.node);
if (pkg) {
var foundImport = someUI5RequireInScope(path, function(ui5Require) {
if (arrayStartsWith(pkg, ui5Require.pkg)) {
ui5Require.referenced = true;
return true;
}
});
// TODO: we could also catch missing imports... more work necessary here to whitelist certain stuff
if (!foundImport && pkg[0] === "sap" && pkg[1] === "fpa") {
printCodeFrame("Warning! Missing Import:", path);
}
}
}
}
};
function printCodeFrame(message, path) {
var startLoc = path.node.loc.start;
console.log(message);
console.log(codeFrame(path.hub.file.code,
startLoc.line, startLoc.column + 1, { highlightCode: true }));
}
function someUI5RequireInScope(path, callback) {
var scope = path.scope;
do {
if (scope.ui5Requires) {
for (var i = 0, len = scope.ui5Requires.length; i < len; ++i) {
if (callback(scope.ui5Requires[i]) === true) {
return true;
}
}
}
scope = scope.parent;
} while (scope);
return false;
}
function isDotMemberExpr(expr, exprNames) {
var subExpr = expr;
var len = exprNames.length;
for (var i = len - 1; i > 0; --i) {
if (!(t.isMemberExpression(subExpr) &&
t.isIdentifier(subExpr.property) &&
subExpr.property.name === exprNames[i]
)) {
return false;
}
subExpr = subExpr.object;
}
if (t.isIdentifier(subExpr) && subExpr.name === exprNames[0]) {
return true;
}
return false;
}
function arrayStartsWith(arr, subArr) {
for (var i = 0, len = subArr.length; i < len; ++i) {
if (arr[i] !== subArr[i]) {
return false;
}
}
return true;
}
function memberExprToTokenArr(memberExpr) {
var arr = [];
var subExpr = memberExpr;
do {
if (!t.isIdentifier(subExpr.property)) {
return;
}
arr.unshift(subExpr.property.name);
subExpr = subExpr.object;
} while (t.isMemberExpression(subExpr));
if (!t.isIdentifier(subExpr)) {
return;
}
arr.unshift(subExpr.name);
return arr;
}
function memberExprToDotStr(memberExpr) {
var str = "";
var subExpr = memberExpr;
do {
if (!t.isIdentifier(subExpr.property)) {
return;
}
str = str.length === 0 ? subExpr.property.name : subExpr.property.name + "." + str;
subExpr = subExpr.object;
} while (t.isMemberExpression(subExpr));
if (!t.isIdentifier(subExpr)) {
return;
}
str = str.length === 0 ? subExpr.name : subExpr.name + "." + str;
return str;
}
};