├── .npmignore ├── .eslintrc ├── .gitignore ├── .editorconfig ├── package.json ├── LICENCE ├── main.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | * 2 | !/main.cjs.js* 3 | !/main.mjs.js* 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | extends: sudeti 2 | rules: 3 | no-console: 0 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /package-lock.json 3 | /main.cjs.js* 4 | /main.mjs.js* 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{.eslintrc,package.json}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-transform-promise-to-bluebird", 3 | "version": "2.0.0", 4 | "description": "Transforms *Promise* to *bluebird*", 5 | "main": "main.cjs.js", 6 | "module": "main.mjs.js", 7 | "devDependencies": { 8 | "@babel/core": "^7.0.0", 9 | "@babel/preset-env": "^7.0.0" 10 | }, 11 | "dependencies": { 12 | "@babel/helper-module-imports": "^7.0.0" 13 | }, 14 | "scripts": { 15 | "prepublish": "node ./build.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/chpio/babel-plugin-transform-promise-to-bluebird.git" 20 | }, 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/chpio/babel-plugin-transform-promise-to-bluebird/issues" 24 | }, 25 | "homepage": "https://github.com/chpio/babel-plugin-transform-promise-to-bluebird#readme" 26 | } 27 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 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 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | import {addNamed} from '@babel/helper-module-imports'; 2 | 3 | export default function promiseToBluebird({types: t}) { 4 | return { 5 | visitor: { 6 | ReferencedIdentifier(path) { 7 | const {node, parent, scope} = path; 8 | 9 | if (node.name !== 'Promise') return; 10 | if (t.isMemberExpression(parent)) return; 11 | if (scope.getBindingIdentifier('Promise')) return; 12 | 13 | path.replaceWith(addNamed(path, 'default', 'bluebird', {nameHint: 'Promise'})); 14 | }, 15 | 16 | MemberExpression(path) { 17 | const {node} = path; 18 | const obj = node.object; 19 | 20 | if (obj.name !== 'Promise') return; 21 | if (!path.isReferenced()) return; 22 | if (path.scope.getBindingIdentifier('Promise')) return; 23 | 24 | if (node.computed) { 25 | path.replaceWith( 26 | t.memberExpression( 27 | addNamed(path, 'default', 'bluebird', {nameHint: 'Promise'}), 28 | node.property, 29 | true 30 | ) 31 | ); 32 | } else { 33 | path.replaceWith( 34 | addNamed( 35 | path, 36 | node.property.name, 37 | 'bluebird', 38 | {nameHint: 'Promise'} 39 | ) 40 | ); 41 | } 42 | }, 43 | }, 44 | }; 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-transform-promise-to-bluebird 2 | 3 | This plugin transforms `Promise` to `bluebird`. 4 | 5 | ## Example 6 | ```javascript 7 | export default function main() { 8 | const taskA = getResultAsync(1337); 9 | const taskB = new Promise((resolve, reject) => 10 | nodeCallbackFunc(42, (err, res) => err ? reject(err) : resolve(res)) 11 | ); 12 | return Promise.all([taskA, taskB]).then(([resA, resB]) => resA + resB); 13 | } 14 | ``` 15 | Gets converted to: 16 | ```javascript 17 | import {all, default as Promise} from 'bluebird'; 18 | 19 | export default function main() { 20 | const taskA = getResultAsync(1337); 21 | const taskB = new Promise((resolve, reject) => 22 | nodeCallbackFunc(42, (err, res) => err ? reject(err) : resolve(res)) 23 | ); 24 | return all([taskA, taskB]).then(([resA, resB]) => resA + resB); 25 | } 26 | ``` 27 | 28 | ## Usage 29 | 30 | 1. Install *bluebird*: `npm install --save bluebird` 31 | 2. Install the *promise-to-bluebird* plugin: `npm install --save-dev babel-plugin-transform-promise-to-bluebird` 32 | 3. Add *transform-promise-to-bluebird* to your *.babelrc* file: 33 | ```json 34 | { 35 | "plugins": ["transform-promise-to-bluebird"] 36 | } 37 | ``` 38 | If you'r using the *transform-runtime* plugin add *transform-promise-to-bluebird* before 39 | *transform-runtime*: 40 | ```json 41 | { 42 | "plugins": [ 43 | "transform-promise-to-bluebird", 44 | "transform-runtime" 45 | ] 46 | } 47 | ``` 48 | --------------------------------------------------------------------------------