check valid function/class token when parsing export default before converting to a declaration - fixes #2145

This commit is contained in:
Sebastian McKenzie 2015-08-02 21:36:52 +01:00
parent a0f9d5fbc8
commit 94e345e0b2
7 changed files with 207 additions and 1 deletions

View File

@ -648,9 +648,10 @@ pp.parseExport = function (node) {
}
this.parseExportFrom(node, true);
} else if (this.eat(tt._default)) { // export default ...
let possibleDeclaration = this.match(tt._function) || this.match(tt._class);
let expr = this.parseMaybeAssign();
let needsSemi = true;
if (expr.type === "FunctionExpression" || expr.type === "ClassExpression") {
if (possibleDeclaration) {
needsSemi = false;
if (expr.id) {
expr.type = expr.type === "FunctionExpression" ? "FunctionDeclaration" : "ClassDeclaration";

View File

@ -0,0 +1 @@
export default function a() {}

View File

@ -0,0 +1,98 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"declaration": {
"type": "FunctionDeclaration",
"start": 15,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 30
}
},
"id": {
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
}
},
"name": "a"
},
"generator": false,
"expression": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 28,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 30
}
},
"body": []
}
}
}
]
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export default (function a() {});

View File

@ -0,0 +1,99 @@
{
"type": "File",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 33
}
},
"program": {
"type": "Program",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 33
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 33
}
},
"declaration": {
"type": "FunctionExpression",
"start": 16,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 31
}
},
"id": {
"type": "Identifier",
"start": 25,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 26
}
},
"name": "a"
},
"generator": false,
"expression": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 29,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 29
},
"end": {
"line": 1,
"column": 31
}
},
"body": []
},
"parenthesizedExpression": true
}
}
]
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}