jsdoc-to-assert

Convert comment to assert. runtime assert

MIT 32 个版本
安装
npm install jsdoc-to-assert
yarn add jsdoc-to-assert
pnpm add jsdoc-to-assert
bun add jsdoc-to-assert
README

jsdoc-to-assert Build Status

Convert JSDoc to assert that runtime assert.

Easy to use it with Babel.

Example

jsdoc-to-assert create detection expression string:

Array.isArray(x) && x.every(function (item) {
    return typeof item === 'number';
});

from following JSDoc comment:

/**
 * @param {number[]} x 
 */

Installation

npm install jsdoc-to-assert

Usage

import {AssertGenerator, CommentConverter, CodeGenerator} from "jsdoc-to-assert";

AssertGenerator class

Create assertion AST from comment nodes.

/**
 * @typedef {Object} AssertGeneratorOptions
 * @property {Function} generator
 */
const defaultOptions = {
    Generator: CodeGenerator
};
/**
 *
 * @param {Array<Object>} comments AST's comment nodes. it should be BlockComment
 * @param {AssertGeneratorOptions} options
 * @returns {Array<String>} array of assertion
 */
static createAsserts(comments, options = {});
/**
 * @param tagNode tagNode is defined by doctorin
 * @param {CodeGenerator} Generator
 * @return {string|undefined} return assertion code string
 * Reference https://esdoc.org/tags.html#type-syntax
 * https://github.com/eslint/doctrine/blob/master/test/parse.js
 */
static createAssertFromTag(tagNode, Generator = CodeGenerator);

CommentConverter class

/**
 * Parse comment nodes which is provided by JavaScript parser like esprima, babylon 
 * and return assertions code strings.
 * This is mutable function.
 * @param {Array<Object>} comment
 * @param {AssertGeneratorOptions} [options]
 * @returns {string[]}
 */
static toAsserts(comment, options);    

CodeGenerator class

Assertion code generator class

/**
 * @param commentTagNode commentTagNode is doctrine tag node
 */
constructor(commentTagNode);
/**
 * wrap assert function
 * @param {string} expression
 * @returns {string}
 */
assert(expression) {
    return `console.assert(${expression},'${expression}');`;
}

Tests

npm test

Realated

Comment to assert

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

版本列表
2.8.0 2017-01-18
2.7.1 2016-12-19
2.7.0 2016-12-19
2.6.2 2016-12-19
2.6.1 2016-11-18
2.6.0 2016-11-16
2.5.3 2016-09-07
2.5.2 2016-08-28
2.5.1 2016-08-28
2.5.0 2016-08-28
2.4.5 2016-08-25
2.4.4 2016-08-25
2.4.3 2016-08-25
2.4.2 2016-08-14
2.4.1 2016-08-14
2.4.0 2016-08-14
2.3.1 2016-08-14
2.3.0 2016-08-14
2.2.1 2016-05-25
2.2.0 2016-05-25
2.1.4 2016-03-21
2.1.3 2016-03-21
2.1.2 2016-03-21
2.1.1 2016-03-21
2.1.0 2016-03-21
2.0.2 2016-03-21
2.0.1 2016-03-21
2.0.0 2016-03-21
1.1.2 2016-03-21
1.1.1 2016-03-21
1.1.0 2016-03-21
1.0.1 2016-03-21