diff --git a/package.json b/package.json index 8afae39..49f9e0e 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ }, "scripts": { "pretest": "npm run build", - "test": "mocha", + "test": "mocha --compilers js:buble/register", "pretest-coverage": "npm run build", "test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js", "posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist", @@ -42,6 +42,7 @@ "devDependencies": { "acorn": "^2.6.4", "babel-core": "^5.8.32", + "buble": "^0.6.4", "codecov.io": "^0.1.6", "console-group": "^0.2.0", "es6-promise": "^3.0.2", diff --git a/src/Bundle.js b/src/Bundle.js index 13c3a04..a050de2 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -167,6 +167,12 @@ export default class Bundle { msg += `: ${err.message}`; throw new Error( msg ); }) + .then( source => { + if ( typeof source === 'string' ) return source; + if ( source && typeof source === 'object' && source.code ) return source; + + throw new Error( `Error loading ${id}: load hook should return a string, a { code, map } object, or nothing/null` ); + }) .then( source => transform( source, id, this.transformers ) ) .then( source => { const { code, originalCode, ast, sourceMapChain } = source; diff --git a/src/ast/attachScopes.js b/src/ast/attachScopes.js index 92798e8..51fc28a 100644 --- a/src/ast/attachScopes.js +++ b/src/ast/attachScopes.js @@ -29,7 +29,7 @@ export default function attachScopes ( statement ) { let newScope; // create new function scope - if ( /Function/.test( node.type ) ) { + if ( /(Function|Class)/.test( node.type ) ) { newScope = new Scope({ parent: scope, block: false, @@ -38,7 +38,7 @@ export default function attachScopes ( statement ) { // named function expressions - the name is considered // part of the function's scope - if ( node.type === 'FunctionExpression' && node.id ) { + if ( /(Function|Class)Expression/.test( node.type ) && node.id ) { newScope.addDeclaration( node, false, false ); } } diff --git a/test/form/assignment-to-exports-class-declaration/_config.js b/test/form/assignment-to-exports-class-declaration/_config.js new file mode 100644 index 0000000..0f3d2ef --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'does not rewrite class declaration IDs', + options: { + moduleName: 'myModule' + } +}; diff --git a/test/form/assignment-to-exports-class-declaration/_expected/amd.js b/test/form/assignment-to-exports-class-declaration/_expected/amd.js new file mode 100644 index 0000000..c2d0691 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/amd.js @@ -0,0 +1,6 @@ +define(['exports'], function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/cjs.js b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js new file mode 100644 index 0000000..b062474 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js @@ -0,0 +1,4 @@ +'use strict'; + +exports.Foo = class Foo {} +exports.Foo = lol( exports.Foo ); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/es6.js b/test/form/assignment-to-exports-class-declaration/_expected/es6.js new file mode 100644 index 0000000..08e82c9 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/es6.js @@ -0,0 +1,4 @@ +Foo = class Foo {} +Foo = lol( Foo ); + +export { Foo }; \ No newline at end of file diff --git a/test/form/assignment-to-exports-class-declaration/_expected/iife.js b/test/form/assignment-to-exports-class-declaration/_expected/iife.js new file mode 100644 index 0000000..fa37538 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/iife.js @@ -0,0 +1,7 @@ +(function (exports) { + 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}((this.myModule = this.myModule || {}))); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/umd.js b/test/form/assignment-to-exports-class-declaration/_expected/umd.js new file mode 100644 index 0000000..bbefc87 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/umd.js @@ -0,0 +1,10 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.myModule = global.myModule || {}))); +}(this, function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +})); diff --git a/test/form/assignment-to-exports-class-declaration/main.js b/test/form/assignment-to-exports-class-declaration/main.js new file mode 100644 index 0000000..ceae376 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/main.js @@ -0,0 +1,2 @@ +export let Foo = class Foo {} +Foo = lol( Foo ); diff --git a/test/function/load-returns-string-or-null/_config.js b/test/function/load-returns-string-or-null/_config.js new file mode 100644 index 0000000..ea80699 --- /dev/null +++ b/test/function/load-returns-string-or-null/_config.js @@ -0,0 +1,15 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'throws error if load returns something wacky', + options: { + plugins: [{ + load: function () { + return 42; + } + }] + }, + error: function ( err ) { + assert.ok( /load hook should return a string, a \{ code, map \} object, or nothing\/null/.test( err.message ) ); + } +};