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

refactor: replace internal typescript method call with hasModifier #48

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
key: convertChild(node.name),
value: convertChild(node.initializer),
computed: nodeUtils.isComputedProperty(node.name),
static: nodeUtils.hasStaticModifierFlag(node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
readonly:
nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined
});
Expand Down Expand Up @@ -1061,7 +1061,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
key: convertChild(node.name),
value: method,
computed: nodeUtils.isComputedProperty(node.name),
static: nodeUtils.hasStaticModifierFlag(node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
kind: 'method'
});

Expand Down Expand Up @@ -1106,7 +1106,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {

// TypeScript uses this even for static methods named "constructor"
case SyntaxKind.Constructor: {
const constructorIsStatic = nodeUtils.hasStaticModifierFlag(node),
const constructorIsStatic = nodeUtils.hasModifier(
SyntaxKind.StaticKeyword,
node
),
constructorIsAbstract = nodeUtils.hasModifier(
SyntaxKind.AbstractKeyword,
node
Expand Down
21 changes: 5 additions & 16 deletions src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export default {
isJSXToken,
getDeclarationKind,
getTSNodeAccessibility,
hasStaticModifierFlag,
findNextToken,
findFirstMatchingToken,
findChildOfKind,
Expand Down Expand Up @@ -203,11 +202,14 @@ function isESTreeClassMember(node: ts.Node): boolean {

/**
* Checks if a ts.Node has a modifier
* @param {number} modifierKind TypeScript SyntaxKind modifier
* @param {ts.KeywordSyntaxKind} modifierKind TypeScript SyntaxKind modifier
* @param {ts.Node} node TypeScript AST node
* @returns {boolean} has the modifier specified
*/
function hasModifier(modifierKind: number, node: ts.Node): boolean {
function hasModifier(
modifierKind: ts.KeywordSyntaxKind,
node: ts.Node
): boolean {
return (
!!node.modifiers &&
!!node.modifiers.length &&
Expand Down Expand Up @@ -393,19 +395,6 @@ function getTSNodeAccessibility(node: ts.Node): string | null {
return null;
}

/**
* Returns true if the given ts.Node has the modifier flag set which corresponds
* to the static keyword.
* @param {ts.Node} node The ts.Node
* @returns {boolean} whether or not the static modifier flag is set
*/
function hasStaticModifierFlag(node: ts.Node): boolean {
/**
* TODO: Remove dependency on private TypeScript method
*/
return Boolean((ts as any).getModifierFlags(node) & ts.ModifierFlags.Static);
}

/**
* Finds the next token based on the previous one and its parent
* Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
Expand Down