This project relies on babel-plugin-macros, you can install both from npm:
npm install --save-dev assert.macro babel-plugin-macrosIn your babel config, add "macros" to your plugins:
{
"plugins": ["macros"]
}To include the assertions in the babel output, make sure you set the ENABLE_ASSERTIONS environment variable to "true". For example:
ENABLE_ASSERTIONS=true npm run buildAll usages of of the call to assert, including the import will be removed completely, if ENABLE_ASSERTIONS is not "true".
import assert from 'assert.macro';
class ShoppingCart {
applyDiscount(discount) {
assert(discount > 0, "Discount can't be 0 or negative.");
assert(discount <= 1, "The discount shouldn't make it more expensive.");
this._total *= discount;
}
}- Documents intent
- More explicit than comments
- Able to catch bugs
- Refactoring by Martin Fowler
- Programming With Assertions (Java)
- Invariant by Facebook
MIT © Lion Ralfs