Parse parameters inside function's env (#652)

* Parse parameters inside function context

* Add test for new.target inside parameters
This commit is contained in:
Nicolò Ribaudo 2017-10-31 16:31:35 +01:00 committed by Daniel Tschinder
parent fee7de2c1d
commit cd050e1405
4 changed files with 369 additions and 4 deletions

View File

@ -1461,7 +1461,9 @@ export default class ExpressionParser extends LValParser {
isConstructor: boolean,
type: string,
): T {
const oldInFunc = this.state.inFunction;
const oldInMethod = this.state.inMethod;
this.state.inFunction = true;
this.state.inMethod = node.kind || true;
this.initFunction(node, isAsync);
this.expect(tt.parenL);
@ -1473,6 +1475,7 @@ export default class ExpressionParser extends LValParser {
);
node.generator = !!isGenerator;
this.parseFunctionBodyAndFinish(node, type);
this.state.inFunction = oldInFunc;
this.state.inMethod = oldInMethod;
return node;
}
@ -1484,9 +1487,12 @@ export default class ExpressionParser extends LValParser {
params: N.Expression[],
isAsync?: boolean,
): N.ArrowFunctionExpression {
const oldInFunc = this.state.inFunction;
this.state.inFunction = true;
this.initFunction(node, isAsync);
this.setArrowFunctionParameters(node, params);
this.parseFunctionBody(node, true);
this.state.inFunction = oldInFunc;
return this.finishNode(node, "ArrowFunctionExpression");
}
@ -1536,17 +1542,14 @@ export default class ExpressionParser extends LValParser {
node.body = this.parseMaybeAssign();
node.expression = true;
} else {
// Start a new scope with regard to labels and the `inFunction`
// Start a new scope with regard to labels and the `inGenerator`
// flag (restore them to their old value afterwards).
const oldInFunc = this.state.inFunction;
const oldInGen = this.state.inGenerator;
const oldLabels = this.state.labels;
this.state.inFunction = true;
this.state.inGenerator = node.generator;
this.state.labels = [];
node.body = this.parseBlock(true);
node.expression = false;
this.state.inFunction = oldInFunc;
this.state.inGenerator = oldInGen;
this.state.labels = oldLabels;
}

View File

@ -779,7 +779,9 @@ export default class StatementParser extends ExpressionParser {
isAsync?: boolean,
optionalId?: boolean,
): T {
const oldInFunc = this.state.inFunction;
const oldInMethod = this.state.inMethod;
this.state.inFunction = true;
this.state.inMethod = false;
this.initFunction(node, isAsync);
@ -811,6 +813,7 @@ export default class StatementParser extends ExpressionParser {
isStatement ? "FunctionDeclaration" : "FunctionExpression",
allowExpressionBody,
);
this.state.inFunction = oldInFunc;
this.state.inMethod = oldInMethod;
return node;
}

View File

@ -0,0 +1,2 @@
function Foo(x = new.target) {}
function Bar() { (x = new.target) => {} }

View File

@ -0,0 +1,357 @@
{
"type": "File",
"start": 0,
"end": 73,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 41
}
},
"program": {
"type": "Program",
"start": 0,
"end": 73,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 41
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 31
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
},
"identifierName": "Foo"
},
"name": "Foo"
},
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "AssignmentPattern",
"start": 13,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 27
}
},
"left": {
"type": "Identifier",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "x"
},
"name": "x"
},
"right": {
"type": "MetaProperty",
"start": 17,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 27
}
},
"meta": {
"type": "Identifier",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "new"
},
"name": "new"
},
"property": {
"type": "Identifier",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
},
"identifierName": "target"
},
"name": "target"
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 29,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 29
},
"end": {
"line": 1,
"column": 31
}
},
"body": [],
"directives": []
}
},
{
"type": "FunctionDeclaration",
"start": 32,
"end": 73,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 41
}
},
"id": {
"type": "Identifier",
"start": 41,
"end": 44,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "Bar"
},
"name": "Bar"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 47,
"end": 73,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 41
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 49,
"end": 71,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 39
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 49,
"end": 71,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 39
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "AssignmentPattern",
"start": 50,
"end": 64,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 32
}
},
"left": {
"type": "Identifier",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 19
},
"identifierName": "x"
},
"name": "x"
},
"right": {
"type": "MetaProperty",
"start": 54,
"end": 64,
"loc": {
"start": {
"line": 2,
"column": 22
},
"end": {
"line": 2,
"column": 32
}
},
"meta": {
"type": "Identifier",
"start": 54,
"end": 57,
"loc": {
"start": {
"line": 2,
"column": 22
},
"end": {
"line": 2,
"column": 25
},
"identifierName": "new"
},
"name": "new"
},
"property": {
"type": "Identifier",
"start": 58,
"end": 64,
"loc": {
"start": {
"line": 2,
"column": 26
},
"end": {
"line": 2,
"column": 32
},
"identifierName": "target"
},
"name": "target"
}
}
}
],
"body": {
"type": "BlockStatement",
"start": 69,
"end": 71,
"loc": {
"start": {
"line": 2,
"column": 37
},
"end": {
"line": 2,
"column": 39
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}