diff --git a/lib/convert.js b/lib/convert.js index 9e6301e..f496a16 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -110,18 +110,30 @@ module.exports = function convert(config) { greaterThanToken.end ], loc: nodeUtils.getLocFor(firstTypeArgument.pos - 1, greaterThanToken.end, ast), - params: typeArguments.map(typeArgument => ({ - type: AST_NODE_TYPES.TSTypeReference, - range: [ - typeArgument.getStart(), - typeArgument.getEnd() - ], - loc: nodeUtils.getLoc(typeArgument, ast), - typeName: convertChild(typeArgument.typeName || typeArgument), - typeParameters: (typeArgument.typeArguments) - ? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments) - : null - })) + params: typeArguments.map(typeArgument => { + if (nodeUtils.isTypeKeyword(typeArgument.kind)) { + return { + type: AST_NODE_TYPES[`TS${SyntaxKind[typeArgument.kind]}`], + range: [ + typeArgument.getStart(), + typeArgument.getEnd() + ], + loc: nodeUtils.getLoc(typeArgument, ast) + }; + } + return { + type: AST_NODE_TYPES.TSTypeReference, + range: [ + typeArgument.getStart(), + typeArgument.getEnd() + ], + loc: nodeUtils.getLoc(typeArgument, ast), + typeName: convertChild(typeArgument.typeName || typeArgument), + typeParameters: (typeArgument.typeArguments) + ? convertTypeArgumentsToTypeParameters(typeArgument.typeArguments) + : undefined + }; + }) }; } @@ -790,10 +802,13 @@ module.exports = function convert(config) { value: convertChild(node.initializer), computed: nodeUtils.isComputedProperty(node.name), static: nodeUtils.hasStaticModifierFlag(node), - readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node), - typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null + readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) }); + if (node.type) { + result.typeAnnotation = convertTypeAnnotation(node.type); + } + if (node.decorators) { result.decorators = convertDecorators(node.decorators); } diff --git a/lib/node-utils.js b/lib/node-utils.js index d10207e..40fb339 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -183,7 +183,8 @@ module.exports = { convertToken, convertTokens, getNodeContainer, - isWithinTypeAnnotation + isWithinTypeAnnotation, + isTypeKeyword }; /* eslint-enable no-use-before-define */ @@ -313,6 +314,27 @@ function isJSXToken(node) { ); } +/** + * Returns true if the given TSNode.kind value corresponds to a type keyword + * @param {number} kind TypeScript SyntaxKind + * @returns {boolean} is a type keyword + */ +function isTypeKeyword(kind) { + switch (kind) { + case SyntaxKind.AnyKeyword: + case SyntaxKind.BooleanKeyword: + case SyntaxKind.NeverKeyword: + case SyntaxKind.NumberKeyword: + case SyntaxKind.ObjectKeyword: + case SyntaxKind.StringKeyword: + case SyntaxKind.SymbolKeyword: + case SyntaxKind.VoidKeyword: + return true; + default: + return false; + } +} + /** * Returns the declaration kind of the given TSNode * @param {TSNode} node TypeScript AST node diff --git a/tests/ast-alignment/spec.js b/tests/ast-alignment/spec.js index 188ff0c..2c0cb5b 100644 --- a/tests/ast-alignment/spec.js +++ b/tests/ast-alignment/spec.js @@ -423,6 +423,9 @@ const fixturePatternsToTest = [ "typescript/basics/function-with-await.src.ts", "typescript/errorRecovery/class-extends-empty-implements.src.ts", "typescript/basics/const-enum.src.ts", + "typescript/basics/class-with-readonly-property.src.ts", + "typescript/expressions/call-expression-type-arguments.src.ts", + "typescript/expressions/new-expression-type-arguments.src.ts", { pattern: "typescript/basics/export-named-enum.src.ts", @@ -486,7 +489,6 @@ const fixturePatternsToTest = [ // "typescript/basics/class-with-protected-parameter-properties.src.ts", // "typescript/basics/class-with-public-parameter-properties.src.ts", // "typescript/basics/class-with-readonly-parameter-properties.src.ts", - // "typescript/basics/class-with-readonly-property.src.ts", // "typescript/basics/class-with-type-parameter-default.src.ts", // "typescript/basics/class-with-type-parameter-underscore.src.ts", // "typescript/basics/class-with-type-parameter.src.ts", @@ -544,8 +546,6 @@ const fixturePatternsToTest = [ // "typescript/decorators/property-decorators/property-decorator-instance-member.src.ts", // "typescript/decorators/property-decorators/property-decorator-static-member.src.ts", // "typescript/errorRecovery/interface-empty-extends.src.ts", - // "typescript/expressions/call-expression-type-arguments.src.ts", - // "typescript/expressions/new-expression-type-arguments.src.ts", // "typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts", // "typescript/namespaces-and-modules/declare-namespace-with-exported-function.src.ts", // "typescript/namespaces-and-modules/module-with-default-exports.src.ts", diff --git a/tests/lib/__snapshots__/typescript.js.snap b/tests/lib/__snapshots__/typescript.js.snap index bb6bfe6..20d7c36 100644 --- a/tests/lib/__snapshots__/typescript.js.snap +++ b/tests/lib/__snapshots__/typescript.js.snap @@ -887,25 +887,7 @@ Object { 76, 82, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 43, - "line": 2, - }, - "start": Object { - "column": 37, - "line": 2, - }, - }, - "range": Array [ - 76, - 82, - ], - "type": "TSStringKeyword", - }, - "typeParameters": null, + "type": "TSStringKeyword", }, ], "range": Array [ @@ -1342,7 +1324,6 @@ Object { "readonly": false, "static": false, "type": "TSAbstractClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -1382,7 +1363,6 @@ Object { "readonly": false, "static": false, "type": "TSAbstractClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -1755,7 +1735,6 @@ Object { "readonly": true, "static": false, "type": "TSAbstractClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -2209,25 +2188,7 @@ Object { 68, 74, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 35, - "line": 2, - }, - "start": Object { - "column": 29, - "line": 2, - }, - }, - "range": Array [ - 68, - 74, - ], - "type": "TSStringKeyword", - }, - "typeParameters": null, + "type": "TSStringKeyword", }, ], "range": Array [ @@ -6645,7 +6606,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -7050,7 +7010,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, Object { "loc": Object { @@ -7086,7 +7045,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -8996,7 +8954,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -9329,7 +9286,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, Object { "loc": Object { @@ -9365,7 +9321,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -9908,7 +9863,6 @@ Object { ], "type": "TSTypeLiteral", }, - "typeParameters": null, }, ], "range": Array [ @@ -10112,25 +10066,7 @@ Object { 104, 107, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 21, - "line": 5, - }, - "start": Object { - "column": 18, - "line": 5, - }, - }, - "range": Array [ - 104, - 107, - ], - "type": "TSAnyKeyword", - }, - "typeParameters": null, + "type": "TSAnyKeyword", }, ], "range": Array [ @@ -11722,7 +11658,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -12843,7 +12778,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -13438,7 +13372,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -17862,7 +17795,6 @@ Object { "readonly": true, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -27743,25 +27675,7 @@ Object { 62, 68, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 68, - "line": 1, - }, - "start": Object { - "column": 62, - "line": 1, - }, - }, - "range": Array [ - 62, - 68, - ], - "type": "TSStringKeyword", - }, - "typeParameters": null, + "type": "TSStringKeyword", }, ], "range": Array [ @@ -32967,7 +32881,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -35058,25 +34971,7 @@ Object { 35, 41, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 41, - "line": 1, - }, - "start": Object { - "column": 35, - "line": 1, - }, - }, - "range": Array [ - 35, - 41, - ], - "type": "TSStringKeyword", - }, - "typeParameters": null, + "type": "TSStringKeyword", }, ], "range": Array [ @@ -36897,7 +36792,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": Object { "loc": Object { "end": Object { @@ -37743,7 +37637,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -38221,7 +38114,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -39785,7 +39677,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -51284,7 +51175,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -51379,7 +51269,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": Object { "arguments": Array [], "callee": Object { @@ -51987,7 +51876,6 @@ Object { "readonly": false, "static": true, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -52102,7 +51990,6 @@ Object { "readonly": false, "static": true, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, ], @@ -52619,7 +52506,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -52696,7 +52582,6 @@ Object { "readonly": false, "static": false, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, ], @@ -53069,7 +52954,6 @@ Object { "readonly": false, "static": true, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, Object { @@ -53146,7 +53030,6 @@ Object { "readonly": false, "static": true, "type": "ClassProperty", - "typeAnnotation": null, "value": null, }, ], @@ -59796,7 +59679,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -59885,25 +59767,7 @@ Object { 14, 20, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 10, - "line": 2, - }, - "start": Object { - "column": 4, - "line": 2, - }, - }, - "range": Array [ - 14, - 20, - ], - "type": "TSNumberKeyword", - }, - "typeParameters": null, + "type": "TSNumberKeyword", }, ], "range": Array [ @@ -60308,7 +60172,6 @@ Object { ], "type": "Identifier", }, - "typeParameters": null, }, ], "range": Array [ @@ -61099,25 +60962,7 @@ Object { 77, 80, ], - "type": "TSTypeReference", - "typeName": Object { - "loc": Object { - "end": Object { - "column": 57, - "line": 2, - }, - "start": Object { - "column": 54, - "line": 2, - }, - }, - "range": Array [ - 77, - 80, - ], - "type": "TSAnyKeyword", - }, - "typeParameters": null, + "type": "TSAnyKeyword", }, ], "range": Array [