Skip to content

Commit

Permalink
Throw error if new.target is used outside of a function (babel#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism authored and danez committed Mar 10, 2017
1 parent 35e7732 commit ca652bd
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 96 deletions.
8 changes: 7 additions & 1 deletion src/parser/expression.js
Expand Up @@ -662,7 +662,13 @@ pp.parseNew = function () {
const meta = this.parseIdentifier(true);

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();
Expand Down
95 changes: 0 additions & 95 deletions test/fixtures/es2015/uncategorised/336/expected.json

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/es2015/uncategorised/336/options.json
@@ -0,0 +1,3 @@
{
"throws": "new.target can only be used in functions (1:4)"
}
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/394/actual.js
@@ -0,0 +1 @@
function foo() { new.target }
152 changes: 152 additions & 0 deletions test/fixtures/es2015/uncategorised/394/expected.json
@@ -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": []
}
}

0 comments on commit ca652bd

Please sign in to comment.