Permalink
Browse files

Recognize Token.Template as a ReturnStatement argument.

When a Template is broken across multiple lines, the existing hasArgument test fails.
The Template is scanned as a single token so we just need to check for it after the return.

Fixes #1829
Closes gh-1882
  • Loading branch information...
johnjbarton authored and ariya committed Nov 17, 2017
1 parent cab42bb commit 9973bab92f824db9ed44ae8931a8890309f2322b
View
@@ -2451,8 +2451,10 @@ export class Parser {
const node = this.createNode();
this.expectKeyword('return');
const hasArgument = !this.match(';') && !this.match('}') &&
!this.hasLineTerminator && this.lookahead.type !== Token.EOF;
const hasArgument = (!this.match(';') && !this.match('}') &&
!this.hasLineTerminator && this.lookahead.type !== Token.EOF) ||
this.lookahead.type === Token.Template;
const argument = hasArgument ? this.parseExpression() : null;
this.consumeSemicolon();
@@ -0,0 +1,5 @@
function a() {
return `
test
`;
}
@@ -0,0 +1,301 @@
{
"type": "Program",
"body": [
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "a",
"range": [
9,
10
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
}
},
"params": [],
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ReturnStatement",
"argument": {
"type": "TemplateLiteral",
"quasis": [
{
"type": "TemplateElement",
"value": {
"raw": "\n \ttest\n ",
"cooked": "\n \ttest\n "
},
"tail": true,
"range": [
24,
37
],
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 4,
"column": 3
}
}
}
],
"expressions": [],
"range": [
24,
37
],
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 4,
"column": 3
}
}
},
"range": [
17,
38
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 4,
"column": 4
}
}
}
],
"range": [
13,
40
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 5,
"column": 1
}
}
},
"generator": false,
"expression": false,
"async": false,
"range": [
0,
40
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
}
}
],
"sourceType": "script",
"range": [
0,
40
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"tokens": [
{
"type": "Keyword",
"value": "function",
"range": [
0,
8
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
},
{
"type": "Identifier",
"value": "a",
"range": [
9,
10
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
}
},
{
"type": "Punctuator",
"value": "(",
"range": [
10,
11
],
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": "Punctuator",
"value": ")",
"range": [
11,
12
],
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
}
}
},
{
"type": "Punctuator",
"value": "{",
"range": [
13,
14
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
}
},
{
"type": "Keyword",
"value": "return",
"range": [
17,
23
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
}
}
},
{
"type": "Template",
"value": "`\n \ttest\n `",
"range": [
24,
37
],
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 4,
"column": 3
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
37,
38
],
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 4
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
39,
40
],
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
}
}
]
}

0 comments on commit 9973bab

Please sign in to comment.