Skip to content

Commit 1e0714e

Browse files
committed
Define 'FROM' token only when in an import or export statement and from is followed by a string. More tests around assignment after import, not all passing yet.
1 parent 452f073 commit 1e0714e

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/lexer.coffee

+6-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ exports.Lexer = class Lexer
115115
if id is 'from' and @tag() is 'YIELD'
116116
@token 'FROM', id
117117
return id.length
118-
if id is 'from' and (@seenImport or @seenExport)
119-
@token 'FROM', id
120-
return id.length
121118
if id is 'as' and (@seenImport or @seenExport) and @tokens[@tokens.length - 1][0] in ['IDENTIFIER', 'IMPORT_ALL']
122119
@token 'AS', id
123120
return id.length
@@ -218,6 +215,12 @@ exports.Lexer = class Lexer
218215
stringToken: ->
219216
[quote] = STRING_START.exec(@chunk) || []
220217
return 0 unless quote
218+
219+
# If the preceding token is `from` and this is an import or export statement,
220+
# properly tag the `from`.
221+
if @tokens.length and @tokens[@tokens.length - 1][1] is 'from' and (@seenImport or @seenExport)
222+
@tokens[@tokens.length - 1][0] = 'FROM'
223+
221224
regex = switch quote
222225
when "'" then STRING_SINGLE
223226
when '"' then STRING_DOUBLE

test/modules.coffee

+46-2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,33 @@ test "multiline complex import", ->
185185
} from 'lib';"""
186186
eq toJS(input), output
187187

188+
test "a variable can be assigned after an import", ->
189+
input = """
190+
import { foo } from 'lib'
191+
bar = 5"""
192+
output = """
193+
var bar;
194+
195+
import {
196+
foo
197+
} from 'lib';
198+
bar = 5;"""
199+
eq toJS(input), output
200+
201+
test "variables can be assigned before and after an import", ->
202+
input = """
203+
foo = 5
204+
import { bar } from 'lib'
205+
baz = 7"""
206+
output = """
207+
var foo, baz;
208+
209+
foo = 5;
210+
import {
211+
bar
212+
} from 'lib';
213+
baz = 7;"""
214+
eq toJS(input), output
188215

189216
# Export statements
190217

@@ -365,14 +392,31 @@ test "`from` not part of an import or export statement can still be assigned", -
365392
from = 5
366393
eq 5, from
367394

368-
test "`from` can be assigned after an import", ->
395+
test "a variable named `from` can be assigned after an import", ->
369396
input = """
370397
import { foo } from 'lib'
371398
from = 5"""
372399
output = """
373400
var from;
374401
375-
import { foo } from 'lib';
402+
import {
403+
foo
404+
} from 'lib';
405+
from = 5;"""
406+
eq toJS(input), output
407+
408+
test "`from` can be assigned after a multiline import", ->
409+
input = """
410+
import {
411+
foo
412+
} from 'lib'
413+
from = 5"""
414+
output = """
415+
var from;
416+
417+
import {
418+
foo
419+
} from 'lib';
376420
from = 5;"""
377421
eq toJS(input), output
378422

0 commit comments

Comments
 (0)