Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

unassert-js/unassert

Repository files navigation

unassert

Encouragesprogramming with assertions by providing tools to compile them away.

Build StatusNPM versionCode StyleLicense

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"

RELATED MODULES

INSTALL

$ npm install --save-dev unassert

EXAMPLE

For givenmath.js below,

constassert=require('node:assert');functionadd(a,b){assert(typeofa==='number');console.assert(!isNaN(a));assert.equal(typeofb,'number');assert.ok(!isNaN(b));returna+b;}

ApplyunassertAst 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');constfilepath=join(__dirname,'math.js');constast=parse(readFileSync(filepath),{ecmaVersion:'2022'});constmodifiedAst=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));constfilepath=join(__dirname,'math.js');constast=parse(readFileSync(filepath),{ecmaVersion:'2022'});constmodifiedAst=unassertAst(ast);console.log(generate(modifiedAst));

Then you will see assert calls disappear.

functionadd(a,b){returna+b;}

API

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

const modifiedAst = unassertAst(ast, options)

const{ unassertAst}=require('unassert')
import{unassertAst}from'unassert'
return type
object (ECMAScript AST)

Remove assertion calls fromast (ECMAScript AST). Default behaviour can be customized byoptions.ast is manipulated directly so returnedmodifiedAst will be the same instance ofast.

options

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

options.modules

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,

  • import assert from "assert"
  • import assert from "assert/strict"
  • import assert from "node:assert"
  • import assert from "node:assert/strict"
  • import * as assert from "assert"
  • import * as assert from "node:assert"
  • import * as assert from "assert/strict"
  • import * as assert from "node:assert/strict"
  • import { strict as assert } from "assert"
  • import { strict as assert } from "node:assert"
  • import { default as assert } from "assert"
  • import { default as assert } from "node:assert"
  • const assert = require("assert")
  • const assert = require("node:assert")
  • const assert = require("assert/strict")
  • const assert = require("node:assert/strict")
  • const assert = require("assert").strict
  • const assert = require("node:assert").strict
  • const { strict: assert } = require("assert")
  • const { strict: assert } = require("node:assert")

and assignments.

  • assert = require("assert")
  • assert = require("node:assert")
  • assert = require("assert/strict")
  • assert = require("node:assert/strict")
  • assert = require("assert").strict
  • assert = require("node:assert").strict

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

  • 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.match(string, regexp[, message])
  • assert.doesNotMatch(string, regexp[, message])
  • assert.throws(block, [error], [message])
  • assert.doesNotThrow(block, [message])
  • await assert.rejects(asyncFn, [error], [message])
  • await assert.doesNotReject(asyncFn, [error], [message])
  • assert.fail([message])
  • assert.fail(actual, expected, message, operator)
  • assert.ifError(value)

in addition, unassert removesconsole.assert calls as well.

  • console.assert(value, [message])

Auto Variable Tracking

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

So if import declaration is as follows,

  • import strictAssert, { ok, equal as eq } from 'node:assert/strict';

unassert removes allstrictAssert,ok,eq calls.

Please seecustomization example for more details.

const visitor = createVisitor(options)

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

Create visitor object to be used withestraverse.replace. Visitor can be customized byoptions.

const options = defaultOptions()

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

Returns default options object forunassertAst andcreateVisitor function. In other words, returns

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

CUSTOMIZATION

You can customize options such as target module names.

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

example

For givencustom.js below,

importinvariantfrom'invariant';importnassertfrom'nanoassert';import*asuvuassertfrom'uvu/assert';import{strictaspowerAssert}from'power-assert';import{defaultaslooseAssert}from'node:assert';importstrictAssert,{ok,equalaseq}from'node:assert/strict';asyncfunctionadd(a,b){strictAssert(!isNaN(a));looseAssert(typeofa==='number');eq(typeofb,'number');ok(!isNaN(b));powerAssert(typeofa===typeofb);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');awaitstrictAssert.rejects(prms);awaitstrictAssert.doesNotReject(prms2);returna+b;}

ApplyunassertAst 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));constfilepath=join(__dirname,'custom.js');constast=parse(readFileSync(filepath),{ecmaVersion:'2022'});constmodifiedAst=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.

asyncfunctionadd(a,b){returna+b;}

OUR SUPPORT POLICY

We support Node under maintenance. In other words, we stop supporting old Node version whentheir 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.

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under theMIT license.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp