Skip to content

Commit

Permalink
support arbitrary expressions in export default
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Aug 5, 2014
1 parent c064adb commit 124cd57
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,41 @@ function isExportDeclaration(node) {
}

Module.prototype.buildExports = function () {
// [localVariable, exportedVariable]
// [expression, exportedVariable]
var exports = []

this.exports.forEach(function (node) {
var declaration = node.declaration

if (node.default) {
if (declaration.type !== 'Identifier') throw new Error('wtf')
exports.push([declaration.name, 'default'])
exports.push([declaration, 'default'])
return
}

if (node.specifiers) {
return node.specifiers.forEach(function (specifier) {
var id = specifier.id.name
exports.push([id, id])
var id = specifier.id
exports.push([id, id.name])
})
}

if (n.FunctionDeclaration.check(declaration)) {
var id = declaration.id.name
if (!id) throw new Error('unnamed function declaration')
return exports.push([id, id])
var id = declaration.id
if (!id.name) throw new Error('unnamed function declaration')
return exports.push([id, id.name])
}

if (n.VariableDeclaration.check(declaration)) {
return declaration.declarations.forEach(function (declaration) {
var id = declaration.id.name
exports.push([id, id])
var id = declaration.id
exports.push([id, id.name])
})
}

if (n.ClassDeclaration.check(declaration)) {
var id = declaration.id.name
if (!id) throw new Error('unnamed class')
return exports.push([id, id])
var id = declaration.id
if (!id.name) throw new Error('unnamed class')
return exports.push([id, id.name])
}

throw new Error('wtf')
Expand Down Expand Up @@ -97,7 +96,7 @@ Module.prototype.removeExports = function () {
* Returns a property
*/

function buildGetter(name, as) {
function buildGetter(expression, as) {
return b.property(
'init',
b.identifier(as),
Expand All @@ -109,7 +108,7 @@ function buildGetter(name, as) {
null,
[],
b.blockStatement(
[b.returnStatement(b.identifier(name))]
[b.returnStatement(expression)]
)
)
),
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/export-default-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var o = {
getter: function () { return o },
set setter(value) { o.prop2 = value },
prop1: null,
prop2: null
}
var a

export default a = o.getter().prop1 = o.setter = function () { return 'a' }
16 changes: 16 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ describe('Build Exports', function () {
vm.runInThisContext('if (exports.default !== 1) throw new Error()', context)
})

it('export-default-expr', function () {
var ast = read('export-default-expr')
var m = Module(ast)
m.buildExports()
m.removeExports()
var result = recast.print(m.ast)
assert(!~result.code.indexOf('export default'))
var context = vm.createContext()
vm.runInThisContext(vmExports, context)
vm.runInThisContext(result.code, context)
vm.runInThisContext('if (exports.default() !== "a") throw new Error()', context)
vm.runInThisContext('if (a() !== "a") throw new Error()', context)
vm.runInThisContext('if (o.prop1() !== "a") throw new Error()', context)
vm.runInThisContext('if (o.prop2() !== "a") throw new Error()', context)
})

it('export-multiple', function () {
var ast = read('export-multiple')
var m = Module(ast)
Expand Down

0 comments on commit 124cd57

Please sign in to comment.