├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-import-asserts 2 | Babel plugin that does the following: 3 | 4 | For every `import {foo, bar} from './baz';` it adds 5 | ``` 6 | console.assert(foo, 'foo is undefined'); 7 | console.assert(baz, 'baz is undefined'); 8 | ``` 9 | below the import statement. The same goes for `import foo from './baz';`. 10 | 11 | Motivation: 12 | 13 | 1. catching hard to debug errors when you 1) forget to export a thing from an `index.js` file and then 2) try to import this thing from that `index.js` file in another place 14 | 15 | 2. catch the case when you use a default import although it would have to be a regular import. (`import foo from './bar'` instead of `import {foo} from './bar'`). this happens way to often and there currently is no default safety net preventing you from doing this. 16 | 17 | **Note**: Only use this for development builds, you don't wanna bloat the production bundle. 18 | 19 | **Note 2**: This only works for codebases where you can guarantee that every imported thing will be instantly bound. It is theoretically possible for imported things to *initially* be `undefined` and *later* defined. This is because ES6 Imports are "live binding". See [here](https://github.com/ModuleLoader/es6-module-loader/wiki/Circular-References-&-Bindings) for more information. 20 | 21 | # Usage 22 | ``` 23 | $ babel --plugins import-asserts script.js 24 | ``` 25 | 26 | or in a `.babelrc`: 27 | ``` 28 | { 29 | "stage": 0, 30 | "plugins": [ 31 | "import-asserts" 32 | ] 33 | } 34 | ``` 35 | 36 | or in webpack: 37 | `loader: 'babel?{"plugins":["import-asserts"]}'}` 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (babel) { 2 | var t = babel.types; 3 | 4 | function console_(method, arguments) { 5 | return t.expressionStatement(t.callExpression( 6 | t.memberExpression( 7 | t.identifier('console'), 8 | t.identifier(method)), 9 | arguments)); 10 | } 11 | 12 | function consoleTest(thing) { 13 | 14 | var thingIsUndefined = 15 | t.unaryExpression('!', t.identifier(thing)); 16 | 17 | var consoleStatement = console_('log', 18 | [t.literal('['), t.identifier('__filename'), t.literal(']'), t.literal(thing + ' is undefined.')]); 19 | 20 | var logIfUndefined = t.logicalExpression('&&', thingIsUndefined, consoleStatement); 21 | 22 | return logIfUndefined; 23 | 24 | } 25 | 26 | 27 | return new babel.Plugin("import-asserts", { 28 | 29 | visitor: { 30 | ImportDeclaration: function (node, parent) { 31 | var self = this; 32 | node.specifiers.map(function(specifier, idx) { 33 | var name = specifier.local.name; 34 | self.insertAfter(consoleTest(name)); 35 | 36 | }) 37 | } 38 | } 39 | }); 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-import-asserts", 3 | "version": "0.1.0", 4 | "description": "babel plugin that adds console.asserts which check that your imports are not undefined", 5 | "author": { 6 | "name": "Jonathan Werner" 7 | }, 8 | "license": "MIT", 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/jonathanewerner/babel-plugin-import-asserts.git" 12 | }, 13 | "peerDependencies": { 14 | "babel-core": ">=5.2.0" 15 | }, 16 | "main": "index.js", 17 | "devDependencies": { 18 | "babel": "^5.6.0" 19 | }, 20 | "keywords": [ 21 | "babel", 22 | "babel-plugin", 23 | "object", 24 | "assign", 25 | "extend", 26 | "polyfill" 27 | ] 28 | } 29 | --------------------------------------------------------------------------------