Skip to content

Commit 42ce538

Browse files
committed
Fix: Export default function should be FunctionDeclaration (fixes #81)
1 parent 7fe5865 commit 42ce538

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

espree.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,8 +4471,8 @@ function parseParams(firstRestricted) {
44714471
};
44724472
}
44734473

4474-
function parseFunctionDeclaration() {
4475-
var id, body, token, tmp, firstRestricted, message, previousStrict, previousYieldAllowed, generator,
4474+
function parseFunctionDeclaration(identifierIsOptional) {
4475+
var id = null, body, token, tmp, firstRestricted, message, previousStrict, previousYieldAllowed, generator,
44764476
marker = markerCreate(),
44774477
allowGenerators = extra.ecmaFeatures.generators;
44784478

@@ -4484,21 +4484,24 @@ function parseFunctionDeclaration() {
44844484
generator = true;
44854485
}
44864486

4487-
token = lookahead;
4487+
if (!identifierIsOptional || !match("(")) {
44884488

4489-
id = parseVariableIdentifier();
4489+
token = lookahead;
44904490

4491-
if (strict) {
4492-
if (syntax.isRestrictedWord(token.value)) {
4493-
throwErrorTolerant(token, Messages.StrictFunctionName);
4494-
}
4495-
} else {
4496-
if (syntax.isRestrictedWord(token.value)) {
4497-
firstRestricted = token;
4498-
message = Messages.StrictFunctionName;
4499-
} else if (syntax.isStrictModeReservedWord(token.value)) {
4500-
firstRestricted = token;
4501-
message = Messages.StrictReservedWord;
4491+
id = parseVariableIdentifier();
4492+
4493+
if (strict) {
4494+
if (syntax.isRestrictedWord(token.value)) {
4495+
throwErrorTolerant(token, Messages.StrictFunctionName);
4496+
}
4497+
} else {
4498+
if (syntax.isRestrictedWord(token.value)) {
4499+
firstRestricted = token;
4500+
message = Messages.StrictFunctionName;
4501+
} else if (syntax.isStrictModeReservedWord(token.value)) {
4502+
firstRestricted = token;
4503+
message = Messages.StrictReservedWord;
4504+
}
45024505
}
45034506
}
45044507

@@ -4733,9 +4736,8 @@ function parseExportDefaultDeclaration() {
47334736
// export default function () {}
47344737
// export default class {}
47354738
if (lookahead.value === "function") {
4736-
// TODO: change this to declaration once we get FunctionDeclaration with nullable name
4737-
declaration = parseFunctionExpression();
4738-
return markerApply(marker, astNodeFactory.createExportDefaultDeclaration(declaration));
4739+
declaration = parseFunctionDeclaration(true);
4740+
return markerApply(marker, astNodeFactory.createExportDefaultDeclaration(declaration));
47394741
} else if (lookahead.value === "class") {
47404742
declaration = parseClassDeclaration(true);
47414743
return markerApply(marker, astNodeFactory.createExportDefaultDeclaration(declaration));

tests/fixtures/ecma-features/modules/export-default-function.result.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
{
55
"type": "ExportDefaultDeclaration",
66
"declaration": {
7-
"type": "FunctionExpression",
7+
"type": "FunctionDeclaration",
88
"id": null,
99
"params": [],
1010
"defaults": [],
@@ -74,4 +74,4 @@ module.exports = {
7474
"column": 29
7575
}
7676
}
77-
};
77+
};

0 commit comments

Comments
 (0)