From 77289715176011cd70009ad452faf33599e51007 Mon Sep 17 00:00:00 2001 From: James Henry Date: Mon, 22 Aug 2016 19:41:01 +0100 Subject: [PATCH] Fix: Check for arguments property on NewExpression (fixes #60) --- lib/ast-converter.js | 2 +- .../basics/new-without-parens.result.js | 293 ++++++++++++++++++ .../fixtures/basics/new-without-parens.src.js | 2 + 3 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/basics/new-without-parens.result.js create mode 100644 tests/fixtures/basics/new-without-parens.src.js diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 03c1a73..528b180 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1533,7 +1533,7 @@ module.exports = function(ast, extra) { assign(result, { type: "NewExpression", callee: convertChild(node.expression), - arguments: node.arguments.map(convertChild) + arguments: (node.arguments) ? node.arguments.map(convertChild) : [] }); } break; diff --git a/tests/fixtures/basics/new-without-parens.result.js b/tests/fixtures/basics/new-without-parens.result.js new file mode 100644 index 0000000..1d7c82d --- /dev/null +++ b/tests/fixtures/basics/new-without-parens.result.js @@ -0,0 +1,293 @@ +module.exports = { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "range": [ + 0, + 23 + ], + "body": [ + { + "type": "FunctionDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 0, + 16 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "range": [ + 9, + 10 + ], + "name": "X" + }, + "generator": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 14, + 16 + ], + "body": [] + } + }, + { + "type": "ExpressionStatement", + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "range": [ + 17, + 23 + ], + "expression": { + "type": "NewExpression", + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "range": [ + 17, + 22 + ], + "callee": { + "type": "Identifier", + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "range": [ + 21, + 22 + ], + "name": "X" + }, + "arguments": [] + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "function", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "range": [ + 0, + 8 + ] + }, + { + "type": "Identifier", + "value": "X", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "range": [ + 9, + 10 + ] + }, + { + "type": "Punctuator", + "value": "(", + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 11, + 12 + ] + }, + { + "type": "Punctuator", + "value": ")", + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "range": [ + 12, + 13 + ] + }, + { + "type": "Punctuator", + "value": "{", + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "range": [ + 14, + 15 + ] + }, + { + "type": "Punctuator", + "value": "}", + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "range": [ + 15, + 16 + ] + }, + { + "type": "Keyword", + "value": "new", + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "range": [ + 17, + 20 + ] + }, + { + "type": "Identifier", + "value": "X", + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "range": [ + 21, + 22 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "range": [ + 22, + 23 + ] + } + ] +}; diff --git a/tests/fixtures/basics/new-without-parens.src.js b/tests/fixtures/basics/new-without-parens.src.js new file mode 100644 index 0000000..fd30dda --- /dev/null +++ b/tests/fixtures/basics/new-without-parens.src.js @@ -0,0 +1,2 @@ +function X () {} +new X; \ No newline at end of file