diff --git a/index.js b/index.js index 218ce95..04762af 100644 --- a/index.js +++ b/index.js @@ -290,7 +290,7 @@ exports.astNodeVisitor = { comment.value.indexOf('@classdesc') !== -1 || !noClassdescRegEx.test(comment.value) ) { - node.leadingComments.push(comment); + node.leadingComments.unshift(comment); node.parent.leadingComments.splice(i, 1); const ignore = parser.astBuilder.build('/** @ignore */').comments[0]; diff --git a/test/dest/expected.json b/test/dest/expected.json index d614b5b..67fe4d0 100644 --- a/test/dest/expected.json +++ b/test/dest/expected.json @@ -237,6 +237,49 @@ "scope": "inner", "memberof": "module:test" }, + { + "comment": "/** @ignore */", + "meta": { + "range": [ + 434, + 465 + ], + "filename": "LeadingComments.js", + "lineno": 14, + "columnno": 0, + "code": { + "name": "exports.LeadingComments", + "type": "ClassDeclaration" + } + }, + "ignore": true, + "name": "LeadingComments", + "longname": "LeadingComments", + "kind": "class", + "scope": "global" + }, + { + "comment": "/**\n * Doclet\n */", + "meta": { + "range": [ + 441, + 465 + ], + "filename": "LeadingComments.js", + "lineno": 14, + "columnno": 7, + "code": { + "name": "LeadingComments", + "type": "ClassDeclaration", + "paramnames": [] + } + }, + "classdesc": "Doclet", + "name": "LeadingComments", + "longname": "LeadingComments", + "kind": "class", + "scope": "global" + }, { "comment": "/**\n * @module test/sub/NumberStore\n */", "meta": { diff --git a/test/src/sub/LeadingComments.js b/test/src/sub/LeadingComments.js new file mode 100644 index 0000000..4b1ce07 --- /dev/null +++ b/test/src/sub/LeadingComments.js @@ -0,0 +1,14 @@ +// Import statement required for JSDoc to include first comment as leading comment of the class. +// Otherwise, it gets moved in the AST to the `Program` node from the `ExportNamedDeclaration` class node: +// https://github.com/jsdoc/jsdoc/blob/main/packages/jsdoc-ast/lib/walker.js#L465 +// This itself may be a bug, as it seems to be intended for @module comments. +import ''; // eslint-disable-line + +/* + Comment +*/ + +/** + * Doclet + */ +export class LeadingComments {}