123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import {parse as jsdocTypePrattParse} from 'jsdoc-type-pratt-parser';
-
- const stripEncapsulatingBrackets = (container, isArr) => {
- if (isArr) {
- const firstItem = container[0];
- firstItem.rawType = firstItem.rawType.replace(
- /^\{/u, ''
- );
-
- const lastItem = container[container.length - 1];
- lastItem.rawType = lastItem.rawType.replace(/\}$/u, '');
-
- return;
- }
- container.rawType = container.rawType.replace(
- /^\{/u, ''
- ).replace(/\}$/u, '');
- };
-
- const commentParserToESTree = (jsdoc, mode) => {
- const {tokens: {
- delimiter: delimiterRoot,
- lineEnd: lineEndRoot,
- postDelimiter: postDelimiterRoot,
- end: endRoot,
- description: descriptionRoot
- }} = jsdoc.source[0];
- const ast = {
- delimiter: delimiterRoot,
- description: descriptionRoot,
-
- descriptionLines: [],
-
- // `end` will be overwritten if there are other entries
- end: endRoot,
- postDelimiter: postDelimiterRoot,
- lineEnd: lineEndRoot,
-
- type: 'JsdocBlock'
- };
-
- const tags = [];
- let lastDescriptionLine;
- let lastTag = null;
- jsdoc.source.slice(1).forEach((info, idx) => {
- const {tokens} = info;
- const {
- delimiter,
- description,
- postDelimiter,
- start,
- tag,
- end,
- type: rawType
- } = tokens;
-
- if (tag || end) {
- if (lastDescriptionLine === undefined) {
- lastDescriptionLine = idx;
- }
-
- // Clean-up with last tag before end or new tag
- 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
- let parsedType = null;
- try {
- parsedType = jsdocTypePrattParse(lastTag.rawType, mode);
- } catch {
- // Ignore
- }
-
- lastTag.parsedType = parsedType;
- }
-
- if (end) {
- ast.end = end;
-
- return;
- }
-
- const {
- end: ed,
- ...tkns
- } = tokens;
-
- const tagObj = {
- ...tkns,
- descriptionLines: [],
- rawType: '',
- type: 'JsdocTag',
- typeLines: []
- };
- tagObj.tag = tagObj.tag.replace(/^@/u, '');
-
- lastTag = tagObj;
-
- tags.push(tagObj);
- }
-
- if (rawType) {
- // Will strip rawType brackets after this tag
- lastTag.typeLines.push(
- {
- delimiter,
- postDelimiter,
- rawType,
- start,
- type: 'JsdocTypeLine'
- }
- );
- lastTag.rawType += rawType;
- }
-
- if (description) {
- const holder = lastTag || ast;
- holder.descriptionLines.push({
- delimiter,
- description,
- postDelimiter,
- start,
- type: 'JsdocDescriptionLine'
- });
- holder.description += holder.description
- ? '\n' + description
- : description;
- }
- });
-
- ast.lastDescriptionLine = lastDescriptionLine;
- ast.tags = tags;
-
- return ast;
- };
-
- const jsdocVisitorKeys = {
- JsdocBlock: ['tags', 'descriptionLines'],
- JsdocDescriptionLine: [],
- JsdocTypeLine: [],
- JsdocTag: ['descriptionLines', 'typeLines', 'parsedType']
- };
-
- export {commentParserToESTree, jsdocVisitorKeys};
|