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

Commit fa7e22a

Browse files
committed
Add node util function isOptional and isComputedProperty
1 parent ddeaaea commit fa7e22a

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/convert.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ module.exports = function convert(config) {
643643
type: AST_NODE_TYPES.Property,
644644
key: convertChild(node.name),
645645
value: convertChild(node.initializer),
646-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
646+
computed: nodeUtils.isComputedProperty(node.name),
647647
method: false,
648648
shorthand: false,
649649
kind: "init"
@@ -685,7 +685,7 @@ module.exports = function convert(config) {
685685
type: (isAbstract) ? AST_NODE_TYPES.TSAbstractClassProperty : AST_NODE_TYPES.ClassProperty,
686686
key: convertChild(node.name),
687687
value: convertChild(node.initializer),
688-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
688+
computed: nodeUtils.isComputedProperty(node.name),
689689
static: nodeUtils.hasStaticModifierFlag(node),
690690
accessibility: nodeUtils.getTSNodeAccessibility(node),
691691
decorators: convertDecorators(node.decorators),
@@ -730,7 +730,7 @@ module.exports = function convert(config) {
730730
type: AST_NODE_TYPES.Property,
731731
key: convertChild(node.name),
732732
value: method,
733-
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
733+
computed: nodeUtils.isComputedProperty(node.name),
734734
method: nodeIsMethod,
735735
shorthand: false,
736736
kind: "init"
@@ -747,8 +747,6 @@ module.exports = function convert(config) {
747747
return convertedParam;
748748
});
749749

750-
const isMethodNameComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);
751-
752750
/**
753751
* TypeScript class methods can be defined as "abstract"
754752
*/
@@ -760,7 +758,7 @@ module.exports = function convert(config) {
760758
type: methodDefinitionType,
761759
key: convertChild(node.name),
762760
value: method,
763-
computed: isMethodNameComputed,
761+
computed: nodeUtils.isComputedProperty(node.name),
764762
static: nodeUtils.hasStaticModifierFlag(node),
765763
kind: "method",
766764
accessibility: nodeUtils.getTSNodeAccessibility(node),
@@ -818,7 +816,7 @@ module.exports = function convert(config) {
818816
};
819817

820818
const constructorIdentifierLoc = ast.getLineAndCharacterOfPosition(firstConstructorToken.getStart()),
821-
constructorIsComputed = !!node.name && (node.name.kind === SyntaxKind.ComputedPropertyName);
819+
constructorIsComputed = !!node.name && nodeUtils.isComputedProperty(node.name);
822820

823821
let constructorKey;
824822

@@ -1691,8 +1689,8 @@ module.exports = function convert(config) {
16911689
case SyntaxKind.MethodSignature: {
16921690
Object.assign(result, {
16931691
type: AST_NODE_TYPES.TSMethodSignature,
1694-
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1695-
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1692+
optional: nodeUtils.isOptional(node),
1693+
computed: nodeUtils.isComputedProperty(node.name),
16961694
key: convertChild(node.name),
16971695
params: node.parameters.map(parameter => convertChild(parameter)),
16981696
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
@@ -1708,8 +1706,8 @@ module.exports = function convert(config) {
17081706
case SyntaxKind.PropertySignature: {
17091707
Object.assign(result, {
17101708
type: AST_NODE_TYPES.TSPropertySignature,
1711-
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1712-
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1709+
optional: nodeUtils.isOptional(node),
1710+
computed: nodeUtils.isComputedProperty(node.name),
17131711
key: convertChild(node.name),
17141712
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
17151713
});

lib/node-utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ module.exports = {
184184
hasJSXAncestor,
185185
unescapeIdentifier,
186186
unescapeStringLiteralText,
187+
isComputedProperty,
188+
isOptional,
187189
fixExports,
188190
getTokenType,
189191
convertToken,
@@ -443,6 +445,25 @@ function unescapeStringLiteralText(text) {
443445
return unescape(text);
444446
}
445447

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

0 commit comments

Comments
 (0)