Skip to content

Commit

Permalink
ES6: meta property new.target.
Browse files Browse the repository at this point in the history
Closes jquery#1203.
  • Loading branch information
ariya committed Jun 30, 2015
1 parent 819cada commit f313a4b
Show file tree
Hide file tree
Showing 18 changed files with 718 additions and 0 deletions.
21 changes: 21 additions & 0 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
LabeledStatement: 'LabeledStatement',
LogicalExpression: 'LogicalExpression',
MemberExpression: 'MemberExpression',
MetaProperty: 'MetaProperty',
MethodDefinition: 'MethodDefinition',
NewExpression: 'NewExpression',
ObjectExpression: 'ObjectExpression',
Expand Down Expand Up @@ -2045,6 +2046,14 @@
return this;
},

finishMetaProperty: function (meta, property) {
this.type = Syntax.MetaProperty;
this.meta = meta;
this.property = property;
this.finish();
return this;
},

finishNewExpression: function (callee, args) {
this.type = Syntax.NewExpression;
this.callee = callee;
Expand Down Expand Up @@ -3267,6 +3276,18 @@
var callee, args, node = new Node();

expectKeyword('new');

if (match('.')) {
lex();
if (lookahead.type === Token.Identifier && lookahead.value === 'target') {
if (state.inFunctionBody) {
lex();
return node.finishMetaProperty('new', 'target');
}
}
throwUnexpectedToken(lookahead);
}

callee = isolateCoverGrammar(parseLeftHandSideExpression);
args = match('(') ? parseArguments() : [];

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/ES6/meta-property/assign-new-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
let x = new.target;
}
151 changes: 151 additions & 0 deletions test/fixtures/ES6/meta-property/assign-new-target.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"range": [
0,
40
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"type": "Program",
"body": [
{
"range": [
0,
40
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"type": "FunctionDeclaration",
"id": {
"range": [
9,
10
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"type": "Identifier",
"name": "f"
},
"params": [],
"defaults": [],
"body": {
"range": [
13,
40
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 3,
"column": 1
}
},
"type": "BlockStatement",
"body": [
{
"range": [
19,
38
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 23
}
},
"type": "VariableDeclaration",
"declarations": [
{
"range": [
23,
37
],
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 22
}
},
"type": "VariableDeclarator",
"id": {
"range": [
23,
24
],
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
}
},
"type": "Identifier",
"name": "x"
},
"init": {
"range": [
27,
37
],
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 22
}
},
"type": "MetaProperty",
"meta": "new",
"property": "target"
}
}
],
"kind": "let"
}
]
},
"generator": false,
"expression": false
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/ES6/meta-property/invalid-dots.failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"index":29,"lineNumber":1,"column":30,"message":"Error: Line 1: Unexpected token .","description":"Unexpected token ."}
1 change: 1 addition & 0 deletions test/fixtures/ES6/meta-property/invalid-dots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = function() { y = new..target; }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"index":12,"lineNumber":1,"column":13,"message":"Error: Line 1: Unexpected identifier","description":"Unexpected identifier"}
1 change: 1 addition & 0 deletions test/fixtures/ES6/meta-property/invalid-new-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = new.target;
3 changes: 3 additions & 0 deletions test/fixtures/ES6/meta-property/new-new-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
new new.target;
}
131 changes: 131 additions & 0 deletions test/fixtures/ES6/meta-property/new-new-target.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"range": [
0,
36
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"type": "Program",
"body": [
{
"range": [
0,
36
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"type": "FunctionDeclaration",
"id": {
"range": [
9,
10
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"type": "Identifier",
"name": "f"
},
"params": [],
"defaults": [],
"body": {
"range": [
13,
36
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 3,
"column": 1
}
},
"type": "BlockStatement",
"body": [
{
"range": [
19,
34
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 19
}
},
"type": "ExpressionStatement",
"expression": {
"range": [
19,
33
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 18
}
},
"type": "NewExpression",
"callee": {
"range": [
23,
33
],
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 18
}
},
"type": "MetaProperty",
"meta": "new",
"property": "target"
},
"arguments": []
}
}
]
},
"generator": false,
"expression": false
}
]
}
3 changes: 3 additions & 0 deletions test/fixtures/ES6/meta-property/new-target-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
new.target;
}
Loading

0 comments on commit f313a4b

Please sign in to comment.