-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 1.4 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
// @flow
'use strict';
const {getIdentifierKind} = require('babel-identifiers');
const {findFlowBinding} = require('babel-flow-scope');
/*::
type Path = {
parentPath: Path,
[key: string]: any,
};
*/
function isImport(path) {
return (
path.parentPath &&
path.parentPath.parentPath &&
path.parentPath.parentPath.isImportDeclaration()
);
}
function getImportKind(path) {
return (
path.parentPath.node.importKind ||
path.parentPath.parent.importKind ||
'value'
);
}
function isTypeImport(path) {
return (
isImport(path) &&
getImportKind(path) !== 'value'
);
}
function isTypeOfValue(path) {
return path.parentPath.parentPath.isTypeofTypeAnnotation();
}
function isReference(path) {
return getIdentifierKind(path) === 'reference';
}
function isFlowReference(path) {
let binding = findFlowBinding(path, path.node.name);
return !!binding && isFlowIdentifier(binding.path);
}
function isFlowIdentifier(path /*: Path */) /*: boolean */ {
if (path.isTypeParameter()) {
return true;
} else if (!path.isIdentifier()) {
return false;
} else if (isTypeImport(path)) {
return true;
} else if (isTypeOfValue(path)) {
return false;
} else if (path.parentPath.isFlow()) {
if (isReference(path)) {
return isFlowReference(path);
} else {
return true;
}
} else {
return false;
}
}
exports.isFlowIdentifier = isFlowIdentifier;