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

Commit a361063

Browse files
committed
Add node util function isOptional and isComputedProperty
1 parent c7cebe6 commit a361063

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
@@ -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

@@ -1726,8 +1724,8 @@ module.exports = function convert(config) {
17261724
case SyntaxKind.MethodSignature: {
17271725
Object.assign(result, {
17281726
type: AST_NODE_TYPES.TSMethodSignature,
1729-
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1730-
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1727+
optional: nodeUtils.isOptional(node),
1728+
computed: nodeUtils.isComputedProperty(node.name),
17311729
key: convertChild(node.name),
17321730
params: node.parameters.map(parameter => convertChild(parameter)),
17331731
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
@@ -1743,8 +1741,8 @@ module.exports = function convert(config) {
17431741
case SyntaxKind.PropertySignature: {
17441742
Object.assign(result, {
17451743
type: AST_NODE_TYPES.TSPropertySignature,
1746-
optional: (node.questionToken) ? (node.questionToken.kind === SyntaxKind.QuestionToken) : false,
1747-
computed: node.name.kind === SyntaxKind.ComputedPropertyName,
1744+
optional: nodeUtils.isOptional(node),
1745+
computed: nodeUtils.isComputedProperty(node.name),
17481746
key: convertChild(node.name),
17491747
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
17501748
});

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

0 commit comments

Comments
 (0)