Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed May 4, 2016
0 parents commit cb5745e
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
"es2015",
"stage-1"
],
"plugins": [
"transform-object-assign",
"add-module-exports"
],
"env": {
"development": {
"sourceMaps": "inline",
"plugins": [
"espower"
]
}
}
}
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
extends: airbnb/base
parser: babel-eslint

env:
node: true
mocha: true

rules:
valid-jsdoc:
- 2
- prefer:
return: returns

max-len: 1
arrow-body-style: 1
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Thumbs.db
.DS_Store

node_modules
bower_components

.nyc_output
coverage
docs
lib
spec
npm-debug.log
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# keep the empty
# publish only the file that defined in the "files" of package.json
# (follow the same pattern rules as .gitignore if .npmignore not exists)
# https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package
45 changes: 45 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
notifications:
email: false

# Quick boot options
sudo: false
cache:
directories:
- node_modules

# Ignore version tags
branches:
except:
- /^v[0-9]/

env:
global:
# CODECLIMATE_REPO_TOKEN
- secure: SACFdmNF5LxAcxdqYlQJG5Mk18WsFGapZ7XiPV5aTeYznmdjgVDYc8TgXteO7wkx0fNvxBt8OhuuywawADImotm2dq/cfyzoGbtx0F3/x0XU7mAPVSJuCU3Ta1Ri6Ep8ubT2mv/XHV7nVtvKM2ANTEpW48v8zDRGgZJTBpjdLpodrELFd3SPLJYLDtzqbssL1dErB/hwllVhrLPRznvgxqeOFK9wyouMVWghTDvKp6RW3PHiiTS4sPU/xEmfHxcKcUTqC+TNdfjOet/zTcVI3GQ5HKO4TR2ogElTvFcT/b3W5S+3N9MkjLc98PZBplPoT1+YbYOWmFxWe6nuHxq6wTrsrU9fmHn4Jnud638nLiQkvXNj3kdQK66UqWCB9Cs089UTYX8q4M4NzgemwM/BFOD0UMkL53fEE8TAJ3xSMfb8lq5J88i2aPg4fnB+rmowzEKrBYakYd2Ze8GTmdFtrKq5XDWT//Xq/hfpCZ8/wgNH7nIjieVPEI7Saw119kbXVCTi6HfXnPPHbveMI9sIo8gCFyS2o7RC2e8+B5YZ84XcvWZuX7+isY3uAdTM29gGiBfHj0voHEe52FmLohZEVpTe2rWSYRj03RdQU+NvP1zFGh+TTZbS742jzcBlnd6V5N/1Iu6NyWl8hq7N/nCkWX3rfMiEDsYkAAry/oICHCg=

# Language options
language: node_js
before_install:
- if [[ `npm -v` != 3* ]]; then npm i -g npm@3; fi
node_js:
- "0"
- 4
- 5
before_script:
- npm prune
script:
- npm run compile
- npm run cover
- npm run lint

# Npm publish options
before_deploy:
- npm run build
deploy:
skip_cleanup: true # keep the devDependencies
provider: npm
email: [email protected]
api_key:
secure: BJCwSqb2aq29EvcdwagN49i9GF8kUYmbvNslrldYGcZL3jXytbJH3wMMrFjiK2HkTqkMhabDDoOxoOLr+4XfupJMYszzVbQHSmamX5f8i5yhKrXj2v6h5UrRJCMStaBmTOJk8zQxcgjGtardIN1yPNmjHsBHXt34YIrmUrOIlQJMV5+I+SFnfnGSepF4y6D0yNkhK+wOTZ76XrW0N3NIx5BxuUxaFgy/6dpEh9iq5grixNCImEeCjuuc6llOfYW3ck6kl02QSmAzbwY5BmaJFNx13CFLLSoWD/cunyQeajxJegh3wFgTC0Akfr2jvhYSVbm69H4qlWuKp/HpdVSSoQ3evyXmlVwxBBpFfVGQW15hLrEL/tIVvH+vFHvwwO4JP43z+5+96K2PMsxJsjIRkFkeDgeKhfC0zY8oVtG5gvtvjxipxA9gUDWS6EWYNMq114KRjpU/AiARGGc7Wf55pQbJS48+2dvvmu9LiYDk1m/U8LGa7KGkzdcVWoE8B0z0xdzhYq/SrTWD43MPRHppC+z4rt104igsXHvb2IbWhAuG9TuDNbWZNJS5sAxs/T7hszEpK1D5SAyVnA04TfslwvERBHccAxXZb87mQoAyE/X41202TvCiu7b1cw/BL4dZRG0jP+nhNWdV5Eg6+Cnvs3uAfwAe8KTM/cCaeQZ6+xY=
on:
node: 4
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
babel-plugin-transform-bluebird
---

<p align="right">
<a href="https://npmjs.org/package/babel-plugin-transform-bluebird">
<img src="https://img.shields.io/npm/v/babel-plugin-transform-bluebird.svg?style=flat-square">
</a>
<a href="https://travis-ci.org/59naga/babel-plugin-transform-bluebird">
<img src="http://img.shields.io/travis/59naga/babel-plugin-transform-bluebird.svg?style=flat-square">
</a>
<a href="https://codeclimate.com/github/59naga/babel-plugin-transform-bluebird/coverage">
<img src="https://img.shields.io/codeclimate/github/59naga/babel-plugin-transform-bluebird.svg?style=flat-square">
</a>
<a href="https://codeclimate.com/github/59naga/babel-plugin-transform-bluebird">
<img src="https://img.shields.io/codeclimate/coverage/github/59naga/babel-plugin-transform-bluebird.svg?style=flat-square">
</a>
<a href="https://gemnasium.com/59naga/babel-plugin-transform-bluebird">
<img src="https://img.shields.io/gemnasium/59naga/babel-plugin-transform-bluebird.svg?style=flat-square">
</a>
</p>

Replace `Promise` with an [bluebird](https://github.com/petkaantonov/bluebird#readme) plugin.

Installation
---
```bash
npm install bluebird babel-plugin-transform-bluebird --save
```

Example
---

**In**

```js
Promise.resolve()
```

**Out**

```js
var Promise = require("bluebird");
Promise.resolve();
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["transform-bluebird"]
}
```

### Via CLI

```bash
$ babel --plugins transform-bluebird script.js
```

### Via Node API

```js
require("babel-core").transform("code", {
plugins: ["transform-bluebird"]
});
```

Development
---
Requirement global
* NodeJS v5.11.0
* Npm v3.8.6

```bash
git clone https://github.com/59naga/babel-plugin-transform-bluebird
cd babel-plugin-transform-bluebird
npm install

npm test
```

License
---
[MIT](http://59naga.mit-license.org/)
80 changes: 80 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "babel-plugin-transform-bluebird",
"version": "0.0.0",
"description": "replace `Promise` with bluebird plugin",
"main": "lib",
"files": [
"lib"
],
"scripts": {
"start": "abby compile, watch:*",
"watch:raw": "abby mocha --log --watch",
"watch:src": "babel src --out-dir lib --watch",
"watch:test": "babel test --out-dir spec --watch",
"compile": "babel -V && babel src --out-dir lib && babel test --out-dir spec",
"test": "abby compile, mocha",
"mocha": "mocha 'spec/**/*.js'",
"cover": "abby cover:* --parse serial",
"cover:test": "nyc --reporter=lcov --reporter=text abby mocha",
"cover:report": "npm-if TRAVIS \"codeclimate-test-reporter < coverage/lcov.info\"",
"lint": "eslint src test",
"build": "abby \"babel -V && babel src --out-dir lib --source-maps\" --env"
},
"nyc": {
"exclude": [
"spec"
]
},
"abigail": {
"plugins": {
"log": false,
"parse": {
"raw": true
},
"watch": {
"enable": false,
"value": [
"lib/**/*.js",
"spec/**/*.js"
]
}
}
},
"devDependencies": {
"abigail": "^1.6.1",
"babel-cli": "^6.6.5",
"babel-core": "^6.7.6",
"babel-eslint": "^6.0.2",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-espower": "^2.1.2",
"babel-plugin-transform-object-assign": "^6.5.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-1": "^6.5.0",
"babel-register": "^6.7.2",
"bluebird": "^3.3.5",
"chokidar": "^1.4.3",
"codeclimate-test-reporter": "^0.3.1",
"eslint": "^2.6.0",
"eslint-config-airbnb": "^7.0.0",
"mocha": "^2.4.5",
"npm-statement": "^0.0.0",
"nyc": "^6.1.1",
"power-assert": "^1.3.1",
"q": "^1.4.1"
},
"keywords": [
"babel-plugin",
"Promise",
"bluebird"
],
"repository": {
"type": "git",
"url": "https://github.com/59naga/babel-plugin-transform-bluebird.git"
},
"author": "59naga <[email protected]> (http://berabou.me/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/59naga/babel-plugin-transform-bluebird/issues"
},
"homepage": "https://github.com/59naga/babel-plugin-transform-bluebird#readme"
}
52 changes: 52 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// private
const defineName = 'Promise';

/**
* @module babel-plugin-transform-bluebird
* @returns {babelPlugin} unknown
*/
export default ({ template }) => {
return ({
visitor: {
Program: {
exit(path) {
let injectBluebird = false;
for (const node of path.node.body) {
const expression = node.expression || {};
const specifiers = node.specifiers || [];
const declarations = node.declarations || [];

// found "new Promise" or "Promise.method()"
const calleeName = expression.callee && (expression.callee.name || expression.callee.object.name);
if (calleeName === defineName) {
injectBluebird = true;
}

// found "var foo = new Promise" or "var foo = Promise.resolve()"
const declarationInits = declarations.map((declaration) => declaration.init.callee.name || declaration.init.callee.object.name);
if (declarationInits.indexOf(defineName) > -1) {
injectBluebird = true;
}

// found "import Promise"
const localNames = specifiers.map((specifier) => specifier.local.name);
if (localNames.indexOf(defineName) > -1) {
return;
}

// found "var Promise"
const declarationIds = declarations.map((declaration) => declaration.id.name);
if (declarationIds.indexOf(defineName) > -1) {
return;
}
}

if (injectBluebird) {
const topNodes = [template(`var ${defineName} = require("bluebird")`)()];
path.unshiftContainer('body', topNodes);
}
},
},
},
});
};
39 changes: 39 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// dependencies
import assert from 'power-assert';
import { transform } from 'babel-core';
import specs from './specs';
import vm from 'vm';

// target
import plugin from '../lib';

// environment
const options = {
presets: [
'es2015',
],
plugins: [
plugin,
plugin,
plugin,
],
};
const vmGlobal = {
require: (name) => require(name),
};

// specs
describe('babel-plugin-transform-bluebird', () => {
it('noop', () => {
const result = transform('', options);
assert(result.code === '"use strict";');
});

specs.forEach((test) => {
it(test.description, () => {
const result = transform(test.code, options);
const returnValue = vm.runInNewContext(result.code, vmGlobal);
assert(returnValue instanceof test.expected);
});
});
});
Loading

0 comments on commit cb5745e

Please sign in to comment.