diff --git a/Makefile.js b/Makefile.js index 50d6cfa..eca49ed 100644 --- a/Makefile.js +++ b/Makefile.js @@ -39,7 +39,8 @@ const NODE_MODULES = "./node_modules/", MAKEFILE = "./Makefile.js", /* eslint-disable no-use-before-define */ JS_FILES = `${find("lib/").filter(fileType("js")).join(" ")} parser.js`, - TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" "); + TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" "), + TOOLS_FILES = find("tools/").filter(fileType("js")).join(" "); /* eslint-enable no-use-before-define */ //------------------------------------------------------------------------------ @@ -88,6 +89,12 @@ target.lint = function() { errors++; } + echo("Validating JavaScript tools files"); + lastReturn = nodeCLI.exec("eslint", TOOLS_FILES); + if (lastReturn.code !== 0) { + errors++; + } + if (errors) { exit(1); } diff --git a/lib/ast-node-types.js b/lib/ast-node-types.js index 5c8ef69..10adc1c 100644 --- a/lib/ast-node-types.js +++ b/lib/ast-node-types.js @@ -38,6 +38,7 @@ module.exports = { ContinueStatement: "ContinueStatement", DebuggerStatement: "DebuggerStatement", DeclareFunction: "DeclareFunction", + Decorator: "Decorator", DoWhileStatement: "DoWhileStatement", EmptyStatement: "EmptyStatement", ExperimentalRestProperty: "ExperimentalRestProperty", @@ -106,6 +107,7 @@ module.exports = { TSConstructorType: "TSConstructorType", TSConstructSignature: "TSConstructSignature", TSDeclareKeyword: "TSDeclareKeyword", + TSEnumDeclaration: "TSEnumDeclaration", TSIndexSignature: "TSIndexSignature", TSInterfaceBody: "TSInterfaceBody", TSInterfaceDeclaration: "TSInterfaceDeclaration", diff --git a/lib/convert.js b/lib/convert.js index 093ef5c..cf71e7d 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -202,6 +202,26 @@ module.exports = function convert(config) { return classImplementsNode; } + /** + * Converts an array of TSNode decorators into an array of ESTreeNode decorators + * @param {TSNode[]} decorators An array of TSNode decorators to be converted + * @returns {ESTreeNode[]} an array of converted ESTreeNode decorators + */ + function convertDecorators(decorators) { + if (!decorators || !decorators.length) { + return []; + } + return decorators.map(decorator => { + const expression = convertChild(decorator.expression); + return { + type: AST_NODE_TYPES.Decorator, + range: [decorator.getStart(), decorator.end], + loc: nodeUtils.getLoc(decorator, ast), + expression + }; + }); + } + /** * For nodes that are copied directly from the TypeScript AST into * ESTree mostly as-is. The only difference is the addition of a type @@ -232,6 +252,11 @@ module.exports = function convert(config) { result.typeParameters = (node.typeParameters) ? convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters) : null; + } else if (key === "decorators") { + const decorators = convertDecorators(node.decorators); + if (decorators && decorators.length) { + result.decorators = decorators; + } } else { if (Array.isArray(node[key])) { result[key] = node[key].map(convertChild); @@ -276,18 +301,6 @@ module.exports = function convert(config) { return tagNameToken; } - /** - * Converts an array of TSNode decorators into an array of ESTreeNode decorators - * @param {TSNode[]} decorators An array of TSNode decorators to be converted - * @returns {ESTreeNode[]} an array of converted ESTreeNode decorators - */ - function convertDecorators(decorators) { - if (!decorators || !decorators.length) { - return []; - } - return decorators.map(decorator => convertChild(decorator.expression)); - } - /** * The core of the conervsion logic: * Identify and convert each relevant TypeScript SyntaxKind diff --git a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-instance-member.result.js b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-instance-member.result.js index 990e66d..6451437 100644 --- a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-instance-member.result.js @@ -203,26 +203,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 19, + 18, 38 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 24 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 19, - 31 + 38 ], "loc": { "start": { @@ -231,32 +231,49 @@ module.exports = { }, "end": { "line": 2, - "column": 17 + "column": 24 } }, - "name": "configurable" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 32, - 37 + 19, + 31 ], "loc": { "start": { "line": 2, - "column": 18 + "column": 5 }, "end": { "line": 2, - "column": 23 + "column": 17 } }, - "value": false, - "raw": "false" - } - ] + "name": "configurable" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 32, + 37 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "value": false, + "raw": "false" + } + ] + } } ] } @@ -644,4 +661,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-static-member.result.js b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-static-member.result.js index 6449044..f70c444 100644 --- a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-static-member.result.js +++ b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-factory-static-member.result.js @@ -203,26 +203,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 19, + 18, 37 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 23 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 19, - 22 + 37 ], "loc": { "start": { @@ -231,50 +231,50 @@ module.exports = { }, "end": { "line": 2, - "column": 8 + "column": 23 } }, - "name": "foo" - }, - "arguments": [ - { - "type": "ObjectExpression", + "callee": { + "type": "Identifier", "range": [ - 23, - 36 + 19, + 22 ], "loc": { "start": { "line": 2, - "column": 9 + "column": 5 }, "end": { "line": 2, - "column": 22 + "column": 8 } }, - "properties": [ - { - "type": "Property", - "range": [ - 25, - 34 - ], - "loc": { - "start": { - "line": 2, - "column": 11 - }, - "end": { - "line": 2, - "column": 20 - } + "name": "foo" + }, + "arguments": [ + { + "type": "ObjectExpression", + "range": [ + 23, + 36 + ], + "loc": { + "start": { + "line": 2, + "column": 9 }, - "key": { - "type": "Identifier", + "end": { + "line": 2, + "column": 22 + } + }, + "properties": [ + { + "type": "Property", "range": [ 25, - 28 + 34 ], "loc": { "start": { @@ -283,38 +283,55 @@ module.exports = { }, "end": { "line": 2, - "column": 14 + "column": 20 } }, - "name": "baz" - }, - "value": { - "type": "Literal", - "range": [ - 30, - 34 - ], - "loc": { - "start": { - "line": 2, - "column": 16 + "key": { + "type": "Identifier", + "range": [ + 25, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 14 + } }, - "end": { - "line": 2, - "column": 20 - } + "name": "baz" }, - "value": true, - "raw": "true" - }, - "computed": false, - "method": false, - "shorthand": false, - "kind": "init" - } - ] - } - ] + "value": { + "type": "Literal", + "range": [ + 30, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "value": true, + "raw": "true" + }, + "computed": false, + "method": false, + "shorthand": false, + "kind": "init" + } + ] + } + ] + } } ] } diff --git a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-instance-member.result.js b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-instance-member.result.js index 8f15e94..2ef1aef 100644 --- a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-instance-member.result.js @@ -203,22 +203,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 15, + 14, 21 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 11 } }, - "name": "hidden" + "expression": { + "type": "Identifier", + "range": [ + 15, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "name": "hidden" + } } ] } @@ -552,4 +569,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-static-member.result.js b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-static-member.result.js index 301e980..3dc323f 100644 --- a/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-static-member.result.js +++ b/tests/fixtures/typescript/decorators/accessor-decorators/accessor-decorator-static-member.result.js @@ -259,22 +259,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 18, + 17, 27 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 14 } }, - "name": "adminonly" + "expression": { + "type": "Identifier", + "range": [ + 18, + 27 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "name": "adminonly" + } } ] } diff --git a/tests/fixtures/typescript/decorators/class-decorators/class-decorator-factory.result.js b/tests/fixtures/typescript/decorators/class-decorators/class-decorator-factory.result.js index af4fb36..d1c7b20 100644 --- a/tests/fixtures/typescript/decorators/class-decorators/class-decorator-factory.result.js +++ b/tests/fixtures/typescript/decorators/class-decorators/class-decorator-factory.result.js @@ -71,26 +71,26 @@ module.exports = { "implements": [], "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 1, + 0, 36 ], "loc": { "start": { "line": 1, - "column": 1 + "column": 0 }, "end": { "line": 3, "column": 2 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 1, - 10 + 36 ], "loc": { "start": { @@ -98,51 +98,51 @@ module.exports = { "column": 1 }, "end": { - "line": 1, - "column": 10 + "line": 3, + "column": 2 } }, - "name": "Component" - }, - "arguments": [ - { - "type": "ObjectExpression", + "callee": { + "type": "Identifier", "range": [ - 11, - 35 + 1, + 10 ], "loc": { "start": { "line": 1, - "column": 11 + "column": 1 }, "end": { - "line": 3, - "column": 1 + "line": 1, + "column": 10 } }, - "properties": [ - { - "type": "Property", - "range": [ - 17, - 32 - ], - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 19 - } + "name": "Component" + }, + "arguments": [ + { + "type": "ObjectExpression", + "range": [ + 11, + 35 + ], + "loc": { + "start": { + "line": 1, + "column": 11 }, - "key": { - "type": "Identifier", + "end": { + "line": 3, + "column": 1 + } + }, + "properties": [ + { + "type": "Property", "range": [ 17, - 25 + 32 ], "loc": { "start": { @@ -151,38 +151,55 @@ module.exports = { }, "end": { "line": 2, - "column": 12 + "column": 19 } }, - "name": "selector" - }, - "value": { - "type": "Literal", - "range": [ - 27, - 32 - ], - "loc": { - "start": { - "line": 2, - "column": 14 + "key": { + "type": "Identifier", + "range": [ + 17, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + } }, - "end": { - "line": 2, - "column": 19 - } + "name": "selector" }, - "value": "foo", - "raw": "'foo'" - }, - "computed": false, - "method": false, - "shorthand": false, - "kind": "init" - } - ] - } - ] + "value": { + "type": "Literal", + "range": [ + 27, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "value": "foo", + "raw": "'foo'" + }, + "computed": false, + "method": false, + "shorthand": false, + "kind": "init" + } + ] + } + ] + } } ] } diff --git a/tests/fixtures/typescript/decorators/class-decorators/class-decorator.result.js b/tests/fixtures/typescript/decorators/class-decorators/class-decorator.result.js index c05b767..ae8c6fd 100644 --- a/tests/fixtures/typescript/decorators/class-decorators/class-decorator.result.js +++ b/tests/fixtures/typescript/decorators/class-decorators/class-decorator.result.js @@ -71,22 +71,39 @@ module.exports = { "implements": [], "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 1, + 0, 7 ], "loc": { "start": { "line": 1, - "column": 1 + "column": 0 }, "end": { "line": 1, "column": 7 } }, - "name": "sealed" + "expression": { + "type": "Identifier", + "range": [ + 1, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "sealed" + } } ] } diff --git a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-instance-member.result.js b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-instance-member.result.js index 6bf94df..4edc70c 100644 --- a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-instance-member.result.js @@ -132,26 +132,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 15, + 14, 30 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 20 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 15, - 23 + 30 ], "loc": { "start": { @@ -160,32 +160,49 @@ module.exports = { }, "end": { "line": 2, - "column": 13 + "column": 20 } }, - "name": "onlyRead" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 24, - 29 + 15, + 23 ], "loc": { "start": { "line": 2, - "column": 14 + "column": 5 }, "end": { "line": 2, - "column": 19 + "column": 13 } }, - "value": false, - "raw": "false" - } - ] + "name": "onlyRead" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 24, + 29 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "value": false, + "raw": "false" + } + ] + } } ] } @@ -465,4 +482,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-static-member.result.js b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-static-member.result.js index 00df8ea..12b5e69 100644 --- a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-static-member.result.js +++ b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-factory-static-member.result.js @@ -132,26 +132,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 15, + 14, 25 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 15 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 15, - 18 + 25 ], "loc": { "start": { @@ -160,32 +160,49 @@ module.exports = { }, "end": { "line": 2, - "column": 8 + "column": 15 } }, - "name": "Foo" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 19, - 24 + 15, + 18 ], "loc": { "start": { "line": 2, - "column": 9 + "column": 5 }, "end": { "line": 2, - "column": 14 + "column": 8 } }, - "value": false, - "raw": "false" - } - ] + "name": "Foo" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 19, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "value": false, + "raw": "false" + } + ] + } } ] } diff --git a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-instance-member.result.js b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-instance-member.result.js index 1e5ad09..ae988b2 100644 --- a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-instance-member.result.js @@ -132,22 +132,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 15, + 14, 23 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 13 } }, - "name": "onlyRead" + "expression": { + "type": "Identifier", + "range": [ + 15, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "name": "onlyRead" + } } ] } @@ -373,4 +390,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-static-member.result.js b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-static-member.result.js index 9e52d52..1910238 100644 --- a/tests/fixtures/typescript/decorators/method-decorators/method-decorator-static-member.result.js +++ b/tests/fixtures/typescript/decorators/method-decorators/method-decorator-static-member.result.js @@ -132,22 +132,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 15, + 14, 18 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 8 } }, - "name": "Foo" + "expression": { + "type": "Identifier", + "range": [ + 15, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "name": "Foo" + } } ] } diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js index 768e362..b3d7dbc 100644 --- a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js @@ -161,26 +161,26 @@ module.exports = { }, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 33, + 32, 51 ], "loc": { "start": { "line": 2, - "column": 17 + "column": 16 }, "end": { "line": 2, "column": 35 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 33, - 39 + 51 ], "loc": { "start": { @@ -189,31 +189,48 @@ module.exports = { }, "end": { "line": 2, - "column": 23 + "column": 35 } }, - "name": "Inject" - }, - "arguments": [ - { + "callee": { "type": "Identifier", "range": [ - 40, - 50 + 33, + 39 ], "loc": { "start": { "line": 2, - "column": 24 + "column": 17 }, "end": { "line": 2, - "column": 34 + "column": 23 } }, - "name": "APP_CONFIG" - } - ] + "name": "Inject" + }, + "arguments": [ + { + "type": "Identifier", + "range": [ + 40, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "name": "APP_CONFIG" + } + ] + } } ] } diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js index 7f7a515..63803ab 100644 --- a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js @@ -178,26 +178,26 @@ module.exports = { }, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 21, + 20, 34 ], "loc": { "start": { "line": 2, - "column": 9 + "column": 8 }, "end": { "line": 2, "column": 22 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 21, - 28 + 34 ], "loc": { "start": { @@ -206,32 +206,49 @@ module.exports = { }, "end": { "line": 2, - "column": 16 + "column": 22 } }, - "name": "special" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 29, - 33 + 21, + 28 ], "loc": { "start": { "line": 2, - "column": 17 + "column": 9 }, "end": { "line": 2, - "column": 21 + "column": 16 } }, - "value": true, - "raw": "true" - } - ] + "name": "special" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 29, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "value": true, + "raw": "true" + } + ] + } } ] } @@ -573,4 +590,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js index 6d44940..dcb0c6d 100644 --- a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js @@ -178,26 +178,26 @@ module.exports = { }, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 34, + 33, 47 ], "loc": { "start": { "line": 2, - "column": 16 + "column": 15 }, "end": { "line": 2, "column": 29 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 34, - 41 + 47 ], "loc": { "start": { @@ -206,32 +206,49 @@ module.exports = { }, "end": { "line": 2, - "column": 23 + "column": 29 } }, - "name": "special" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 42, - 46 + 34, + 41 ], "loc": { "start": { "line": 2, - "column": 24 + "column": 16 }, "end": { "line": 2, - "column": 28 + "column": 23 } }, - "value": true, - "raw": "true" - } - ] + "name": "special" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 42, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "value": true, + "raw": "true" + } + ] + } } ] } diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js index a723cb8..12a897c 100644 --- a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js @@ -288,22 +288,39 @@ module.exports = { }, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 27, + 26, 35 ], "loc": { "start": { "line": 2, - "column": 11 + "column": 10 }, "end": { "line": 2, "column": 19 } }, - "name": "required" + "expression": { + "type": "Identifier", + "range": [ + 27, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": "required" + } } ] } @@ -717,4 +734,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js index 661c38f..fa9c94f 100644 --- a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js @@ -288,22 +288,39 @@ module.exports = { }, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 40, + 39, 48 ], "loc": { "start": { "line": 2, - "column": 18 + "column": 17 }, "end": { "line": 2, "column": 26 } }, - "name": "required" + "expression": { + "type": "Identifier", + "range": [ + 40, + 48 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "name": "required" + } } ] } diff --git a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-instance-member.result.js b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-instance-member.result.js index 51a1a55..5ab2db2 100644 --- a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-instance-member.result.js @@ -92,26 +92,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 27, + 26, 34 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 12 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 27, - 32 + 34 ], "loc": { "start": { @@ -120,12 +120,29 @@ module.exports = { }, "end": { "line": 2, - "column": 10 + "column": 12 } }, - "name": "Input" - }, - "arguments": [] + "callee": { + "type": "Identifier", + "range": [ + 27, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "name": "Input" + }, + "arguments": [] + } } ], "typeAnnotation": null @@ -205,26 +222,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 46, + 45, 54 ], "loc": { "start": { "line": 3, - "column": 5 + "column": 4 }, "end": { "line": 3, "column": 13 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 46, - 52 + 54 ], "loc": { "start": { @@ -233,12 +250,29 @@ module.exports = { }, "end": { "line": 3, - "column": 11 + "column": 13 } }, - "name": "Output" - }, - "arguments": [] + "callee": { + "type": "Identifier", + "range": [ + 46, + 52 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "name": "Output" + }, + "arguments": [] + } } ], "typeAnnotation": null @@ -645,4 +679,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-static-member.result.js b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-static-member.result.js index 9a4933f..ed82988 100644 --- a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-static-member.result.js +++ b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-factory-static-member.result.js @@ -92,26 +92,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 15, + 14, 33 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 23 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 15, - 27 + 33 ], "loc": { "start": { @@ -120,32 +120,49 @@ module.exports = { }, "end": { "line": 2, - "column": 17 + "column": 23 } }, - "name": "configurable" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 28, - 32 + 15, + 27 ], "loc": { "start": { "line": 2, - "column": 18 + "column": 5 }, "end": { "line": 2, - "column": 22 + "column": 17 } }, - "value": true, - "raw": "true" - } - ] + "name": "configurable" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 28, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "value": true, + "raw": "true" + } + ] + } } ], "typeAnnotation": null @@ -190,26 +207,26 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "CallExpression", + "type": "Decorator", "range": [ - 54, + 53, 73 ], "loc": { "start": { "line": 4, - "column": 5 + "column": 4 }, "end": { "line": 4, "column": 24 } }, - "callee": { - "type": "Identifier", + "expression": { + "type": "CallExpression", "range": [ 54, - 66 + 73 ], "loc": { "start": { @@ -218,32 +235,49 @@ module.exports = { }, "end": { "line": 4, - "column": 17 + "column": 24 } }, - "name": "configurable" - }, - "arguments": [ - { - "type": "Literal", + "callee": { + "type": "Identifier", "range": [ - 67, - 72 + 54, + 66 ], "loc": { "start": { "line": 4, - "column": 18 + "column": 5 }, "end": { "line": 4, - "column": 23 + "column": 17 } }, - "value": false, - "raw": "false" - } - ] + "name": "configurable" + }, + "arguments": [ + { + "type": "Literal", + "range": [ + 67, + 72 + ], + "loc": { + "start": { + "line": 4, + "column": 18 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "value": false, + "raw": "false" + } + ] + } } ], "typeAnnotation": null @@ -632,4 +666,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-instance-member.result.js b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-instance-member.result.js index a00abaa..0d6a772 100644 --- a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-instance-member.result.js +++ b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-instance-member.result.js @@ -92,22 +92,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 15, + 14, 18 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 8 } }, - "name": "foo" + "expression": { + "type": "Identifier", + "range": [ + 15, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "name": "foo" + } } ], "typeAnnotation": null @@ -152,22 +169,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 27, + 26, 30 ], "loc": { "start": { "line": 3, - "column": 5 + "column": 4 }, "end": { "line": 3, "column": 8 } }, - "name": "bar" + "expression": { + "type": "Identifier", + "range": [ + 27, + 30 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "name": "bar" + } } ], "typeAnnotation": null @@ -412,4 +446,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-static-member.result.js b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-static-member.result.js index 76c42c8..c03092e 100644 --- a/tests/fixtures/typescript/decorators/property-decorators/property-decorator-static-member.result.js +++ b/tests/fixtures/typescript/decorators/property-decorators/property-decorator-static-member.result.js @@ -92,22 +92,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 15, + 14, 18 ], "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, "column": 8 } }, - "name": "baz" + "expression": { + "type": "Identifier", + "range": [ + 15, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "name": "baz" + } } ], "typeAnnotation": null @@ -152,22 +169,39 @@ module.exports = { "accessibility": null, "decorators": [ { - "type": "Identifier", + "type": "Decorator", "range": [ - 34, + 33, 37 ], "loc": { "start": { "line": 3, - "column": 5 + "column": 4 }, "end": { "line": 3, "column": 8 } }, - "name": "qux" + "expression": { + "type": "Identifier", + "range": [ + 34, + 37 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "name": "qux" + } } ], "typeAnnotation": null @@ -448,4 +482,4 @@ module.exports = { } } ] -}; +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.result.js b/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.result.js new file mode 100644 index 0000000..8db1d4d --- /dev/null +++ b/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.result.js @@ -0,0 +1,203 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [ + { + "type": "TSEnumDeclaration", + "range": [ + 0, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "decorators": [ + { + "type": "Decorator", + "range": [ + 0, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "expression": { + "type": "Identifier", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "name": "dec" + } + } + ], + "name": { + "type": "Identifier", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "E" + }, + "members": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Punctuator", + "value": "@", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "Identifier", + "value": "dec", + "range": [ + 1, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 4 + } + } + }, + { + "type": "Keyword", + "value": "enum", + "range": [ + 5, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "E", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "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 + } + } + } + ] +}; \ No newline at end of file diff --git a/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.src.ts b/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.src.ts new file mode 100644 index 0000000..c5d3bb0 --- /dev/null +++ b/tests/fixtures/typescript/errorRecovery/decorator-on-enum-declaration.src.ts @@ -0,0 +1 @@ +@dec enum E {} \ No newline at end of file diff --git a/tools/update-typescript-tests.js b/tools/update-typescript-tests.js index e8c620f..9b1ca9f 100644 --- a/tools/update-typescript-tests.js +++ b/tools/update-typescript-tests.js @@ -1,21 +1,16 @@ /** * @fileoverview A simple script to update existing tests to reflect new - * parser changes. + * parser changes. * @author Nicholas C. Zakas - + */ "use strict"; -/* - * Usage: - * node tools/update-typescript-tests.js - * - */ //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -var shelljs = require("shelljs"), +const shelljs = require("shelljs"), parser = require("../parser"), tester = require("../tests/lib/tester"), path = require("path"); @@ -26,50 +21,60 @@ var shelljs = require("shelljs"), /** * Returns a raw copy of the given AST - * @param {object} ast the AST object - * @returns {object} copy of the AST object + * @param {Object} ast the AST object + * @returns {Object} copy of the AST object */ function getRaw(ast) { return JSON.parse(JSON.stringify(ast)); } - +/** + * Returns the expected AST + * @param {string} code the source code + * @param {Object} config the parser config + * @returns {Object} the expected AST object + */ function getExpectedResult(code, config) { try { return tester.getRaw(parser.parse(code, config)); } catch (ex) { - var raw = getRaw(ex); + const raw = getRaw(ex); raw.message = ex.message; return raw; } } +/** + * Returns an array of relevant filenames within the directory + * @param {string} directory path of the directory to scan + * @returns {string[]} list of relevant filenames + */ function getTestFilenames(directory) { - return shelljs.find(directory).filter(function(filename) { - return filename.indexOf(".src.ts") > -1; - }).map(function(filename) { - return filename.substring(directory.length - 1, filename.length - 7); // strip off ".src.ts" - }); + return shelljs.find(directory).filter(filename => filename.indexOf(".src.ts") > -1).map(filename => filename.substring(directory.length - 1, filename.length - 7) // strip off ".src.ts" + ); } +/** + * Writes the given AST to the given filename + * @param {*} result the AST to write to a file + * @param {*} testResultFilename name of the file to write to + * @returns {void} + */ function outputResult(result, testResultFilename) { - shelljs.echo("module.exports = " + JSON.stringify(result, null, " ") + ";").to(testResultFilename); + shelljs.echo(`module.exports = ${JSON.stringify(result, null, " ")};`).to(testResultFilename); } //------------------------------------------------------------------------------ // Setup //------------------------------------------------------------------------------ -var FIXTURES_DIR = "./tests/fixtures/typescript"; - -var testFiles = getTestFilenames(FIXTURES_DIR); - +const FIXTURES_DIR = "./tests/fixtures/typescript"; +const testFiles = getTestFilenames(FIXTURES_DIR); +const assert = require("chai").assert; -// update all tests in ecma-features -testFiles.forEach(function(filename) { +testFiles.forEach(filename => { - var feature = path.dirname(filename), - code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.ts"), + const code = shelljs.cat(`${path.resolve(FIXTURES_DIR, filename)}.src.ts`), config = { loc: true, range: true, @@ -78,8 +83,16 @@ testFiles.forEach(function(filename) { errorOnUnknownASTType: true }; - var testResultFilename = path.resolve(__dirname, "..", FIXTURES_DIR, filename) + ".result.js"; - var result = getExpectedResult(code, config); + const testResultFilename = `${path.resolve(__dirname, "..", FIXTURES_DIR, filename)}.result.js`; + const result = getExpectedResult(code, config); + + const expected = require(testResultFilename); + + try { + assert.deepEqual(result, expected); + } catch (e) { + shelljs.echo("DIFFERENT", e); + outputResult(result, testResultFilename); + } - outputResult(result, testResultFilename); });