Skip to content

Commit 3eb4533

Browse files
committed
Throw an error if an import or export statement isn’t at the top-level scope, with tests
1 parent be49dc0 commit 3eb4533

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/nodes.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,9 @@ exports.Module = class Module extends Base
12431243
makeReturn: THIS
12441244

12451245
compileNode: (o) ->
1246+
if o.indent.length isnt 0
1247+
@error "#{@type} statements must be at top-level scope"
1248+
12461249
code = []
12471250

12481251
code.push @makeCode "#{@tab}#{@type} "

test/error_messages.coffee

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,22 @@ test "anonymous classes cannot be exported", ->
10081008
@constructor: ->
10091009
console.log 'hello, world!'
10101010
''', 'SyntaxError: anonymous classes cannot be exported'
1011+
1012+
test "imports and exports must be top-level", ->
1013+
assertErrorFormat '''
1014+
if foo
1015+
import { bar } from 'lib'
1016+
''', '''
1017+
[stdin]:2:3: error: import statements must be at top-level scope
1018+
import { bar } from 'lib'
1019+
^^^^^^^^^^^^^^^^^^^^^^^^^
1020+
'''
1021+
1022+
assertErrorFormat '''
1023+
foo = ->
1024+
export { bar }
1025+
''', '''
1026+
[stdin]:2:3: error: export statements must be at top-level scope
1027+
export { bar }
1028+
^^^^^^^^^^^^^^
1029+
'''

0 commit comments

Comments
 (0)