npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-jsdoc-runtime-typecheck

v1.2.1

Published

Babel plugin, that adds typecheck, based on jsDoc.

Downloads

34

Readme

#Babel jsDoc runtime typecheck

npm license npm Travis

Overview

This plugin will add runtime typecheck, based on jsDoc annotation. It transform code like this:

// from

/**
 * @param {Number} a
 * @returns {Number}
 * @typecheck
 */
function test(a) {
    return a;
}

// to

function test(a) {
    __executeTypeCheck__('test', 'a', a, 'Number');
    
    return __executeTypeCheck__('test', 'return', a, 'Number');
}

Result:

CAUTION: Use this plugin only in development, it will slow down your code (a lot of additional function calls and large helper function).

Motivation

Flow is good solution, but it adds custom syntax to javascript code and adding it to existing project is quite hard. IDE's like Webstorm has good support of jsDoc and can add cool code completion tips, based on users comments. So, with this plugin, you can easy start to use benefits of strong typing in javascript code without any pain. Using this plugin in development also will speed up development, because it will reduce number of weird errors and behaviors.

How to

Install

npm install babel-plugin-jsdoc-runtime-typecheck --save-dev

Use

.babelrc

{
    "plugins": ["jsdoc-runtime-typecheck"]
}

js code - global directive

// @typecheck

/**
 * @param {String} str
 * @returns {String}
 */
function makeMeLaugh(str) {
    return str + ' - ha-ha-ha!';
}

js code - local directive

/**
 * @param {String} str
 * @returns {String}
 * @typecheck
 */
function makeMeLaugh(str) {
    return str + ' - ha-ha-ha!';
}

Configure

useDirective

By default, plugin will only parse docs with special directive @typecheck, you can change it like this:

{
    "plugins": [
        ["jsdoc-runtime-typecheck",
            {
                //useDirective: 'typecheck' - this is default
                //useDirective: false - if you want to check all functions with jsDoc (useful for new projects)
                useDirective: 'makeMeHappy' - your custom directive
            }
        ]
    ]
}

Then, use it:

// @makeMeHappy 

// or

/**
 * @makeMeHappy 
 * @param {Number} a
 * @returns {Number}
 */

useStrict

You can enable strict mode - in this mode plugin throw compilation exception when it can find error by static analyze.

Setup:

{
    "plugins": [
        ["jsdoc-runtime-typecheck",
            {
                //useStrict: false - default
                useStrict: true
            }
        ]
    ]
}

Use:

Code:

/**
 * @param {Number} a
 * @param {Number} b
 * @returns {Number}
 * @typecheck
 */
function test(a, b, c) {
    return a + b + c;
}

Result in console:

SyntaxError: input.js: [TYPECHECK STRICT MODE]: Function argument type annotation missing.
   5 |  * @typecheck
   6 |  */
>  7 | function test(a, b, c) {
     |                     ^
   8 |     return a + b + c;
   9 | }
  10 | 

Supports:

jsDoc tags

  • @params can be optional, supported declarations:
    • @param {*} name - no check

    • @param {Number=} name - optional

    • @param {Number} [name] - optional

    • @param {?Number} name

    • @param {!Number} name

    • @param {Number|String} id

    • @param {Array<Number>} collection - check every item in array

    • Check defined keys in Object:

      @param {Object} data 
      @param {Number} data.id
      @param {String} data.name
            
      //or
            
      @param {{id: Number, name: String}} data
    • @param {function(Array<Number>)} name - check type of function

  • @returns or @return - type annotation are same as in params.

Language constructions

Function declaration

/**
 * @param {Number} a
 * @returns {Number}
 */
function myDeclaredFunction(a) {
    return a;
}

/**
 * @param {Number} a
 * @returns {Number}
 */
let myExpressionFunction = function(a) {
    return a;
};

/**
 * @param {Number} a
 * @returns {Number}
 */
let myArrowFunction = (a) => {
    return a;
};

/**
 * @param {Number} a
 * @returns {Number}
 * In this case it will transform body to "{ return a; }" block
 */
let myArrowExpressionFunction = (a) => a;

Object method

let myObject = {
    /**
     * @param {Number} a
     * @returns {Number}
     */
    myMethod(a) {
        return a;
    },
    
    /**
     * @param {Number} a
     * @returns {Number}
     * Will use object field name as function name ("myField" here)
     */
    myField: function(a) {
        return a;
    }
}

Class constructor and method

class MyClass {
    /**
     * @param {Number} a
     */
    constructor(a) {
        this._a = a;
    }
    
    /**
     * @param {Number} a
     * @returns {Number}
     */
    myMethod(a) {
       return a;
    }
    
    /**
     * @param {Number} a
     * @returns {Number}
     */
    static myStaticMethod(a) {
       return a;
    }
    
    /**
     * @param {Number} a
     */
    set a(a) {
        this._a = a;
    }
    
    /**
     * @returns {Number} a
     */
    get a() {
       return this._a;
    }
}