This repository was archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (50 loc) · 1.27 KB
/
index.js
File metadata and controls
66 lines (50 loc) · 1.27 KB
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
'use strict';
var wrapListener = require('./wrap-listener');
function listener(path, file, opts) {
(path.isLiteral() ? addString : addExpression)(path.node, file, opts);
}
function addString(node, file, opts) {
var val = attachNodes(opts) ? node : node.value;
requireMetadata(file).strings.push(val);
}
function addExpression(node, file, opts) {
var val;
if (attachNodes(opts)) {
val = node;
} else {
val = {start: node.start, end: node.end};
val.loc = {
start: copyLoc(node.loc.start),
end: copyLoc(node.loc.end)
};
}
if (attachExpressionSource(opts)) {
val.code = file.code.slice(val.start, val.end);
}
requireMetadata(file).expressions.push(val);
return val;
}
function copyLoc(loc) {
return loc && {line: loc.line, column: loc.column};
}
function requireMetadata(file) {
var metadata = file.metadata;
if (!metadata.requires) {
metadata.requires = {
strings: [],
expressions: []
};
}
return metadata.requires;
}
// OPTION EXTRACTION:
function attachExpressionSource(opts) {
return Boolean(opts && opts.source);
}
function attachNodes(opts) {
return Boolean(opts && opts.nodes);
}
module.exports = wrapListener(listener, 'detective');
module.exports.metadata = function extractMetadataFromResult(result) {
return result.metadata.requires;
};