Skip to content

Commit 21de343

Browse files
author
Alexander Pánek
committed
Add support for simple import *statement* to identifier
I couldn't get import as an expression to work, so I resorted to define it as a statement for now. I'm pretty sure multi-line imports don't work either and there's no alias functionality yet. So this is still *heavily* WIP.
1 parent 2a0f178 commit 21de343

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

src/grammar.coffee

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ grammar =
9797
o 'Return'
9898
o 'Comment'
9999
o 'STATEMENT', -> new StatementLiteral $1
100+
o 'Import'
101+
o 'Export'
100102
]
101103

102104
# All the different types of expressions in our language. The basic unit of
@@ -117,8 +119,6 @@ grammar =
117119
o 'Class'
118120
o 'Throw'
119121
o 'Yield'
120-
o 'Import'
121-
o 'Export'
122122
]
123123

124124
Yield: [
@@ -353,6 +353,7 @@ grammar =
353353

354354
Import: [
355355
o 'IMPORT Expression', -> new Import $2
356+
o 'IMPORT Assignable FROM Expression', -> new Import $4, $2
356357
]
357358

358359
# Ordinary function invocation, or a chained series of calls.

src/lexer.coffee

+10-1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ exports.Lexer = class Lexer
258258
@token 'JS', (script = match[0])[1...-1], 0, script.length
259259
script.length
260260

261+
importToken: ->
262+
match = @chunk.match(IMPORT)
263+
264+
return 0 unless match
265+
266+
@token('IMPORT', match[0], 0, match[0].length)
267+
261268
# Matches regular expression literals, as well as multiline extended ones.
262269
# Lexing regular expressions is difficult to distinguish from division, so we
263270
# borrow some basic heuristics from JavaScript and Ruby.
@@ -774,7 +781,7 @@ JS_KEYWORDS = [
774781
'return', 'throw', 'break', 'continue', 'debugger', 'yield'
775782
'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally'
776783
'class', 'extends', 'super'
777-
'export', 'import', 'default'
784+
'export', 'import', 'from', 'default'
778785
]
779786

780787
# CoffeeScript-only keywords.
@@ -849,6 +856,8 @@ MULTI_DENT = /^(?:\n[^\n\S]*)+/
849856

850857
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/
851858

859+
IMPORT = /^import .+/
860+
852861
# String-matching-regexes.
853862
STRING_START = /^(?:'''|"""|'|")/
854863

src/nodes.coffee

+17-3
Original file line numberDiff line numberDiff line change
@@ -1226,18 +1226,32 @@ exports.Class = class Class extends Base
12261226
#### Import
12271227

12281228
exports.Import = class Import extends Base
1229-
constructor: (@expression) ->
1229+
constructor: (@expression, @identifier) ->
12301230

1231-
children: ['expression']
1231+
children: ['expression', 'identifier']
12321232

12331233
isStatement: YES
12341234
jumps: NO
12351235

12361236
makeReturn: THIS
12371237

12381238
compileNode: (o) ->
1239-
[].concat @makeCode(@tab + 'import '), @expression.compileToFragments(o), @makeCode(';')
1239+
code = []
1240+
1241+
code.push @makeCode(@tab + 'import ')
1242+
1243+
1244+
if @identifier
1245+
code.push @makeCode("#{@identifier.value} from ")
1246+
1247+
1248+
if @expression.base.value?
1249+
code.push @makeCode(@expression.base.value)
1250+
else
1251+
code.push @makeCode(@expression.base)
1252+
code.push @makeCode(';')
12401253

1254+
code
12411255

12421256
#### Assign
12431257

test/modules.coffee

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ test "import module", ->
2121
# console.log toJS input
2222
eq toJS(input), output
2323

24-
# test "module import test, syntax #1", ->
25-
# input = "import foo from 'lib'"
26-
# output = "import foo from 'lib';"
27-
# eq toJS(input), output
24+
test "module import test, syntax #1", ->
25+
input = "import foo from 'lib'"
26+
output = "import foo from 'lib';"
27+
eq toJS(input), output
2828

29-
# test "module import test, syntax #2", ->
30-
# input = "import { foo } from 'lib'"
31-
# output = "import { foo } from 'lib';"
32-
# eq toJS(input), output
29+
test "module import test, syntax #2", ->
30+
input = "import { foo } from 'lib'"
31+
output = "import { foo } from 'lib';"
32+
eq toJS(input), output
3333

3434
# test "module import test, syntax #3", ->
3535
# input = "import { default as foo } from 'lib'"

0 commit comments

Comments
 (0)