/
inspectGlobals.js
149 lines (132 loc) · 3.48 KB
/
inspectGlobals.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const test = require('tape')
const acornGlobals = require('acorn-globals')
const inspectGlobals = require('../src/inspectGlobals')
test('inspectGlobals - basic', (t) => {
const globals = inspectGlobalsTest(`
atob
`)
t.deepEqual(globals, ['atob'])
t.end()
})
test('inspectGlobals - picking out properties on window', (t) => {
const globals = inspectGlobalsTest(`
window.location
`)
t.deepEqual(globals, ['location'])
t.end()
})
test('inspectGlobals - picking out properties on global', (t) => {
const globals = inspectGlobalsTest(`
global.localStorage
`)
t.deepEqual(globals, ['localStorage'])
t.end()
})
test('inspectGlobals - picking out properties on self', (t) => {
const globals = inspectGlobalsTest(`
self.WebSocket
`)
t.deepEqual(globals, ['WebSocket'])
t.end()
})
test('inspectGlobals - not picking up properties from non-global self', (t) => {
const globals = inspectGlobalsTest(`
var self = {}
self.localStorage
`)
t.deepEqual(globals, [])
t.end()
})
test('inspectGlobals - not picking up programmatic property lookups', (t) => {
const globals = inspectGlobalsTest(`
const key = 'hello'
window[key]
`)
t.deepEqual(globals, ['window'])
t.end()
})
test('inspectGlobals - picking up programmatic property lookups + explicit', (t) => {
const globals = inspectGlobalsTest(`
const key = 'hello'
window[key]
window.location
`)
t.deepEqual(globals, ['location', 'window'])
t.end()
})
test('inspectGlobals - picking up userspace global', (t) => {
const globals = inspectGlobalsTest(`
xyz = true
`)
t.deepEqual(globals, ['xyz'])
t.end()
})
test('inspectGlobals - picking up userspace global on globalRef', (t) => {
const globals = inspectGlobalsTest(`
global.xyz = true
`)
t.deepEqual(globals, ['xyz'])
t.end()
})
test('inspectGlobals - not picking up js language features', (t) => {
const globals = inspectGlobalsTest(`
Object
window.Object
`)
t.deepEqual(globals, [])
t.end()
})
test('inspectGlobals - not picking up global ref', (t) => {
const globals = inspectGlobalsTest(`
const key = 'hello'
global
global[key]
`)
t.deepEqual(globals, [])
t.end()
})
test('inspectGlobals - ignore globalRef without property lookup', (t) => {
const globals = inspectGlobalsTest(`
typeof window === undefined
`)
t.deepEqual(globals, [])
t.end()
})
test('inspectGlobals - get granular platform api', (t) => {
const globals = inspectGlobalsTest(`
document.createElement('blink')
location.href
navigator.userAgent
`)
t.deepEqual(globals, ['document.createElement', 'location.href', 'navigator.userAgent'])
t.end()
})
test('inspectGlobals - get granular platform api when nested under global', (t) => {
const globals = inspectGlobalsTest(`
window.location.href
`)
t.deepEqual(globals, ['location.href'])
t.end()
})
test('inspectGlobals - take platform api, up to computed', (t) => {
const globals = inspectGlobalsTest(`
const key = 'hello'
document.body.children[key]
window.location.href[key]
`)
t.deepEqual(globals, ['document.body.children', 'location.href'])
t.end()
})
test('inspectGlobals - raise globals to highest used', (t) => {
const globals = inspectGlobalsTest(`
location.href
location
document.body.children
document.body.children.indexOf
`)
t.deepEqual(globals, ['document.body.children', 'location'])
t.end()
})
function inspectGlobalsTest (code) {
return inspectGlobals(acornGlobals.parse(code))
}