-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.js
204 lines (169 loc) · 5.03 KB
/
patterns.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
const esprima = require("esprima");
const isObjectLiteral = node => node.type === "ObjectExpression";
function extractVersionProperty(properties) {
let result;
properties.forEach(prop => {
if (prop.key.value === "version" || prop.key.name === "version") {
result = prop.value;
return false;
}
return true;
});
return result;
}
function unparseMemberExpression(node) {
const { object } = node;
const unparsedObject = (object.type === "MemberExpression") ?
unparseMemberExpression(object) : object.name;
return `${unparsedObject}.${node.property.name}`;
}
function versionFromNode(source, node) {
let version = node.value;
if (version === undefined) {
// We have a node, but it does not have a value. We're probably dealing with
// an expression of some sort, extract it from the data. This allows
// providing a more useful error message.
version = source.substring(node.range[0], node.range[1]);
}
return version;
}
const patterns = [];
patterns.push((source, node) => {
// matching return { [...], version: "...", [...] }
if (node.type === "ReturnStatement" && isObjectLiteral(node.argument)) {
return extractVersionProperty(node.argument.properties);
}
return undefined;
});
patterns.push((source, node) => {
// matching export ...
if (node.type === "ExportNamedDeclaration") {
const { declarations } = node.declaration;
for (let i = 0; i < declarations.length; ++i) {
const declaration = declarations[i];
if (declaration.id.name === "version") {
return declaration.init;
}
}
}
return undefined;
});
patterns.push((source, node) => {
// Matches assignments to `.version` (e.g. `exports.version = "1.2.3"`) or
// assignment that assign literals containing the property "version"
// (e.g. `module.exports = { version: 1.2.3}`).
let result;
// If we got *a* result but it is in an unexpected location, we want to warn
// the user.
let warn = true;
if (node.left &&
node.left.type === "MemberExpression" &&
node.left.property.name === "version") {
result = node.right;
const unparsed = unparseMemberExpression(node.left);
warn = ["exports.version", "module.exports.version"]
.indexOf(unparsed) === -1;
}
else if (node.value && isObjectLiteral(node.value)) {
result = extractVersionProperty(node.value.properties);
}
else if (node.operator === "=" && isObjectLiteral(node.right)) {
result = extractVersionProperty(node.right.properties);
if (node.left.type === "MemberExpression") {
const unparsed = unparseMemberExpression(node.left);
warn = ["exports", "module.exports"].indexOf(unparsed) === -1;
}
}
if (result && warn) {
const version = versionFromNode(source, result);
// eslint-disable-next-line no-console
console.warn(`WARNING: found version number ${version}, ` +
"but not directly assigned to exports or module.exports.");
}
return result;
});
// matching simple object literals
// useful for JSON
patterns.push(
(source, node) => (isObjectLiteral(node) ?
extractVersionProperty(node.properties) :
undefined));
patterns.match = function match(source, node) {
let result;
patterns.find(pattern => {
result = pattern(source, node);
return result;
});
return result;
};
function traverse(source, node) {
let result = patterns.match(source, node);
if (result) {
// if a pattern matched, we return what we found and exit
return result;
}
// otherwise, we go deeper in the tree
if (node.length || (node.body && node.body.length)) {
if (node.body && node.body.length) {
node = node.body;
}
node.find(n => {
result = traverse(source, n);
return result;
});
return result;
}
if (node.arguments && node.arguments.length) {
node.arguments.find(n => {
result = traverse(source, n);
return result;
});
return result;
}
if (node.body) {
return traverse(source, node.body);
}
if (node.expression) {
return traverse(source, node.expression);
}
if (node.right) {
return traverse(source, node.right);
}
if (node.callee) {
return traverse(source, node.callee);
}
return undefined;
}
exports.parse = function parse(data) {
const options = {
loc: true,
range: true,
sourceType: "module",
};
let ast;
try {
ast = esprima.parse(data, options);
}
catch (ex) {
//
// When we parse with `sourceType: "module"`, it is possible that ES5 code
// will cause errors due to the use of words that are considered to be
// future reserved words (e.g. `var await = 1`, `await` is a future reserved
// word.
//
// These are effective only when parsing "module" source, so try again with
// "script".
//
options.sourceType = "script";
ast = esprima.parse(data, options);
}
const node = traverse(data, ast);
if (!node) {
return undefined;
}
return {
version: versionFromNode(data, node),
line: node.loc.end.line,
};
};