/
findGlobals.js
121 lines (108 loc) · 3.03 KB
/
findGlobals.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
'use strict'
const { default: traverse } = require('@babel/traverse')
const { isInFunctionDeclaration, isMemberLikeExpression } = require('./util')
/**
* Types which are not references to globals
*/
const nonReferenceIdentifiers = new Set([
'ArrayPatten',
'BreakStatement',
'CatchClause',
'ClassMethod',
'ContinueStatement',
'ExportDefaultSpecifier',
'ExportSpecifier',
'FunctionDeclaration',
'FunctionExpression',
'ImportDefaultSpecifier',
'ImportNamespaceSpecifier',
'ImportSpecifier',
'LabeledStatement',
'MetaProperty',
'RestElement',
])
module.exports = { findGlobals }
/**
* @typedef {import('@babel/traverse').NodePath<
* import('@babel/types').Identifier | import('@babel/types').ThisExpression
* >} IdentifierOrThisExpressionNodePath
*/
/**
* @param {import('@babel/types').Node} ast
* @returns {Map<string, IdentifierOrThisExpressionNodePath[]>}
*/
function findGlobals(ast) {
/** @type {ReturnType<typeof findGlobals>} */
const globals = new Map()
traverse(ast, {
// ReferencedIdentifier
Identifier: (path) => {
// skip if not being used as reference
const parentType = path.parent.type
if (nonReferenceIdentifiers.has(parentType)) {
return
}
if (parentType === 'VariableDeclarator' && path.parent.id === path.node) {
return
}
// skip if this is the key side of a member expression
if (
isMemberLikeExpression(path.parent) &&
path.parent.property === path.node
) {
return
}
// skip if this is the key side of an object pattern
if (parentType === 'ObjectProperty' && path.parent.key === path.node) {
return
}
if (parentType === 'ObjectMethod' && path.parent.key === path.node) {
return
}
// skip if it refers to an existing variable
const name = path.node.name
if (path.scope.hasBinding(name, true)) {
return
}
// check if arguments refers to a var, this shouldn't happen in strict mode
if (name === 'arguments') {
if (isInFunctionDeclaration(path)) {
return
}
}
// private names are not globals
if (parentType === 'PrivateName' && path.parent.id === path.node) {
return
}
// class props are not globals
if (parentType === 'ClassProperty' && path.parent.key === path.node) {
return
}
// save global
saveGlobal(path)
},
ThisExpression: (path) => {
if (isInFunctionDeclaration(path)) {
return
}
saveGlobal(path, 'this')
},
})
return globals
/**
* @param {IdentifierOrThisExpressionNodePath} path
* @param {string} [name]
*/
// @ts-ignore - FIXME needs logic changes for type safety
function saveGlobal(path, name = path.node.name) {
// init entry if needed
if (!globals.has(name)) {
globals.set(name, [])
}
// append ref
const refsForName = /** @type {IdentifierOrThisExpressionNodePath[]} */ (
globals.get(name)
)
refsForName.push(path)
}
}