0.3.4 • Published 7 years ago

babel-plugin-jsdoc-to-condition v0.3.4

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

jsdoc-to-condition Build Status

Babel plugin which сreates validation code from jsdoc comments

Example

Before:

/**
 * @param {number} param - this is a param.
 * @param {string} b - this is a param.
 * @param {string[]} [c] - this is a param.
 */
function lonelyFunction(param, b, c) {
}

After:

/**
 * @param {number} param - this is a param.
 * @param {string} b - this is a param.
 * @param {string[]} [c] - this is a param.
 */
function lonelyFunction(param, b, c) {
  if (!(typeof param === 'number')) {
    console._warn('actual.js:6:0: Expected `param` to have type number, got: ' + typeof param);
  }

  if (!(typeof b === 'string')) {
    console._warn('actual.js:6:0: Expected `b` to have type string, got: ' + typeof b);
  }

  if (!(c === undefined || Array.isArray(c) && c.every(function (n) {
    return typeof n === 'string';
  }))) {
    console._warn('actual.js:6:0: Expected `c` to have type Array.<string>=, got: ' + typeof c);
  }
}

Usage

yarn add babel-plugin-jsdoc-to-condition --dev
// .babelrc
{
    "plugins": [
        ["jsdoc-to-condition", {
            "ignore": ["node_modules/"],
            "logger": "debugger; console.info"
        }]
    ]
}