diff --git a/dist/index.cjs.cjs b/dist/index.cjs.cjs index 209e7e3..4dadfb6 100644 --- a/dist/index.cjs.cjs +++ b/dist/index.cjs.cjs @@ -22,6 +22,26 @@ const stripEncapsulatingBrackets = (container, isArr) => { container.rawType = container.rawType.replace(/^\{/u, '').replace(/\}$/u, ''); }; +const cleanUpLastTag = (lastTag, mode) => { + // Strip out `}` that encapsulates and is not part of + // the type + stripEncapsulatingBrackets(lastTag); + + if (lastTag.typeLines.length) { + stripEncapsulatingBrackets(lastTag.typeLines, true); + } // With even a multiline type now in full, add parsing + + + let parsedType = null; + + try { + parsedType = jsdocTypePrattParser.parse(lastTag.rawType, mode); + } catch {// Ignore + } + + lastTag.parsedType = parsedType; +}; + const commentParserToESTree = (jsdoc, mode) => { const { tokens: { @@ -45,7 +65,9 @@ const commentParserToESTree = (jsdoc, mode) => { const tags = []; let lastDescriptionLine; let lastTag = null; - jsdoc.source.slice(1).forEach((info, idx) => { + const isSingleLineBlock = Boolean(jsdoc.source.length === 1 && jsdoc.source[0].tokens.end); + const source = isSingleLineBlock ? jsdoc.source : jsdoc.source.slice(1); + source.forEach((info, idx) => { const { tokens } = info; @@ -66,26 +88,12 @@ const commentParserToESTree = (jsdoc, mode) => { if (lastTag) { - // Strip out `}` that encapsulates and is not part of - // the type - stripEncapsulatingBrackets(lastTag); - - if (lastTag.typeLines.length) { - stripEncapsulatingBrackets(lastTag.typeLines, true); - } // With even a multiline type now in full, add parsing + cleanUpLastTag(lastTag, mode); + } // Stop the iteration when we reached the last tag + // but only when we have multi-line block comment - let parsedType = null; - - try { - parsedType = jsdocTypePrattParser.parse(lastTag.rawType, mode); - } catch {// Ignore - } - - lastTag.parsedType = parsedType; - } - - if (end) { + if (end && !isSingleLineBlock) { ast.end = end; return; } @@ -127,6 +135,17 @@ const commentParserToESTree = (jsdoc, mode) => { type: 'JsdocDescriptionLine' }); holder.description += holder.description ? '\n' + description : description; + } // Clean-up in single line mode + + + if (isSingleLineBlock) { + if (end) { + ast.end = end; + } + + if (lastTag) { + cleanUpLastTag(lastTag, mode); + } } }); ast.lastDescriptionLine = lastDescriptionLine; diff --git a/test/commentParserToESTree.js b/test/commentParserToESTree.js index 8c69a66..b0ba7cb 100644 --- a/test/commentParserToESTree.js +++ b/test/commentParserToESTree.js @@ -1,5 +1,3 @@ -import {expect} from 'chai'; - import {commentParserToESTree} from '../src/commentParserToESTree.js'; import {parseComment} from '../src/parseComment.js';