Skip to content

Commit 3ea0481

Browse files
authored
Merge pull request #4388 from GeoffreyBooth/import-member-named-default
Allow imported module members to be named `default`
2 parents 81b9fe3 + 03eceeb commit 3ea0481

File tree

6 files changed

+177
-111
lines changed

6 files changed

+177
-111
lines changed

lib/coffee-script/grammar.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffee-script/lexer.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffee-script/parser.js

Lines changed: 118 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/grammar.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ grammar =
372372
ImportSpecifier: [
373373
o 'Identifier', -> new ImportSpecifier $1
374374
o 'Identifier AS Identifier', -> new ImportSpecifier $1, $3
375+
o 'DEFAULT', -> new ImportSpecifier new Literal $1
376+
o 'DEFAULT AS Identifier', -> new ImportSpecifier new Literal($1), $3
375377
]
376378

377379
ImportDefaultSpecifier: [
@@ -409,6 +411,7 @@ grammar =
409411
o 'Identifier', -> new ExportSpecifier $1
410412
o 'Identifier AS Identifier', -> new ExportSpecifier $1, $3
411413
o 'Identifier AS DEFAULT', -> new ExportSpecifier $1, new Literal $3
414+
o 'DEFAULT', -> new ExportSpecifier new Literal $1
412415
]
413416

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

src/lexer.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ exports.Lexer = class Lexer
121121
@tokens[@tokens.length - 1][0] = 'IMPORT_ALL'
122122
else if @value() in COFFEE_KEYWORDS
123123
@tokens[@tokens.length - 1][0] = 'IDENTIFIER'
124-
if @tag() in ['IMPORT_ALL', 'IDENTIFIER']
124+
if @tag() in ['DEFAULT', 'IMPORT_ALL', 'IDENTIFIER']
125125
@token 'AS', id
126126
return id.length
127127
if id is 'as' and @seenExport and @tag() is 'IDENTIFIER'

test/modules.coffee

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,51 @@ test "default and wrapped members can be imported multiple times if aliased", ->
685685
foo as bar
686686
} from 'lib';"""
687687
eq toJS(input), output
688+
689+
test "import a member named default", ->
690+
input = "import { default } from 'lib'"
691+
output = """
692+
import {
693+
default
694+
} from 'lib';"""
695+
eq toJS(input), output
696+
697+
test "import an aliased member named default", ->
698+
input = "import { default as def } from 'lib'"
699+
output = """
700+
import {
701+
default as def
702+
} from 'lib';"""
703+
eq toJS(input), output
704+
705+
test "export a member named default", ->
706+
input = "export { default }"
707+
output = """
708+
export {
709+
default
710+
};"""
711+
eq toJS(input), output
712+
713+
test "export an aliased member named default", ->
714+
input = "export { def as default }"
715+
output = """
716+
export {
717+
def as default
718+
};"""
719+
eq toJS(input), output
720+
721+
test "export an imported member named default", ->
722+
input = "import { default } from 'lib'"
723+
output = """
724+
import {
725+
default
726+
} from 'lib';"""
727+
eq toJS(input), output
728+
729+
test "export an imported aliased member named default", ->
730+
input = "import { default as def } from 'lib'"
731+
output = """
732+
import {
733+
default as def
734+
} from 'lib';"""
735+
eq toJS(input), output

0 commit comments

Comments
 (0)