Skip to content

A babel plugin which creates validation code from jsdoc comments

Notifications You must be signed in to change notification settings

jakwuh/babel-plugin-jsdoc-to-condition

 
 

Repository files navigation

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"
        }]
    ]
}

About

A babel plugin which creates validation code from jsdoc comments

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%