Skip to content

Commit cfc18c4

Browse files
committed
New import or export context to Assign, tracked to prevent import { foo } from 'lib' or export foo = 'bar' from adding foo to the var chain
1 parent 1e0714e commit cfc18c4

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/grammar.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ grammar =
365365

366366
Export: [
367367
o 'EXPORT Class', -> new Export $2
368-
o 'EXPORT Identifier = Expression', -> new Export new Assign($2, $4)
368+
o 'EXPORT Identifier = Expression', -> new Export new Assign $2, $4, 'export'
369369
o 'EXPORT DEFAULT Expression', -> new ExportDefault $3
370370
o 'EXPORT ExportImportClause', -> new ExportImport $2
371371
o 'EXPORT ExportImportClause FROM String', -> new ExportImport $2, $4

src/nodes.coffee

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,8 @@ exports.ModuleIdentifier = class ModuleIdentifier extends Base
13231323
children: ['original', 'alias']
13241324

13251325
compileNode: (o) ->
1326+
variableName = @alias.value or @original.value
1327+
o.scope.add variableName, 'import'
13261328
code = []
13271329
code.push @makeCode @original.value
13281330
code.push @makeCode " as #{@alias.value}" if @alias?
@@ -1368,20 +1370,18 @@ exports.Assign = class Assign extends Base
13681370
@value.klass = new Value @variable.base, properties
13691371
@value.name = name
13701372
@value.variable = @variable
1371-
unless @context
1373+
if (not @context) or @context in ['import', 'export']
13721374
varBase = @variable.unwrapAll()
13731375
unless varBase.isAssignable()
13741376
@variable.error "'#{@variable.compile o}' can't be assigned"
13751377
unless varBase.hasProperties?()
1376-
insideModuleStatement = no
1377-
for expression in o.scope.expressions.expressions
1378-
if expression instanceof Import or expression instanceof Export or expression instanceof ExportImport # But *not* ExportDefault
1379-
insideModuleStatement = yes
1380-
unless insideModuleStatement
1381-
if @param
1382-
o.scope.add varBase.value, 'var'
1383-
else
1384-
o.scope.find varBase.value
1378+
if @context in ['import', 'export']
1379+
o.scope.add varBase.value, @context
1380+
else if @param
1381+
o.scope.add varBase.value, 'var'
1382+
else
1383+
o.scope.find varBase.value
1384+
13851385
val = @value.compileToFragments o, LEVEL_LIST
13861386
@variable.front = true if isValue and @variable.base instanceof Obj
13871387
compiledName = @variable.compileToFragments o, LEVEL_LIST
@@ -1392,7 +1392,7 @@ exports.Assign = class Assign extends Base
13921392
compiledName.push @makeCode '"'
13931393
return compiledName.concat @makeCode(": "), val
13941394

1395-
answer = compiledName.concat @makeCode(" #{ @context or '=' } "), val
1395+
answer = compiledName.concat @makeCode(" #{ if @context and @context isnt 'export' then @context else '=' } "), val
13961396
if o.level <= LEVEL_LIST then answer else @wrapInBraces answer
13971397

13981398
# Brief implementation of recursive pattern matching, when assigning array or

test/modules.coffee

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ test "a variable can be assigned after an import", ->
195195
import {
196196
foo
197197
} from 'lib';
198+
198199
bar = 5;"""
199200
eq toJS(input), output
200201

@@ -204,12 +205,14 @@ test "variables can be assigned before and after an import", ->
204205
import { bar } from 'lib'
205206
baz = 7"""
206207
output = """
207-
var foo, baz;
208+
var baz, foo;
208209
209210
foo = 5;
211+
210212
import {
211213
bar
212214
} from 'lib';
215+
213216
baz = 7;"""
214217
eq toJS(input), output
215218

@@ -402,6 +405,7 @@ test "a variable named `from` can be assigned after an import", ->
402405
import {
403406
foo
404407
} from 'lib';
408+
405409
from = 5;"""
406410
eq toJS(input), output
407411

@@ -417,6 +421,7 @@ test "`from` can be assigned after a multiline import", ->
417421
import {
418422
foo
419423
} from 'lib';
424+
420425
from = 5;"""
421426
eq toJS(input), output
422427

0 commit comments

Comments
 (0)