How to use jsdoctypeparser - 10 common examples

To help you get started, we’ve selected a few jsdoctypeparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gajus / eslint-plugin-jsdoc / src / rules / validTypes.js View on Github external
const validNamepathParsing = function (namepath, tagName) {
      try {
        parse(namepath);
      } catch (error) {
        let handled = false;

        if (tagName) {
          if (['memberof', 'memberof!'].includes(tagName)) {
            const endChar = namepath.slice(-1);
            if (['#', '.', '~'].includes(endChar)) {
              try {
                parse(namepath.slice(0, -1));
                handled = true;
              } catch (memberofError) {
                // Use the original error for including the whole type
              }
            }
          } else if (tagName === 'borrows') {
            const startChar = namepath.charAt();
github infamous / infamous / readem.ts View on Github external
source: originalComment,
                content: [],
            }

            // strip the leading stars, if any
            const commentContent = commentMatch[1].replace(leadingStarsRegex, '')

            const re = jsDocTagRegex
            let tagMatch: ReturnType

            // get each part of each JSDoc tag
            while ((tagMatch = re.exec(commentContent))) {
                let type: any

                try {
                    type = tagMatch[2] && jsdocTypeParse(tagMatch[2])
                    type.source = tagMatch[2]
                } catch (e) {
                    type = tagMatch[2]
                }

                comment.content.push({
                    source: tagMatch[0],
                    tag: tagMatch[1],
                    type: type || undefined,
                    name: tagMatch[3],
                    description: (tagMatch[4] && tagMatch[4].trim()) || undefined,
                })
            }

            comments.push(comment)
github gajus / eslint-plugin-jsdoc / src / rules / validTypes.js View on Github external
if (tagName) {
          if (['memberof', 'memberof!'].includes(tagName)) {
            const endChar = namepath.slice(-1);
            if (['#', '.', '~'].includes(endChar)) {
              try {
                parse(namepath.slice(0, -1));
                handled = true;
              } catch (memberofError) {
                // Use the original error for including the whole type
              }
            }
          } else if (tagName === 'borrows') {
            const startChar = namepath.charAt();
            if (['#', '.', '~'].includes(startChar)) {
              try {
                parse(namepath.slice(1));
                handled = true;
              } catch (memberofError) {
                // Use the original error for including the whole type
              }
            }
          }
        }

        if (!handled) {
          report(`Syntax error in namepath: ${namepath}`, null, tag);

          return false;
        }
      }

      return true;
github gajus / eslint-plugin-jsdoc / src / rules / noUndefinedTypes.js View on Github external
jsdocTagsWithPossibleType.forEach((tag) => {
    let parsedType;

    try {
      parsedType = parseType(tag.type);
    } catch (error) {
      // On syntax error, will be handled by valid-types.
      return;
    }

    traverse(parsedType, ({type, name}) => {
      if (type === 'NAME') {
        if (!allDefinedTypes.includes(name)) {
          report(`The type '${name}' is undefined.`, null, tag);
        } else if (!_.includes(extraTypes, name)) {
          context.markVariableAsUsed(name);
        }
      }
    });
  });
}, {
github jscs-dev / jscs-jsdoc / lib / jsdoc.js View on Github external
}
            break;

        case node instanceof TypeBuilder.RecordType:
            // entries: Array.
            node.typeName = 'Object';
            res = cb(node) || [];
            if (node.entries) {
                res.concat(node.entries.map(function(subnode) {
                    subnode.parentElement = node;
                    _iterateDocTypes(subnode, cb);
                }));
            }
            break;

        case node instanceof TypeBuilder.RecordType.Entry:
            // name: string,
            // typeUnion: TypeUnion
            node.typeUnion.collectionNode = node;
            res = _iterateDocTypes(node.typeUnion, cb);
            break;

        case node instanceof TypeBuilder.ModuleName:
            node.typeName = node.name;
            node.module = true;
            res = cb(node);
            break;

        default:
            throw new Error('DocType: Unsupported doc node');
    }
github jscs-dev / jscs-jsdoc / lib / jsdoc.js View on Github external
var assert = require('assert');
var commentParser = require('comment-parser');

var TypeParser = require('jsdoctypeparser').Parser;
var TypeBuilder = require('jsdoctypeparser').Builder;

var getNodeColumn = require('./utils').getNodeColumn;
var getLineInfo = require('./utils').getLineInfo;

// wtf but it needed to stop writing warnings to stdout
// and revert exceptions functionality
TypeBuilder.ENABLE_EXCEPTIONS = true;

// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
var PARSERS = {
    tag: commentParser.PARSERS.parse_tag,
    type: commentParser.PARSERS.parse_type,
    description: commentParser.PARSERS.parse_description,
};
// jscs:enable
var RE_SPACE = /\s/;
github jscs-dev / jscs-jsdoc / lib / jsdoc.js View on Github external
// all: boolean,
            // unknown: boolean,
            // types: Array.
            res = node.types.map(function(subnode) {
                if (node.collectionNode) {
                    subnode.parentElement = node.collectionNode;
                }
                return _iterateDocTypes(subnode, cb);
            }) || [];
            if (node.nullable) {
                var subnode = {typeName: 'null', collectionNode: node};
                res.concat(cb(subnode));
            }
            break;

        case node instanceof TypeBuilder.TypeName:
            // name: string
            node.typeName = node.name;
            res = cb(node);
            break;

        case node instanceof TypeBuilder.GenericType:
            // genericTypeName: string,
            // parameterTypeUnions: Array.
            node.typeName = node.genericTypeName.type;
            res = cb(node) || [];
            if (node.parameterTypeUnions) {
                // node.parameterTypeUnions.collectionNode = node;
                res.concat(node.parameterTypeUnions.map(function(subnode) {
                    subnode.parentElement = node;
                    _iterateDocTypes(subnode, cb);
                }));
github jscs-dev / jscs-jsdoc / lib / jsdoc.js View on Github external
function _iterateDocTypes(node, cb) {
    var res;

    switch (true) {
        case node instanceof TypeBuilder.TypeUnion:
            // optional: boolean,
            // nullable: boolean,
            // variable: boolean,
            // nonNullable: boolean,
            // all: boolean,
            // unknown: boolean,
            // types: Array.
            res = node.types.map(function(subnode) {
                if (node.collectionNode) {
                    subnode.parentElement = node.collectionNode;
                }
                return _iterateDocTypes(subnode, cb);
            }) || [];
            if (node.nullable) {
                var subnode = {typeName: 'null', collectionNode: node};
                res.concat(cb(subnode));
github gajus / eslint-plugin-jsdoc / src / rules / noUndefinedTypes.js View on Github external
jsdocTagsWithPossibleType.forEach((tag) => {
    let parsedType;

    try {
      parsedType = parseType(tag.type);
    } catch (error) {
      // On syntax error, will be handled by valid-types.
      return;
    }

    traverse(parsedType, ({type, name}) => {
      if (type === 'NAME') {
        if (!allDefinedTypes.includes(name)) {
          report(`The type '${name}' is undefined.`, null, tag);
        } else if (!_.includes(extraTypes, name)) {
          context.markVariableAsUsed(name);
        }
      }
    });
  });
}, {
github jscs-dev / jscs-jsdoc / lib / jsdoc.js View on Github external
var assert = require('assert');
var commentParser = require('comment-parser');

var TypeParser = require('jsdoctypeparser').Parser;
var TypeBuilder = require('jsdoctypeparser').Builder;

var getNodeColumn = require('./utils').getNodeColumn;
var getLineInfo = require('./utils').getLineInfo;

// wtf but it needed to stop writing warnings to stdout
// and revert exceptions functionality
TypeBuilder.ENABLE_EXCEPTIONS = true;

// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
var PARSERS = {
    tag: commentParser.PARSERS.parse_tag,
    type: commentParser.PARSERS.parse_type,
    description: commentParser.PARSERS.parse_description,
};
// jscs:enable
var RE_SPACE = /\s/;

module.exports = {

    /**
     * @param {string} commentNode
     * @returns {DocComment}
     */