Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit dd6404a

Browse files
soda0289JamesHenry
authored andcommitted
Breaking: Convert Signature types to be more ESTree like (fixes #262) (#264)
1 parent 379dcaf commit dd6404a

16 files changed

+8491
-224
lines changed

lib/ast-node-types.js

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module.exports = {
106106
TSConstructorType: "TSConstructorType",
107107
TSConstructSignature: "TSConstructSignature",
108108
TSDeclareKeyword: "TSDeclareKeyword",
109+
TSIndexSignature: "TSIndexSignature",
109110
TSInterfaceBody: "TSInterfaceBody",
110111
TSInterfaceDeclaration: "TSInterfaceDeclaration",
111112
TSInterfaceHeritage: "TSInterfaceHeritage",

lib/convert.js

+71-16
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ module.exports = function convert(config) {
618618
type: AST_NODE_TYPES.Property,
619619
key: convertChild(node.name),
620620
value: convertChild(node.initializer),
621-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
621+
computed: nodeUtils.isComputedProperty(node.name),
622622
method: false,
623623
shorthand: false,
624624
kind: "init"
@@ -660,7 +660,7 @@ module.exports = function convert(config) {
660660
type: (isAbstract) ? AST_NODE_TYPES.TSAbstractClassProperty : AST_NODE_TYPES.ClassProperty,
661661
key: convertChild(node.name),
662662
value: convertChild(node.initializer),
663-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
663+
computed: nodeUtils.isComputedProperty(node.name),
664664
static: nodeUtils.hasStaticModifierFlag(node),
665665
accessibility: nodeUtils.getTSNodeAccessibility(node),
666666
decorators: convertDecorators(node.decorators),
@@ -713,7 +713,7 @@ module.exports = function convert(config) {
713713
type: AST_NODE_TYPES.Property,
714714
key: convertChild(node.name),
715715
value: method,
716-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
716+
computed: nodeUtils.isComputedProperty(node.name),
717717
method: nodeIsMethod,
718718
shorthand: false,
719719
kind: "init"
@@ -730,8 +730,6 @@ module.exports = function convert(config) {
730730
return convertedParam;
731731
});
732732

733-
const isMethodNameComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);
734-
735733
/**
736734
* TypeScript class methods can be defined as "abstract"
737735
*/
@@ -743,7 +741,7 @@ module.exports = function convert(config) {
743741
type: methodDefinitionType,
744742
key: convertChild(node.name),
745743
value: method,
746-
computed: isMethodNameComputed,
744+
computed: nodeUtils.isComputedProperty(node.name),
747745
static: nodeUtils.hasStaticModifierFlag(node),
748746
kind: "method",
749747
accessibility: nodeUtils.getTSNodeAccessibility(node),
@@ -805,7 +803,7 @@ module.exports = function convert(config) {
805803
};
806804

807805
const constructorIdentifierLoc = ast.getLineAndCharacterOfPosition(firstConstructorToken.getStart()),
808-
constructorIsComputed = !!node.name && (node.name.kind === SyntaxKind.ComputedPropertyName);
806+
constructorIsComputed = !!node.name && nodeUtils.isComputedProperty(node.name);
809807

810808
let constructorKey;
811809

@@ -1723,6 +1721,72 @@ module.exports = function convert(config) {
17231721

17241722
}
17251723

1724+
case SyntaxKind.MethodSignature: {
1725+
Object.assign(result, {
1726+
type: AST_NODE_TYPES.TSMethodSignature,
1727+
optional: nodeUtils.isOptional(node),
1728+
computed: nodeUtils.isComputedProperty(node.name),
1729+
key: convertChild(node.name),
1730+
params: node.parameters.map(parameter => convertChild(parameter)),
1731+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1732+
accessibility: nodeUtils.getTSNodeAccessibility(node),
1733+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1734+
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1735+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1736+
});
1737+
1738+
if (node.typeParameters) {
1739+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1740+
}
1741+
1742+
break;
1743+
}
1744+
1745+
case SyntaxKind.PropertySignature: {
1746+
Object.assign(result, {
1747+
type: AST_NODE_TYPES.TSPropertySignature,
1748+
optional: nodeUtils.isOptional(node),
1749+
computed: nodeUtils.isComputedProperty(node.name),
1750+
key: convertChild(node.name),
1751+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1752+
initializer: convertChild(node.initializer),
1753+
accessibility: nodeUtils.getTSNodeAccessibility(node),
1754+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1755+
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1756+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1757+
});
1758+
1759+
break;
1760+
}
1761+
1762+
case SyntaxKind.IndexSignature: {
1763+
Object.assign(result, {
1764+
type: AST_NODE_TYPES.TSIndexSignature,
1765+
index: convertChild(node.parameters[0]),
1766+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
1767+
accessibility: nodeUtils.getTSNodeAccessibility(node),
1768+
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
1769+
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
1770+
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
1771+
});
1772+
1773+
break;
1774+
}
1775+
1776+
case SyntaxKind.ConstructSignature: {
1777+
Object.assign(result, {
1778+
type: AST_NODE_TYPES.TSConstructSignature,
1779+
params: node.parameters.map(parameter => convertChild(parameter)),
1780+
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
1781+
});
1782+
1783+
if (node.typeParameters) {
1784+
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
1785+
}
1786+
1787+
break;
1788+
}
1789+
17261790
case SyntaxKind.InterfaceDeclaration: {
17271791
const interfaceHeritageClauses = node.heritageClauses || [];
17281792

@@ -1771,15 +1835,6 @@ module.exports = function convert(config) {
17711835
});
17721836
break;
17731837

1774-
case SyntaxKind.PropertySignature:
1775-
case SyntaxKind.MethodSignature:
1776-
deeplyCopy();
1777-
1778-
if (node.questionToken) {
1779-
result.name.optional = true;
1780-
}
1781-
break;
1782-
17831838
default:
17841839
deeplyCopy();
17851840
}

lib/node-utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ module.exports = {
186186
hasJSXAncestor,
187187
unescapeIdentifier,
188188
unescapeStringLiteralText,
189+
isComputedProperty,
190+
isOptional,
189191
fixExports,
190192
getTokenType,
191193
convertToken,
@@ -446,6 +448,25 @@ function unescapeStringLiteralText(text) {
446448
return unescape(text);
447449
}
448450

451+
/**
452+
* Returns true if a given TSNode is a computed property
453+
* @param {TSNode} node TSNode to be checked
454+
* @returns {boolean} is Computed Property
455+
*/
456+
function isComputedProperty(node) {
457+
return node.kind === SyntaxKind.ComputedPropertyName;
458+
}
459+
460+
/**
461+
* Returns true if a given TSNode is optional (has QuestionToken)
462+
* @param {TSNode} node TSNode to be checked
463+
* @returns {boolean} is Optional
464+
*/
465+
function isOptional(node) {
466+
return (node.questionToken)
467+
? (node.questionToken.kind === SyntaxKind.QuestionToken) : false;
468+
}
469+
449470
/**
450471
* Fixes the exports of the given TSNode
451472
* @param {TSNode} node the TSNode

tests/fixtures/typescript/basics/export-type-class-declaration.result.js

+29-22
Original file line numberDiff line numberDiff line change
@@ -107,67 +107,74 @@ module.exports = {
107107
48
108108
],
109109
"loc": {
110-
"end": {
111-
"line": 2,
112-
"column": 17
113-
},
114110
"start": {
115111
"line": 2,
116112
"column": 4
113+
},
114+
"end": {
115+
"line": 2,
116+
"column": 17
117117
}
118118
},
119-
"name": {
119+
"optional": false,
120+
"computed": false,
121+
"key": {
120122
"type": "Identifier",
121123
"range": [
122124
35,
123125
40
124126
],
125127
"loc": {
126-
"end": {
127-
"line": 2,
128-
"column": 9
129-
},
130128
"start": {
131129
"line": 2,
132130
"column": 4
131+
},
132+
"end": {
133+
"line": 2,
134+
"column": 9
133135
}
134136
},
135137
"name": "count"
136138
},
137139
"typeAnnotation": {
138140
"type": "TypeAnnotation",
139-
"range": [
140-
42,
141-
48
142-
],
143141
"loc": {
144-
"end": {
145-
"line": 2,
146-
"column": 17
147-
},
148142
"start": {
149143
"line": 2,
150144
"column": 11
145+
},
146+
"end": {
147+
"line": 2,
148+
"column": 17
151149
}
152150
},
151+
"range": [
152+
42,
153+
48
154+
],
153155
"typeAnnotation": {
154156
"type": "TSNumberKeyword",
155157
"range": [
156158
42,
157159
48
158160
],
159161
"loc": {
160-
"end": {
161-
"line": 2,
162-
"column": 17
163-
},
164162
"start": {
165163
"line": 2,
166164
"column": 11
165+
},
166+
"end": {
167+
"line": 2,
168+
"column": 17
167169
}
168170
}
169171
}
170-
}
172+
},
173+
"initializer": null,
174+
"accessibility": null,
175+
"readonly": false,
176+
"static": false,
177+
"export": false
171178
}
172179
]
173180
}

0 commit comments

Comments
 (0)