A RetroSearch Logo

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

Search Query:

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

unassert-js/unassert: Encourages programming with assertions by providing tools to compile them away.

Encourages programming with assertions by providing tools to compile them away.

See: "unassert - encourage reliable programming by writing assertions in production" -- talk at NodeFest 2015, and "One more thing..." in talk at NodeFest 2016, titled "From Library to Tool - power-assert as a General Purpose Assertion Enhancement Tool"

$ npm install --save-dev unassert

For given math.js below,

const assert = require('node:assert');

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

Apply unassertAst then generate modified code to console.

via CJS code

const { unassertAst } = require('unassert');
const { parse } = require('acorn');
const { generate } = require('escodegen');
const { readFileSync } = require('node:fs');
const { join, dirname } = require('node:path');

const filepath = join(__dirname, 'math.js');
const ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });
const modifiedAst = unassertAst(ast);

console.log(generate(modifiedAst));

or via ESM code

import { unassertAst } from 'unassert';
import { parse } from 'acorn';
import { generate } from 'escodegen';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));

const filepath = join(__dirname, 'math.js');
const ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });
const modifiedAst = unassertAst(ast);

console.log(generate(modifiedAst));

Then you will see assert calls disappear.

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

unassert package exports three functions. unassertAst is the main function. createVisitor and defaultOptions are for customization.

const modifiedAst = unassertAst(ast, options)
const { unassertAst } = require('unassert')
import { unassertAst } from 'unassert'

Remove assertion calls from ast (ECMAScript AST). Default behaviour can be customized by options. ast is manipulated directly so returned modifiedAst will be the same instance of ast.

Object for configuration options. passed options is Object.assigned with default options. If not passed, default options will be used.

Target module names for assertion call removal.

For example, the default target modules are as follows.

{
  modules: [
    'assert',
    'assert/strict',
    'node:assert',
    'node:assert/strict'
  ]

In this case, unassert will remove assert variable declarations such as,

and assignments.

In this default case, unassert will remove assertion calls such as,

in addition, unassert removes console.assert calls as well.

unassert automatically removes assertion calls based on their imported variable names.

So if import declaration is as follows,

unassert removes all strictAssert, ok, eq calls.

Please see customization example for more details.

const visitor = createVisitor(options)
const { createVisitor } = require('unassert')
import { createVisitor } from 'unassert'
return type object (visitor object for estraverse)

Create visitor object to be used with estraverse.replace. Visitor can be customized by options.

const options = defaultOptions()
const { defaultOptions } = require('unassert')
import { defaultOptions } from 'unassert'

Returns default options object for unassertAst and createVisitor function. In other words, returns

{
  modules: [
    'assert',
    'assert/strict',
    'node:assert',
    'node:assert/strict'
  ]
}

You can customize options such as target module names.

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

For given custom.js below,

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';

async 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');

  await strictAssert.rejects(prms);
  await strictAssert.doesNotReject(prms2);

  return a + b;
}

Apply unassertAst with customized options then generate modified code to console.

import { unassertAst } from 'unassert';
import { parse } from 'acorn';
import { generate } from 'escodegen';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));

const filepath = join(__dirname, 'custom.js');
const ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });
const modifiedAst = unassertAst(ast, {
  modules: [
    'node:assert',
    'node:assert/strict',
    'power-assert',
    'invariant',
    'nanoassert',
    'uvu/assert'
  ]
});

console.log(generate(modifiedAst));

Then you will see all assert calls disappear.

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

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