Skip to content

Commit 7a3113e

Browse files
committed
Support exporting non-default identifiers and classes; limit the default expressions that we allow to be exported to the types of expressions that make sense; more tests and error-checking
1 parent 29467a9 commit 7a3113e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/grammar.coffee

+5-1
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,12 @@ grammar =
370370
Export: [
371371
o 'EXPORT ExportClause', -> new Module 'export', $2
372372
o 'EXPORT ExportClause FROM SimpleString', -> new Module 'export', $2, $4
373-
o 'EXPORT DEFAULT Expression', -> new Module 'export', $3, null, yes
373+
o 'EXPORT Identifier', -> new Module 'export', $2
374374
o 'EXPORT Class', -> new Module 'export', $2
375+
o 'EXPORT DEFAULT Value', -> new Module 'export', $3, null, yes
376+
o 'EXPORT DEFAULT Code', -> new Module 'export', $3, null, yes
377+
o 'EXPORT DEFAULT Assign', -> new Module 'export', $3, null, yes
378+
o 'EXPORT DEFAULT Class', -> new Module 'export', $3, null, yes
375379
]
376380

377381
ExportClause: [

src/nodes.coffee

+4
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ exports.Module = class Module extends Base
12301230
if @type isnt 'import' and @type isnt 'export'
12311231
@error 'module type must be import or export'
12321232

1233+
if @type is 'export' and @default is no and @clause instanceof Class and not @clause.variable?
1234+
@error 'anonymous classes cannot be exported'
1235+
1236+
12331237
children: ['clause', 'moduleName']
12341238

12351239
isStatement: YES

test/error_messages.coffee

+17
Original file line numberDiff line numberDiff line change
@@ -994,3 +994,20 @@ test "`&&=` and `||=` with a space in-between", ->
994994
a or = 1
995995
^
996996
'''
997+
998+
test "anonymous functions cannot be exported", ->
999+
assertErrorFormat '''
1000+
export ->
1001+
console.log 'hello, world!'
1002+
''', '''
1003+
[stdin]:1:8: error: unexpected ->
1004+
export ->
1005+
^^
1006+
'''
1007+
1008+
test "anonymous classes cannot be exported", ->
1009+
assertErrorFormat '''
1010+
export class
1011+
@constructor: ->
1012+
console.log 'hello, world!'
1013+
''', 'SyntaxError: anonymous classes cannot be exported'

test/modules.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ test "export default multiline function", ->
248248
};"""
249249
eq toJS(input), output
250250

251+
test "export predefined function", ->
252+
input = """
253+
foo = (bar) ->
254+
console.log bar
255+
export foo"""
256+
output = """
257+
var foo;
258+
259+
foo = function(bar) {
260+
return console.log(bar);
261+
};
262+
263+
export foo;"""
264+
eq toJS(input), output
265+
251266
# Uncomment this test once ES2015+ `class` support is added
252267

253268
# test "export default class", ->

0 commit comments

Comments
 (0)