forked from othiym23/jstags
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
151 lines (128 loc) · 3.75 KB
/
index.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
'use strict';
var fs = require('fs')
, esprima = require('esprima')
, visit = require('estraverse').traverse
;
function CTagsEntry (name, file, address, type, lineno) {
this.name = name;
this.file = file;
this.address = address;
this.type = type;
this.lineno = lineno;
}
CTagsEntry.prototype.toString = function () {
return [
this.name,
this.file,
this.address,
this.type,
'lineno:' + this.lineno
].join('\t');
}
CTagsEntry.prototype.compare = function (a, b) {
if (a == b) return 0;
if (a < b) return -1;
return 1;
}
var tags = [];
function requireTag(filename, node) {
var name = node.id.name;
var startLine = node.loc.start.line - 1;
var pattern = startLine + '/\\<' + node.id.name + '\\>/;"';
return new CTagsEntry(
name,
filename,
pattern,
'i',
(startLine + 1)
);
}
function isRequire(node) {
return node &&
node.type === 'CallExpression' &&
node.callee &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require';
}
function isObjectExpression(node) {
return node &&
node.type === 'ObjectExpression';
}
function objectFromMemberExpression(node) {
var child = node.init;
while (child.type === 'MemberExpression' && child.object) {
child = child.object;
}
return child;
}
function methodTags(filename, node, object) {
object.properties.forEach(function (property) {
if (property.value.type === 'FunctionExpression') {
var name = node.id.name + '.' + property.key.name;
var startLine = property.key.loc.start.line - 1;
var pattern = startLine + '/\\<' + property.key.name + '\\>/;"';
tags.push(new CTagsEntry(name, filename, pattern, 'm', 'lineno:' + (startLine + 1)));
}
});
}
function functionTag(filename, node) {
var name = node.id.name;
var startLine = node.loc.start.line - 1;
var pattern = startLine + '/\\<' + node.id.name + '\\>/;"';
var type = (name[0] === name[0].toUpperCase()) ? 'c' : 'f';
return new CTagsEntry(name, filename, pattern, type, 'lineno:' + (startLine + 1));
}
var nodeVisitors = {
'VariableDeclarator' : function (filename, node) {
if (!node.init) return;
var target;
if (isRequire(node.init)) {
tags.push(requireTag(filename, node));
}
else if (node.init.type === 'MemberExpression') {
target = objectFromMemberExpression(node);
if (isRequire(target)) {
tags.push(requireTag(filename, node));
}
}
else if (node.init.type === 'AssignmentExpression') {
target = node.init.right;
if (isObjectExpression(target)) {
methodTags(filename, node, target);
}
}
},
'FunctionDeclaration' : function (filename, node) {
tags.push(functionTag(filename, node));
}
};
function visitor (filename) {
return {
enter : function enter(node, stack) {
var handler = nodeVisitors[node.type];
if (handler) handler(filename, node);
// else console.dir(node);
}
};
}
function header() {
return "!_TAG_FILE_FORMAT 2 /extended format/\n" +
"!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/\n" +
"!_TAG_PROGRAM_AUTHOR Forrest L Norvell /[email protected]/\n" +
"!_TAG_PROGRAM_NAME jstags //\n" +
"!_TAG_PROGRAM_URL http://github.com/othiym23/jstags /github repository/\n" +
"!_TAG_PROGRAM_VERSION 0.0.0 //";
}
console.log(header());
module.exports = function tagFiles(filenames) {
filenames.forEach(function (filename) {
fs.readFile(filename, function (err, source) {
if (err) return console.error("Error reading helper: %s", err);
var ast = esprima.parse(source, {loc : true});
visit(ast, visitor(filename));
tags.sort(CTagsEntry.compare).forEach(function (tag) {
console.log(tag.toString());
});
});
});
};