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 all commits
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 @@ -952,7 +952,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 @@ -1062,7 +1062,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 @@ -1107,7 +1107,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 @@ -145,7 +145,6 @@ export default {
isJSXToken,
getDeclarationKind,
getTSNodeAccessibility,
hasStaticModifierFlag,
findNextToken,
findFirstMatchingToken,
findChildOfKind,
Expand Down Expand Up @@ -206,11 +205,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 @@ -394,19 +396,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