-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from 59naga/5to6
Change 5 to 6 syntax
- Loading branch information
Showing
10 changed files
with
189 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"presets": [ | ||
"es2015" | ||
], | ||
"env": { | ||
"development": { | ||
"sourceMap": "inline", | ||
"presets": [ | ||
"power-assert" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
plugins: | ||
- mocha | ||
|
||
extends: standard | ||
extends: | ||
- standard | ||
|
||
env: | ||
node: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
}))) | ||
}) |
Oops, something went wrong.