@@ -9,18 +9,16 @@ import jsx from 'acorn-jsx'
9
9
// @ts -expect-error: untyped.
10
10
import stage3 from 'acorn-stage3'
11
11
import { fromJs } from '../index.js'
12
- import * as mod from '../index.js'
13
12
14
- test ( 'fromJs' , ( ) => {
15
- assert . deepEqual (
16
- Object . keys ( mod ) . sort ( ) ,
17
- [ 'fromJs' ] ,
18
- 'should expose the public api'
19
- )
13
+ test ( 'fromJs' , async function ( t ) {
14
+ await t . test ( 'should expose the public api' , async function ( ) {
15
+ assert . deepEqual ( Object . keys ( await import ( '../index.js' ) ) . sort ( ) , [
16
+ 'fromJs'
17
+ ] )
18
+ } )
20
19
21
- assert . deepEqual (
22
- fromJs ( '1 + "2"' ) ,
23
- {
20
+ await t . test ( 'should work' , async function ( ) {
21
+ assert . deepEqual ( fromJs ( '1 + "2"' ) , {
24
22
type : 'Program' ,
25
23
body : [
26
24
{
@@ -61,21 +59,20 @@ test('fromJs', () => {
61
59
start : { line : 1 , column : 1 , offset : 0 } ,
62
60
end : { line : 1 , column : 8 , offset : 7 }
63
61
}
64
- } ,
65
- 'should work'
66
- )
62
+ } )
63
+ } )
67
64
68
- assert . throws (
69
- function ( ) {
70
- fromJs ( 'import "a"' )
71
- } ,
72
- / C o u l d n o t p a r s e J a v a S c r i p t w i t h A c o r n / ,
73
- 'should fail on an import w/o `module: true`'
65
+ await t . test (
66
+ 'should fail on an import w/o `module: true`' ,
67
+ async function ( ) {
68
+ assert . throws ( function ( ) {
69
+ fromJs ( 'import "a"' )
70
+ } , / C o u l d n o t p a r s e J a v a S c r i p t w i t h A c o r n / )
71
+ }
74
72
)
75
73
76
- assert . deepEqual (
77
- fromJs ( 'import "a"' , { module : true } ) ,
78
- {
74
+ await t . test ( 'should support an import w/ `module: true`' , async function ( ) {
75
+ assert . deepEqual ( fromJs ( 'import "a"' , { module : true } ) , {
79
76
type : 'Program' ,
80
77
body : [
81
78
{
@@ -101,13 +98,11 @@ test('fromJs', () => {
101
98
start : { line : 1 , column : 1 , offset : 0 } ,
102
99
end : { line : 1 , column : 11 , offset : 10 }
103
100
}
104
- } ,
105
- 'should support an import w/ `module: true`'
106
- )
101
+ } )
102
+ } )
107
103
108
- assert . deepEqual (
109
- fromJs ( '<x />' , { plugins : [ jsx ( ) ] } ) ,
110
- {
104
+ await t . test ( 'should support a plugin' , async function ( ) {
105
+ assert . deepEqual ( fromJs ( '<x />' , { plugins : [ jsx ( ) ] } ) , {
111
106
type : 'Program' ,
112
107
body : [
113
108
{
@@ -150,13 +145,11 @@ test('fromJs', () => {
150
145
start : { line : 1 , column : 1 , offset : 0 } ,
151
146
end : { line : 1 , column : 6 , offset : 5 }
152
147
}
153
- } ,
154
- 'should support a plugin'
155
- )
148
+ } )
149
+ } )
156
150
157
- assert . deepEqual (
158
- fromJs ( '#!/bin/sh\n1' , { allowHashBang : true } ) ,
159
- {
151
+ await t . test ( 'should support `options.allowHashBang`' , async function ( ) {
152
+ assert . deepEqual ( fromJs ( '#!/bin/sh\n1' , { allowHashBang : true } ) , {
160
153
type : 'Program' ,
161
154
body : [
162
155
{
@@ -190,47 +183,130 @@ test('fromJs', () => {
190
183
start : { line : 1 , column : 1 , offset : 0 } ,
191
184
end : { line : 2 , column : 2 , offset : 11 }
192
185
}
186
+ } )
187
+ } )
188
+
189
+ assert . deepEqual (
190
+ fromJs ( new Uint8Array ( ) ) ,
191
+ {
192
+ type : 'Program' ,
193
+ body : [ ] ,
194
+ sourceType : 'script' ,
195
+ comments : [ ] ,
196
+ position : {
197
+ start : { line : 1 , column : 1 , offset : 0 } ,
198
+ end : { line : 1 , column : 1 , offset : 0 }
199
+ }
193
200
} ,
194
- 'should support `options.allowHashBang`'
201
+ 'should support empty typed arrays'
202
+ )
203
+
204
+ assert . deepEqual (
205
+ fromJs ( new TextEncoder ( ) . encode ( 'let a = 1' ) ) ,
206
+ {
207
+ type : 'Program' ,
208
+ body : [
209
+ {
210
+ type : 'VariableDeclaration' ,
211
+ declarations : [
212
+ {
213
+ type : 'VariableDeclarator' ,
214
+ id : {
215
+ type : 'Identifier' ,
216
+ name : 'a' ,
217
+ position : {
218
+ start : { line : 1 , column : 5 , offset : 4 } ,
219
+ end : { line : 1 , column : 6 , offset : 5 }
220
+ }
221
+ } ,
222
+ init : {
223
+ type : 'Literal' ,
224
+ value : 1 ,
225
+ position : {
226
+ start : { line : 1 , column : 9 , offset : 8 } ,
227
+ end : { line : 1 , column : 10 , offset : 9 }
228
+ }
229
+ } ,
230
+ position : {
231
+ start : { line : 1 , column : 5 , offset : 4 } ,
232
+ end : { line : 1 , column : 10 , offset : 9 }
233
+ }
234
+ }
235
+ ] ,
236
+ kind : 'let' ,
237
+ position : {
238
+ start : { line : 1 , column : 1 , offset : 0 } ,
239
+ end : { line : 1 , column : 10 , offset : 9 }
240
+ }
241
+ }
242
+ ] ,
243
+ sourceType : 'script' ,
244
+ comments : [ ] ,
245
+ position : {
246
+ start : { line : 1 , column : 1 , offset : 0 } ,
247
+ end : { line : 1 , column : 10 , offset : 9 }
248
+ }
249
+ } ,
250
+ 'should support typed arrays'
251
+ )
252
+
253
+ assert . deepEqual (
254
+ fromJs ( new Uint8Array ( ) ) ,
255
+ {
256
+ type : 'Program' ,
257
+ body : [ ] ,
258
+ sourceType : 'script' ,
259
+ comments : [ ] ,
260
+ position : {
261
+ start : { line : 1 , column : 1 , offset : 0 } ,
262
+ end : { line : 1 , column : 1 , offset : 0 }
263
+ }
264
+ } ,
265
+ 'should support empty typed arrays'
195
266
)
196
267
} )
197
268
198
- test ( 'fixtures' , async function ( ) {
269
+ test ( 'fixtures' , async function ( t ) {
199
270
const base = new URL ( 'fixtures/' , import . meta. url )
200
271
const filenames = await fs . readdir ( base )
201
- const tests = filenames . filter ( ( d ) => d . charAt ( 0 ) !== '.' )
272
+ const tests = filenames . filter ( function ( d ) {
273
+ return d . charAt ( 0 ) !== '.'
274
+ } )
202
275
203
276
let index = - 1
204
277
while ( ++ index < tests . length ) {
205
278
const filename = tests [ index ]
206
- const valueUrl = new URL ( filename + '/index.js' , base )
207
- const treeUrl = new URL ( filename + '/index.json' , base )
208
- const value = String ( await fs . readFile ( valueUrl ) )
209
- const parts = filename . split ( '-' )
210
- const module = parts . includes ( 'module' )
211
- /** @type {Array<Plugin> } */
212
- const plugins = [ ]
213
-
214
- if ( parts . includes ( 'jsx' ) ) {
215
- plugins . push ( jsx ( ) )
216
- }
217
279
218
- if ( parts . includes ( 'stage3' ) ) {
219
- plugins . push ( stage3 )
220
- }
280
+ await t . test ( filename , async function ( ) {
281
+ const valueUrl = new URL ( filename + '/index.js' , base )
282
+ const treeUrl = new URL ( filename + '/index.json' , base )
283
+ const value = String ( await fs . readFile ( valueUrl ) )
284
+ const parts = filename . split ( '-' )
285
+ const module = parts . includes ( 'module' )
286
+ /** @type {Array<Plugin> } */
287
+ const plugins = [ ]
221
288
222
- const actual = fromJs ( value , { module , plugins } )
223
- /** @type { string } */
224
- let expected
289
+ if ( parts . includes ( 'jsx' ) ) {
290
+ plugins . push ( jsx ( ) )
291
+ }
225
292
226
- try {
227
- expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
228
- } catch {
229
- // New fixture.
230
- expected = JSON . stringify ( actual , null , 2 ) + '\n'
231
- await fs . writeFile ( treeUrl , expected )
232
- }
293
+ if ( parts . includes ( 'stage3' ) ) {
294
+ plugins . push ( stage3 )
295
+ }
296
+
297
+ const actual = fromJs ( value , { module, plugins} )
298
+ /** @type {string } */
299
+ let expected
300
+
301
+ try {
302
+ expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
303
+ } catch {
304
+ // New fixture.
305
+ expected = JSON . stringify ( actual , null , 2 ) + '\n'
306
+ await fs . writeFile ( treeUrl , expected )
307
+ }
233
308
234
- assert . deepEqual ( actual , expected , filename )
309
+ assert . deepEqual ( actual , expected )
310
+ } )
235
311
}
236
312
} )
0 commit comments