{"name":"unassert","description":"Remove assertions from AST to encourage Design by Contract (DbC)","version":"1.2.0","author":{"name":"Takuto Wada","email":"takuto.wada@gmail.com","url":"http://github.com/twada"},"bugs":{"url":"https://github.com/twada/unassert/issues"},"dependencies":{"deep-equal":"^1.0.0","escallmatch":"^1.4.1","esprima":"^2.5.0","espurify":"^1.3.0","estraverse":"^4.1.0"},"devDependencies":{"escodegen":"^1.7.0","mocha":"^2.3.3"},"files":["README.md","CHANGELOG.md","index.js","package.json"],"homepage":"http://github.com/twada/unassert","keywords":["DbC","assert","assertion"],"license":"MIT","main":"index.js","repository":{"type":"git","url":"git+ssh://git@github.com/twada/unassert.git"},"scripts":{"test":"mocha"},"gitHead":"9dfc23ebd4e06851fe20818d2fc6814ab103366d","_id":"unassert@1.2.0","_shasum":"aae97474feac4f7c0b016bb8cc4fff9d0720c426","_from":".","_npmVersion":"2.11.3","_nodeVersion":"0.12.7","_npmUser":{"name":"twada","email":"takuto.wada@gmail.com"},"maintainers":[{"name":"twada","email":"takuto.wada@gmail.com"}],"dist":{"shasum":"aae97474feac4f7c0b016bb8cc4fff9d0720c426","tarball":"http://registry-npm.ywwl.com/unassert/download/unassert-1.2.0.tgz","integrity":"sha512-qzUT7fuHbHmjbcKlEbNN6Y9ZBPXTPlWswZkFCo+yVK+pP+eumXXFRh+GSQYIaO39a5AZXTMYPAiGhcoo75qsLg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCaz3L4sOVnZMb3fdTpWCLtMChW5NhEMhFWdXwXBuTlygIhAKR+RBPcPcvM3tAq7422/IGyrRMVxt3cEtshCsbAl1NM"}],"size":2626,"noattachment":false,"key":"/unassert/-/unassert-1.2.0.tgz"},"directories":{},"readme":"[![unassert][unassert-banner]][unassert-url]\n\nEncourages [programming with assertions](https://en.wikipedia.org/wiki/Assertion_(software_development)) by providing tools to compile them away.\n\n[![Build Status][ci-image]][ci-url]\n[![NPM version][npm-image]][npm-url]\n[![Code Style][style-image]][style-url]\n[![License][license-image]][license-url]\n\nSee: \"[unassert - encourage reliable programming by writing assertions in production](http://www.slideshare.net/t_wada/unassert)\" -- 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](https://speakerdeck.com/twada/from-library-to-tool-power-assert-as-a-general-purpose-assertion-enhancement-tool)\"\n\n\n#### RELATED MODULES\n\n- [unassertify](https://github.com/unassert-js/unassertify): Browserify transform for unassert\n- [babel-plugin-unassert](https://github.com/unassert-js/babel-plugin-unassert): Babel plugin for unassert\n- [webpack-unassert-loader](https://github.com/unassert-js/webpack-unassert-loader): Webpack loader for unassert\n- [gulp-unassert](https://github.com/unassert-js/gulp-unassert): Gulp plugin for unassert\n- [unassert-cli](https://github.com/unassert-js/unassert-cli): CLI for unassert\n- [rollup-plugin-unassert](https://github.com/unassert-js/rollup-plugin-unassert): RollupJS plugin for unassert\n\n\nINSTALL\n---------------------------------------\n\n```\n$ npm install --save-dev unassert\n```\n\n\nEXAMPLE\n---------------------------------------\n\nFor given `math.js` below,\n\n```javascript\nconst assert = require('node:assert');\n\nfunction add (a, b) {\n  assert(typeof a === 'number');\n  console.assert(!isNaN(a));\n  assert.equal(typeof b, 'number');\n  assert.ok(!isNaN(b));\n  return a + b;\n}\n```\n\nApply `unassertAst` then generate modified code to console.\n\nvia CJS code\n```javascript\nconst { unassertAst } = require('unassert');\nconst { parse } = require('acorn');\nconst { generate } = require('escodegen');\nconst { readFileSync } = require('node:fs');\nconst { join, dirname } = require('node:path');\n\nconst filepath = join(__dirname, 'math.js');\nconst ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });\nconst modifiedAst = unassertAst(ast);\n\nconsole.log(generate(modifiedAst));\n```\n\nor via ESM code\n```javascript\nimport { unassertAst } from 'unassert';\nimport { parse } from 'acorn';\nimport { generate } from 'escodegen';\nimport { readFileSync } from 'node:fs';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\nconst filepath = join(__dirname, 'math.js');\nconst ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });\nconst modifiedAst = unassertAst(ast);\n\nconsole.log(generate(modifiedAst));\n```\n\nThen you will see assert calls disappear.\n\n```javascript\nfunction add(a, b) {\n  return a + b;\n}\n```\n\n\nAPI\n---------------------------------------\n\nunassert package exports three functions. [`unassertAst`](https://github.com/unassert-js/unassert#const-modifiedast--unassertastast-options) is the main function. [`createVisitor`](https://github.com/unassert-js/unassert#const-visitor--createvisitoroptions) and [`defaultOptions`](https://github.com/unassert-js/unassert#const-options--defaultoptions) are for customization.\n\n\n### const modifiedAst = unassertAst(ast, options)\n\n```javascript\nconst { unassertAst } = require('unassert')\n```\n\n```javascript\nimport { unassertAst } from 'unassert'\n```\n\n| return type                                                   |\n|:--------------------------------------------------------------|\n| `object` ([ECMAScript AST](https://github.com/estree/estree)) |\n\nRemove assertion calls from `ast` ([ECMAScript AST](https://github.com/estree/estree)). Default behaviour can be customized by `options`. `ast` is manipulated directly so returned `modifiedAst` will be the same instance of `ast`.\n\n#### options\n\nObject for configuration options. passed `options` is `Object.assign`ed with default options. If not passed, default options will be used.\n\n##### options.modules\n\nTarget module names for assertion call removal.\n\nFor example, the default target modules are as follows.\n\n```javascript\n{\n  modules: [\n    'assert',\n    'assert/strict',\n    'node:assert',\n    'node:assert/strict'\n  ]\n```\n\nIn this case, unassert will remove assert variable declarations such as,\n\n* `import assert from \"assert\"`\n* `import assert from \"assert/strict\"`\n* `import assert from \"node:assert\"`\n* `import assert from \"node:assert/strict\"`\n* `import * as assert from \"assert\"`\n* `import * as assert from \"node:assert\"`\n* `import * as assert from \"assert/strict\"`\n* `import * as assert from \"node:assert/strict\"`\n* `import { strict as assert } from \"assert\"`\n* `import { strict as assert } from \"node:assert\"`\n* `import { default as assert } from \"assert\"`\n* `import { default as assert } from \"node:assert\"`\n* `const assert = require(\"assert\")`\n* `const assert = require(\"node:assert\")`\n* `const assert = require(\"assert/strict\")`\n* `const assert = require(\"node:assert/strict\")`\n* `const assert = require(\"assert\").strict`\n* `const assert = require(\"node:assert\").strict`\n* `const { strict: assert } = require(\"assert\")`\n* `const { strict: assert } = require(\"node:assert\")`\n\nand assignments.\n\n* `assert = require(\"assert\")`\n* `assert = require(\"node:assert\")`\n* `assert = require(\"assert/strict\")`\n* `assert = require(\"node:assert/strict\")`\n* `assert = require(\"assert\").strict`\n* `assert = require(\"node:assert\").strict`\n\nIn this default case, unassert will remove assertion calls such as,\n\n* `assert(value, [message])`\n* `assert.ok(value, [message])`\n* `assert.equal(actual, expected, [message])`\n* `assert.notEqual(actual, expected, [message])`\n* `assert.strictEqual(actual, expected, [message])`\n* `assert.notStrictEqual(actual, expected, [message])`\n* `assert.deepEqual(actual, expected, [message])`\n* `assert.notDeepEqual(actual, expected, [message])`\n* `assert.deepStrictEqual(actual, expected, [message])`\n* `assert.notDeepStrictEqual(actual, expected, [message])`\n* `assert.match(string, regexp[, message])`\n* `assert.doesNotMatch(string, regexp[, message])`\n* `assert.throws(block, [error], [message])`\n* `assert.doesNotThrow(block, [message])`\n* `await assert.rejects(asyncFn, [error], [message])`\n* `await assert.doesNotReject(asyncFn, [error], [message])`\n* `assert.fail([message])`\n* `assert.fail(actual, expected, message, operator)`\n* `assert.ifError(value)`\n\nin addition, unassert removes `console.assert` calls as well.\n\n* `console.assert(value, [message])`\n\n\n#### Auto Variable Tracking\n\nunassert automatically removes assertion calls based on their imported variable names.\n\nSo if import declaration is as follows,\n\n* `import strictAssert, { ok, equal as eq } from 'node:assert/strict';`\n\nunassert removes all `strictAssert`, `ok`, `eq` calls.\n\nPlease see [customization example](https://github.com/unassert-js/unassert#example-1) for more details.\n\n\n### const visitor = createVisitor(options)\n\n```javascript\nconst { createVisitor } = require('unassert')\n```\n\n```javascript\nimport { createVisitor } from 'unassert'\n```\n\n| return type                                                                       |\n|:----------------------------------------------------------------------------------|\n| `object` (visitor object for [estraverse](https://github.com/estools/estraverse)) |\n\nCreate visitor object to be used with `estraverse.replace`. Visitor can be customized by `options`.\n\n\n### const options = defaultOptions()\n\n```javascript\nconst { defaultOptions } = require('unassert')\n```\n\n```javascript\nimport { defaultOptions } from 'unassert'\n```\n\nReturns default options object for `unassertAst` and `createVisitor` function. In other words, returns\n\n```javascript\n{\n  modules: [\n    'assert',\n    'assert/strict',\n    'node:assert',\n    'node:assert/strict'\n  ]\n}\n```\n\nCUSTOMIZATION\n---------------------------------------\n\nYou can customize options such as target module names.\n\n```javascript\n{\n  modules: [\n    'node:assert',\n    'node:assert/strict',\n    'power-assert',\n    'invariant',\n    'nanoassert',\n    'uvu/assert'\n  ]\n}\n```\n\n### example\n\nFor given `custom.js` below,\n\n```javascript\nimport invariant from 'invariant';\nimport nassert from 'nanoassert';\nimport * as uvuassert from 'uvu/assert';\nimport { strict as powerAssert } from 'power-assert';\nimport { default as looseAssert } from 'node:assert';\nimport strictAssert, { ok, equal as eq } from 'node:assert/strict';\n\nasync function add (a, b) {\n  strictAssert(!isNaN(a));\n  looseAssert(typeof a === 'number');\n  eq(typeof b, 'number');\n  ok(!isNaN(b));\n  powerAssert(typeof a === typeof b);\n\n  nassert(!isNaN(a));\n\n  uvuassert.is(Math.sqrt(4), 2);\n  uvuassert.is(Math.sqrt(144), 12);\n  uvuassert.is(Math.sqrt(2), Math.SQRT2);\n\n  invariant(someTruthyVal, 'This will not throw');\n  invariant(someFalseyVal, 'This will throw an error with this message');\n\n  await strictAssert.rejects(prms);\n  await strictAssert.doesNotReject(prms2);\n\n  return a + b;\n}\n```\n\nApply `unassertAst` with customized options then generate modified code to console.\n\n```javascript\nimport { unassertAst } from 'unassert';\nimport { parse } from 'acorn';\nimport { generate } from 'escodegen';\nimport { readFileSync } from 'node:fs';\nimport { join, dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\nconst filepath = join(__dirname, 'custom.js');\nconst ast = parse(readFileSync(filepath), { ecmaVersion: '2022' });\nconst modifiedAst = unassertAst(ast, {\n  modules: [\n    'node:assert',\n    'node:assert/strict',\n    'power-assert',\n    'invariant',\n    'nanoassert',\n    'uvu/assert'\n  ]\n});\n\nconsole.log(generate(modifiedAst));\n```\n\nThen you will see all assert calls disappear.\n\n```javascript\nasync function add(a, b) {\n  return a + b;\n}\n```\n\n\nOUR SUPPORT POLICY\n---------------------------------------\n\nWe support Node under maintenance. In other words, we stop supporting old Node version when [their maintenance ends](https://github.com/nodejs/LTS).\n\nThis means that any other environment is not supported.\n\nNOTE: 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.\n\n\nAUTHOR\n---------------------------------------\n* [Takuto Wada](https://github.com/twada)\n\n\nCONTRIBUTORS\n---------------------------------------\n* [Renée Kooi](https://github.com/goto-bus-stop)\n* [Jordan Harband](https://github.com/ljharb)\n\n\nLICENSE\n---------------------------------------\nLicensed under the [MIT](https://github.com/unassert-js/unassert/blob/master/LICENSE) license.\n\n\n[unassert-url]: https://github.com/unassert-js/unassert\n[unassert-banner]: https://raw.githubusercontent.com/unassert-js/unassert-js-logo/master/banner/banner-official-fullcolor.png\n\n[npm-url]: https://npmjs.org/package/unassert\n[npm-image]: https://badge.fury.io/js/unassert.svg\n\n[ci-image]: https://github.com/unassert-js/unassert/workflows/Node.js%20CI/badge.svg\n[ci-url]: https://github.com/unassert-js/unassert/actions?query=workflow%3A%22Node.js+CI%22\n\n[style-url]: https://github.com/standard/semistandard\n[style-image]: https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg\n\n[license-url]: https://github.com/unassert-js/unassert/blob/master/LICENSE\n[license-image]: https://img.shields.io/badge/license-MIT-brightgreen.svg\n","publish_time":1443172716161,"_cnpm_publish_time":1443172716161,"dist-tags":{"latest":"2.0.2"}}