Skip to content

Commit ca652bd

Browse files
existentialismdanez
authored andcommitted
Throw error if new.target is used outside of a function (babel#402)
1 parent 35e7732 commit ca652bd

File tree

5 files changed

+163
-96
lines changed

5 files changed

+163
-96
lines changed

src/parser/expression.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,13 @@ pp.parseNew = function () {
662662
const meta = this.parseIdentifier(true);
663663

664664
if (this.eat(tt.dot)) {
665-
return this.parseMetaProperty(node, meta, "target");
665+
const metaProp = this.parseMetaProperty(node, meta, "target");
666+
667+
if (!this.state.inFunction) {
668+
this.raise(metaProp.property.start, "new.target can only be used in functions");
669+
}
670+
671+
return metaProp;
666672
}
667673

668674
node.callee = this.parseNoCallExpr();

test/fixtures/es2015/uncategorised/336/expected.json

-95
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "new.target can only be used in functions (1:4)"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function foo() { new.target }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 29,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 29
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 29,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 1,
26+
"column": 29
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "FunctionDeclaration",
33+
"start": 0,
34+
"end": 29,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 29
43+
}
44+
},
45+
"id": {
46+
"type": "Identifier",
47+
"start": 9,
48+
"end": 12,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 9
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 12
57+
},
58+
"identifierName": "foo"
59+
},
60+
"name": "foo"
61+
},
62+
"generator": false,
63+
"expression": false,
64+
"async": false,
65+
"params": [],
66+
"body": {
67+
"type": "BlockStatement",
68+
"start": 15,
69+
"end": 29,
70+
"loc": {
71+
"start": {
72+
"line": 1,
73+
"column": 15
74+
},
75+
"end": {
76+
"line": 1,
77+
"column": 29
78+
}
79+
},
80+
"body": [
81+
{
82+
"type": "ExpressionStatement",
83+
"start": 17,
84+
"end": 27,
85+
"loc": {
86+
"start": {
87+
"line": 1,
88+
"column": 17
89+
},
90+
"end": {
91+
"line": 1,
92+
"column": 27
93+
}
94+
},
95+
"expression": {
96+
"type": "MetaProperty",
97+
"start": 17,
98+
"end": 27,
99+
"loc": {
100+
"start": {
101+
"line": 1,
102+
"column": 17
103+
},
104+
"end": {
105+
"line": 1,
106+
"column": 27
107+
}
108+
},
109+
"meta": {
110+
"type": "Identifier",
111+
"start": 17,
112+
"end": 20,
113+
"loc": {
114+
"start": {
115+
"line": 1,
116+
"column": 17
117+
},
118+
"end": {
119+
"line": 1,
120+
"column": 20
121+
},
122+
"identifierName": "new"
123+
},
124+
"name": "new"
125+
},
126+
"property": {
127+
"type": "Identifier",
128+
"start": 21,
129+
"end": 27,
130+
"loc": {
131+
"start": {
132+
"line": 1,
133+
"column": 21
134+
},
135+
"end": {
136+
"line": 1,
137+
"column": 27
138+
},
139+
"identifierName": "target"
140+
},
141+
"name": "target"
142+
}
143+
}
144+
}
145+
],
146+
"directives": []
147+
}
148+
}
149+
],
150+
"directives": []
151+
}
152+
}

0 commit comments

Comments
 (0)