-
Notifications
You must be signed in to change notification settings - Fork 65
/
findGlobals.spec.js
191 lines (184 loc) · 6.15 KB
/
findGlobals.spec.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict'
const fs = require('fs')
const path = require('path')
const test = require('ava')
const { parse } = require('../src/index')
const { findGlobals } = require('../src/findGlobals')
function detect(code) {
const ast = parse(code)
const globals = findGlobals(ast)
const globalNames = [...globals.keys()].sort()
return globalNames
}
function read(file) {
return fs.readFileSync(path.resolve(__dirname + '/fixtures/', file), 'utf8')
}
// function buildQualifiedVariableName(node) {
// const namePath = [];
// for (const i = node.parents.length - 1; i >= 0; i--) {
// const parent = node.parents[i];
// if (parent.type === "Identifier") {
// namePath.push(parent.name);
// } else if (parent.type === "MemberExpression") {
// namePath.push(parent.property.name || parent.property.value);
// } else if (parent.type === "ThisExpression") {
// namePath.push("this");
// } else {
// break;
// }
// }
// return namePath.join('.');
// }
test('object method', (t) => {
t.deepEqual(detect('const x = { xyz () {} }'), [])
})
test('arguments keyword', (t) => {
t.deepEqual(detect('function a () { arguments }'), [])
})
test('arguments keyword 2', (t) => {
t.deepEqual(detect('function a () { x(arguments) }'), ['x'])
})
test('arguments keyword 3', (t) => {
t.deepEqual(detect('const x = function () { arguments }'), [])
})
test('arguments keyword 4', (t) => {
t.deepEqual(detect('async function a () { arguments }'), [])
})
test('arguments keyword 5', (t) => {
t.deepEqual(detect('const x = async function () { arguments }'), [])
})
test('argument.js - parameters from inline arguments', (t) => {
t.deepEqual(detect(read('argument.js')), [])
})
test('arrow_functions.js - arguments of arrow functions are not globals', (t) => {
t.deepEqual(
detect(read('arrow_functions.js')),
['z', 'b', 'c', 'arguments'].sort()
)
})
test('assign_implicit.js - assign from an implicit global', (t) => {
t.deepEqual(detect(read('assign_implicit.js')), ['bar'])
})
test('catch-pattern.js - pattern in catch', (t) => {
t.deepEqual(detect(read('catch-pattern.js')), [])
})
test('class.js - ES2015 classes', (t) => {
t.deepEqual(
detect(read('class.js')),
['G', 'OtherClass_', 'SuperClass', 'this'].sort()
)
})
test('class-expression.js - class as expression', (t) => {
t.deepEqual(detect(read('class-expression.js')), [])
})
test('default-argument.js - ES2015 default argument', (t) => {
t.deepEqual(detect(read('default-argument.js')), ['c', 'h', 'j', 'k'])
})
test('destructuring-rest.js - ES2015 destructuring rest', (t) => {
t.deepEqual(detect(read('destructuring-rest.js')), [])
})
test('destructuring.js - ES2015 variable destructuring', (t) => {
t.deepEqual(detect(read('destructuring.js')), ['g'])
})
test('detect.js - check locals and globals', (t) => {
t.deepEqual(
detect(read('detect.js')),
[
'w',
'foo',
'process',
'console',
'AAA',
'BBB',
'CCC',
'xyz',
'ZZZ',
'BLARG',
'RAWR',
].sort()
)
})
// test('detect.js - check variable names', (t) => {
// t.deepEqual(
// detect(read('detect.js')).map(function (node) { return '[' + node.nodes.map(buildQualifiedVariableName) + ']'; }),
// [
// '[w.foo,w]',
// '[foo]',
// '[process.nextTick]',
// '[console.log,console.log]',
// '[AAA.aaa]',
// '[BBB.bbb]',
// '[CCC.x]',
// '[xyz]',
// '[ZZZ,ZZZ.foo]',
// '[BLARG]',
// '[RAWR,RAWR.foo]'
// ].sort()
// );
// // });
// test('export.js - Anything that has been imported is not a global', (t) => {
// t.deepEqual(detect(read('export.js')), ['baz']);
// // });
// test('export-default-anonymous-class.js - export anonymous class as default', (t) => {
// t.deepEqual(detect(read('export-default-anonymous-class.js')), []);
// // });
// test('export-default-anonymous-function.js - export anonymous function as default', (t) => {
// t.deepEqual(detect(read('export-default-anonymous-function.js')), []);
// // });
// test('import.js - Anything that has been imported is not a global', (t) => {
// t.deepEqual(detect(read('import.js')), ['whatever']);
// // });
test('labels.js - labels for while loops are not globals', (t) => {
t.deepEqual(detect(read('labels.js')), [])
})
// test('multiple-exports.js - multiple-exports', (t) => {
// t.deepEqual(detect(read('multiple-exports.js')), ['bar', 'exports']);
// // });
test('named_arg.js - named argument / parameter', (t) => {
t.deepEqual(detect(read('named_arg.js')), [])
})
test('names-in-object-prototype.js - check names in object prototype', (t) => {
t.deepEqual(detect(read('names-in-object-prototype.js')).sort(), [
'__proto__',
'constructor',
'hasOwnProperty',
])
})
test('obj.js - globals on the right-hand of a colon in an object literal', (t) => {
t.deepEqual(detect(read('obj.js')), ['bar', 'module'])
})
// test('properties.js - check variable names', (t) => {
// t.deepEqual(
// detect(read('properties.js')).map(function (node) { return '[' + node.nodes.map(buildQualifiedVariableName) + ']'; }),
// [
// '[simple_g]',
// '[qualified_g]',
// '[ugly.chained.methodCall,ugly.chained.lookup]',
// '[uglier.chained.property.lookup]'
// ].sort()
// );
// // });
test('reserved-words.js - check we do not force into strict mode', (t) => {
t.deepEqual(detect(read('reserved-words.js')), ['console'])
})
test('rest-argument.js - ES2015 rest argument', (t) => {
t.deepEqual(detect(read('rest-argument.js')), [])
})
test('return_hash.js - named argument / parameter', (t) => {
t.deepEqual(detect(read('return_hash.js')), [])
})
test('right_hand.js - globals on the right-hand of assignment', (t) => {
t.deepEqual(
detect(read('right_hand.js')),
['exports', '__dirname', '__filename'].sort()
)
})
test('try_catch.js - the exception in a try catch block is a local', (t) => {
t.deepEqual(detect(read('try_catch.js')), [])
})
// test('this.js - `this` is considered a global', (t) => {
// t.deepEqual(detect(read('this.js')), ['this']);
// });
test('BigInt - sees global property primordials like BigInt', (t) => {
t.deepEqual(detect('BigInt(123)'), ['BigInt'])
})