forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeclaration_info.js
72 lines (68 loc) · 1.93 KB
/
declaration_info.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
/* jshint node:true, undef:true, unused:true */
var recast = require('recast');
var types = recast.types;
var n = types.namedTypes;
/**
* Represents information about a declaration that creates a local binding
* represented by `identifier`. For example, given that `declaration` is the
* following variable declaration:
*
* var a = 1;
*
* Then `identifier` references the `a` node in the variable declaration's
* first declarator. Likewise, given that `declaration` is this function
* declaration:
*
* function add(a, b) {}
*
* Then `identifier` references the `add` node, the declaration's `id`.
*
* @constructor
* @param {Node} declaration
* @param {Identifier} identifier
*/
function DeclarationInfo(declaration, identifier) {
/**
* @type {Node}
* @name DeclarationInfo#declaration
*/
this.declaration = declaration;
/**
* @type {Identifier}
* @name DeclarationInfo#identifier
*/
this.identifier = identifier;
}
/**
* Get the declaration info for the given identifier path, if the identifier is
* actually part of a declaration.
*
* @param {NodePath} identifierPath
* @return {?DeclarationInfo}
*/
DeclarationInfo.forIdentifierPath = function(identifierPath) {
if (n.VariableDeclarator.check(identifierPath.parent.node)) {
return new DeclarationInfo(
identifierPath.parent.parent.node,
identifierPath.node
);
} else if (n.ClassDeclaration.check(identifierPath.parent.node)) {
return new DeclarationInfo(
identifierPath.parent.node,
identifierPath.node
);
} else if (n.FunctionDeclaration.check(identifierPath.parent.node)) {
return new DeclarationInfo(
identifierPath.parent.node,
identifierPath.node
);
} else if (n.ImportSpecifier.check(identifierPath.parent.node)) {
return new DeclarationInfo(
identifierPath.parent.parent.node,
identifierPath.node
);
} else {
return null;
}
};
module.exports = DeclarationInfo;