-
Notifications
You must be signed in to change notification settings - Fork 481
/
finders.js
51 lines (43 loc) · 1.22 KB
/
finders.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
'use strict';
var t = require('babel-types');
/**
* Try to find the part of JavaScript a comment is referring to, by
* looking at the syntax tree closest to that comment.
*
* @param {Object} path abstract syntax tree path
* @returns {?Object} ast path, if one is found.
* @private
*/
function findTarget(path) {
if (!path) {
return path;
}
if (t.isExportDefaultDeclaration(path) ||
t.isExportNamedDeclaration(path) && path.has('declaration')) {
path = path.get('declaration');
}
// var x = init;
if (t.isVariableDeclaration(path)) {
path = path.get('declarations')[0];
// foo.x = TARGET
} else if (t.isExpressionStatement(path)) {
path = path.get('expression').get('right');
}
return path.node && path;
}
/**
* Try to find a JavaScript class that this comment refers to,
* whether an expression in an assignment, or a declaration.
*
* @param {Object} path abstract syntax tree path
* @returns {?Object} ast path, if one is found.
* @private
*/
function findClass(path) {
var target = findTarget(path);
if (target && (target.isClassDeclaration() || target.isClassExpression())) {
return target;
}
}
module.exports.findTarget = findTarget;
module.exports.findClass = findClass;