Skip to content

Commit

Permalink
feat: Add .getSignature() method to nodes that have a return type.
Browse files Browse the repository at this point in the history
This is kind of lazily added on this mixin for now.
  • Loading branch information
dsherret committed Sep 30, 2018
1 parent bab0860 commit f65c529
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,10 @@ export interface ReturnTypedNode {
* Removes the return type.
*/
removeReturnType(): this;
/**
* Gets the signature of the node from the type checker.
*/
getSignature(): Signature;
}

declare type ReturnTypedNodeExtensionType = Node<ts.SignatureDeclaration>;
Expand Down
38 changes: 23 additions & 15 deletions src/compiler/base/ReturnTypedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Constructor, WriterFunction } from "../../types";
import { SyntaxKind, ts } from "../../typescript";
import { getTextFromStringOrWriter, StringUtils } from "../../utils";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { Node, Signature } from "../common";
import { Type } from "../type/Type";
import { TypeNode } from "../type/TypeNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
Expand Down Expand Up @@ -34,15 +34,16 @@ export interface ReturnTypedNode {
* Removes the return type.
*/
removeReturnType(): this;
/**
* Gets the signature of the node from the type checker.
*/
getSignature(): Signature;
}

export function ReturnTypedNode<T extends Constructor<ReturnTypedNodeExtensionType>>(Base: T): Constructor<ReturnTypedNode> & T {
return class extends Base implements ReturnTypedNode {
getReturnType() {
const signature = this.context.typeChecker.getSignatureFromNode(this);
if (signature == null)
throw new errors.NotImplementedError("Expected the index signature to have a type checker signature.");
return signature.getReturnType();
return this.getSignature().getReturnType();
}

getReturnTypeNode() {
Expand Down Expand Up @@ -81,6 +82,23 @@ export function ReturnTypedNode<T extends Constructor<ReturnTypedNodeExtensionTy
}
}

removeReturnType() {
const returnTypeNode = this.getReturnTypeNode();
if (returnTypeNode == null)
return this;

const colonToken = returnTypeNode.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
removeChildren({ children: [colonToken, returnTypeNode], removePrecedingSpaces: true });
return this;
}

getSignature() {
const signature = this.context.typeChecker.getSignatureFromNode(this);
if (signature == null)
throw new errors.NotImplementedError("Expected the node to have a signature.");
return signature;
}

set(structure: Partial<ReturnTypedNodeStructure>) {
callBaseSet(Base.prototype, this, structure);

Expand All @@ -92,16 +110,6 @@ export function ReturnTypedNode<T extends Constructor<ReturnTypedNodeExtensionTy
return this;
}

removeReturnType() {
const returnTypeNode = this.getReturnTypeNode();
if (returnTypeNode == null)
return this;

const colonToken = returnTypeNode.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
removeChildren({ children: [colonToken, returnTypeNode], removePrecedingSpaces: true });
return this;
}

getStructure() {
const returnTypeNode = this.getReturnTypeNode();
return callBaseGetStructure<ReturnTypedNodeStructure>(Base.prototype, this, {
Expand Down

0 comments on commit f65c529

Please sign in to comment.