-
Notifications
You must be signed in to change notification settings - Fork 2
/
unused_import.js
103 lines (89 loc) · 2.33 KB
/
unused_import.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
var readline = require('readline'),
fs = require('fs');
var UnUsedImport = function(filePath) {
this.filePath = filePath
var index = filePath.indexOf('.');
this.fileName = filePath.substring(0, index)
this.unUsedClassArr = []
}
UnUsedImport.prototype = {
getUsedClass: function(cb) {
var self = this
var rl = readline.createInterface({
input: fs.createReadStream(self.filePath),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
if (line.indexOf('#') > -1) {
var isFind = self._findClassName(line, '#import ');
if (!isFind) {
isFind = self._findClassName(line, '#include ');
}
}
});
rl.on('close', function(line) {
cb(self)
});
},
getUnusedImport: function(cb) {
var self = this
var rl = readline.createInterface({
input: fs.createReadStream(self.filePath),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
//console.log("line " + line.indexOf('#') );
isImportLine = false;
if (self.unUsedClassArr.length > 0) {
self._findUnused(line);
}
});
rl.on('close', function(line) {
cb(self)
});
},
_findClassName: function(line, marcoKey) {
var index = line.indexOf(marcoKey);
//console.log("marcoKey:" + marcoKey)
if (index > -1) {
var className = line.substring(marcoKey.length + 1, line.length)
index = className.indexOf('.h');
className = className.substring(0, index)
// console.log("className:" + className)
if (className != this.fileName) {
this.unUsedClassArr.push(className)
}
return true;
} else {
return false;
}
},
_findUnused: function(line) {
var tempArr = this.unUsedClassArr;
for (var i = 0; i < this.unUsedClassArr.length; i++) {
var className = this.unUsedClassArr[i]
var toFindStr = className + " ";
if (line.indexOf(toFindStr) > -1) {
tempArr.splice(i, 1)
}
}
this.unUsedClassArr = tempArr;
},
}
if (!process.argv[2]) {
console.log('usage: node unused_import.js filepath')
return
}
var unUsedImport = new UnUsedImport(process.argv[2])
unUsedImport.getUsedClass(function() {
//console.log("unused:" + unUsedImport.unUsedClassArr)
unUsedImport.getUnusedImport(function() {
var ret = unUsedImport.unUsedClassArr;
//console.log ("ret:" + ret);
for (var i in ret) {
console.log(ret[i] + '.h' + '\t')
}
});
});