Skip to content

Automatically adds `var assert = require('assert');` to your code.

License

Notifications You must be signed in to change notification settings

azu/babel-plugin-auto-import-assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-auto-import-assert

Automatically adds var assert = require('assert'); to your code, if not imported/required assert module.

Installation

npm install babel-plugin-auto-import-assert

Usage

$ babel --plugins babel-plugin-auto-import-assert /path/to/src/target.js > /path/to/build/target.js

or shortly,

$ babel --plugins auto-import-assert /path/to/src/target.js > /path/to/build/target.js
var babel = require('babel-core');
var jsCode = fs.readFileSync('/path/to/src/target.js');
var transformed = babel.transform(jsCode, {
    presets: [...],
    plugins: ['babel-plugin-auto-import-assert']
});
console.log(transformed.code);
{
  "presets": [
    ...
  ],
  "env": {
    "development": {
      "plugins": [
        "babel-plugin-auto-import-assert"
      ],
    }
  }
}
$ babel /path/to/src/target.js > /path/to/build/target.js

EXAMPLE

For given math.js below,

'use strict';

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

Run babel with --plugins auto-import-assert to transform code.

$ babel --plugins auto-import-assert /path/to/demo/math.js > /path/to/build/math.js

Add import assert from 'assert'; to your code.

'use strict';

var assert = require('assert');

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

Tests

npm test

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

About

Automatically adds `var assert = require('assert');` to your code.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published