1
+ import {
2
+ describeBabel7 ,
3
+ describeBabel8 ,
4
+ } from "../../../scripts/repo-utils/index.cjs" ;
1
5
import * as t from "../lib/index.js" ;
2
6
import { parse } from "@babel/parser" ;
3
7
@@ -15,8 +19,28 @@ describe("retrievers", function () {
15
19
] ,
16
20
[
17
21
"function declarations" ,
18
- getBody ( "var foo = 1; function bar() { var baz = 2; }" ) ,
19
- [ "bar" , "foo" ] ,
22
+ getBody ( "function bar() { var baz = 2; }" ) ,
23
+ [ "bar" ] ,
24
+ ] ,
25
+ [
26
+ "function declarations with parameters" ,
27
+ getBody (
28
+ "function f(a, { b }, c = 1, [{ _: [ d ], ...e }]) { var baz = 2; }" ,
29
+ ) ,
30
+ [ "f" , "a" , "c" , "b" , "e" , "d" ] ,
31
+ ] ,
32
+ [
33
+ "function expressions with parameters" ,
34
+ getBody (
35
+ "(function f(a, { b }, c = 1, [{ _: [ d ], ...e }]) { var baz = 2; })" ,
36
+ ) [ 0 ] . expression ,
37
+ [ "f" , "a" , "c" , "b" , "e" , "d" ] ,
38
+ ] ,
39
+ [ "class declarations" , getBody ( "class C { a(b) { let c } }" ) [ 0 ] , [ "C" ] ] ,
40
+ [
41
+ "class expressions" ,
42
+ getBody ( "(class C { a(b) { let c } })" ) [ 0 ] . expression ,
43
+ [ "C" ] ,
20
44
] ,
21
45
[
22
46
"object methods" ,
@@ -33,11 +57,28 @@ describe("retrievers", function () {
33
57
getBody ( "(class { #a(b) { let c } })" ) [ 0 ] . expression . body . body ,
34
58
[ "b" ] ,
35
59
] ,
60
+ [
61
+ "for-in statement" ,
62
+ getBody ( "for ([{ _: [ d ], ...e }] in rhs);" ) ,
63
+ [ "e" , "d" ] ,
64
+ ] ,
65
+ [
66
+ "for-of statement" ,
67
+ getBody ( "for ([{ _: [ d ], ...e }] of rhs);" ) ,
68
+ [ "e" , "d" ] ,
69
+ ] ,
70
+ [ "catch clause" , getBody ( "try { } catch (e) {}" ) [ 0 ] . handler , [ "e" ] ] ,
71
+ [ "labeled statement" , getBody ( "label: x" ) , [ "label" ] ] ,
36
72
[
37
73
"export named declarations" ,
38
74
getBody ( "export const foo = 'foo';" ) ,
39
75
[ "foo" ] ,
40
76
] ,
77
+ [
78
+ "export function declarations" ,
79
+ getBody ( "export function foo() {}" ) ,
80
+ [ "foo" ] ,
81
+ ] ,
41
82
[
42
83
"export default class declarations" ,
43
84
getBody ( "export default class foo {}" ) ,
@@ -69,11 +110,165 @@ describe("retrievers", function () {
69
110
getBody ( "var [ a, ...{ b, ...c } ] = {}" ) ,
70
111
[ "a" , "b" , "c" ] ,
71
112
] ,
113
+ [ "unary expression" , getBody ( "void x" ) [ 0 ] . expression , [ "x" ] ] ,
72
114
[ "update expression" , getBody ( "++x" ) [ 0 ] . expression , [ "x" ] ] ,
73
115
[ "assignment expression" , getBody ( "x ??= 1" ) [ 0 ] . expression , [ "x" ] ] ,
74
116
] ) ( "%s" , ( _ , program , bindingNames ) => {
75
117
const ids = t . getBindingIdentifiers ( program ) ;
76
118
expect ( Object . keys ( ids ) ) . toEqual ( bindingNames ) ;
77
119
} ) ;
78
120
} ) ;
121
+ describe ( "getBindingIdentifiers(%, /* duplicates */ true)" , function ( ) {
122
+ it . each ( [
123
+ [ "variable declarations" , getBody ( "var a = 1, a = 2" ) , { a : 2 } ] ,
124
+ [
125
+ "function declarations with parameters" ,
126
+ getBody ( "function f(f) { var f = 1; }" ) ,
127
+ { f : 2 } ,
128
+ ] ,
129
+ ] ) ( "%s" , ( _ , program , expected ) => {
130
+ const ids = t . getBindingIdentifiers ( program , true ) ;
131
+ for ( const name of Object . keys ( ids ) ) {
132
+ ids [ name ] = Array . isArray ( ids [ name ] ) ? ids [ name ] . length : 1 ;
133
+ }
134
+ expect ( ids ) . toEqual ( expected ) ;
135
+ } ) ;
136
+ } ) ;
137
+ describe ( "getOuterBindingIdentifiers" , function ( ) {
138
+ it . each ( [
139
+ [
140
+ "variable declarations" ,
141
+ getBody ( "var a = 1; let b = 2; const c = 3;" ) ,
142
+ [ "a" , "b" , "c" ] ,
143
+ ] ,
144
+ [
145
+ "function declarations" ,
146
+ getBody ( "function bar() { var baz = 2; }" ) ,
147
+ [ "bar" ] ,
148
+ ] ,
149
+ [
150
+ "function declarations with parameters" ,
151
+ getBody (
152
+ "function f(a, { b }, c = 1, [{ _: [ d ], ...e }]) { var baz = 2; }" ,
153
+ ) ,
154
+ [ "f" ] ,
155
+ ] ,
156
+ [
157
+ "function expressions with parameters" ,
158
+ getBody (
159
+ "(function f(a, { b }, c = 1, [{ _: [ d ], ...e }]) { var baz = 2; })" ,
160
+ ) [ 0 ] . expression ,
161
+ [ ] ,
162
+ ] ,
163
+ [ "class declarations" , getBody ( "class C { a(b) { let c } }" ) [ 0 ] , [ "C" ] ] ,
164
+ [
165
+ "object methods" ,
166
+ getBody ( "({ a(b) { let c } })" ) [ 0 ] . expression . properties [ 0 ] ,
167
+ [ "b" ] ,
168
+ ] ,
169
+ [
170
+ "class methods" ,
171
+ getBody ( "(class { a(b) { let c } })" ) [ 0 ] . expression . body . body ,
172
+ [ "b" ] ,
173
+ ] ,
174
+ [
175
+ "class private methods" ,
176
+ getBody ( "(class { #a(b) { let c } })" ) [ 0 ] . expression . body . body ,
177
+ [ "b" ] ,
178
+ ] ,
179
+ [
180
+ "for-in statement" ,
181
+ getBody ( "for ([{ _: [ d ], ...e }] in rhs);" ) ,
182
+ [ "e" , "d" ] ,
183
+ ] ,
184
+ [
185
+ "for-of statement" ,
186
+ getBody ( "for ([{ _: [ d ], ...e }] of rhs);" ) ,
187
+ [ "e" , "d" ] ,
188
+ ] ,
189
+ [ "catch clause" , getBody ( "try { } catch (e) {}" ) [ 0 ] . handler , [ "e" ] ] ,
190
+ [ "labeled statement" , getBody ( "label: x" ) , [ "label" ] ] ,
191
+ [
192
+ "export named declarations" ,
193
+ getBody ( "export const foo = 'foo';" ) ,
194
+ [ "foo" ] ,
195
+ ] ,
196
+ [
197
+ "export function declarations" ,
198
+ getBody ( "export function foo() {}" ) ,
199
+ [ "foo" ] ,
200
+ ] ,
201
+ [
202
+ "export default class declarations" ,
203
+ getBody ( "export default class foo {}" ) ,
204
+ [ "foo" ] ,
205
+ ] ,
206
+ [
207
+ "export default referenced identifiers" ,
208
+ getBody ( "export default foo" ) ,
209
+ [ ] ,
210
+ ] ,
211
+ [ "export all declarations" , getBody ( "export * from 'x'" ) , [ ] ] ,
212
+ [
213
+ "export all as namespace declarations" ,
214
+ getBody ( "export * as ns from 'x'" ) ,
215
+ [ ] , // exported bindings are not associated with declarations
216
+ ] ,
217
+ [
218
+ "export namespace specifiers" ,
219
+ getBody ( "export * as ns from 'x'" ) [ 0 ] . specifiers ,
220
+ [ "ns" ] ,
221
+ ] ,
222
+ [
223
+ "object patterns" ,
224
+ getBody ( "const { a, b: { ...c } = { d } } = {}" ) ,
225
+ [ "a" , "c" ] ,
226
+ ] ,
227
+ [
228
+ "array patterns" ,
229
+ getBody ( "var [ a, ...{ b, ...c } ] = {}" ) ,
230
+ [ "a" , "b" , "c" ] ,
231
+ ] ,
232
+ [ "unary expression" , getBody ( "void x" ) [ 0 ] . expression , [ "x" ] ] ,
233
+ [ "update expression" , getBody ( "++x" ) [ 0 ] . expression , [ "x" ] ] ,
234
+ [ "assignment expression" , getBody ( "x ??= 1" ) [ 0 ] . expression , [ "x" ] ] ,
235
+ ] ) ( "%s" , ( _ , program , bindingNames ) => {
236
+ const ids = t . getOuterBindingIdentifiers ( program ) ;
237
+ expect ( Object . keys ( ids ) ) . toEqual ( bindingNames ) ;
238
+ } ) ;
239
+ } ) ;
240
+ describeBabel7 ( "getOuterBindingIdentifiers - Babel 7" , function ( ) {
241
+ it . each ( [
242
+ [
243
+ "class expressions" ,
244
+ getBody ( "(class C { a(b) { let c } })" ) [ 0 ] . expression ,
245
+ [ "C" ] ,
246
+ ] ,
247
+ ] ) ( "%s" , ( _ , program , bindingNames ) => {
248
+ const ids = t . getOuterBindingIdentifiers ( program ) ;
249
+ expect ( Object . keys ( ids ) ) . toEqual ( bindingNames ) ;
250
+ } ) ;
251
+ } ) ;
252
+ describeBabel8 ( "getOuterBindingIdentifiers - Babel 8" , function ( ) {
253
+ it . each ( [
254
+ [
255
+ "class expressions" ,
256
+ getBody ( "(class C { a(b) { let c } })" ) [ 0 ] . expression ,
257
+ [ ] ,
258
+ ] ,
259
+ ] ) ( "%s" , ( _ , program , bindingNames ) => {
260
+ const ids = t . getOuterBindingIdentifiers ( program ) ;
261
+ expect ( Object . keys ( ids ) ) . toEqual ( bindingNames ) ;
262
+ } ) ;
263
+ } ) ;
264
+ describe ( "getBindingIdentifiers(%, false, false, /* newBindingsOnly */ true)" , function ( ) {
265
+ it . each ( [
266
+ [ "unary expression" , getBody ( "void x" ) [ 0 ] . expression , [ ] ] ,
267
+ [ "update expression" , getBody ( "++x" ) [ 0 ] . expression , [ ] ] ,
268
+ [ "assignment expression" , getBody ( "x ??= 1" ) [ 0 ] . expression , [ ] ] ,
269
+ ] ) ( "%s" , ( _ , program , bindingNames ) => {
270
+ const ids = t . getBindingIdentifiers ( program , false , false , true ) ;
271
+ expect ( Object . keys ( ids ) ) . toEqual ( bindingNames ) ;
272
+ } ) ;
273
+ } ) ;
79
274
} ) ;
0 commit comments