diff --git a/lib/ast-converter.js b/lib/ast-converter.js index ccddc35..03c1a73 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1419,10 +1419,11 @@ module.exports = function(ast, extra) { case SyntaxKind.PrefixUnaryExpression: case SyntaxKind.PostfixUnaryExpression: + var operator = TOKEN_TO_TEXT[node.operator]; assign(result, { // ESTree uses UpdateExpression for ++/-- - type: /^(?:\+\+|\-\-)$/.test(TOKEN_TO_TEXT[node.operator.kind]) ? "UpdateExpression" : "UnaryExpression", - operator: SyntaxKind[node.operator], + type: /^(?:\+\+|\-\-)$/.test(operator) ? "UpdateExpression" : "UnaryExpression", + operator: operator, prefix: node.kind === SyntaxKind.PrefixUnaryExpression, argument: convertChild(node.operand) }); diff --git a/tests/fixtures/basics/update-expression.result.js b/tests/fixtures/basics/update-expression.result.js new file mode 100644 index 0000000..a3f82e5 --- /dev/null +++ b/tests/fixtures/basics/update-expression.result.js @@ -0,0 +1,584 @@ +module.exports = { + "body": [ + { + "declarations": [ + { + "id": { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "name": "i", + "range": [ + 4, + 5 + ], + "type": "Identifier" + }, + "init": { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 9 + ], + "raw": "0", + "type": "Literal", + "value": 0 + }, + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "range": [ + 4, + 9 + ], + "type": "VariableDeclarator" + } + ], + "kind": "var", + "loc": { + "end": { + "column": 10, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 10 + ], + "type": "VariableDeclaration" + }, + { + "body": { + "body": [ + { + "expression": { + "argument": { + "loc": { + "end": { + "column": 3, + "line": 3 + }, + "start": { + "column": 2, + "line": 3 + } + }, + "name": "i", + "range": [ + 28, + 29 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 5, + "line": 3 + }, + "start": { + "column": 2, + "line": 3 + } + }, + "operator": "++", + "prefix": false, + "range": [ + 28, + 31 + ], + "type": "UpdateExpression" + }, + "loc": { + "end": { + "column": 6, + "line": 3 + }, + "start": { + "column": 2, + "line": 3 + } + }, + "range": [ + 28, + 32 + ], + "type": "ExpressionStatement" + } + ], + "loc": { + "end": { + "column": 1, + "line": 4 + }, + "start": { + "column": 13, + "line": 2 + } + }, + "range": [ + 24, + 34 + ], + "type": "BlockStatement" + }, + "expression": false, + "generator": false, + "id": { + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "name": "f", + "range": [ + 20, + 21 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 1, + "line": 4 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "params": [], + "range": [ + 11, + 34 + ], + "type": "FunctionDeclaration" + }, + { + "expression": { + "arguments": [], + "callee": { + "loc": { + "end": { + "column": 1, + "line": 5 + }, + "start": { + "column": 0, + "line": 5 + } + }, + "name": "f", + "range": [ + 35, + 36 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 3, + "line": 5 + }, + "start": { + "column": 0, + "line": 5 + } + }, + "range": [ + 35, + 38 + ], + "type": "CallExpression" + }, + "loc": { + "end": { + "column": 4, + "line": 5 + }, + "start": { + "column": 0, + "line": 5 + } + }, + "range": [ + 35, + 39 + ], + "type": "ExpressionStatement" + } + ], + "loc": { + "end": { + "column": 4, + "line": 5 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 39 + ], + "sourceType": "script", + "tokens": [ + { + "loc": { + "end": { + "column": 3, + "line": 1 + }, + "start": { + "column": 0, + "line": 1 + } + }, + "range": [ + 0, + 3 + ], + "type": "Keyword", + "value": "var" + }, + { + "loc": { + "end": { + "column": 5, + "line": 1 + }, + "start": { + "column": 4, + "line": 1 + } + }, + "range": [ + 4, + 5 + ], + "type": "Identifier", + "value": "i" + }, + { + "loc": { + "end": { + "column": 7, + "line": 1 + }, + "start": { + "column": 6, + "line": 1 + } + }, + "range": [ + 6, + 7 + ], + "type": "Punctuator", + "value": "=" + }, + { + "loc": { + "end": { + "column": 9, + "line": 1 + }, + "start": { + "column": 8, + "line": 1 + } + }, + "range": [ + 8, + 9 + ], + "type": "Numeric", + "value": "0" + }, + { + "loc": { + "end": { + "column": 10, + "line": 1 + }, + "start": { + "column": 9, + "line": 1 + } + }, + "range": [ + 9, + 10 + ], + "type": "Punctuator", + "value": ";" + }, + { + "loc": { + "end": { + "column": 8, + "line": 2 + }, + "start": { + "column": 0, + "line": 2 + } + }, + "range": [ + 11, + 19 + ], + "type": "Keyword", + "value": "function" + }, + { + "loc": { + "end": { + "column": 10, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "range": [ + 20, + 21 + ], + "type": "Identifier", + "value": "f" + }, + { + "loc": { + "end": { + "column": 11, + "line": 2 + }, + "start": { + "column": 10, + "line": 2 + } + }, + "range": [ + 21, + 22 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 12, + "line": 2 + }, + "start": { + "column": 11, + "line": 2 + } + }, + "range": [ + 22, + 23 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 14, + "line": 2 + }, + "start": { + "column": 13, + "line": 2 + } + }, + "range": [ + 24, + 25 + ], + "type": "Punctuator", + "value": "{" + }, + { + "loc": { + "end": { + "column": 3, + "line": 3 + }, + "start": { + "column": 2, + "line": 3 + } + }, + "range": [ + 28, + 29 + ], + "type": "Identifier", + "value": "i" + }, + { + "loc": { + "end": { + "column": 5, + "line": 3 + }, + "start": { + "column": 3, + "line": 3 + } + }, + "range": [ + 29, + 31 + ], + "type": "Punctuator", + "value": "++" + }, + { + "loc": { + "end": { + "column": 6, + "line": 3 + }, + "start": { + "column": 5, + "line": 3 + } + }, + "range": [ + 31, + 32 + ], + "type": "Punctuator", + "value": ";" + }, + { + "loc": { + "end": { + "column": 1, + "line": 4 + }, + "start": { + "column": 0, + "line": 4 + } + }, + "range": [ + 33, + 34 + ], + "type": "Punctuator", + "value": "}" + }, + { + "loc": { + "end": { + "column": 1, + "line": 5 + }, + "start": { + "column": 0, + "line": 5 + } + }, + "range": [ + 35, + 36 + ], + "type": "Identifier", + "value": "f" + }, + { + "loc": { + "end": { + "column": 2, + "line": 5 + }, + "start": { + "column": 1, + "line": 5 + } + }, + "range": [ + 36, + 37 + ], + "type": "Punctuator", + "value": "(" + }, + { + "loc": { + "end": { + "column": 3, + "line": 5 + }, + "start": { + "column": 2, + "line": 5 + } + }, + "range": [ + 37, + 38 + ], + "type": "Punctuator", + "value": ")" + }, + { + "loc": { + "end": { + "column": 4, + "line": 5 + }, + "start": { + "column": 3, + "line": 5 + } + }, + "range": [ + 38, + 39 + ], + "type": "Punctuator", + "value": ";" + } + ], + "type": "Program" +}; diff --git a/tests/fixtures/basics/update-expression.src.js b/tests/fixtures/basics/update-expression.src.js new file mode 100644 index 0000000..1c94b62 --- /dev/null +++ b/tests/fixtures/basics/update-expression.src.js @@ -0,0 +1,5 @@ +var i = 0; +function f() { + i++; +} +f(); \ No newline at end of file diff --git a/tests/lib/basics.js b/tests/lib/basics.js new file mode 100644 index 0000000..0a41ad7 --- /dev/null +++ b/tests/lib/basics.js @@ -0,0 +1,88 @@ +/** + * @fileoverview Tests for basic expressions + * @author Nicholas C. Zakas + * @copyright jQuery Foundation and other contributors, https://jquery.org/ + * MIT License + */ + +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +var assert = require("chai").assert, + leche = require("leche"), + path = require("path"), + parser = require("../../parser"), + shelljs = require("shelljs"); + +//------------------------------------------------------------------------------ +// Setup +//------------------------------------------------------------------------------ + +var FIXTURES_DIR = "./tests/fixtures/basics"; + +var testFiles = shelljs.find(FIXTURES_DIR).filter(function(filename) { + return filename.indexOf(".src.js") > -1; +}).map(function(filename) { + return filename.substring(FIXTURES_DIR.length - 1, filename.length - 7); // strip off ".src.js" +}); + +/** + * Returns a raw copy of the given AST + * @param {object} ast the AST object + * @returns {object} copy of the AST object + */ +function getRaw(ast) { + return JSON.parse(JSON.stringify(ast)); +} + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +describe("basics", function() { + + var config; + + beforeEach(function() { + config = { + loc: true, + range: true, + tokens: true, + ecmaFeatures: {} + }; + }); + + leche.withData(testFiles, function(filename) { + // Uncomment and fill in filename to focus on a single file + // var filename = "jsx/invalid-matching-placeholder-in-closing-tag"; + var code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.js"); + + it("should parse correctly", function() { + var expected = require(path.resolve(__dirname, "../../", FIXTURES_DIR, filename) + ".result.js"); + var result; + + try { + result = parser.parse(code, config); + result = getRaw(result); + } catch (ex) { + + // format of error isn't exactly the same, just check if it's expected + if (expected.message) { + return; + } else { + throw ex; + } + + } + // console.log(JSON.stringify(result, null, 4)); + assert.deepEqual(result, expected); + + }); + + }); + + +});