unassert-js / unassert

Encourages programming with assertions by providing tools to compile them away.
MIT License
192 stars 13 forks source link

Variable Tracking: remove assertion calls based on their imported variable names #34

Closed twada closed 2 years ago

twada commented 2 years ago

Automatically remove assertion calls based on their imported variable names.

EXAMPLE

options:

{
  modules: [
    'node:assert',
    'node:assert/strict',
    'power-assert',
    'invariant',
    'nanoassert',
    'uvu/assert'
  ]
}

input:

import invariant from 'invariant';
import nassert from 'nanoassert';
import * as uvuassert from 'uvu/assert';
import { strict as powerAssert } from 'power-assert';
import { default as looseAssert } from 'node:assert';
import strictAssert, { ok, equal as eq } from 'node:assert/strict';

function add (a, b) {
  strictAssert(!isNaN(a));
  looseAssert(typeof a === 'number');
  eq(typeof b, 'number');
  ok(!isNaN(b));
  powerAssert(typeof a === typeof b);

  nassert(!isNaN(a));

  uvuassert.is(Math.sqrt(4), 2);
  uvuassert.is(Math.sqrt(144), 12);
  uvuassert.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:

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