mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Merge pull request #4483 from GeoffreyBooth/fix-export-default
Fix #4451 and 4481: `default` in `export` statements
This commit is contained in:
commit
c1e3c02d13
6 changed files with 138 additions and 108 deletions
|
@ -395,6 +395,8 @@
|
|||
return new ExportSpecifier($1, new Literal($3));
|
||||
}), o('DEFAULT', function() {
|
||||
return new ExportSpecifier(new Literal($1));
|
||||
}), o('DEFAULT AS Identifier', function() {
|
||||
return new ExportSpecifier(new Literal($1), $3);
|
||||
})
|
||||
],
|
||||
Invocation: [
|
||||
|
|
|
@ -69,7 +69,7 @@
|
|||
};
|
||||
|
||||
Lexer.prototype.identifierToken = function() {
|
||||
var alias, colon, colonOffset, id, idLength, input, match, poppedToken, prev, ref2, ref3, ref4, ref5, ref6, ref7, tag, tagToken;
|
||||
var alias, colon, colonOffset, id, idLength, input, match, poppedToken, prev, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, tag, tagToken;
|
||||
if (!(match = IDENTIFIER.exec(this.chunk))) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -95,19 +95,19 @@
|
|||
return id.length;
|
||||
}
|
||||
}
|
||||
if (id === 'as' && this.seenExport && this.tag() === 'IDENTIFIER') {
|
||||
if (id === 'as' && this.seenExport && ((ref4 = this.tag()) === 'IDENTIFIER' || ref4 === 'DEFAULT')) {
|
||||
this.token('AS', id);
|
||||
return id.length;
|
||||
}
|
||||
if (id === 'default' && this.seenExport) {
|
||||
if (id === 'default' && this.seenExport && ((ref5 = this.tag()) === 'EXPORT' || ref5 === 'AS')) {
|
||||
this.token('DEFAULT', id);
|
||||
return id.length;
|
||||
}
|
||||
ref4 = this.tokens, prev = ref4[ref4.length - 1];
|
||||
tag = colon || (prev != null) && (((ref5 = prev[0]) === '.' || ref5 === '?.' || ref5 === '::' || ref5 === '?::') || !prev.spaced && prev[0] === '@') ? 'PROPERTY' : 'IDENTIFIER';
|
||||
ref6 = this.tokens, prev = ref6[ref6.length - 1];
|
||||
tag = colon || (prev != null) && (((ref7 = prev[0]) === '.' || ref7 === '?.' || ref7 === '::' || ref7 === '?::') || !prev.spaced && prev[0] === '@') ? 'PROPERTY' : 'IDENTIFIER';
|
||||
if (tag === 'IDENTIFIER' && (indexOf.call(JS_KEYWORDS, id) >= 0 || indexOf.call(COFFEE_KEYWORDS, id) >= 0) && !(this.exportSpecifierList && indexOf.call(COFFEE_KEYWORDS, id) >= 0)) {
|
||||
tag = id.toUpperCase();
|
||||
if (tag === 'WHEN' && (ref6 = this.tag(), indexOf.call(LINE_BREAK, ref6) >= 0)) {
|
||||
if (tag === 'WHEN' && (ref8 = this.tag(), indexOf.call(LINE_BREAK, ref8) >= 0)) {
|
||||
tag = 'LEADING_WHEN';
|
||||
} else if (tag === 'FOR') {
|
||||
this.seenFor = true;
|
||||
|
@ -172,7 +172,7 @@
|
|||
tagToken.origin = [tag, alias, tagToken[2]];
|
||||
}
|
||||
if (poppedToken) {
|
||||
ref7 = [poppedToken[2].first_line, poppedToken[2].first_column], tagToken[2].first_line = ref7[0], tagToken[2].first_column = ref7[1];
|
||||
ref9 = [poppedToken[2].first_line, poppedToken[2].first_column], tagToken[2].first_line = ref9[0], tagToken[2].first_column = ref9[1];
|
||||
}
|
||||
if (colon) {
|
||||
colonOffset = input.lastIndexOf(':');
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -412,6 +412,7 @@ grammar =
|
|||
o 'Identifier AS Identifier', -> new ExportSpecifier $1, $3
|
||||
o 'Identifier AS DEFAULT', -> new ExportSpecifier $1, new Literal $3
|
||||
o 'DEFAULT', -> new ExportSpecifier new Literal $1
|
||||
o 'DEFAULT AS Identifier', -> new ExportSpecifier new Literal($1), $3
|
||||
]
|
||||
|
||||
# Ordinary function invocation, or a chained series of calls.
|
||||
|
|
|
@ -124,10 +124,10 @@ exports.Lexer = class Lexer
|
|||
if @tag() in ['DEFAULT', 'IMPORT_ALL', 'IDENTIFIER']
|
||||
@token 'AS', id
|
||||
return id.length
|
||||
if id is 'as' and @seenExport and @tag() is 'IDENTIFIER'
|
||||
if id is 'as' and @seenExport and @tag() in ['IDENTIFIER', 'DEFAULT']
|
||||
@token 'AS', id
|
||||
return id.length
|
||||
if id is 'default' and @seenExport
|
||||
if id is 'default' and @seenExport and @tag() in ['EXPORT', 'AS']
|
||||
@token 'DEFAULT', id
|
||||
return id.length
|
||||
|
||||
|
|
|
@ -718,7 +718,7 @@ test "export an aliased member named default", ->
|
|||
};"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export an imported member named default", ->
|
||||
test "import an imported member named default", ->
|
||||
input = "import { default } from 'lib'"
|
||||
output = """
|
||||
import {
|
||||
|
@ -726,7 +726,7 @@ test "export an imported member named default", ->
|
|||
} from 'lib';"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export an imported aliased member named default", ->
|
||||
test "import an imported aliased member named default", ->
|
||||
input = "import { default as def } from 'lib'"
|
||||
output = """
|
||||
import {
|
||||
|
@ -734,6 +734,22 @@ test "export an imported aliased member named default", ->
|
|||
} from 'lib';"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export an imported member named default", ->
|
||||
input = "export { default } from 'lib'"
|
||||
output = """
|
||||
export {
|
||||
default
|
||||
} from 'lib';"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "export an imported aliased member named default", ->
|
||||
input = "export { default as def } from 'lib'"
|
||||
output = """
|
||||
export {
|
||||
default as def
|
||||
} from 'lib';"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "#4394: export shouldn't prevent variable declarations", ->
|
||||
input = """
|
||||
x = 1
|
||||
|
@ -749,3 +765,11 @@ test "#4394: export shouldn't prevent variable declarations", ->
|
|||
};
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
||||
test "#4451: `default` in an export statement is only treated as a keyword when it follows `export` or `as`", ->
|
||||
input = "export default { default: 1 }"
|
||||
output = """
|
||||
export default {
|
||||
"default": 1
|
||||
};
|
||||
"""
|
||||
|
|
Loading…
Add table
Reference in a new issue