JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 30765
  • Score
    100M100P100Q143786F
  • License MIT

Babel plugin to encourage reliable programming by writing assertions in production code, and compiling them away from release

Package Exports

  • babel-plugin-unassert

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-unassert) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

babel-plugin-unassert

Babel plugin to encourage reliable programming by writing assertions in production code, and compiling them away from release.

Build Status NPM version Dependency Status License

babel-plugin-unassert removes assertions on build. So you can use assertions to declare preconditions, postconditions and invariants.

INSTALL

$ npm install --save-dev babel-plugin-unassert

CAUTION

For Babel 5 or lower, you need to use the 1.2.x release of babel-plugin-unassert.

$ npm install --save-dev babel-plugin-unassert@1.2.0

HOW TO USE

via Babel CLI

$ babel --plugins babel-plugin-unassert /path/to/src/target.js > /path/to/build/target.js

or shortly,

$ babel --plugins unassert /path/to/src/target.js > /path/to/build/target.js

via Babel API

var babel = require('babel-core');
var jsCode = fs.readFileSync('/path/to/src/target.js');
var transformed = babel.transform(jsCode, {
    presets: [...],
    plugins: ['babel-plugin-unassert']
});
console.log(transformed.code);

via .babelrc

{
  "presets": [
    ...
  ],
  "env": {
    "production": {
      "plugins": [
        "babel-plugin-unassert"
      ]
    }
  }
}
$ babel /path/to/src/target.js > /path/to/build/target.js

EXAMPLE

For given math.js below,

'use strict';

var assert = require('assert');

function add (a, b) {
    console.assert(typeof a === 'number');
    assert(!isNaN(a));
    assert.equal(typeof b, 'number');
    assert.ok(!isNaN(b));
    return a + b;
}

Run babel with --plugins unassert to transform tests.

$ babel --plugins unassert /path/to/demo/math.js > /path/to/build/math.js

You will see assert calls and declarations disappear.

'use strict';

function add(a, b) {
    return a + b;
}

ES6 module and power-assert support

babel-plugin-unassert supports ES6 module syntax and power-assert.

with .babelrc,

{
  "presets": [
    ...
  ],
  "env": {
    "development": {
      "plugins": [
        "babel-plugin-espower"
      ],
    },
    "production": {
      "plugins": [
        "babel-plugin-unassert"
      ]
    }
  }
}

production code below

import assert from 'power-assert';

class Calc {
    add (a, b) {
        assert(!(isNaN(a) || isNaN(b)));
        assert(typeof a === 'number');
        assert(typeof b === 'number');
        return a + b;
    }
}

becomes

'use strict';

class Calc {
    add(a, b) {
        return a + b;
    }
}

in production, and produces power-assert messages like

AssertionError:   # example.js:5

  assert(!(isNaN(a) || isNaN(b)))
         | |     |  |  |     |
         | |     |  |  true  NaN
         | false 3  true
         false

in development.

SUPPORTED PATTERNS

Assertion expressions are removed when they match patterns below. In other words, babel-plugin-unassert removes assertion calls that are compatible with Node.js standard assert API (and console.assert).

  • assert(value, [message])
  • assert.ok(value, [message])
  • assert.equal(actual, expected, [message])
  • assert.notEqual(actual, expected, [message])
  • assert.strictEqual(actual, expected, [message])
  • assert.notStrictEqual(actual, expected, [message])
  • assert.deepEqual(actual, expected, [message])
  • assert.notDeepEqual(actual, expected, [message])
  • assert.deepStrictEqual(actual, expected, [message])
  • assert.notDeepStrictEqual(actual, expected, [message])
  • assert.fail(actual, expected, message, operator)
  • assert.throws(block, [error], [message])
  • assert.doesNotThrow(block, [message])
  • assert.ifError(value)
  • console.assert(value, [message])

babel-plugin-unassert also removes assert variable declarations,

  • var assert = require("assert")
  • var assert = require("power-assert")
  • import assert from "assert"
  • import assert from "power-assert"

and assignments.

  • assert = require("assert")
  • assert = require("power-assert")

AUTHOR

LICENSE

Licensed under the MIT license.