Skip to content

Commit

Permalink
Throw an error if an import or export statement isn’t at the top-leve…
Browse files Browse the repository at this point in the history
…l scope, with tests
  • Loading branch information
GeoffreyBooth committed Aug 19, 2016
1 parent be49dc0 commit 3eb4533
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,9 @@ exports.Module = class Module extends Base
makeReturn: THIS

compileNode: (o) ->
if o.indent.length isnt 0

This comment has been minimized.

Copy link
@lydell

lydell Aug 19, 2016

Collaborator

I think this should be if o.scope.parent.

This comment has been minimized.

Copy link
@GeoffreyBooth

GeoffreyBooth Aug 19, 2016

Author Owner

This test fails with o.scope.parent:

if foo
  import { bar } from 'lib'

For whatever reason, there is no scope parent in this case. And import statements cannot be in if blocks.

This comment has been minimized.

Copy link
@lydell

lydell Aug 19, 2016

Collaborator

Ah, sorry, I forgot that import/export can’t be inside if, for, try etc. I only thought of inside functions (which is where o.scope.parent exists).

@error "#{@type} statements must be at top-level scope"

code = []

code.push @makeCode "#{@tab}#{@type} "
Expand Down
19 changes: 19 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,22 @@ test "anonymous classes cannot be exported", ->
@constructor: ->
console.log 'hello, world!'
''', 'SyntaxError: anonymous classes cannot be exported'

test "imports and exports must be top-level", ->
assertErrorFormat '''
if foo
import { bar } from 'lib'
''', '''
[stdin]:2:3: error: import statements must be at top-level scope
import { bar } from 'lib'
^^^^^^^^^^^^^^^^^^^^^^^^^
'''

assertErrorFormat '''
foo = ->
export { bar }
''', '''
[stdin]:2:3: error: export statements must be at top-level scope
export { bar }
^^^^^^^^^^^^^^
'''

0 comments on commit 3eb4533

Please sign in to comment.