Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flow node type guards #249

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ function isFlagSetOnObject(obj: { flags: number }, flag: number): boolean {
return isFlagSet(obj.flags, flag);
}

/**
* Test if the given node has the given `FlowFlags` set.
*
* @category Flow Nodes - Flag Utilities
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowFlagSet(flowNode, ts.FlowFlags.Referenced)) {
* // ...
* }
* ```
*/
export const isFlowFlagSet: (node: ts.Node, flag: ts.FlowFlags) => boolean =
isFlagSetOnObject;

/**
* Test if the given node has the given `ModifierFlags` set.
*
Expand Down
1 change: 1 addition & 0 deletions src/flowNodes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./typeGuards";
204 changes: 204 additions & 0 deletions src/flowNodes/typeGuards/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import ts from "typescript";

/**
* Test if a flow node is a `FlowStart`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowStart(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowStart`.
*/
export function isFlowStart(flowNode: ts.FlowNode): flowNode is ts.FlowStart {
return (

Check warning on line 19 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L19

Added line #L19 was not covered by tests
"node" in flowNode &&
(flowNode.node === undefined ||
ts.isFunctionExpression(flowNode.node) ||
ts.isArrowFunction(flowNode.node) ||
ts.isMethodDeclaration(flowNode.node) ||
ts.isGetAccessorDeclaration(flowNode.node) ||
ts.isSetAccessorDeclaration(flowNode.node))
);
}

/**
* Test if a flow node is a `FlowLabel`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowLabel(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowLabel`.
*/
export function isFlowLabel(flowNode: ts.FlowNode): flowNode is ts.FlowLabel {
return "antecedents" in flowNode && !("antecedent" in flowNode);
}

/**
* Test if a flow node is a `FlowAssignment`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowAssignment(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowAssignment`.
*/
export function isFlowAssignment(
flowNode: ts.FlowNode
): flowNode is ts.FlowAssignment {
return (

Check warning on line 67 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L67

Added line #L67 was not covered by tests
"node" in flowNode &&
"antecedent" in flowNode &&
!("antecedents" in flowNode) &&
(ts.isExpression(flowNode.node) ||
ts.isVariableDeclaration(flowNode.node) ||
ts.isBindingElement(flowNode.node))
);
}

/**
* Test if a flow node is a `FlowCall`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowCall(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowCall`.
*/
export function isFlowCall(flowNode: ts.FlowNode): flowNode is ts.FlowCall {
return (

Check warning on line 93 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L93

Added line #L93 was not covered by tests
"node" in flowNode &&
"antecedent" in flowNode &&
!("antecedents" in flowNode) &&
ts.isCallExpression(flowNode.node)
);
}

/**
* Test if a flow node is a `FlowCondition`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowCondition(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowCondition`.
*/
export function isFlowCondition(
flowNode: ts.FlowNode
): flowNode is ts.FlowCondition {
return (

Check warning on line 119 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L119

Added line #L119 was not covered by tests
"node" in flowNode &&
"antecedent" in flowNode &&
!("antecedents" in flowNode) &&
ts.isExpression(flowNode.node)
);
}

/**
* Test if a flow node is a `FlowSwitchClause`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowSwitchClause(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowSwitchClause`.
*/
export function isFlowSwitchClause(
flowNode: ts.FlowNode
): flowNode is ts.FlowSwitchClause {
return (

Check warning on line 145 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L145

Added line #L145 was not covered by tests
"switchStatement" in flowNode &&
"clauseStart" in flowNode &&
"clauseEnd" in flowNode &&
"antecedent" in flowNode &&
!("antecedents" in flowNode) &&
ts.isSwitchStatement(flowNode.switchStatement)
);
}

/**
* Test if a flow node is a `FlowArrayMutation`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowArrayMutation(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowArrayMutation`.
*/
export function isFlowArrayMutation(
flowNode: ts.FlowNode
): flowNode is ts.FlowArrayMutation {
return (

Check warning on line 173 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L173

Added line #L173 was not covered by tests
"node" in flowNode &&
"antecedent" in flowNode &&
!("antecedents" in flowNode) &&
(ts.isCallExpression(flowNode.node) || ts.isBinaryExpression(flowNode.node))
);
}

/**
* Test if a flow node is a `FlowReduceLabel`.
*
* @category Flow Nodes - Type Guards
* @example
* ```ts
* declare const flowNode: ts.FlowNode;
*
* if (isFlowReduceLabel(flowNode)) {
* // ...
* }
* ```
*
* @returns Whether the given node appears to be a `FlowReduceLabel`.
*/
export function isFlowReduceLabel(
flowNode: ts.FlowNode
): flowNode is ts.FlowReduceLabel {
return (

Check warning on line 199 in src/flowNodes/typeGuards/index.ts

View check run for this annotation

Codecov / codecov/patch

src/flowNodes/typeGuards/index.ts#L199

Added line #L199 was not covered by tests
"target" in flowNode &&
"antecedents" in flowNode &&
"antecedent" in flowNode
);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./comments";
export * from "./compilerOptions";
export * from "./flags";
export * from "./flowNodes";
export * from "./modifiers";
export * from "./nodes";
export * from "./scopes";
Expand Down
21 changes: 21 additions & 0 deletions src/nodes/typeGuards/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,3 +1187,24 @@
export function isVoidKeyword(node: ts.Node): node is VoidKeyword {
return node.kind === ts.SyntaxKind.VoidKeyword;
}

/**
* Test if a node has a `flowNode` property.
*
* @category Nodes - Type Guards
* @example
* ```ts
* declare const node: ts.Node;
*
* if (hasFlowNode(node)) {
* // ...
* }
* ```
*
* @returns Whether the given node has a `flowNode` property.
*/
export function hasFlowNode(
node: ts.Node
): node is ts.Node & { flowNode: ts.FlowNode } {
return "flowNode" in node;

Check warning on line 1209 in src/nodes/typeGuards/single.ts

View check run for this annotation

Codecov / codecov/patch

src/nodes/typeGuards/single.ts#L1209

Added line #L1209 was not covered by tests
}
Loading