Skip to content

Commit 695cb91

Browse files
committed
[docgen] Refactor code to use require() instead of JSON.parse(fs.readFileSync())
While working through #37929 I noticed that the tests in the `docgen` package read fixture data manually by chaining `JSON.parse()` and `fs.readFileSync()`, often at times separated by a number of lines. In this patch we're replacing those with the more natural `require()` statement which removes noise from the test modules and hopefully will also remove a point of confusion for those coming in to modify it. We shouldn't have to ask, "why is this code doing something that looks normal but is doing it in a notably different manner?" After reviewing the history of the work, initially introduced in #13329, I could find no explanation for the approach and found no discussion about it in the PR.
1 parent 84b8887 commit 695cb91

2 files changed

Lines changed: 96 additions & 504 deletions

File tree

packages/docgen/test/get-export-entries.js

Lines changed: 44 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
/**
2-
* External dependencies
3-
*/
4-
const fs = require( 'fs' );
5-
const path = require( 'path' );
6-
71
/**
82
* Internal dependencies
93
*/
104
const getExportEntries = require( '../lib/get-export-entries' );
115

126
describe( 'Export entries', () => {
137
it( 'default class (anonymous)', () => {
14-
const token = fs.readFileSync(
15-
path.join(
16-
__dirname,
17-
'./fixtures/default-class-anonymous/exports.json'
18-
),
19-
'utf-8'
20-
);
21-
const name = getExportEntries( JSON.parse( token ) );
8+
const token = require( './fixtures/default-class-anonymous/exports.json' );
9+
const name = getExportEntries( token );
2210
expect( name ).toHaveLength( 1 );
2311
expect( name[ 0 ] ).toEqual( {
2412
localName: '*default*',
@@ -30,14 +18,8 @@ describe( 'Export entries', () => {
3018
} );
3119

3220
it( 'default class (named)', () => {
33-
const token = fs.readFileSync(
34-
path.join(
35-
__dirname,
36-
'./fixtures/default-class-named/exports.json'
37-
),
38-
'utf-8'
39-
);
40-
const name = getExportEntries( JSON.parse( token ) );
21+
const token = require( './fixtures/default-class-named/exports.json' );
22+
const name = getExportEntries( token );
4123
expect( name ).toHaveLength( 1 );
4224
expect( name[ 0 ] ).toEqual( {
4325
localName: 'ClassDeclaration',
@@ -49,14 +31,8 @@ describe( 'Export entries', () => {
4931
} );
5032

5133
it( 'default function (anonymous)', () => {
52-
const token = fs.readFileSync(
53-
path.join(
54-
__dirname,
55-
'./fixtures/default-function-anonymous/exports.json'
56-
),
57-
'utf-8'
58-
);
59-
const name = getExportEntries( JSON.parse( token ) );
34+
const token = require( './fixtures/default-function-anonymous/exports.json' );
35+
const name = getExportEntries( token );
6036
expect( name ).toHaveLength( 1 );
6137
expect( name[ 0 ] ).toEqual( {
6238
localName: '*default*',
@@ -68,14 +44,8 @@ describe( 'Export entries', () => {
6844
} );
6945

7046
it( 'default function (named)', () => {
71-
const token = fs.readFileSync(
72-
path.join(
73-
__dirname,
74-
'./fixtures/default-function-named/exports.json'
75-
),
76-
'utf-8'
77-
);
78-
const name = getExportEntries( JSON.parse( token ) );
47+
const token = require( './fixtures/default-function-named/exports.json' );
48+
const name = getExportEntries( token );
7949
expect( name ).toHaveLength( 1 );
8050
expect( name[ 0 ] ).toEqual( {
8151
localName: 'myDeclaration',
@@ -87,14 +57,8 @@ describe( 'Export entries', () => {
8757
} );
8858

8959
it( 'default identifier', () => {
90-
const token = fs.readFileSync(
91-
path.join(
92-
__dirname,
93-
'./fixtures/default-identifier/exports.json'
94-
),
95-
'utf-8'
96-
);
97-
const name = getExportEntries( JSON.parse( token ) );
60+
const token = require( './fixtures/default-identifier/exports.json' );
61+
const name = getExportEntries( token );
9862
expect( name ).toHaveLength( 1 );
9963
expect( name[ 0 ] ).toEqual( {
10064
localName: 'ClassDeclaration',
@@ -106,14 +70,8 @@ describe( 'Export entries', () => {
10670
} );
10771

10872
it( 'default import (named)', () => {
109-
const token = fs.readFileSync(
110-
path.join(
111-
__dirname,
112-
'./fixtures/default-import-named/exports.json'
113-
),
114-
'utf-8'
115-
);
116-
const name = getExportEntries( JSON.parse( token ) );
73+
const token = require( './fixtures/default-import-named/exports.json' );
74+
const name = getExportEntries( token );
11775
expect( name ).toHaveLength( 1 );
11876
expect( name[ 0 ] ).toEqual( {
11977
localName: 'fnDeclaration',
@@ -125,14 +83,8 @@ describe( 'Export entries', () => {
12583
} );
12684

12785
it( 'default import (default)', () => {
128-
const token = fs.readFileSync(
129-
path.join(
130-
__dirname,
131-
'./fixtures/default-import-default/exports.json'
132-
),
133-
'utf-8'
134-
);
135-
const name = getExportEntries( JSON.parse( token ) );
86+
const token = require( './fixtures/default-import-default/exports.json' );
87+
const name = getExportEntries( token );
13688
expect( name ).toHaveLength( 1 );
13789
expect( name[ 0 ] ).toEqual( {
13890
localName: 'fnDeclaration',
@@ -144,14 +96,8 @@ describe( 'Export entries', () => {
14496
} );
14597

14698
it( 'default named export', () => {
147-
const tokens = fs.readFileSync(
148-
path.join(
149-
__dirname,
150-
'./fixtures/default-named-export/exports.json'
151-
),
152-
'utf-8'
153-
);
154-
const namedExport = getExportEntries( JSON.parse( tokens )[ 0 ] );
99+
const tokens = require( './fixtures/default-named-export/exports.json' );
100+
const namedExport = getExportEntries( tokens[ 0 ] );
155101
expect( namedExport ).toHaveLength( 1 );
156102
expect( namedExport[ 0 ] ).toEqual( {
157103
localName: 'functionDeclaration',
@@ -160,7 +106,7 @@ describe( 'Export entries', () => {
160106
lineStart: 4,
161107
lineEnd: 4,
162108
} );
163-
const defaultExport = getExportEntries( JSON.parse( tokens )[ 1 ] );
109+
const defaultExport = getExportEntries( tokens[ 1 ] );
164110
expect( defaultExport ).toHaveLength( 1 );
165111
expect( defaultExport[ 0 ] ).toEqual( {
166112
localName: 'functionDeclaration',
@@ -172,11 +118,8 @@ describe( 'Export entries', () => {
172118
} );
173119

174120
it( 'default variable', () => {
175-
const token = fs.readFileSync(
176-
path.join( __dirname, './fixtures/default-variable/exports.json' ),
177-
'utf-8'
178-
);
179-
const name = getExportEntries( JSON.parse( token ) );
121+
const token = require( './fixtures/default-variable/exports.json' );
122+
const name = getExportEntries( token );
180123
expect( name ).toHaveLength( 1 );
181124
expect( name[ 0 ] ).toEqual( {
182125
localName: '*default*',
@@ -188,11 +131,8 @@ describe( 'Export entries', () => {
188131
} );
189132

190133
it( 'named class', () => {
191-
const token = fs.readFileSync(
192-
path.join( __dirname, './fixtures/named-class/exports.json' ),
193-
'utf-8'
194-
);
195-
const name = getExportEntries( JSON.parse( token ) );
134+
const token = require( './fixtures/named-class/exports.json' );
135+
const name = getExportEntries( token );
196136
expect( name ).toHaveLength( 1 );
197137
expect( name[ 0 ] ).toEqual( {
198138
localName: 'MyDeclaration',
@@ -204,11 +144,8 @@ describe( 'Export entries', () => {
204144
} );
205145

206146
it( 'named default', () => {
207-
const token = fs.readFileSync(
208-
path.join( __dirname, './fixtures/named-default/exports.json' ),
209-
'utf-8'
210-
);
211-
const name = getExportEntries( JSON.parse( token ) );
147+
const token = require( './fixtures/named-default/exports.json' );
148+
const name = getExportEntries( token );
212149
expect( name ).toHaveLength( 1 );
213150
expect( name[ 0 ] ).toEqual( {
214151
localName: 'default',
@@ -220,14 +157,8 @@ describe( 'Export entries', () => {
220157
} );
221158

222159
it( 'named default (exported)', () => {
223-
const token = fs.readFileSync(
224-
path.join(
225-
__dirname,
226-
'./fixtures/named-default-exported/exports.json'
227-
),
228-
'utf-8'
229-
);
230-
const name = getExportEntries( JSON.parse( token ) );
160+
const token = require( './fixtures/named-default-exported/exports.json' );
161+
const name = getExportEntries( token );
231162
expect( name ).toHaveLength( 1 );
232163
expect( name[ 0 ] ).toEqual( {
233164
localName: 'default',
@@ -239,11 +170,8 @@ describe( 'Export entries', () => {
239170
} );
240171

241172
it( 'named function', () => {
242-
const token = fs.readFileSync(
243-
path.join( __dirname, './fixtures/named-function/exports.json' ),
244-
'utf-8'
245-
);
246-
const name = getExportEntries( JSON.parse( token ) );
173+
const token = require( './fixtures/named-function/exports.json' );
174+
const name = getExportEntries( token );
247175
expect( name ).toHaveLength( 1 );
248176
expect( name[ 0 ] ).toEqual( {
249177
localName: 'myDeclaration',
@@ -255,11 +183,8 @@ describe( 'Export entries', () => {
255183
} );
256184

257185
it( 'named identifier', () => {
258-
const token = fs.readFileSync(
259-
path.join( __dirname, './fixtures/named-identifier/exports.json' ),
260-
'utf-8'
261-
);
262-
const name = getExportEntries( JSON.parse( token ) );
186+
const token = require( './fixtures/named-identifier/exports.json' );
187+
const name = getExportEntries( token );
263188
expect( name ).toHaveLength( 1 );
264189
expect( name[ 0 ] ).toEqual( {
265190
localName: 'myDeclaration',
@@ -268,14 +193,8 @@ describe( 'Export entries', () => {
268193
lineStart: 6,
269194
lineEnd: 6,
270195
} );
271-
const tokenObject = fs.readFileSync(
272-
path.join(
273-
__dirname,
274-
'./fixtures/named-identifier-destructuring/exports.json'
275-
),
276-
'utf-8'
277-
);
278-
const nameObject = getExportEntries( JSON.parse( tokenObject ) );
196+
const tokenObject = require( './fixtures/named-identifier-destructuring/exports.json' );
197+
const nameObject = getExportEntries( tokenObject );
279198
expect( nameObject ).toHaveLength( 1 );
280199
expect( nameObject[ 0 ] ).toEqual( {
281200
localName: 'someDeclaration',
@@ -287,11 +206,8 @@ describe( 'Export entries', () => {
287206
} );
288207

289208
it( 'named identifiers', () => {
290-
const token = fs.readFileSync(
291-
path.join( __dirname, './fixtures/named-identifiers/exports.json' ),
292-
'utf-8'
293-
);
294-
const name = getExportEntries( JSON.parse( token ) );
209+
const token = require( './fixtures/named-identifiers/exports.json' );
210+
const name = getExportEntries( token );
295211
expect( name ).toHaveLength( 3 );
296212
expect( name[ 0 ] ).toEqual( {
297213
localName: 'functionDeclaration',
@@ -314,16 +230,8 @@ describe( 'Export entries', () => {
314230
lineStart: 16,
315231
lineEnd: 16,
316232
} );
317-
const tokenIdentifiersAndInline = fs.readFileSync(
318-
path.join(
319-
__dirname,
320-
'./fixtures/named-identifiers-and-inline/exports.json'
321-
),
322-
'utf-8'
323-
);
324-
const nameInline0 = getExportEntries(
325-
JSON.parse( tokenIdentifiersAndInline )[ 0 ]
326-
);
233+
const tokenIdentifiersAndInline = require( './fixtures/named-identifiers-and-inline/exports.json' );
234+
const nameInline0 = getExportEntries( tokenIdentifiersAndInline[ 0 ] );
327235
expect( nameInline0 ).toHaveLength( 2 );
328236
expect( nameInline0[ 0 ] ).toEqual( {
329237
localName: 'functionDeclaration',
@@ -339,9 +247,7 @@ describe( 'Export entries', () => {
339247
lineStart: 11,
340248
lineEnd: 11,
341249
} );
342-
const nameInline1 = getExportEntries(
343-
JSON.parse( tokenIdentifiersAndInline )[ 1 ]
344-
);
250+
const nameInline1 = getExportEntries( tokenIdentifiersAndInline[ 1 ] );
345251
expect( nameInline1 ).toHaveLength( 1 );
346252
expect( nameInline1[ 0 ] ).toEqual( {
347253
localName: 'variableDeclaration',
@@ -353,14 +259,8 @@ describe( 'Export entries', () => {
353259
} );
354260

355261
it( 'named import namespace', () => {
356-
const token = fs.readFileSync(
357-
path.join(
358-
__dirname,
359-
'./fixtures/named-import-namespace/exports.json'
360-
),
361-
'utf-8'
362-
);
363-
const name = getExportEntries( JSON.parse( token ) );
262+
const token = require( './fixtures/named-import-namespace/exports.json' );
263+
const name = getExportEntries( token );
364264
expect( name ).toHaveLength( 1 );
365265
expect( name[ 0 ] ).toEqual( {
366266
localName: 'variables',
@@ -372,11 +272,8 @@ describe( 'Export entries', () => {
372272
} );
373273

374274
it( 'named variable', () => {
375-
const token = fs.readFileSync(
376-
path.join( __dirname, './fixtures/named-variable/exports.json' ),
377-
'utf-8'
378-
);
379-
const name = getExportEntries( JSON.parse( token ) );
275+
const token = require( './fixtures/named-variable/exports.json' );
276+
const name = getExportEntries( token );
380277
expect( name ).toHaveLength( 1 );
381278
expect( name[ 0 ] ).toEqual( {
382279
localName: 'myDeclaration',
@@ -388,11 +285,8 @@ describe( 'Export entries', () => {
388285
} );
389286

390287
it( 'named variables', () => {
391-
const token = fs.readFileSync(
392-
path.join( __dirname, './fixtures/named-variables/exports.json' ),
393-
'utf-8'
394-
);
395-
const name = getExportEntries( JSON.parse( token ) );
288+
const token = require( './fixtures/named-variables/exports.json' );
289+
const name = getExportEntries( token );
396290
expect( name ).toHaveLength( 2 );
397291
expect( name[ 0 ] ).toEqual( {
398292
localName: 'firstDeclaration',
@@ -411,11 +305,8 @@ describe( 'Export entries', () => {
411305
} );
412306

413307
it( 'namespace (*)', () => {
414-
const token = fs.readFileSync(
415-
path.join( __dirname, './fixtures/namespace/exports.json' ),
416-
'utf-8'
417-
);
418-
const name = getExportEntries( JSON.parse( token ) );
308+
const token = require( './fixtures/namespace/exports.json' );
309+
const name = getExportEntries( token );
419310
expect( name ).toHaveLength( 1 );
420311
expect( name[ 0 ] ).toEqual( {
421312
localName: '*',

0 commit comments

Comments
 (0)