Skip to content

Commit

Permalink
Merge pull request #29 from 59naga/5to6
Browse files Browse the repository at this point in the history
Change 5 to 6 syntax
  • Loading branch information
59naga committed Apr 24, 2016
2 parents 9e69707 + 7ee2022 commit 3d14024
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 126 deletions.
13 changes: 13 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
"es2015"
],
"env": {
"development": {
"sourceMap": "inline",
"presets": [
"power-assert"
]
}
}
}
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
plugins:
- mocha

extends: standard
extends:
- standard

env:
node: true
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ coverage
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Compiled commonJS files
lib
spec

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
Expand Down
19 changes: 11 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ branches:

# 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 cover
- npm run lint

before_install:
- npm install npm@3 --global

# Coverage report
after_success:
- export TRAVIS_PASSED=true
- npm install codeclimate-test-reporter --global
- codeclimate-test-reporter < coverage/lcov.info
env:
global:
# CODECLIMATE_REPO_TOKEN
- secure: BfmL4mRysi4/qUKuN9Hvx3QsbTRH6XtxfuXj3XXaIQiRNwYAU0FBiVjoJzzKk1AhXmLBzWco3jWFWGY0OpXCnzKLHYYZYB3L/UPGq5aqX004pk2ZaPFO8+nfdua15FP2/7vShB+4FbMTztQO4GZMdOi1QTXOdTJgjISQfIqZ7RIeWEaF3UlySt0Xety9Y2nN3YpdPUIPJW2TWP9dIA/XyMHa9knve7TdPzpXkJelpoi3+EnzEtEUWaPtDTMXwOq4zYM4E1bXeA5ZWPmbA1JlO4lDX1aeOz6MFTmBaPTt8K/TT2PLegh8u6JVyG45R1y8OTKyRPQR4+Dy5vp+NLlSMSugq2lxixcK4tuf17a7zPAAEQLNg7LSR9kg3/gyqjthXa5gMYEVI7kE0+mrJl5stVnkGCJaIBawxr4Miqot6qOkh3JSrv0YKh4q1f+XMgtJFlW4UhtJ5zqp/wRSaQpsWKmGqN6xPRMR//5vrTis7uFuCOvxsAN0wzmjelXuNc2nWLuZsP9A91sTghssdikm9maaiP+keAc4gMxy/GuNf0fqyylcaw+9cY9E4Fsg+0Qlv2p1TPOAmXdr0VIkVAuym8cQr+ULu9w4H1F2YrScYnwThTv9J9jhZXqmVh7Ysh9pTluZoTd9ymgkso0Av5xHN56ltHWF1Sku4FFGmnUdI+Q=

# Npm publish options
before_deploy:
- npm run build
deploy:
skip_cleanup: true # keep the devDependencies
provider: npm
email: [email protected]
api_key:
Expand Down
46 changes: 0 additions & 46 deletions lib/index.js

This file was deleted.

54 changes: 42 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,57 @@
"lib"
],
"scripts": {
"start": "mocha --bail --watch --require intelli-espower-loader --timeout 30000",
"test": "istanbul cover _mocha --report text --report html --report lcov -- -R spec --timeout 30000",
"posttest": "npm run lint",
"lint": "eslint lib test"
"start": "abby compile, watch:*",
"watch:src": "babel src --out-dir lib --watch",
"watch:test": "babel test --out-dir spec --ignore test/spec.js --watch",
"watch:copy": "abby compile:copy --watch test/spec.js",
"watch:mocha": "abby mocha --log --watch lib/**/*.js,spec/**/*.js",
"compile": "abby compile:* --parse serial",
"compile:version": "babel -V",
"compile:src": "babel src --out-dir lib --source-maps",
"compile:test": "babel test --out-dir spec --ignore test/spec.js",
"compile:copy": "cp test/spec.js spec/spec.js",
"test": "abby compile, mocha.",
"mocha": "mocha spec/index.js",
"cover": "abby cover:* --parse serial --launch force",
"cover:test": "nyc --reporter=lcov --reporter=text npm test",
"cover:report": "npm-if TRAVIS \"codeclimate-test-reporter < coverage/lcov.info\"",
"lint": "eslint src test",
"build": "abby compile --log --env"
},
"nyc": {
"exclude": [
"spec"
]
},
"abigail": {
"plugins": {
"log": false,
"parse": "raw",
"watch": false
}
},
"dependencies": {
"babel-template": "^6.5.0",
"lodash": "^4.3.0"
"lodash.get": "^4.2.1"
},
"devDependencies": {
"abigail": "^1.6.1",
"babel-cli": "^6.5.1",
"babel-core": "^6.5.1",
"babel-plugin-transform-export-extensions": "^6.5.0",
"babel-preset-es2015": "^6.5.0",
"eslint": "^1.9.0",
"eslint-config-standard": "^4.4.0",
"eslint-plugin-mocha": "^1.1.0",
"babel-preset-power-assert": "^1.0.0",
"chokidar": "^1.4.3",
"codeclimate-test-reporter": "^0.3.1",
"eslint": "^2.8.0",
"eslint-config-standard": "^5.1.0",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-promise": "^1.1.0",
"eslint-plugin-standard": "^1.3.1",
"espower-loader": "^1.0.0",
"intelli-espower-loader": "^1.0.1",
"istanbul": "^0.4.2",
"mocha": "^2.4.5",
"npm-statement": "^0.0.0",
"nyc": "^6.4.0",
"power-assert": "^1.2.0"
},
"keywords": [
Expand All @@ -38,7 +68,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/59naga/babel-plugin-add-module-exports.git"
"url": "https://github.com/59naga/babel-plugin-add-module-exports.git"
},
"bugs": {
"url": "https://github.com/59naga/babel-plugin-add-module-exports/issues"
Expand Down
42 changes: 42 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import babelTemplate from 'babel-template'
import _get from 'lodash.get'

export default {
visitor: {
Program: {
exit (path) {
if (path.BABEL_PLUGIN_ADD_MODULE_EXPORTS) {
return
}

let hasExportDefault = false
let hasExportNamed = false
path.get('body').forEach((path) => {
if (path.isExportDefaultDeclaration()) {
hasExportDefault = true
return
}
if (path.isExportNamedDeclaration()) {
// HACK detect export-from statements for default
const name = _get(path.get('declaration'), 'container.specifiers[0].exported.name')
if (name === 'default') {
hasExportDefault = true
} else {
hasExportNamed = true
}
return
}
})

if (hasExportDefault && !hasExportNamed) {
const topNodes = []
topNodes.push(babelTemplate("module.exports = exports['default']")())

path.pushContainer('body', topNodes)
}

path.BABEL_PLUGIN_ADD_MODULE_EXPORTS = true
}
}
}
}
42 changes: 42 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import vm from 'vm'
import util from 'util'
import {transform as babelTransform} from 'babel-core'
import assert from 'assert'

export function createSandbox () {
const exports = {}
const sandbox = {
exports,
module: { exports },
require (path) {
delete require.cache[require.resolve(path)]
return require(path)
}
}

return sandbox
}

export function testPlugin (code, options, fn) {
const result = babelTransform(code, options)
const sandbox = createSandbox()

vm.runInNewContext(result.code, sandbox)

fn(sandbox.module.exports)
}

export function inspect (object) {
const result = util.inspect(object)
return result.replace('Object {', '{') // HACK the module.export inspect
}

export function equal (actual, expected) {
if (typeof expected === 'string') {
assert(actual.toString() === expected)
} else if (typeof expected === 'function') {
assert(actual() === expected())
} else {
assert(inspect(actual) === inspect(expected))
}
}
76 changes: 21 additions & 55 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,49 @@
var vm = require('vm')
var util = require('util')
var babel = require('babel-core')
var assert = require('power-assert')
var testCases = require('./spec')

function createSandbox () {
const exports = {}
const sandbox = {
exports,
module: { exports },
require: (path) => {
delete require.cache[require.resolve(path)]
return require(path)
}
}

return sandbox
}

function testPlugin (code, options, fn) {
const result = babel.transform(code, options)
const sandbox = createSandbox()

vm.runInNewContext(result.code, sandbox)

fn(sandbox.module.exports)
}

function inspect (object) {
const result = util.inspect(object)
return result.replace('Object {', '{') // HACK the module.export inspect
}

function equal (actual, expected) {
if (typeof expected === 'string') {
assert(actual.toString() === expected)
} else if (typeof expected === 'function') {
assert(actual() === expected())
} else {
assert(inspect(actual) === inspect(expected))
}
}
import assert from 'assert'
import * as heplers from './helpers'
import testCases from './spec'

describe('babel-plugin-add-module-exports', () => {
it('should not export default to `module.exports` by default.', () =>
testPlugin(testCases[0].code, {
heplers.testPlugin(testCases[0].code, {
presets: ['es2015']
}, (module) => {
assert(module !== 'default-entry')
assert(module.default === 'default-entry')
}))

it('should handle duplicated plugin references (#1)', () =>
testPlugin(testCases[0].code, {
heplers.testPlugin(testCases[0].code, {
presets: ['es2015'],
plugins: [
'../lib/index.js',
'../lib/index.js',
'../lib/index.js'
'./lib/index.js',
'./lib/index.js',
'./lib/index.js'
]
}, (module) => {
assert(module === 'default-entry')

// @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722
assert(module.default === undefined)
}))

testCases.forEach(testCase =>
testCases.forEach((testCase) =>
it(`should ${testCase.name}`, () =>
testPlugin(testCase.code, {
heplers.testPlugin(testCase.code, {
presets: ['es2015'],
plugins: [
'transform-export-extensions',
'../lib/index.js'
'transform-export-extensions', // use export-from syntax
'./lib/index.js'
]
}, (module) => {
// assert module root (module.exports) object
equal(module, testCase.expected.module)
heplers.equal(module, testCase.expected.module)

if (typeof testCase.expected.exports !== 'object') {
return // avoid "Object.keys called on non-object" in node-v0
}

// assert each common entry is exported without error
Object.keys(testCase.expected.exports).forEach(key =>
equal(module[key], testCase.expected.exports[key]))
Object.keys(testCase.expected.exports).forEach((key) =>
heplers.equal(module[key], testCase.expected.exports[key]))
})))
})
Loading

0 comments on commit 3d14024

Please sign in to comment.