diff --git a/.gitignore b/.gitignore index 4b4cc07..d2ccb92 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ coverage node_modules npm-debug.log _test.js - +.DS_Store \ No newline at end of file diff --git a/lib/ast-converter.js b/lib/ast-converter.js index b22d597..a200843 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -956,7 +956,6 @@ module.exports = function(ast, extra) { method = { type: "FunctionExpression", id: null, - params: node.parameters.map(convertChild), generator: false, expression: false, body: convertChild(node.body), @@ -975,6 +974,9 @@ module.exports = function(ast, extra) { } if (parent.kind === SyntaxKind.ObjectLiteralExpression) { + + method.params = node.parameters.map(convertChild); + assign(result, { type: "Property", key: convertChild(node.name), @@ -986,7 +988,20 @@ module.exports = function(ast, extra) { }); } else { // class + + /** + * Unlinke in object literal methods, class method params can have decorators + */ + method.params = node.parameters.map(function(param) { + var convertedParam = convertChild(param); + convertedParam.decorators = (param.decorators) ? param.decorators.map(function(d) { + return convertChild(d.expression); + }) : []; + return convertedParam; + }); + var methodNameIsComputed = (node.name.kind === SyntaxKind.ComputedPropertyName); + assign(result, { type: "MethodDefinition", key: convertChild(node.name), @@ -998,6 +1013,7 @@ module.exports = function(ast, extra) { return convertChild(d.expression); }) : [] }); + } if (node.kind === SyntaxKind.GetAccessor) { @@ -1020,7 +1036,13 @@ module.exports = function(ast, extra) { constructor = { type: "FunctionExpression", id: null, - params: node.parameters.map(convertChild), + params: node.parameters.map(function(param) { + var convertedParam = convertChild(param); + convertedParam.decorators = (param.decorators) ? param.decorators.map(function(d) { + return convertChild(d.expression); + }) : []; + return convertedParam; + }), generator: false, expression: false, body: convertChild(node.body), diff --git a/tests/fixtures/ecma-features/classes/class-accessor-properties.result.js b/tests/fixtures/ecma-features/classes/class-accessor-properties.result.js index 556e8b3..d00777a 100644 --- a/tests/fixtures/ecma-features/classes/class-accessor-properties.result.js +++ b/tests/fixtures/ecma-features/classes/class-accessor-properties.result.js @@ -221,7 +221,8 @@ module.exports = { 25, 26 ], - "name": "c" + "name": "c", + "decorators": [] } ], "body": { diff --git a/tests/fixtures/ecma-features/classes/class-static-methods-and-accessor-properties.result.js b/tests/fixtures/ecma-features/classes/class-static-methods-and-accessor-properties.result.js index 618ca0b..0d737f2 100644 --- a/tests/fixtures/ecma-features/classes/class-static-methods-and-accessor-properties.result.js +++ b/tests/fixtures/ecma-features/classes/class-static-methods-and-accessor-properties.result.js @@ -299,7 +299,8 @@ module.exports = { 52, 53 ], - "name": "b" + "name": "b", + "decorators": [] } ], "body": { 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 new file mode 100644 index 0000000..3c8e9a9 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js @@ -0,0 +1,825 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 115 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "decorators": [], + "range": [ + 0, + 115 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "Service" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 20, + 113 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "key": { + "type": "Identifier", + "name": "constructor", + "range": [ + 20, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "range": [ + 52, + 58 + ], + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 42 + } + }, + "name": "config", + "decorators": [ + { + "arguments": [ + { + "loc": { + "end": { + "column": 34, + "line": 2 + }, + "start": { + "column": 24, + "line": 2 + } + }, + "name": "APP_CONFIG", + "range": [ + 40, + 50 + ], + "type": "Identifier" + } + ], + "callee": { + "loc": { + "end": { + "column": 23, + "line": 2 + }, + "start": { + "column": 17, + "line": 2 + } + }, + "name": "Inject", + "range": [ + 33, + 39 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 35, + "line": 2 + }, + "start": { + "column": 17, + "line": 2 + } + }, + "range": [ + 33, + 51 + ], + "type": "CallExpression" + } + ] + } + ], + "generator": false, + "expression": false, + "body": { + "type": "BlockStatement", + "range": [ + 71, + 113 + ], + "loc": { + "start": { + "line": 2, + "column": 55 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "range": [ + 81, + 107 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 34 + } + }, + "expression": { + "type": "AssignmentExpression", + "range": [ + 81, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "range": [ + 81, + 91 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "object": { + "type": "ThisExpression", + "range": [ + 81, + 85 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "range": [ + 86, + 91 + ], + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "name": "title" + }, + "computed": false + }, + "right": { + "type": "MemberExpression", + "range": [ + 94, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "object": { + "type": "Identifier", + "range": [ + 94, + 100 + ], + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 27 + } + }, + "name": "config" + }, + "property": { + "type": "Identifier", + "range": [ + 101, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "name": "title" + }, + "computed": false + } + } + } + ] + }, + "range": [ + 31, + 113 + ], + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + "computed": false, + "static": false, + "kind": "constructor" + } + ], + "range": [ + 14, + 115 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + "superClass": null, + "implements": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Service", + "range": [ + 6, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Identifier", + "value": "constructor", + "range": [ + 20, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "@", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Identifier", + "value": "Inject", + "range": [ + 33, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 39, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "Identifier", + "value": "APP_CONFIG", + "range": [ + 40, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 50, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + } + } + }, + { + "type": "Identifier", + "value": "config", + "range": [ + 52, + 58 + ], + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 42 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 58, + 59 + ], + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 43 + } + } + }, + { + "type": "Identifier", + "value": "AppConfig", + "range": [ + 60, + 69 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 53 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 69, + 70 + ], + "loc": { + "start": { + "line": 2, + "column": 53 + }, + "end": { + "line": 2, + "column": 54 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 71, + 72 + ], + "loc": { + "start": { + "line": 2, + "column": 55 + }, + "end": { + "line": 2, + "column": 56 + } + } + }, + { + "type": "Keyword", + "value": "this", + "range": [ + 81, + 85 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": ".", + "range": [ + 85, + 86 + ], + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "Identifier", + "value": "title", + "range": [ + 86, + 91 + ], + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 92, + 93 + ], + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "Identifier", + "value": "config", + "range": [ + 94, + 100 + ], + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + { + "type": "Punctuator", + "value": ".", + "range": [ + 100, + 101 + ], + "loc": { + "start": { + "line": 3, + "column": 27 + }, + "end": { + "line": 3, + "column": 28 + } + } + }, + { + "type": "Identifier", + "value": "title", + "range": [ + 101, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 106, + 107 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 34 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 112, + 113 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 114, + 115 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.src.ts b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.src.ts new file mode 100644 index 0000000..066334b --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.src.ts @@ -0,0 +1,5 @@ +class Service { + constructor(@Inject(APP_CONFIG) config: AppConfig) { + this.title = config.title; + } +} 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 new file mode 100644 index 0000000..6e007c5 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js @@ -0,0 +1,540 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 52 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "decorators": [], + "range": [ + 0, + 52 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "Foo" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 16, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "key": { + "type": "Identifier", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": "bar" + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "range": [ + 35, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "name": "baz", + "decorators": [ + { + "arguments": [ + { + "loc": { + "end": { + "column": 21, + "line": 2 + }, + "start": { + "column": 17, + "line": 2 + } + }, + "range": [ + 29, + 33 + ], + "raw": "true", + "type": "Literal", + "value": true + } + ], + "callee": { + "loc": { + "end": { + "column": 16, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "name": "special", + "range": [ + 21, + 28 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 22, + "line": 2 + }, + "start": { + "column": 9, + "line": 2 + } + }, + "range": [ + 21, + 34 + ], + "type": "CallExpression" + } + ] + } + ], + "generator": false, + "expression": false, + "body": { + "type": "BlockStatement", + "range": [ + 48, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "body": [] + }, + "range": [ + 19, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 38 + } + } + }, + "computed": false, + "static": false, + "kind": "method", + "decorators": [] + } + ], + "range": [ + 10, + 52 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 16, + 19 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "@", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "special", + "range": [ + 21, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 28, + 29 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Boolean", + "value": "true", + "range": [ + 29, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + } + }, + { + "type": "Identifier", + "value": "baz", + "range": [ + 35, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 38, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 27 + } + } + }, + { + "type": "Identifier", + "value": "number", + "range": [ + 40, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 48, + 49 + ], + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 37 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 49, + 50 + ], + "loc": { + "start": { + "line": 2, + "column": 37 + }, + "end": { + "line": 2, + "column": 38 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 51, + 52 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.src.ts b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.src.ts new file mode 100644 index 0000000..9b8c50c --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.src.ts @@ -0,0 +1,3 @@ +class Foo { + bar(@special(true) baz: number) {} +} 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 new file mode 100644 index 0000000..c0c3934 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js @@ -0,0 +1,558 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 65 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "decorators": [], + "range": [ + 0, + 65 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "name": "StaticFoo" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 22, + 63 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "key": { + "type": "Identifier", + "range": [ + 29, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "name": "bar" + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "range": [ + 48, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "name": "baz", + "decorators": [ + { + "arguments": [ + { + "loc": { + "end": { + "column": 28, + "line": 2 + }, + "start": { + "column": 24, + "line": 2 + } + }, + "range": [ + 42, + 46 + ], + "raw": "true", + "type": "Literal", + "value": true + } + ], + "callee": { + "loc": { + "end": { + "column": 23, + "line": 2 + }, + "start": { + "column": 16, + "line": 2 + } + }, + "name": "special", + "range": [ + 34, + 41 + ], + "type": "Identifier" + }, + "loc": { + "end": { + "column": 29, + "line": 2 + }, + "start": { + "column": 16, + "line": 2 + } + }, + "range": [ + 34, + 47 + ], + "type": "CallExpression" + } + ] + } + ], + "generator": false, + "expression": false, + "body": { + "type": "BlockStatement", + "range": [ + 61, + 63 + ], + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "body": [] + }, + "range": [ + 32, + 63 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 45 + } + } + }, + "computed": false, + "static": true, + "kind": "method", + "decorators": [] + } + ], + "range": [ + 16, + 65 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + "superClass": null, + "implements": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "StaticFoo", + "range": [ + 6, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 16, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Keyword", + "value": "static", + "range": [ + 22, + 28 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "bar", + "range": [ + 29, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "@", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Identifier", + "value": "special", + "range": [ + 34, + 41 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "Boolean", + "value": "true", + "range": [ + 42, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 28 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + } + } + }, + { + "type": "Identifier", + "value": "baz", + "range": [ + 48, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 51, + 52 + ], + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "Identifier", + "value": "number", + "range": [ + 53, + 59 + ], + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 41 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 59, + 60 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 42 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 61, + 62 + ], + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 62, + 63 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 45 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 64, + 65 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.src.ts b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.src.ts new file mode 100644 index 0000000..26013fa --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.src.ts @@ -0,0 +1,3 @@ +class StaticFoo { + static bar(@special(true) baz: number) {} +} 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 new file mode 100644 index 0000000..7a14d27 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js @@ -0,0 +1,684 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "decorators": [], + "range": [ + 0, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "Greeter" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 20, + 95 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "key": { + "type": "Identifier", + "range": [ + 20, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": "greet" + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "range": [ + 36, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "name": "name", + "decorators": [ + { + "loc": { + "end": { + "column": 19, + "line": 2 + }, + "start": { + "column": 11, + "line": 2 + } + }, + "name": "required", + "range": [ + 27, + 35 + ], + "type": "Identifier" + } + ] + } + ], + "generator": false, + "expression": false, + "body": { + "type": "BlockStatement", + "range": [ + 50, + 95 + ], + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "body": [ + { + "type": "ReturnStatement", + "range": [ + 60, + 89 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 37 + } + }, + "argument": { + "type": "BinaryExpression", + "range": [ + 67, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "operator": "+", + "left": { + "type": "BinaryExpression", + "range": [ + 67, + 82 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "operator": "+", + "left": { + "type": "Literal", + "range": [ + 67, + 75 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "value": "Hello ", + "raw": "\"Hello \"" + }, + "right": { + "type": "Identifier", + "range": [ + 78, + 82 + ], + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "name": "name" + } + }, + "right": { + "type": "Literal", + "range": [ + 85, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "value": "!", + "raw": "\"!\"" + } + } + } + ] + }, + "range": [ + 25, + 95 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + "computed": false, + "static": false, + "kind": "method", + "decorators": [] + } + ], + "range": [ + 14, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + "superClass": null, + "implements": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "Greeter", + "range": [ + 6, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Identifier", + "value": "greet", + "range": [ + 20, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "@", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "Identifier", + "value": "required", + "range": [ + 27, + 35 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + { + "type": "Identifier", + "value": "name", + "range": [ + 36, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 40, + 41 + ], + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 42, + 48 + ], + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 32 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 48, + 49 + ], + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 50, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + } + } + }, + { + "type": "Keyword", + "value": "return", + "range": [ + 60, + 66 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + { + "type": "String", + "value": "\"Hello \"", + "range": [ + 67, + 75 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 76, + 77 + ], + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "name", + "range": [ + 78, + 82 + ], + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 83, + 84 + ], + "loc": { + "start": { + "line": 3, + "column": 31 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "String", + "value": "\"!\"", + "range": [ + 85, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 36 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 88, + 89 + ], + "loc": { + "start": { + "line": 3, + "column": 36 + }, + "end": { + "line": 3, + "column": 37 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 94, + 95 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 96, + 97 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.src.ts b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.src.ts new file mode 100644 index 0000000..fa5f490 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.src.ts @@ -0,0 +1,5 @@ +class Greeter { + greet(@required name: string) { + return "Hello " + name + "!"; + } +} 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 new file mode 100644 index 0000000..99c154b --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js @@ -0,0 +1,702 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 110 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "decorators": [], + "range": [ + 0, + 110 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": "StaticGreeter" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 26, + 108 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "key": { + "type": "Identifier", + "range": [ + 33, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "name": "greet" + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "range": [ + 49, + 53 + ], + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "name": "name", + "decorators": [ + { + "loc": { + "end": { + "column": 26, + "line": 2 + }, + "start": { + "column": 18, + "line": 2 + } + }, + "name": "required", + "range": [ + 40, + 48 + ], + "type": "Identifier" + } + ] + } + ], + "generator": false, + "expression": false, + "body": { + "type": "BlockStatement", + "range": [ + 63, + 108 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "body": [ + { + "type": "ReturnStatement", + "range": [ + 73, + 102 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 37 + } + }, + "argument": { + "type": "BinaryExpression", + "range": [ + 80, + 101 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "operator": "+", + "left": { + "type": "BinaryExpression", + "range": [ + 80, + 95 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "operator": "+", + "left": { + "type": "Literal", + "range": [ + 80, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "value": "Hello ", + "raw": "\"Hello \"" + }, + "right": { + "type": "Identifier", + "range": [ + 91, + 95 + ], + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "name": "name" + } + }, + "right": { + "type": "Literal", + "range": [ + 98, + 101 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "value": "!", + "raw": "\"!\"" + } + } + } + ] + }, + "range": [ + 38, + 108 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + "computed": false, + "static": true, + "kind": "method", + "decorators": [] + } + ], + "range": [ + 20, + 110 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + "superClass": null, + "implements": [] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "StaticGreeter", + "range": [ + 6, + 19 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 19 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Keyword", + "value": "static", + "range": [ + 26, + 32 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Identifier", + "value": "greet", + "range": [ + 33, + 38 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 38, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": "@", + "range": [ + 39, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + { + "type": "Identifier", + "value": "required", + "range": [ + 40, + 48 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 26 + } + } + }, + { + "type": "Identifier", + "value": "name", + "range": [ + 49, + 53 + ], + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 31 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "range": [ + 53, + 54 + ], + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 32 + } + } + }, + { + "type": "Identifier", + "value": "string", + "range": [ + 55, + 61 + ], + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 39 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 61, + 62 + ], + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 63, + 64 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 42 + } + } + }, + { + "type": "Keyword", + "value": "return", + "range": [ + 73, + 79 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + { + "type": "String", + "value": "\"Hello \"", + "range": [ + 80, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 89, + 90 + ], + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 25 + } + } + }, + { + "type": "Identifier", + "value": "name", + "range": [ + 91, + 95 + ], + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 96, + 97 + ], + "loc": { + "start": { + "line": 3, + "column": 31 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "String", + "value": "\"!\"", + "range": [ + 98, + 101 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 36 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 101, + 102 + ], + "loc": { + "start": { + "line": 3, + "column": 36 + }, + "end": { + "line": 3, + "column": 37 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 107, + 108 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 109, + 110 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + } + ] +}; diff --git a/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.src.ts b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.src.ts new file mode 100644 index 0000000..ad512a2 --- /dev/null +++ b/tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.src.ts @@ -0,0 +1,5 @@ +class StaticGreeter { + static greet(@required name: string) { + return "Hello " + name + "!"; + } +}