Skip to content

Commit

Permalink
Fix bug on retrieving JSDoc from module dep
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Feb 1, 2019
1 parent 4baa4a1 commit 0f3fe9c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
41 changes: 29 additions & 12 deletions packages/docgen/src/get-intermediate-representation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,42 @@ const hasImportWithName = ( node, name ) =>

const isImportDeclaration = ( node ) => node.type === 'ImportDeclaration';

const someSpecifierMatchesName = ( name, node ) => node.specifiers.some( ( specifier ) => {
if ( specifier.type === 'ImportDefaultSpecifier' ) {
return name === 'default';
} else if (
specifier.type === 'ExportSpecifier' ||
specifier.type === 'ImportNamespaceSpecifier'
) {
return name === specifier.local.name;
}
return name === specifier.imported.name;
} );
// const someSpecifierMatchesName = ( name, node ) => node.specifiers.some( ( specifier ) => {
// if ( specifier.type === 'ImportDefaultSpecifier' ) {
// return name === 'default';
// } else if (
// specifier.type === 'ExportSpecifier' ||
// specifier.type === 'ImportNamespaceSpecifier'
// ) {
// return name === specifier.local.name;
// }
// return name === specifier.imported.name;
// } );

const someImportMatchesName = ( name, token ) => {
let matches = false;
token.specifiers.forEach( ( specifier ) => {
if ( ( specifier.type === 'ImportDefaultSpecifier' ) && ( name === 'default' ) ) {
matches = true;
}
if ( ( specifier.type === 'ImportSpecifier' ) && ( name === specifier.imported.name ) ) {
matches = true;
}
} );
return matches;
};

const someEntryMatchesName = ( name, entry, token ) =>
( token.type === 'ExportNamedDeclaration' && entry.localName === name ) ||
( token.type === 'ImportDeclaration' && someImportMatchesName( name, token ) );

const getJSDocFromDependency = ( token, entry, parseDependency ) => {
let doc;
const ir = parseDependency( getDependencyPath( token ) );
if ( entry.localName === NAMESPACE_EXPORT ) {
doc = ir.filter( ( { name } ) => name !== DEFAULT_EXPORT );
} else {
doc = ir.find( ( { name } ) => someSpecifierMatchesName( name, token ) );
doc = ir.find( ( { name } ) => someEntryMatchesName( name, entry, token ) );
}
return doc;
};
Expand Down
5 changes: 0 additions & 5 deletions packages/docgen/tests/test-get-intermediate-representation.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ test( 'IR - JSDoc in export statement (named export)', function( t ) {
t.end();
} );

test( 'IR - JSDoc in export statement (namespace export)', function( t ) {
t.equals( true, false );
t.end();
} );

test( 'IR - JSDoc in same file (default export)', function( t ) {
const token = fs.readFileSync(
path.join( __dirname, './fixtures/default-identifier.json' ),
Expand Down

0 comments on commit 0f3fe9c

Please sign in to comment.