Skip to content

Commit de78f48

Browse files
author
Alexander Pánek
committed
NamedImports work now and can be mixed with Identifiers as well
I think I'm getting a hang of how it's supposed to work. 🙌 What works now, as you can see in all the tests that are not commented out and should run through: ```coffee import foo import bar from lib import { foo } from lib import { foo as boo, bar } from lib import { foo, bar } from lib ```
1 parent d86540a commit de78f48

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

src/grammar.coffee

+16-10
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,6 @@ grammar =
139139
o 'IDENTIFIER', -> new IdentifierLiteral $1
140140
]
141141

142-
IdentifierList: [
143-
o 'Identifier', -> [$1]
144-
o 'IdentifierList , Identifier', -> $1.concat $3
145-
o 'IdentifierList OptComma TERMINATOR Identifier', -> $1.concat $4
146-
o 'INDENT IdentifierList OptComma OUTDENT', -> $2
147-
o 'IdentifierList OptComma INDENT IdentifierList OptComma OUTDENT', -> $1.concat $4
148-
]
149-
150142
Property: [
151143
o 'PROPERTY', -> new PropertyName $1
152144
]
@@ -360,13 +352,27 @@ grammar =
360352
]
361353

362354
NamedImports: [
363-
o '{ }', -> new IdentifierList []
364-
o '{ IdentifierList OptComma }', -> new IdentifierList $2
355+
o '{ }', -> new ImportsList []
356+
o '{ ImportsList OptComma }', -> new ImportsList $2
365357
]
366358

367359
ImportClause: [
368360
o 'NamedImports'
361+
o 'ImportSpecifier'
362+
o '* IMPORT_AS Identifier'
363+
]
364+
365+
ImportSpecifier: [
369366
o 'Identifier'
367+
o 'Identifier IMPORT_AS Identifier', -> new ImportSpecifier $1, $3
368+
]
369+
370+
ImportsList: [
371+
o 'ImportSpecifier', -> [$1]
372+
o 'ImportsList , ImportSpecifier', -> $1.concat $3
373+
o 'ImportsList OptComma TERMINATOR ImportSpecifier', -> $1.concat $4
374+
o 'INDENT ImportsList OptComma OUTDENT', -> $2
375+
o 'ImportsList OptComma INDENT ImportsList OptComma OUTDENT', -> $1.concat $4
370376
]
371377

372378
Import: [

src/nodes.coffee

+9-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ exports.Import = class Import extends Base
12531253
code.push @makeCode(';')
12541254
code
12551255

1256-
exports.IdentifierList = class IdentifierList extends Base
1256+
exports.ImportsList = class ImportsList extends Base
12571257
constructor: (identifiers) ->
12581258
@identifiers = identifiers or []
12591259

@@ -1280,6 +1280,14 @@ exports.IdentifierList = class IdentifierList extends Base
12801280

12811281
code
12821282

1283+
exports.ImportSpecifier = class ImportSpecifier extends Base
1284+
constructor: (@original, @alias) ->
1285+
1286+
children: ['from', 'as']
1287+
1288+
compileNode: (o) ->
1289+
return [@makeCode("#{@original.value} as #{@alias.value}")]
1290+
12831291
#### Assign
12841292

12851293
# The **Assign** is used to assign a local variable to value, or to set the

test/modules.coffee

+17-12
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,25 @@ test "module import test, syntax #2", ->
3131
output = "import { foo } from 'lib';"
3232
eq toJS(input), output
3333

34-
# test "module import test, syntax #3", ->
35-
# input = "import { default as foo } from 'lib'"
36-
# output = "import { default as foo } from 'lib';"
37-
# eq toJS(input), output
34+
test "module import test, syntax #3", ->
35+
input = "import { bar as foo } from 'lib'"
36+
output = "import { bar as foo } from 'lib';"
37+
eq toJS(input), output
3838

39-
# test "module import test, syntax #4", ->
40-
# input = "import { square, diag } from 'lib'"
41-
# output = "import { square, diag } from 'lib';"
42-
# eq toJS(input), output
39+
test "module import test, syntax #3", ->
40+
input = "import { oof, bar as foo } from 'lib'"
41+
output = "import { oof, bar as foo } from 'lib';"
42+
eq toJS(input), output
4343

44-
# test "module import test, syntax #5", ->
45-
# input = "import { foo } from 'lib' # with a comment"
46-
# output = "import { foo } from 'lib' ;"
47-
# eq toJS(input), output
44+
test "module import test, syntax #4", ->
45+
input = "import { square, diag } from 'lib'"
46+
output = "import { square, diag } from 'lib';"
47+
eq toJS(input), output
48+
49+
test "module import test, syntax #5", ->
50+
input = "import { foo } from 'lib' # with a comment"
51+
output = "import { foo } from 'lib';"
52+
eq toJS(input), output
4853

4954
# test "module export test, syntax #1", ->
5055
# input = "export default mixin"

0 commit comments

Comments
 (0)