Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fix: Convert MetaProperty (new.target) nodes correcly (fixes #194) #195

Merged
merged 1 commit into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,23 +1819,27 @@ module.exports = function(ast, extra) {
break;

case SyntaxKind.NewExpression:

// new.target
if (node.expression.kind === SyntaxKind.PropertyAccessExpression) {
assign(result, {
type: "MetaProperty",
meta: convertChild(node.expression.name),
property: convertChild(node.expression.propertyName)
});
} else {
assign(result, {
type: "NewExpression",
callee: convertChild(node.expression),
arguments: (node.arguments) ? node.arguments.map(convertChild) : []
});
}
assign(result, {
type: "NewExpression",
callee: convertChild(node.expression),
arguments: (node.arguments) ? node.arguments.map(convertChild) : []
});
break;

case SyntaxKind.MetaProperty:
var newToken = convertToken(node.getFirstToken(), ast);

assign(result, {
type: "MetaProperty",
meta: {
type: "Identifier",
range: newToken.range,
loc: newToken.loc,
name: "new"
},
property: convertChild(node.name)
});
break;


// Literals
Expand Down
237 changes: 237 additions & 0 deletions tests/fixtures/basics/new-with-member-expression.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
module.exports = {
"type": "Program",
"range": [
0,
14
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"body": [
{
"type": "ExpressionStatement",
"range": [
0,
14
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"expression": {
"type": "NewExpression",
"range": [
0,
13
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"callee": {
"type": "MemberExpression",
"range": [
4,
11
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 11
}
},
"object": {
"type": "Identifier",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
},
"name": "foo"
},
"property": {
"type": "Identifier",
"range": [
8,
11
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 11
}
},
"name": "bar"
},
"computed": false
},
"arguments": []
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "Keyword",
"value": "new",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
{
"type": "Identifier",
"value": "foo",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Punctuator",
"value": ".",
"range": [
7,
8
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
}
},
{
"type": "Identifier",
"value": "bar",
"range": [
8,
11
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": "Punctuator",
"value": "(",
"range": [
11,
12
],
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
}
}
},
{
"type": "Punctuator",
"value": ")",
"range": [
12,
13
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
13,
14
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
}
}
]
};
1 change: 1 addition & 0 deletions tests/fixtures/basics/new-with-member-expression.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new foo.bar();
2 changes: 1 addition & 1 deletion tests/lib/ecma-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var testFiles = shelljs.find(FIXTURES_DIR).filter(function(filename) {
}).map(function(filename) {
return filename.substring(FIXTURES_DIR.length - 1, filename.length - 7); // strip off ".src.js"
}).filter(function(filename) {
return !(/error\-|invalid\-|globalReturn|experimental|newTarget/.test(filename));
return !(/error\-|invalid\-|globalReturn|experimental/.test(filename));
});

// var moduleTestFiles = testFiles.filter(function(filename) {
Expand Down