From 16484a2266f496026bfa8def06274a7eb663a814 Mon Sep 17 00:00:00 2001 From: Rasmus Eneman Date: Sat, 18 Mar 2017 15:48:01 +0100 Subject: [PATCH] New: Add type annotation to class properties (fixes #190) --- lib/ast-converter.js | 3 +- ...ass-with-accessibility-modifiers.result.js | 73 ++++++++++++++++++- ...ecorator-factory-instance-member.result.js | 8 +- ...-decorator-factory-static-member.result.js | 8 +- ...operty-decorator-instance-member.result.js | 8 +- ...property-decorator-static-member.result.js | 8 +- 6 files changed, 93 insertions(+), 15 deletions(-) diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 36db57c..08eac2a 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1082,7 +1082,8 @@ module.exports = function(ast, extra) { accessibility: getTSNodeAccessibility(node), decorators: (node.decorators) ? node.decorators.map(function(d) { return convertChild(d.expression); - }) : [] + }) : [], + typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null }); break; diff --git a/tests/fixtures/typescript/basics/class-with-accessibility-modifiers.result.js b/tests/fixtures/typescript/basics/class-with-accessibility-modifiers.result.js index f9d174f..45c030f 100644 --- a/tests/fixtures/typescript/basics/class-with-accessibility-modifiers.result.js +++ b/tests/fixtures/typescript/basics/class-with-accessibility-modifiers.result.js @@ -90,7 +90,41 @@ module.exports = { "computed": false, "static": false, "accessibility": "private", - "decorators": [] + "decorators": [], + "typeAnnotation": { + "loc": { + "end": { + "column": 22, + "line": 2 + }, + "start": { + "column": 16, + "line": 2 + } + }, + "range": [ + 28, + 34 + ], + "type": "TypeAnnotation", + "typeAnnotation": { + "loc": { + "end": { + "column": 22, + "line": 2 + }, + "start": { + "column": 16, + "line": 2 + } + }, + "range": [ + 28, + 34 + ], + "type": "TSStringKeyword" + } + } }, { "type": "ClassProperty", @@ -130,7 +164,42 @@ module.exports = { "computed": false, "static": true, "accessibility": "public", - "decorators": [] + "decorators": [], + "typeAnnotation": { + "loc": { + "end": { + "column": 28, + "line": 3 + }, + "start": { + "column": 22, + "line": 3 + } + }, + "range": [ + 58, + 64 + ], + "type": "TypeAnnotation", + "typeAnnotation": { + "loc": { + "end": { + "column": 28, + "line": 3 + }, + "start": { + "column": 22, + "line": 3 + } + }, + "range": [ + 58, + 64 + ], + "type": "TSNumberKeyword" + } + } + }, { "type": "MethodDefinition", 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 13d258b..51a1a55 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 @@ -127,7 +127,8 @@ module.exports = { }, "arguments": [] } - ] + ], + "typeAnnotation": null }, { "type": "ClassProperty", @@ -239,7 +240,8 @@ module.exports = { }, "arguments": [] } - ] + ], + "typeAnnotation": null } ], "range": [ @@ -643,4 +645,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 4fa4ec4..9a4933f 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 @@ -147,7 +147,8 @@ module.exports = { } ] } - ] + ], + "typeAnnotation": null }, { "type": "ClassProperty", @@ -244,7 +245,8 @@ module.exports = { } ] } - ] + ], + "typeAnnotation": null } ], "range": [ @@ -630,4 +632,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 fbce854..a00abaa 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 @@ -109,7 +109,8 @@ module.exports = { }, "name": "foo" } - ] + ], + "typeAnnotation": null }, { "type": "ClassProperty", @@ -168,7 +169,8 @@ module.exports = { }, "name": "bar" } - ] + ], + "typeAnnotation": null } ], "range": [ @@ -410,4 +412,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 46bfead..76c42c8 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 @@ -109,7 +109,8 @@ module.exports = { }, "name": "baz" } - ] + ], + "typeAnnotation": null }, { "type": "ClassProperty", @@ -168,7 +169,8 @@ module.exports = { }, "name": "qux" } - ] + ], + "typeAnnotation": null } ], "range": [ @@ -446,4 +448,4 @@ module.exports = { } } ] -}; \ No newline at end of file +};