Throw error if new.target is used outside of a function (#402)

This commit is contained in:
Brian Ng 2017-03-08 09:34:22 -06:00 committed by Daniel Tschinder
parent 35e7732156
commit ca652bd934
5 changed files with 163 additions and 96 deletions

View File

@ -662,7 +662,13 @@ pp.parseNew = function () {
const meta = this.parseIdentifier(true); const meta = this.parseIdentifier(true);
if (this.eat(tt.dot)) { if (this.eat(tt.dot)) {
return this.parseMetaProperty(node, meta, "target"); const metaProp = this.parseMetaProperty(node, meta, "target");
if (!this.state.inFunction) {
this.raise(metaProp.property.start, "new.target can only be used in functions");
}
return metaProp;
} }
node.callee = this.parseNoCallExpr(); node.callee = this.parseNoCallExpr();

View File

@ -1,95 +0,0 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "MetaProperty",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"meta": {
"type": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"name": "new"
},
"property": {
"type": "Identifier",
"start": 4,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 10
}
},
"name": "target"
}
}
}
]
}
}

View File

@ -0,0 +1,3 @@
{
"throws": "new.target can only be used in functions (1:4)"
}

View File

@ -0,0 +1 @@
function foo() { new.target }

View File

@ -0,0 +1,152 @@
{
"type": "File",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"program": {
"type": "Program",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"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": [],
"body": {
"type": "BlockStatement",
"start": 15,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 29
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 17,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 27
}
},
"expression": {
"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"
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}