├── package.json ├── README.md ├── LICENSE └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-transform-invariant-location", 3 | "version": "1.0.1", 4 | "description": "Babel plugin that rewrites invariant calls with their source location", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/github/babel-plugin-transform-invariant-location.git" 9 | }, 10 | "license": "MIT", 11 | "homepage": "https://github.com/github/babel-plugin-transform-invariant-location#readme" 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # transform-invariant-location 2 | 3 | A Babel plugin to annotate [Flow `invariant()` calls](https://github.com/zertosh/invariant) with an additional string argument detailing the current file and line number. This is useful to track down exceptions that are raised by `invariant()` in minified bundles at runtime. 4 | 5 | Before: 6 | 7 | ```js 8 | invariant(foo) 9 | invariant(foo, "foo is missing") 10 | ``` 11 | 12 | After: 13 | 14 | ```js 15 | invariant(foo, "path/to/source.js:42") 16 | invariant(foo, "foo is missing -- path/to/source.js:42") 17 | ``` 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 GitHub, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const visited = new WeakSet() 2 | 3 | module.exports = function(opts) { 4 | const t = opts.types 5 | 6 | return { 7 | visitor: { 8 | CallExpression: function(path) { 9 | if (path.node.callee.name === 'invariant') { 10 | if (visited.has(path.node)) { 11 | return 12 | } 13 | 14 | let moduleName = this.getModuleName() 15 | if (moduleName) { 16 | moduleName = t.stringLiteral(moduleName) + '.js' 17 | } else { 18 | try { 19 | moduleName = this.file.opts.filename.replace(`${this.cwd}/`, '') 20 | } catch (err) { 21 | return 22 | } 23 | } 24 | 25 | const location = `${moduleName}:${path.node.loc.start.line}` 26 | const message = path.node.arguments.length === 2 ? 27 | `${path.node.arguments[1].value} -- ${location}` : 28 | location 29 | 30 | const newNode = t.callExpression( 31 | t.identifier('invariant'), [path.node.arguments[0], t.stringLiteral(message)] 32 | ) 33 | visited.add(newNode) 34 | 35 | path.replaceWith(newNode) 36 | } 37 | } 38 | } 39 | } 40 | } 41 | --------------------------------------------------------------------------------