Skip to content

Commit

Permalink
fix(bug): adds support for missing node types
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Feb 25, 2019
1 parent cafa7ad commit 6a90228
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {
isArrayBindingPattern,
isArrayTypeNode,
isBindingElement,
isCallSignatureDeclaration,
isClassDeclaration,
isConditionalTypeNode,
isConstructorDeclaration,
isEnumDeclaration,
isEnumMember,
isExportAssignment,
isExportDeclaration,
isExportSpecifier,
Expand All @@ -23,9 +27,12 @@ import {
isObjectBindingPattern,
isParameter,
isParenthesizedTypeNode,
isPropertyAccessExpression,
isPropertyDeclaration,
isPropertySignature,
isQualifiedName,
isToken,
isTupleTypeNode,
isTypeAliasDeclaration,
isTypeLiteralNode,
isTypeParameterDeclaration,
Expand All @@ -37,6 +44,7 @@ import {
isVariableDeclarationList,
isVariableStatement,
Node,
OptionalTypeNode,
SyntaxKind
} from "typescript";
import {IsReferencedOptions} from "./is-referenced-options";
Expand Down Expand Up @@ -81,6 +89,14 @@ import {getIdentifiersForNode} from "../../util/get-identifiers-for-node";
import {isKeywordTypeNode} from "../../util/is-keyword-type-node";
import {visitExportAssignment} from "./visitor/visit-export-assignment";
import {hasExportModifier} from "../../../declaration-transformers/util/modifier/modifier-util";
import {visitEnumDeclaration} from "./visitor/visit-enum-declaration";
import {visitEnumMember} from "./visitor/visit-enum-member";
import {visitPropertyAccessExpression} from "./visitor/visit-property-access-expression";
import {visitQualifiedName} from "./visitor/visit-qualified-name";
import {visitConditionalTypeNode} from "./visitor/visit-conditional-type-node";
import {visitTupleTypeNode} from "./visitor/visit-tuple-type-node";
import {visitOptionalTypeNode} from "./visitor/visit-optional-type-node";
import {visitCallSignatureDeclaration} from "./visitor/visit-call-signature-declaration";

/**
* Visits the given node. Returns true if it references the node to check for references, and false otherwise
Expand All @@ -90,7 +106,6 @@ import {hasExportModifier} from "../../../declaration-transformers/util/modifier
*/
function visitNode(currentNode: Node, options: VisitorOptions): void {
if (options.node === currentNode || nodeContainsChild(options.node, currentNode)) return;

if (isExportDeclaration(currentNode)) return visitExportDeclaration(currentNode, options);
else if (isExportAssignment(currentNode)) return visitExportAssignment(currentNode, options);
else if (isImportDeclaration(currentNode)) return visitImportDeclaration(currentNode, options);
Expand All @@ -101,6 +116,11 @@ function visitNode(currentNode: Node, options: VisitorOptions): void {
else if (isUnionTypeNode(currentNode)) return visitUnionTypeNode(currentNode, options);
else if (isIntersectionTypeNode(currentNode)) return visitIntersectionTypeNode(currentNode, options);
else if (isInterfaceDeclaration(currentNode)) return visitInterfaceDeclaration(currentNode, options);
else if (isEnumDeclaration(currentNode)) return visitEnumDeclaration(currentNode, options);
else if (isEnumMember(currentNode)) return visitEnumMember(currentNode, options);
else if (isConditionalTypeNode(currentNode)) return visitConditionalTypeNode(currentNode, options);
else if (isTupleTypeNode(currentNode)) return visitTupleTypeNode(currentNode, options);
else if (isPropertyAccessExpression(currentNode)) return visitPropertyAccessExpression(currentNode, options);
else if (isClassDeclaration(currentNode)) return visitClassDeclaration(currentNode, options);
else if (isLiteralTypeNode(currentNode)) return visitLiteralTypeNode(currentNode, options);
else if (isParenthesizedTypeNode(currentNode)) return visitParenthesizedTypeNode(currentNode, options);
Expand All @@ -115,6 +135,7 @@ function visitNode(currentNode: Node, options: VisitorOptions): void {
else if (isPropertySignature(currentNode)) return visitPropertySignature(currentNode, options);
else if (isPropertyDeclaration(currentNode)) return visitPropertyDeclaration(currentNode, options);
else if (isMethodDeclaration(currentNode)) return visitMethodDeclaration(currentNode, options);
else if (isCallSignatureDeclaration(currentNode)) return visitCallSignatureDeclaration(currentNode, options);
else if (isFunctionTypeNode(currentNode)) return visitFunctionTypeNode(currentNode, options);
else if (isMethodSignature(currentNode)) return visitMethodSignature(currentNode, options);
else if (isIndexSignatureDeclaration(currentNode)) return visitIndexSignatureDeclaration(currentNode, options);
Expand All @@ -127,6 +148,8 @@ function visitNode(currentNode: Node, options: VisitorOptions): void {
else if (isBindingElement(currentNode)) return visitBindingElement(currentNode, options);
else if (isObjectBindingPattern(currentNode) || isArrayBindingPattern(currentNode)) return visitBindingPattern(currentNode, options);
else if (isToken(currentNode)) return visitToken(currentNode, options);
else if (isQualifiedName(currentNode)) return visitQualifiedName(currentNode, options);
else if (currentNode.kind === SyntaxKind.OptionalType) return visitOptionalTypeNode(currentNode as OptionalTypeNode, options);

throw new TypeError(`Could not handle Node of kind: ${SyntaxKind[currentNode.kind]}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {CallSignatureDeclaration, isComputedPropertyName} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given CallSignatureDeclaration.
* @param {CallSignatureDeclaration} currentNode
* @param {VisitorOptions} options
*/
export function visitCallSignatureDeclaration(currentNode: CallSignatureDeclaration, {continuation}: VisitorOptions): void {
// Check if any of the type parameters references the Node
if (currentNode.typeParameters != null) {
for (const typeParameter of currentNode.typeParameters) {
continuation(typeParameter, currentNode);
}
}

// Check if any of the parameters references the node
if (currentNode.parameters != null) {
for (const parameter of currentNode.parameters) {
continuation(parameter, currentNode);
}
}

// Check if the type of the function references the Node
if (currentNode.type != null) {
continuation(currentNode.type, currentNode);
}

if (currentNode.name != null && isComputedPropertyName(currentNode.name)) {
continuation(currentNode.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ConditionalTypeNode} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given ConditionalTypeNode.
* @param {ConditionalTypeNode} currentNode
* @param {VisitorOptions} options
*/
export function visitConditionalTypeNode(currentNode: ConditionalTypeNode, {continuation}: VisitorOptions): void {
continuation(currentNode.checkType);
continuation(currentNode.extendsType);
continuation(currentNode.falseType);
continuation(currentNode.trueType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {EnumDeclaration} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given EnumDeclaration.
* @param {EnumDeclaration} currentNode
* @param {VisitorOptions} options
*/
export function visitEnumDeclaration(currentNode: EnumDeclaration, {continuation}: VisitorOptions): void {
for (const member of currentNode.members) {
continuation(member, currentNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {EnumMember, isComputedPropertyName} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given EnumMember.
* @param {EnumMember} currentNode
* @param {VisitorOptions} options
*/
export function visitEnumMember(currentNode: EnumMember, {continuation}: VisitorOptions): void {
// Check if the initializer references the Node
if (currentNode.initializer != null) {
continuation(currentNode.initializer);
}

// Check if the name is a binding pattern that references the identifier
if (isComputedPropertyName(currentNode.name)) {
continuation(currentNode.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import {VisitorOptions} from "../visitor-options";
* @param {InterfaceDeclaration} currentNode
* @param {VisitorOptions} options
*/
export function visitInterfaceDeclaration(currentNode: InterfaceDeclaration, {continuation, context}: VisitorOptions): void {
export function visitInterfaceDeclaration(currentNode: InterfaceDeclaration, {continuation}: VisitorOptions): void {
// Check if any of the heritage clauses references the Node
if (currentNode.heritageClauses != null) {
for (const heritageClause of currentNode.heritageClauses) {
for (const type of heritageClause.types) {
continuation(type, context);
continuation(type, currentNode);
}
}
}

// Check if any of the type parameters references the Node
if (currentNode.typeParameters != null) {
for (const typeParameter of currentNode.typeParameters) {
continuation(typeParameter, context);
continuation(typeParameter, currentNode);
}
}

// Check if any of the members references the Node
for (const member of currentNode.members) {
continuation(member, context);
continuation(member, currentNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {OptionalTypeNode} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given OptionalTypeNode.
* @param {OptionalTypeNode} currentNode
* @param {VisitorOptions} options
*/
export function visitOptionalTypeNode(currentNode: OptionalTypeNode, {continuation}: VisitorOptions): void {
continuation(currentNode.type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {PropertyAccessExpression} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given PropertyAccessExpression.
* @param {PropertyAccessExpression} currentNode
* @param {VisitorOptions} options
*/
export function visitPropertyAccessExpression(currentNode: PropertyAccessExpression, {continuation}: VisitorOptions): void {
continuation(currentNode.expression);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isIdentifier, PropertyDeclaration} from "typescript";
import {isComputedPropertyName, PropertyDeclaration} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
Expand All @@ -18,7 +18,7 @@ export function visitPropertyDeclaration(currentNode: PropertyDeclaration, {cont
}

// Check if the name is a binding pattern that references the identifier
if (!isIdentifier(currentNode.name)) {
if (isComputedPropertyName(currentNode.name)) {
continuation(currentNode.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {QualifiedName} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given QualifiedName.
* @param {QualifiedName} currentNode
* @param {VisitorOptions} options
*/
export function visitQualifiedName(currentNode: QualifiedName, {continuation}: VisitorOptions): void {
continuation(currentNode.left);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {TupleTypeNode} from "typescript";
import {VisitorOptions} from "../visitor-options";

/**
* Visits the given TupleTypeNode.
* @param {TupleTypeNode} currentNode
* @param {VisitorOptions} options
*/
export function visitTupleTypeNode(currentNode: TupleTypeNode, {continuation}: VisitorOptions): void {
for (const typeNode of currentNode.elementTypes) {
continuation(typeNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export function visitTypeAliasDeclaration(currentNode: TypeAliasDeclaration, {co
// Check if any of the type parameters references the Node
if (currentNode.typeParameters != null) {
for (const typeParameter of currentNode.typeParameters) {
continuation(typeParameter);
continuation(typeParameter, currentNode);
}
}

continuation(currentNode.type);
continuation(currentNode.type, currentNode);
}

0 comments on commit 6a90228

Please sign in to comment.