├── .gitignore ├── test ├── fixtures │ ├── esm_default_binding │ │ ├── expected.mjs │ │ ├── expected-presets-env.mjs │ │ └── fixture.mjs │ ├── esm_import_specifier │ │ ├── expected.mjs │ │ ├── expected-presets-env.mjs │ │ └── fixture.mjs │ ├── esm_namespace_import │ │ ├── expected.mjs │ │ ├── expected-presets-env.mjs │ │ └── fixture.mjs │ ├── cjs │ │ ├── expected.js │ │ └── fixture.js │ ├── customization │ │ ├── expected.js │ │ └── fixture.js │ ├── esm_default_binding_node_protocol │ │ ├── expected.mjs │ │ ├── expected-presets-env.mjs │ │ └── fixture.mjs │ ├── esm_default_binding_powerassert │ │ ├── expected.mjs │ │ ├── expected-presets-env.mjs │ │ └── fixture.mjs │ ├── cjs_node_protocol │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_powerassert │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_strictmode │ │ ├── expected.js │ │ └── fixture.js │ ├── node_assert_api │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_assignment_strictmode │ │ ├── expected.js │ │ ├── expected-presets-env.js │ │ └── fixture.js │ ├── cjs_node_protocol_strictmode │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_powerassert_strictmode │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_assignment │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_assignment_singlevar │ │ ├── expected.js │ │ ├── expected-presets-env.js │ │ └── fixture.js │ ├── cjs_singlevar │ │ ├── expected.js │ │ └── fixture.js │ ├── cjs_singlevar_strictmode │ │ ├── expected.js │ │ └── fixture.js │ ├── not_an_expression_statement │ │ ├── expected.js │ │ └── fixture.js │ └── non_block_statement │ │ ├── expected.js │ │ └── fixture.js ├── customization-test.js └── test.js ├── .github └── workflows │ └── node.js.yml ├── LICENSE ├── package.json ├── index.js ├── CHANGELOG.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding/expected.mjs: -------------------------------------------------------------------------------- 1 | function add(a, b) { 2 | return a + b; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/esm_import_specifier/expected.mjs: -------------------------------------------------------------------------------- 1 | function add(a, b) { 2 | return a + b; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/esm_namespace_import/expected.mjs: -------------------------------------------------------------------------------- 1 | function add(a, b) { 2 | return a + b; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/cjs/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/customization/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_node_protocol/expected.mjs: -------------------------------------------------------------------------------- 1 | function add(a, b) { 2 | return a + b; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_powerassert/expected.mjs: -------------------------------------------------------------------------------- 1 | function add(a, b) { 2 | return a + b; 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/cjs_node_protocol/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_powerassert/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_strictmode/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/node_assert_api/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_strictmode/expected.js: -------------------------------------------------------------------------------- 1 | var assert; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_node_protocol_strictmode/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_powerassert_strictmode/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding/expected-presets-env.mjs: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_import_specifier/expected-presets-env.mjs: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_namespace_import/expected-presets-env.mjs: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert; 4 | 5 | function add(a, b) { 6 | return a + b; 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_strictmode/expected-presets-env.js: -------------------------------------------------------------------------------- 1 | var assert; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_node_protocol/expected-presets-env.mjs: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_powerassert/expected-presets-env.mjs: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function add(a, b) { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_singlevar/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var add, assert; 4 | 5 | add = function (a, b) { 6 | return a + b; 7 | }; 8 | -------------------------------------------------------------------------------- /test/fixtures/cjs_singlevar/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var foo = 'FOO', 4 | bar = 'BAR'; 5 | 6 | function add(a, b) { 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_singlevar/expected-presets-env.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var add, assert; 4 | 5 | add = function add(a, b) { 6 | return a + b; 7 | }; 8 | -------------------------------------------------------------------------------- /test/fixtures/cjs_singlevar_strictmode/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var foo = 'FOO', 4 | bar = 'BAR'; 5 | 6 | function add(a, b) { 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding/fixture.mjs: -------------------------------------------------------------------------------- 1 | import assert from 'assert'; 2 | 3 | function add (a, b) { 4 | assert(!isNaN(a)); 5 | assert.equal(typeof b, 'number'); 6 | assert.ok(!isNaN(b)); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/esm_namespace_import/fixture.mjs: -------------------------------------------------------------------------------- 1 | import * as assert from 'assert'; 2 | 3 | function add (a, b) { 4 | assert(!isNaN(a)); 5 | assert.equal(typeof b, 'number'); 6 | assert.ok(!isNaN(b)); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/esm_import_specifier/fixture.mjs: -------------------------------------------------------------------------------- 1 | import {strict as assert} from 'assert'; 2 | 3 | function add (a, b) { 4 | assert(!isNaN(a)); 5 | assert.equal(typeof b, 'number'); 6 | assert.ok(!isNaN(b)); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/cjs/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.ok(!isNaN(b)); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_node_protocol/fixture.mjs: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | 3 | function add (a, b) { 4 | assert(!isNaN(a)); 5 | assert.equal(typeof b, 'number'); 6 | assert.ok(!isNaN(b)); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/esm_default_binding_powerassert/fixture.mjs: -------------------------------------------------------------------------------- 1 | import assert from 'power-assert'; 2 | 3 | function add (a, b) { 4 | assert(!isNaN(a)); 5 | assert.equal(typeof b, 'number'); 6 | assert.ok(!isNaN(b)); 7 | return a + b; 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/not_an_expression_statement/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | if (assert(a)) { 5 | return null; 6 | } 7 | 8 | if (!assert(b)) { 9 | return null; 10 | } 11 | 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /test/fixtures/cjs_node_protocol/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('node:assert'); 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.ok(!isNaN(b)); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/cjs_powerassert/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('power-assert'); 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.ok(!isNaN(b)); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/cjs_strictmode/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert').strict; 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.deepEqual({a: 1}, {a: '1'}); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/non_block_statement/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | if (!isNaN(a)) {} 5 | 6 | if (typeof b === 'number') {} 7 | 8 | if (typeof a === 'number') {} else if (typeof b === 'number') {} else {} 9 | 10 | return a + b; 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/cjs_node_protocol_strictmode/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('node:assert').strict; 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.ok(!isNaN(b)); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/cjs_powerassert_strictmode/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('power-assert').strict; 4 | 5 | function add (a, b) { 6 | assert(!isNaN(a)); 7 | assert.equal(typeof b, 'number'); 8 | assert.ok(!isNaN(b)); 9 | return a + b; 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/cjs_singlevar/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var foo = 'FOO', 4 | assert = require('assert'), 5 | bar = 'BAR'; 6 | 7 | function add (a, b) { 8 | assert(!isNaN(a)); 9 | assert.equal(typeof b, 'number'); 10 | assert.ok(!isNaN(b)); 11 | return a + b; 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_strictmode/fixture.js: -------------------------------------------------------------------------------- 1 | var assert; 2 | assert = require('assert').strict; 3 | function add (a, b) { 4 | console.assert(typeof a === 'number'); 5 | assert(!isNaN(a)); 6 | assert.equal(typeof b, 'number'); 7 | assert.ok(!isNaN(b)); 8 | return a + b; 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert; 4 | assert = require('assert'); 5 | function add (a, b) { 6 | console.assert(typeof a === 'number'); 7 | assert(!isNaN(a)); 8 | assert.equal(typeof b, 'number'); 9 | assert.ok(!isNaN(b)); 10 | return a + b; 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/cjs_singlevar_strictmode/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var foo = 'FOO', 4 | assert = require('assert').strict, 5 | bar = 'BAR'; 6 | 7 | function add (a, b) { 8 | assert(!isNaN(a)); 9 | assert.equal(typeof b, 'number'); 10 | assert.ok(!isNaN(b)); 11 | return a + b; 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/not_an_expression_statement/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add(a, b) { 4 | assert(!isNaN(a), 'assertion message'); 5 | if (assert(a)) { 6 | return null; 7 | } 8 | console.assert(a); 9 | if (!assert(b)) { 10 | return null; 11 | } 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /test/fixtures/cjs_assignment_singlevar/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var add, assert; 4 | assert = require('assert'); 5 | add = function (a, b) { 6 | console.assert(typeof a === 'number'); 7 | assert(!isNaN(a)); 8 | assert.equal(typeof b, 'number'); 9 | assert.ok(!isNaN(b)); 10 | return a + b; 11 | }; 12 | -------------------------------------------------------------------------------- /test/fixtures/non_block_statement/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | 5 | function add (a, b) { 6 | if (!isNaN(a)) assert(0 < a); 7 | if (typeof b === 'number') { 8 | assert(0 < b); 9 | } 10 | 11 | if (typeof a === 'number') 12 | assert(0 < a); 13 | else if (typeof b === 'number') 14 | assert(0 < b); 15 | else 16 | assert(false); 17 | 18 | return a + b; 19 | } 20 | -------------------------------------------------------------------------------- /test/fixtures/customization/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var invariant = require('invariant'); 4 | const nassert = require('nanoassert'); 5 | import * as uassert from 'uvu/assert'; 6 | 7 | 8 | function add (a, b) { 9 | nassert(!isNaN(a)); 10 | 11 | uassert.is(Math.sqrt(4), 2); 12 | uassert.is(Math.sqrt(144), 12); 13 | uassert.is(Math.sqrt(2), Math.SQRT2); 14 | 15 | invariant(someTruthyVal, 'This will not throw'); 16 | invariant(someFalseyVal, 'This will throw an error with this message'); 17 | 18 | return a + b; 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [12.x, 14.x, 15.x, 16.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v2 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v2 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | - run: npm ci 29 | - run: npm run build --if-present 30 | - run: npm test 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2021 Takuto Wada, https://github.com/unassert-js/babel-plugin-unassert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /test/customization-test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | delete require.cache[require.resolve('..')]; 4 | const unassert = require('..'); 5 | const assert = require('assert'); 6 | const fs = require('fs'); 7 | const path = require('path'); 8 | const babel = require('@babel/core'); 9 | 10 | const testTransform = (fixtureName, unassertOptions) => { 11 | it(fixtureName, () => { 12 | unassertOptions = unassertOptions || {}; 13 | const fixtureFilepath = path.resolve(__dirname, 'fixtures', fixtureName, `fixture.js`); 14 | const expectedFilepath = path.resolve(__dirname, 'fixtures', fixtureName, `expected.js`); 15 | const result = babel.transformFileSync(fixtureFilepath, { 16 | plugins: [ 17 | [unassert, unassertOptions] 18 | ] 19 | }); 20 | const actual = result.code; 21 | const expected = fs.readFileSync(expectedFilepath).toString(); 22 | assert.strictEqual(actual + '\n', expected); 23 | }); 24 | }; 25 | 26 | describe('customization option', () => { 27 | testTransform('customization', { 28 | variables: [ 29 | 'invariant', 30 | 'nassert', 31 | 'uassert' 32 | ], 33 | modules: [ 34 | 'invariant', 35 | 'nanoassert', 36 | 'uvu/assert' 37 | ] 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-unassert", 3 | "description": "Babel plugin for unassert: Encourages programming with assertions by providing tools to compile them away", 4 | "version": "3.2.0", 5 | "author": { 6 | "name": "Takuto Wada", 7 | "email": "takuto.wada@gmail.com", 8 | "url": "https://github.com/twada" 9 | }, 10 | "bugs": "https://github.com/unassert-js/babel-plugin-unassert/issues", 11 | "contributors": [ 12 | { 13 | "name": "Notas Hellout", 14 | "url": "https://github.com/make-github-pseudonymous-again" 15 | } 16 | ], 17 | "devDependencies": { 18 | "@babel/core": "^7.0.0", 19 | "@babel/preset-env": "^7.0.0", 20 | "mocha": "^9.0.0", 21 | "semistandard": "^16.0.0", 22 | "snazzy": "^9.0.0" 23 | }, 24 | "files": [ 25 | "README.md", 26 | "CHANGELOG.md", 27 | "LICENSE", 28 | "index.js", 29 | "package.json" 30 | ], 31 | "homepage": "https://github.com/unassert-js/babel-plugin-unassert", 32 | "keywords": [ 33 | "DbC", 34 | "assert", 35 | "babel", 36 | "babel-plugin", 37 | "unassert" 38 | ], 39 | "license": "MIT", 40 | "main": "index.js", 41 | "repository": { 42 | "type": "git", 43 | "url": "https://github.com/unassert-js/babel-plugin-unassert.git" 44 | }, 45 | "scripts": { 46 | "lint": "semistandard --verbose index.js test/test.js | snazzy", 47 | "fmt": "semistandard --fix index.js test/test.js", 48 | "test": "mocha --timeout 0" 49 | }, 50 | "semistandard": { 51 | "ignore": [], 52 | "globals": [ 53 | "describe", 54 | "beforeEach", 55 | "it" 56 | ] 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test/fixtures/node_assert_api/fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function add (a, b) { 4 | console.assert(typeof a === 'number'); 5 | 6 | assert(!isNaN(a)); 7 | assert(!isNaN(a), 'assertion message'); 8 | 9 | assert.ok(!isNaN(b)); 10 | assert.ok(!isNaN(b), 'assertion message'); 11 | 12 | assert.equal(typeof b, 'number'); 13 | assert.equal(typeof b, 'number', 'assertion message'); 14 | 15 | assert.strictEqual(typeof b, 'number'); 16 | assert.strictEqual(typeof b, 'number', 'assertion message'); 17 | 18 | assert.deepEqual(typeof b, 'number'); 19 | assert.deepEqual(typeof b, 'number', 'assertion message'); 20 | 21 | assert.deepStrictEqual(typeof b, 'number'); 22 | assert.deepStrictEqual(typeof b, 'number', 'assertion message'); 23 | 24 | assert.notEqual(typeof a, 'object'); 25 | assert.notEqual(typeof a, 'object', 'assertion message'); 26 | 27 | assert.notStrictEqual(typeof a, 'object'); 28 | assert.notStrictEqual(typeof a, 'object', 'assertion message'); 29 | 30 | assert.notDeepEqual(typeof a, 'object'); 31 | assert.notDeepEqual(typeof a, 'object', 'assertion message'); 32 | 33 | assert.notDeepStrictEqual(typeof a, 'object'); 34 | assert.notDeepStrictEqual(typeof a, 'object', 'assertion message'); 35 | 36 | assert.throws(function () { 37 | validate(a); 38 | }); 39 | assert.throws(function () { 40 | validate(a); 41 | }, 'assertion message'); 42 | assert.throws(function () { 43 | validate(a); 44 | }, Error, 'assertion message'); 45 | 46 | assert.doesNotThrow(function () { 47 | validate(b); 48 | }, 'assertion message'); 49 | assert.doesNotThrow(function () { 50 | validate(b); 51 | }); 52 | 53 | assert.ifError(a); 54 | assert.fail(a, b, 'assertion message', '=='); 55 | 56 | assert.fail('message'); 57 | assert.doesNotReject(asyncFn, error, message); 58 | assert.rejects(asyncFn, error, message); 59 | return a + b; 60 | } 61 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | delete require.cache[require.resolve('..')]; 4 | const unassert = require('..'); 5 | const assert = require('assert'); 6 | const fs = require('fs'); 7 | const path = require('path'); 8 | const babel = require('@babel/core'); 9 | 10 | const testTransform = (fixtureName, options) => { 11 | it(fixtureName, () => { 12 | options = options || {}; 13 | const sourceType = options.sourceType || 'script'; 14 | const extension = sourceType === 'module' ? 'mjs' : 'js'; 15 | const dialect = options.dialect ? `-presets-${options.dialect}` : ''; 16 | const fixtureFilepath = path.resolve(__dirname, 'fixtures', fixtureName, `fixture.${extension}`); 17 | const expectedFilepath = path.resolve(__dirname, 'fixtures', fixtureName, `expected${dialect}.${extension}`); 18 | const result = babel.transformFileSync(fixtureFilepath, Object.assign({ 19 | sourceType: sourceType, 20 | plugins: [unassert] 21 | }, options.babelOptions)); 22 | const actual = result.code; 23 | const expected = fs.readFileSync(expectedFilepath).toString(); 24 | assert.strictEqual(actual + '\n', expected); 25 | }); 26 | }; 27 | 28 | describe('babel-plugin-unassert', () => { 29 | testTransform('node_assert_api'); 30 | testTransform('non_block_statement'); 31 | testTransform('cjs'); 32 | testTransform('cjs_strictmode'); 33 | testTransform('cjs_singlevar'); 34 | testTransform('cjs_singlevar_strictmode'); 35 | testTransform('cjs_node_protocol'); 36 | testTransform('cjs_node_protocol_strictmode'); 37 | testTransform('cjs_powerassert'); 38 | testTransform('cjs_powerassert_strictmode'); 39 | testTransform('cjs_assignment'); 40 | testTransform('cjs_assignment_singlevar'); 41 | testTransform('cjs_assignment_strictmode'); 42 | testTransform('esm_default_binding', { sourceType: 'module' }); 43 | testTransform('esm_default_binding_node_protocol', { sourceType: 'module' }); 44 | testTransform('esm_default_binding_powerassert', { sourceType: 'module' }); 45 | testTransform('esm_namespace_import', { sourceType: 'module' }); 46 | testTransform('esm_import_specifier', { sourceType: 'module' }); 47 | testTransform('not_an_expression_statement'); 48 | }); 49 | 50 | describe('babel-plugin-unassert with presets', () => { 51 | const opt = { babelOptions: { presets: ['@babel/env'] } }; 52 | testTransform('node_assert_api', opt); 53 | testTransform('non_block_statement', opt); 54 | testTransform('cjs', opt); 55 | testTransform('cjs_strictmode', opt); 56 | testTransform('cjs_singlevar', opt); 57 | testTransform('cjs_singlevar_strictmode', opt); 58 | testTransform('cjs_node_protocol', opt); 59 | testTransform('cjs_node_protocol_strictmode', opt); 60 | testTransform('cjs_powerassert', opt); 61 | testTransform('cjs_powerassert_strictmode', opt); 62 | testTransform('cjs_assignment', opt); 63 | testTransform('cjs_assignment_singlevar', Object.assign({}, opt, { dialect: 'env' })); 64 | testTransform('cjs_assignment_strictmode', Object.assign({}, opt, { dialect: 'env' })); 65 | testTransform('esm_default_binding', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' })); 66 | testTransform('esm_default_binding_node_protocol', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' })); 67 | testTransform('esm_default_binding_powerassert', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' })); 68 | testTransform('esm_namespace_import', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' })); 69 | testTransform('esm_import_specifier', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' })); 70 | testTransform('not_an_expression_statement', opt); 71 | }); 72 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * babel-plugin-unassert 3 | * Babel plugin for unassert 4 | * Encourages programming with assertions by providing tools to compile them away. 5 | * 6 | * https://github.com/unassert-js/babel-plugin-unassert 7 | * 8 | * Copyright (c) 2015-2021 Takuto Wada 9 | * Licensed under the MIT license. 10 | * https://github.com/unassert-js/babel-plugin-unassert/blob/master/LICENSE 11 | */ 12 | 'use strict'; 13 | 14 | module.exports = (babel, options) => { 15 | const config = Object.assign({ 16 | variables: [ 17 | 'assert' 18 | ], 19 | modules: [ 20 | 'assert', 21 | 'power-assert', 22 | 'node:assert' 23 | ] 24 | }, options); 25 | 26 | function isAssertionModuleName (lit) { 27 | const modules = config.modules; 28 | return modules.some((name) => lit.equals('value', name)); 29 | } 30 | 31 | function isAssertionVariableName (id) { 32 | const variables = config.variables; 33 | return variables.some((name) => id.equals('name', name)); 34 | } 35 | 36 | function isAssertionMethod (callee) { 37 | const variables = config.variables; 38 | return variables.some((name) => callee.matchesPattern(name, true)); 39 | } 40 | 41 | function isAssertionFunction (callee) { 42 | return callee.isIdentifier() && isAssertionVariableName(callee); 43 | } 44 | 45 | const isRequireAssert = (id, init) => { 46 | if (!id.isIdentifier()) { 47 | return false; 48 | } 49 | if (!isAssertionVariableName(id)) { 50 | return false; 51 | } 52 | if (!init.isCallExpression()) { 53 | return false; 54 | } 55 | const callee = init.get('callee'); 56 | if (!callee.isIdentifier() || !callee.equals('name', 'require')) { 57 | return false; 58 | } 59 | const arg = init.get('arguments')[0]; 60 | return (arg.isLiteral() && isAssertionModuleName(arg)); 61 | }; 62 | 63 | const isRequireAssertStrict = (id, init) => { 64 | if (!init.isMemberExpression()) { 65 | return false; 66 | } 67 | if (!isRequireAssert(id, init.get('object'))) { 68 | return false; 69 | } 70 | const prop = init.get('property'); 71 | if (!prop.isIdentifier()) { 72 | return false; 73 | } 74 | return prop.equals('name', 'strict'); 75 | }; 76 | 77 | const isRemovalTarget = (id, init) => isRequireAssert(id, init) || isRequireAssertStrict(id, init); 78 | 79 | return { 80 | visitor: { 81 | AssignmentExpression (nodePath, pluginPass) { 82 | if (!nodePath.equals('operator', '=')) { 83 | return; 84 | } 85 | if (isRemovalTarget(nodePath.get('left'), nodePath.get('right'))) { 86 | nodePath.remove(); 87 | } 88 | }, 89 | VariableDeclarator (nodePath, pluginPass) { 90 | if (isRemovalTarget(nodePath.get('id'), nodePath.get('init'))) { 91 | nodePath.remove(); 92 | } 93 | }, 94 | ImportDeclaration (nodePath, pluginPass) { 95 | const source = nodePath.get('source'); 96 | if (!(isAssertionModuleName(source))) { 97 | return; 98 | } 99 | const firstSpecifier = nodePath.get('specifiers')[0]; 100 | if (!(firstSpecifier.isImportDefaultSpecifier() || firstSpecifier.isImportNamespaceSpecifier() || firstSpecifier.isImportSpecifier())) { 101 | return; 102 | } 103 | const local = firstSpecifier.get('local'); 104 | if (isAssertionVariableName(local)) { 105 | nodePath.remove(); 106 | } 107 | }, 108 | CallExpression (nodePath, pluginPass) { 109 | const callee = nodePath.get('callee'); 110 | if (isAssertionFunction(callee) || isAssertionMethod(callee) || callee.matchesPattern('console.assert')) { 111 | if (nodePath.parentPath && nodePath.parentPath.isExpressionStatement()) { 112 | nodePath.remove(); 113 | } 114 | } 115 | } 116 | } 117 | }; 118 | }; 119 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [3.2.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v3.2.0) (2021-10-09) 2 | 3 | 4 | #### Features 5 | 6 | * [Add plugin options for customization](https://github.com/unassert-js/babel-plugin-unassert/pull/19) 7 | 8 | 9 | ## [3.1.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v3.1.0) (2021-05-11) 10 | 11 | 12 | #### Features 13 | 14 | * [Add support for `node:` protocol require/import](https://github.com/unassert-js/babel-plugin-unassert/pull/11) by [@make-github-pseudonymous-again](https://github.com/make-github-pseudonymous-again) 15 | 16 | 17 | ### [3.0.1](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v3.0.1) (2019-05-14) 18 | 19 | 20 | #### Bug Fixes 21 | 22 | * [dealing with ESM import with ImportSpecifier](https://github.com/unassert-js/babel-plugin-unassert/pull/10) (Thanks [@fschopp](https://github.com/fschopp) for reporting and suggestion) 23 | 24 | 25 | ## [3.0.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v3.0.0) (2019-01-15) 26 | 27 | 28 | #### Features 29 | 30 | * Babel7 support 31 | 32 | 33 | #### Breaking Changes 34 | 35 | * Babel7 is incompatible with Babel6 36 | 37 | For Babel6, you need to use [the 2.x release of babel-plugin-unassert](https://github.com/unassert-js/babel-plugin-unassert/tree/2.x). 38 | 39 | ``` 40 | $ npm install --save-dev babel-plugin-unassert@2 41 | ``` 42 | 43 | 44 | ## [2.2.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.2.0) (2019-01-12) 45 | 46 | 47 | #### Features 48 | 49 | * [Support "Strict mode"](https://github.com/unassert-js/babel-plugin-unassert/pull/7) 50 | 51 | 52 | ### [2.1.2](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.1.2) (2016-12-24) 53 | 54 | 55 | #### Chore 56 | 57 | * transfer to unassert-js organization ([b2a95e9](https://github.com/unassert-js/babel-plugin-unassert/commit/b2a95e9eba65a16a86924d53fd63aa4d2ab341ad)) 58 | 59 | 60 | ### [2.1.1](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.1.1) (2016-07-18) 61 | 62 | 63 | #### Bug Fixes 64 | 65 | * [Replace assertion with EmptyStatement if it is not a grandchild of BlockStatement](https://github.com/unassert-js/babel-plugin-unassert/pull/5) 66 | 67 | 68 | ## [2.1.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.1.0) (2016-04-28) 69 | 70 | 71 | #### Features 72 | 73 | * [Support ImportNamespaceSpecifier](https://github.com/unassert-js/babel-plugin-unassert/pull/4) 74 | 75 | 76 | ### [2.0.1](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.0.1) (2016-01-09) 77 | 78 | 79 | #### Bug Fixes 80 | 81 | * remove unnecessary dependencies ([bb28b466](https://github.com/unassert-js/babel-plugin-unassert/commit/bb28b4666de0ffdf7c86f78f03a35ef1372e1d1c)) 82 | 83 | 84 | ## [2.0.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v2.0.0) (2015-11-07) 85 | 86 | 87 | #### Features 88 | 89 | * [Babel 6.x support](https://github.com/unassert-js/babel-plugin-unassert/pull/2) 90 | 91 | 92 | #### Breaking Changes 93 | 94 | * Babel6 is incompatible with Babel5. For Babel5 or lower, you need to use the 1.2.x release of babel-plugin-unassert. 95 | 96 | ``` 97 | $ npm install --save-dev babel-plugin-unassert@1.2.0 98 | ``` 99 | 100 | 101 | ## [1.2.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v1.2.0) (2015-10-03) 102 | 103 | 104 | #### Features 105 | 106 | * support removal of assert variable assignment ([bf45e49d](https://github.com/unassert-js/babel-plugin-unassert/commit/bf45e49d73be3edc51f512a55ae11153ba41c697)) 107 | 108 | 109 | ## [1.1.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v1.1.0) (2015-07-17) 110 | 111 | 112 | #### Features 113 | 114 | * [support removal of assert variable declaration](https://github.com/unassert-js/babel-plugin-unassert/pull/1) 115 | 116 | 117 | ### [1.0.1](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v1.0.1) (2015-07-13) 118 | 119 | 120 | #### Bug Fixes 121 | 122 | * dealing with rename in babel API (Path#remove to Path#dangerouslyRemove) ([c67e4de2](https://github.com/unassert-js/babel-plugin-unassert/commit/c67e4de289d7a0ac3330e1d26821dd965b651978)) 123 | 124 | 125 | ## [1.0.0](https://github.com/unassert-js/babel-plugin-unassert/releases/tag/v1.0.0) (2015-05-26) 126 | 127 | 128 | The first release. 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | babel-plugin-unassert 2 | ================================ 3 | 4 | [Babel](https://babeljs.io/) plugin for unassert: Encourages [programming with assertions](https://en.wikipedia.org/wiki/Assertion_(software_development)) by providing tools to compile them away. 5 | 6 | [![unassert][unassert-banner]][unassert-url] 7 | 8 | [![Build Status][ci-image]][ci-url] 9 | [![NPM version][npm-image]][npm-url] 10 | [![Code Style][style-image]][style-url] 11 | [![License][license-image]][license-url] 12 | 13 | babel-plugin-unassert removes assertions on build. So you can use assertions to declare preconditions, postconditions and invariants. 14 | 15 | 16 | #### RELATED MODULES 17 | 18 | - [unassert](https://github.com/unassert-js/unassert): Encourages programming with assertions by providing tools to compile them away. 19 | - [unassertify](https://github.com/unassert-js/unassertify): Browserify transform for unassert 20 | - [webpack-unassert-loader](https://github.com/unassert-js/webpack-unassert-loader): Webpack loader for unassert 21 | - [gulp-unassert](https://github.com/unassert-js/gulp-unassert): Gulp plugin for unassert 22 | - [unassert-cli](https://github.com/unassert-js/unassert-cli): CLI for unassert 23 | 24 | 25 | INSTALL 26 | --------------------------------------- 27 | 28 | ``` 29 | $ npm install --save-dev babel-plugin-unassert 30 | ``` 31 | 32 | 33 | CAUTION 34 | --------------------------------------- 35 | 36 | Babel7 is incompatible with Babel6. Babel6 is incompatible with Babel5. 37 | 38 | For Babel6, you need to use [the 2.x release of babel-plugin-unassert](https://github.com/unassert-js/babel-plugin-unassert/tree/2.x). 39 | 40 | ``` 41 | $ npm install --save-dev babel-plugin-unassert@2 42 | ``` 43 | 44 | For Babel 5 or lower, you need to use the 1.x release of babel-plugin-unassert. 45 | 46 | ``` 47 | $ npm install --save-dev babel-plugin-unassert@1 48 | ``` 49 | 50 | 51 | HOW TO USE 52 | --------------------------------------- 53 | 54 | 55 | ### via [.babelrc.js](https://babeljs.io/docs/en/configuration#babelrcjs) 56 | 57 | ```javascript 58 | const presets = ['@babel/env']; 59 | const plugins = []; 60 | 61 | if (process.env.NODE_ENV === 'production') { 62 | plugins.push('babel-plugin-unassert'); 63 | } 64 | 65 | module.exports = { presets, plugins }; 66 | ``` 67 | 68 | ``` 69 | $ babel /path/to/src/target.js > /path/to/build/target.js 70 | ``` 71 | 72 | 73 | ### via [@babel/cli](https://babeljs.io/docs/en/babel-cli) 74 | 75 | ``` 76 | $ babel --plugins babel-plugin-unassert /path/to/src/target.js > /path/to/build/target.js 77 | ``` 78 | 79 | 80 | ### via [@babel/core](https://babeljs.io/docs/en/babel-core/) 81 | 82 | ```javascript 83 | const babel = require('@babel/core'); 84 | const transformed = babel.transformFileSync('/path/to/src/target.js', { 85 | presets: ['@babel/env'], 86 | plugins: ['babel-plugin-unassert'] 87 | }); 88 | console.log(transformed.code); 89 | ``` 90 | 91 | 92 | EXAMPLE 93 | --------------------------------------- 94 | 95 | For given `math.js` below, 96 | 97 | ```javascript 98 | 'use strict'; 99 | 100 | const assert = require('assert'); 101 | 102 | function add (a, b) { 103 | console.assert(typeof a === 'number'); 104 | assert(!isNaN(a)); 105 | assert.equal(typeof b, 'number'); 106 | assert.ok(!isNaN(b)); 107 | return a + b; 108 | } 109 | ``` 110 | 111 | Run `babel-cli` with `--plugins babel-plugin-unassert` option to transform. 112 | 113 | ``` 114 | $ babel --plugins babel-plugin-unassert /path/to/demo/math.js > /path/to/build/math.js 115 | ``` 116 | 117 | You will see assert calls and declarations disappear. 118 | 119 | ```javascript 120 | 'use strict'; 121 | 122 | function add(a, b) { 123 | return a + b; 124 | } 125 | ``` 126 | 127 | 128 | #### ES6 module and power-assert support 129 | 130 | babel-plugin-unassert supports ES6 module syntax and [power-assert](https://github.com/power-assert-js/power-assert). 131 | 132 | For given [babel.config.js](https://babeljs.io/docs/en/configuration#babelconfigjs), 133 | 134 | ```javascript 135 | module.exports = function (api) { 136 | const presets = ['@babel/env']; 137 | const plugins = []; 138 | 139 | if (api.env(['development', 'test'])) { 140 | presets.push('babel-preset-power-assert'); 141 | } 142 | 143 | if (api.env('production')) { 144 | plugins.push('babel-plugin-unassert'); 145 | } 146 | 147 | return { 148 | presets, 149 | plugins 150 | }; 151 | }; 152 | ``` 153 | 154 | and production code below, 155 | 156 | ```javascript 157 | import assert from 'assert'; 158 | 159 | class Calc { 160 | add (a, b) { 161 | assert(!(isNaN(a) || isNaN(b))); 162 | assert(typeof a === 'number'); 163 | assert(typeof b === 'number'); 164 | return a + b; 165 | } 166 | } 167 | ``` 168 | 169 | then it becomes in production, 170 | 171 | ```javascript 172 | 'use strict'; 173 | 174 | class Calc { 175 | add(a, b) { 176 | return a + b; 177 | } 178 | } 179 | ``` 180 | 181 | and in development, produces power-assert messages like below 182 | 183 | ``` 184 | AssertionError: # example.js:5 185 | 186 | assert(!(isNaN(a) || isNaN(b))) 187 | | | | | | | 188 | | | | | true NaN 189 | | false 3 true 190 | false 191 | ``` 192 | 193 | 194 | SUPPORTED PATTERNS 195 | --------------------------------------- 196 | 197 | 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](https://nodejs.org/api/assert.html) API (and `console.assert`). 198 | 199 | * `assert(value, [message])` 200 | * `assert.ok(value, [message])` 201 | * `assert.equal(actual, expected, [message])` 202 | * `assert.notEqual(actual, expected, [message])` 203 | * `assert.strictEqual(actual, expected, [message])` 204 | * `assert.notStrictEqual(actual, expected, [message])` 205 | * `assert.deepEqual(actual, expected, [message])` 206 | * `assert.notDeepEqual(actual, expected, [message])` 207 | * `assert.deepStrictEqual(actual, expected, [message])` 208 | * `assert.notDeepStrictEqual(actual, expected, [message])` 209 | * `assert.fail([message])` 210 | * `assert.fail(actual, expected, message, operator)` 211 | * `assert.throws(block, [error], [message])` 212 | * `assert.doesNotThrow(block, [message])` 213 | * `assert.rejects(asyncFn, [error], [message])` 214 | * `assert.doesNotReject(asyncFn, [error], [message])` 215 | * `assert.ifError(value)` 216 | * `console.assert(value, [message])` 217 | 218 | babel-plugin-unassert also removes assert variable declarations such as, 219 | 220 | * `const assert = require("assert")` 221 | * `const assert = require("node:assert")` 222 | * `const assert = require("assert").strict` 223 | * `const assert = require("node:assert").strict` 224 | * `const assert = require("power-assert")` 225 | * `const assert = require("power-assert").strict` 226 | * `import assert from "assert"` 227 | * `import assert from "node:assert"` 228 | * `import assert from "power-assert"` 229 | * `import * as assert from "assert"` 230 | * `import * as assert from "node:assert"` 231 | * `import * as assert from "power-assert"` 232 | * `import {strict as assert} from "assert"` 233 | * `import {strict as assert} from "node:assert"` 234 | * `import {strict as assert} from "power-assert"` 235 | 236 | and assignments. 237 | 238 | * `assert = require("assert")` 239 | * `assert = require("node:assert")` 240 | * `assert = require("assert").strict` 241 | * `assert = require("node:assert").strict` 242 | * `assert = require("power-assert")` 243 | * `assert = require("power-assert").strict` 244 | 245 | 246 | 247 | CUSTOMIZATION 248 | --------------------------------------- 249 | 250 | You can customize [Plugin Options](https://babeljs.io/docs/en/plugins#plugin-options) such as assertion patterns. 251 | 252 | 253 | input: 254 | 255 | ```javascript 256 | 'use strict'; 257 | 258 | var invariant = require('invariant'); 259 | const nassert = require('nanoassert'); 260 | import * as uassert from 'uvu/assert'; 261 | 262 | 263 | function add (a, b) { 264 | nassert(!isNaN(a)); 265 | 266 | uassert.is(Math.sqrt(4), 2); 267 | uassert.is(Math.sqrt(144), 12); 268 | uassert.is(Math.sqrt(2), Math.SQRT2); 269 | 270 | invariant(someTruthyVal, 'This will not throw'); 271 | invariant(someFalseyVal, 'This will throw an error with this message'); 272 | 273 | return a + b; 274 | } 275 | ``` 276 | 277 | output: 278 | 279 | ```javascript 280 | 'use strict'; 281 | 282 | function add(a, b) { 283 | return a + b; 284 | } 285 | ``` 286 | 287 | 288 | via [Config Files](https://babeljs.io/docs/en/config-files) 289 | 290 | ```json 291 | { 292 | "presets": [ 293 | ... 294 | ], 295 | "plugins": [ 296 | ["babel-plugin-unassert", { 297 | "variables": [ 298 | "assert", 299 | "invariant", 300 | "nassert", 301 | "uassert" 302 | ], 303 | "modules": [ 304 | "assert", 305 | "node:assert", 306 | "invariant", 307 | "nanoassert", 308 | "uvu/assert" 309 | ] 310 | }] 311 | ] 312 | } 313 | ``` 314 | 315 | or via [@babel/register](https://babeljs.io/docs/en/babel-register). 316 | 317 | ```javascript 318 | require('@babel/register')({ 319 | presets: [...], 320 | plugins: [ 321 | ['babel-plugin-unassert', { 322 | variables: [ 323 | 'assert', 324 | 'invariant', 325 | 'nassert', 326 | 'uassert' 327 | ], 328 | modules: [ 329 | 'assert', 330 | 'node:assert', 331 | 'invariant', 332 | 'nanoassert', 333 | 'uvu/assert' 334 | ] 335 | }] 336 | ] 337 | }); 338 | ``` 339 | 340 | or via ['@babel/core'](https://babeljs.io/docs/en/babel-core/), 341 | 342 | ```javascript 343 | const babel = require('@babel/core'); 344 | const jsCode = fs.readFileSync('/path/to/test/some_test.js'); 345 | const transformed = babel.transform(jsCode, { 346 | presets: [...], 347 | plugins: [ 348 | ['babel-plugin-unassert', { 349 | variables: [ 350 | 'assert', 351 | 'invariant', 352 | 'nassert', 353 | 'uassert' 354 | ], 355 | modules: [ 356 | 'assert', 357 | 'node:assert', 358 | 'invariant', 359 | 'nanoassert', 360 | 'uvu/assert' 361 | ] 362 | }] 363 | ] 364 | }); 365 | console.log(transformed.code); 366 | ``` 367 | 368 | 369 | 370 | 371 | 372 | #### options 373 | 374 | | type | default value | 375 | |:---------|:--------------------| 376 | | `object` | objects shown below | 377 | 378 | Configuration options for `babel-plugin-unassert`. If not passed, default options will be used. 379 | 380 | ```javascript 381 | { 382 | variables: [ 383 | 'assert' 384 | ], 385 | modules: [ 386 | 'assert', 387 | 'power-assert', 388 | 'node:assert' 389 | ] 390 | } 391 | ``` 392 | 393 | 394 | AUTHOR 395 | --------------------------------------- 396 | * [Takuto Wada](https://github.com/twada) 397 | 398 | 399 | CONTRIBUTORS 400 | --------------------------------------- 401 | * [Notas Hellout](https://github.com/make-github-pseudonymous-again) 402 | 403 | 404 | OUR SUPPORT POLICY 405 | --------------------------------------- 406 | 407 | We support Node under maintenance. In other words, we stop supporting old Node version when [their maintenance ends](https://github.com/nodejs/LTS). 408 | 409 | This means that any other environment is not supported. 410 | 411 | 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. 412 | 413 | 414 | LICENSE 415 | --------------------------------------- 416 | Licensed under the [MIT](https://github.com/unassert-js/babel-plugin-unassert/blob/master/LICENSE) license. 417 | 418 | 419 | [unassert-url]: https://github.com/unassert-js/unassert 420 | [unassert-banner]: https://raw.githubusercontent.com/unassert-js/unassert-js-logo/master/banner/banner-official-fullcolor.png 421 | 422 | [npm-url]: https://npmjs.org/package/babel-plugin-unassert 423 | [npm-image]: https://badge.fury.io/js/babel-plugin-unassert.svg 424 | 425 | [ci-image]: https://github.com/unassert-js/babel-plugin-unassert/workflows/Node.js%20CI/badge.svg 426 | [ci-url]: https://github.com/unassert-js/babel-plugin-unassert/actions?query=workflow%3A%22Node.js+CI%22 427 | 428 | [style-url]: https://github.com/Flet/semistandard 429 | [style-image]: https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg 430 | 431 | [license-url]: https://github.com/unassert-js/babel-plugin-unassert/blob/master/LICENSE 432 | [license-image]: https://img.shields.io/badge/license-MIT-brightgreen.svg 433 | --------------------------------------------------------------------------------