A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/unassert-js/babel-plugin-unassert below:

unassert-js/babel-plugin-unassert: Babel plugin to encourage reliable programming by writing assertions in production code, and compiling them away from release.

Babel plugin for unassert: Encourages programming with assertions by providing tools to compile them away.

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

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

Babel7 is incompatible with Babel6. Babel6 is incompatible with Babel5.

For Babel6, you need to use the 2.x release of babel-plugin-unassert.

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

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

$ npm install --save-dev babel-plugin-unassert@1
const presets = ['@babel/env'];
const plugins = [];

if (process.env.NODE_ENV === 'production') {
  plugins.push('babel-plugin-unassert');
}

module.exports = { presets, plugins };
$ babel /path/to/src/target.js > /path/to/build/target.js
$ babel --plugins babel-plugin-unassert /path/to/src/target.js > /path/to/build/target.js
const babel = require('@babel/core');
const transformed = babel.transformFileSync('/path/to/src/target.js', {
  presets: ['@babel/env'],
  plugins: ['babel-plugin-unassert']
});
console.log(transformed.code);

For given math.js below,

'use strict';

const 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-cli with --plugins babel-plugin-unassert option to transform.

$ babel --plugins babel-plugin-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.

For given babel.config.js,

module.exports = function (api) {
  const presets = ['@babel/env'];
  const plugins = [];

  if (api.env(['development', 'test'])) {
    presets.push('babel-preset-power-assert');
  }

  if (api.env('production')) {
    plugins.push('babel-plugin-unassert');
  }

  return {
    presets,
    plugins
  };
};

and production code below,

import assert from 'assert';

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

then it becomes in production,

'use strict';

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

and in development, produces power-assert messages like below

AssertionError:   # example.js:5

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

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).

babel-plugin-unassert also removes assert variable declarations such as,

and assignments.

You can customize Plugin Options such as assertion patterns.

input:

'use strict';

var invariant = require('invariant');
const nassert = require('nanoassert');
import * as uassert from 'uvu/assert';


function add (a, b) {
    nassert(!isNaN(a));

    uassert.is(Math.sqrt(4), 2);
    uassert.is(Math.sqrt(144), 12);
    uassert.is(Math.sqrt(2), Math.SQRT2);

    invariant(someTruthyVal, 'This will not throw');
    invariant(someFalseyVal, 'This will throw an error with this message');

    return a + b;
}

output:

'use strict';

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

via Config Files

{
  "presets": [
    ...
  ],
  "plugins": [
    ["babel-plugin-unassert", {
      "variables": [
        "assert",
        "invariant",
        "nassert",
        "uassert"
      ],
      "modules": [
        "assert",
        "node:assert",
        "invariant",
        "nanoassert",
        "uvu/assert"
      ]
    }]
  ]
}

or via @babel/register.

require('@babel/register')({
  presets: [...],
  plugins: [
    ['babel-plugin-unassert', {
      variables: [
        'assert',
        'invariant',
        'nassert',
        'uassert'
      ],
      modules: [
        'assert',
        'node:assert',
        'invariant',
        'nanoassert',
        'uvu/assert'
      ]
    }]
  ]
});

or via '@babel/core',

const babel = require('@babel/core');
const jsCode = fs.readFileSync('/path/to/test/some_test.js');
const transformed = babel.transform(jsCode, {
  presets: [...],
  plugins: [
    ['babel-plugin-unassert', {
      variables: [
        'assert',
        'invariant',
        'nassert',
        'uassert'
      ],
      modules: [
        'assert',
        'node:assert',
        'invariant',
        'nanoassert',
        'uvu/assert'
      ]
    }]
  ]
});
console.log(transformed.code);
type default value object objects shown below

Configuration options for babel-plugin-unassert. If not passed, default options will be used.

{
  variables: [
    'assert'
  ],
  modules: [
    'assert',
    'power-assert',
    'node:assert'
  ]
}

We support Node under maintenance. In other words, we stop supporting old Node version when their maintenance ends.

This means that any other environment is not supported.

NOTE: If babel-plugin-unassert works in any of the unsupported environments, it is purely coincidental and has no bearing on future compatibility. Use at your own risk.

Licensed under the MIT license.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4