Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docgen] Refactor code to use require() instead of JSON.parse(fs.readFileSync()) #38148

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 44 additions & 153 deletions packages/docgen/test/get-export-entries.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );

/**
* Internal dependencies
*/
const getExportEntries = require( '../lib/get-export-entries' );

describe( 'Export entries', () => {
it( 'default class (anonymous)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-class-anonymous/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-class-anonymous/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: '*default*',
Expand All @@ -30,14 +18,8 @@ describe( 'Export entries', () => {
} );

it( 'default class (named)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-class-named/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-class-named/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'ClassDeclaration',
Expand All @@ -49,14 +31,8 @@ describe( 'Export entries', () => {
} );

it( 'default function (anonymous)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-function-anonymous/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-function-anonymous/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: '*default*',
Expand All @@ -68,14 +44,8 @@ describe( 'Export entries', () => {
} );

it( 'default function (named)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-function-named/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-function-named/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'myDeclaration',
Expand All @@ -87,14 +57,8 @@ describe( 'Export entries', () => {
} );

it( 'default identifier', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-identifier/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-identifier/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'ClassDeclaration',
Expand All @@ -106,14 +70,8 @@ describe( 'Export entries', () => {
} );

it( 'default import (named)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-import-named/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-import-named/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'fnDeclaration',
Expand All @@ -125,14 +83,8 @@ describe( 'Export entries', () => {
} );

it( 'default import (default)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-import-default/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-import-default/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'fnDeclaration',
Expand All @@ -144,14 +96,8 @@ describe( 'Export entries', () => {
} );

it( 'default named export', () => {
const tokens = fs.readFileSync(
path.join(
__dirname,
'./fixtures/default-named-export/exports.json'
),
'utf-8'
);
const namedExport = getExportEntries( JSON.parse( tokens )[ 0 ] );
const tokens = require( './fixtures/default-named-export/exports.json' );
const namedExport = getExportEntries( tokens[ 0 ] );
expect( namedExport ).toHaveLength( 1 );
expect( namedExport[ 0 ] ).toEqual( {
localName: 'functionDeclaration',
Expand All @@ -160,7 +106,7 @@ describe( 'Export entries', () => {
lineStart: 4,
lineEnd: 4,
} );
const defaultExport = getExportEntries( JSON.parse( tokens )[ 1 ] );
const defaultExport = getExportEntries( tokens[ 1 ] );
expect( defaultExport ).toHaveLength( 1 );
expect( defaultExport[ 0 ] ).toEqual( {
localName: 'functionDeclaration',
Expand All @@ -172,11 +118,8 @@ describe( 'Export entries', () => {
} );

it( 'default variable', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/default-variable/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/default-variable/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: '*default*',
Expand All @@ -188,11 +131,8 @@ describe( 'Export entries', () => {
} );

it( 'named class', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-class/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-class/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'MyDeclaration',
Expand All @@ -204,11 +144,8 @@ describe( 'Export entries', () => {
} );

it( 'named default', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-default/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-default/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'default',
Expand All @@ -220,14 +157,8 @@ describe( 'Export entries', () => {
} );

it( 'named default (exported)', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/named-default-exported/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-default-exported/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'default',
Expand All @@ -239,11 +170,8 @@ describe( 'Export entries', () => {
} );

it( 'named function', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-function/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-function/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'myDeclaration',
Expand All @@ -255,11 +183,8 @@ describe( 'Export entries', () => {
} );

it( 'named identifier', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-identifier/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-identifier/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'myDeclaration',
Expand All @@ -268,14 +193,8 @@ describe( 'Export entries', () => {
lineStart: 6,
lineEnd: 6,
} );
const tokenObject = fs.readFileSync(
path.join(
__dirname,
'./fixtures/named-identifier-destructuring/exports.json'
),
'utf-8'
);
const nameObject = getExportEntries( JSON.parse( tokenObject ) );
const tokenObject = require( './fixtures/named-identifier-destructuring/exports.json' );
const nameObject = getExportEntries( tokenObject );
expect( nameObject ).toHaveLength( 1 );
expect( nameObject[ 0 ] ).toEqual( {
localName: 'someDeclaration',
Expand All @@ -287,11 +206,8 @@ describe( 'Export entries', () => {
} );

it( 'named identifiers', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-identifiers/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-identifiers/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 3 );
expect( name[ 0 ] ).toEqual( {
localName: 'functionDeclaration',
Expand All @@ -314,16 +230,8 @@ describe( 'Export entries', () => {
lineStart: 16,
lineEnd: 16,
} );
const tokenIdentifiersAndInline = fs.readFileSync(
path.join(
__dirname,
'./fixtures/named-identifiers-and-inline/exports.json'
),
'utf-8'
);
const nameInline0 = getExportEntries(
JSON.parse( tokenIdentifiersAndInline )[ 0 ]
);
const tokenIdentifiersAndInline = require( './fixtures/named-identifiers-and-inline/exports.json' );
const nameInline0 = getExportEntries( tokenIdentifiersAndInline[ 0 ] );
expect( nameInline0 ).toHaveLength( 2 );
expect( nameInline0[ 0 ] ).toEqual( {
localName: 'functionDeclaration',
Expand All @@ -339,9 +247,7 @@ describe( 'Export entries', () => {
lineStart: 11,
lineEnd: 11,
} );
const nameInline1 = getExportEntries(
JSON.parse( tokenIdentifiersAndInline )[ 1 ]
);
const nameInline1 = getExportEntries( tokenIdentifiersAndInline[ 1 ] );
expect( nameInline1 ).toHaveLength( 1 );
expect( nameInline1[ 0 ] ).toEqual( {
localName: 'variableDeclaration',
Expand All @@ -353,14 +259,8 @@ describe( 'Export entries', () => {
} );

it( 'named import namespace', () => {
const token = fs.readFileSync(
path.join(
__dirname,
'./fixtures/named-import-namespace/exports.json'
),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-import-namespace/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'variables',
Expand All @@ -372,11 +272,8 @@ describe( 'Export entries', () => {
} );

it( 'named variable', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-variable/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-variable/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: 'myDeclaration',
Expand All @@ -388,11 +285,8 @@ describe( 'Export entries', () => {
} );

it( 'named variables', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/named-variables/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/named-variables/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 2 );
expect( name[ 0 ] ).toEqual( {
localName: 'firstDeclaration',
Expand All @@ -411,11 +305,8 @@ describe( 'Export entries', () => {
} );

it( 'namespace (*)', () => {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/namespace/exports.json' ),
'utf-8'
);
const name = getExportEntries( JSON.parse( token ) );
const token = require( './fixtures/namespace/exports.json' );
const name = getExportEntries( token );
expect( name ).toHaveLength( 1 );
expect( name[ 0 ] ).toEqual( {
localName: '*',
Expand Down
Loading