From 843ac66343d8860ca64db881511d554e1b8605bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Thu, 24 Jan 2019 14:42:50 +0100 Subject: [PATCH] Add test for undocumented declaration (// comment) --- packages/docgen/src/get-jsdoc-from-token.js | 2 +- .../fixtures/default-undocumented-oneliner.js | 2 + .../default-undocumented-oneliner.json | 45 +++++++++++++++++++ .../test-get-intermediate-representation.js | 9 ++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 packages/docgen/tests/fixtures/default-undocumented-oneliner.js create mode 100644 packages/docgen/tests/fixtures/default-undocumented-oneliner.json diff --git a/packages/docgen/src/get-jsdoc-from-token.js b/packages/docgen/src/get-jsdoc-from-token.js index 477fd176d54ea1..1c4bb9885c0b93 100644 --- a/packages/docgen/src/get-jsdoc-from-token.js +++ b/packages/docgen/src/get-jsdoc-from-token.js @@ -11,7 +11,7 @@ const getLeadingComments = require( './get-leading-comments' ); module.exports = function( token ) { let jsdoc; const comments = getLeadingComments( token ); - if ( comments ) { + if ( comments && comments.startsWith( '*\n' ) ) { jsdoc = doctrine.parse( comments, { unwrap: true, recoverable: true } ); } return jsdoc; diff --git a/packages/docgen/tests/fixtures/default-undocumented-oneliner.js b/packages/docgen/tests/fixtures/default-undocumented-oneliner.js new file mode 100644 index 00000000000000..11e8966dd62d1c --- /dev/null +++ b/packages/docgen/tests/fixtures/default-undocumented-oneliner.js @@ -0,0 +1,2 @@ +// This comment should be ignored +export default function() { } diff --git a/packages/docgen/tests/fixtures/default-undocumented-oneliner.json b/packages/docgen/tests/fixtures/default-undocumented-oneliner.json new file mode 100644 index 00000000000000..800319a816bfd8 --- /dev/null +++ b/packages/docgen/tests/fixtures/default-undocumented-oneliner.json @@ -0,0 +1,45 @@ +{ + "type": "ExportDefaultDeclaration", + "start": 34, + "end": 63, + "range": [ + 34, + 63 + ], + "declaration": { + "type": "FunctionDeclaration", + "start": 49, + "end": 63, + "range": [ + 49, + 63 + ], + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 60, + "end": 63, + "range": [ + 60, + 63 + ], + "body": [] + } + }, + "leadingComments": [ + { + "type": "Line", + "value": " This comment should be ignored", + "start": 0, + "end": 33, + "range": [ + 0, + 33 + ] + } + ] +} \ No newline at end of file diff --git a/packages/docgen/tests/test-get-intermediate-representation.js b/packages/docgen/tests/test-get-intermediate-representation.js index 3f16b0c3ed618b..e9e228aa2a935c 100644 --- a/packages/docgen/tests/test-get-intermediate-representation.js +++ b/packages/docgen/tests/test-get-intermediate-representation.js @@ -24,6 +24,15 @@ test( 'IR - undocumented', function( t ) { description: 'Undocumented declaration.', tags: [], } ] ); + const tokenOneliner = fs.readFileSync( + path.join( __dirname, './fixtures/default-undocumented-oneliner.json' ), + 'utf-8' + ); + t.deepEqual( getIntermediateRepresentation( JSON.parse( tokenOneliner ) ), [ { + name: 'default', + description: 'Undocumented declaration.', + tags: [], + } ] ); t.end(); } );