diff --git a/src/parser/expression.js b/src/parser/expression.js index f336e896b7..6c6777f380 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -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(); diff --git a/test/fixtures/es2015/uncategorised/336/expected.json b/test/fixtures/es2015/uncategorised/336/expected.json deleted file mode 100644 index 9822daf8c2..0000000000 --- a/test/fixtures/es2015/uncategorised/336/expected.json +++ /dev/null @@ -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" - } - } - } - ] - } -} \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/336/options.json b/test/fixtures/es2015/uncategorised/336/options.json new file mode 100644 index 0000000000..abb78cbdb2 --- /dev/null +++ b/test/fixtures/es2015/uncategorised/336/options.json @@ -0,0 +1,3 @@ +{ + "throws": "new.target can only be used in functions (1:4)" +} diff --git a/test/fixtures/es2015/uncategorised/394/actual.js b/test/fixtures/es2015/uncategorised/394/actual.js new file mode 100644 index 0000000000..a83d19f0f8 --- /dev/null +++ b/test/fixtures/es2015/uncategorised/394/actual.js @@ -0,0 +1 @@ +function foo() { new.target } diff --git a/test/fixtures/es2015/uncategorised/394/expected.json b/test/fixtures/es2015/uncategorised/394/expected.json new file mode 100644 index 0000000000..30b0e8ac73 --- /dev/null +++ b/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": [] + } +} \ No newline at end of file