How to use the @ts-morph/common.SyntaxKind.ColonToken function in @ts-morph/common

To help you get started, we’ve selected a few @ts-morph/common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / utils / CommentNodeParser.ts View on Github external
if (ts.isClassDeclaration(container)
            || ts.isEnumDeclaration(container)
            || ts.isInterfaceDeclaration(container)
            || ts.isTypeLiteralNode(container)
            || ts.isClassExpression(container)
            || ts.isBlock(container)
            || ts.isModuleBlock(container)
            || ts.isObjectLiteralExpression(container))
        {
            // this function is only used when there are no statements or members, so only do this
            return getTokenEnd(container, SyntaxKind.OpenBraceToken);
        }

        if (ts.isCaseClause(container) || ts.isDefaultClause(container))
            return getTokenEnd(container, SyntaxKind.ColonToken);

        return errors.throwNotImplementedForNeverValueError(container);

        function getTokenEnd(node: ts.Node, kind: SyntaxKind.OpenBraceToken | SyntaxKind.ColonToken) {
            // @code-fence-allow(getChildren): Ok, not searching for comments.
            const token = node.getChildren(sourceFile).find(c => c.kind === kind);
            if (token == null)
                throw new errors.NotImplementedError(`Unexpected scenario where a(n) ${getSyntaxKindName(kind)} was not found.`);
            return token.end;
        }
    }
}
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / expression / object / PropertyAssignment.ts View on Github external
removeInitializer(): ShorthandPropertyAssignment {
        const initializer = this.getInitializerOrThrow();
        const colonToken = initializer.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
        const childIndex = this.getChildIndex();
        const sourceFileText = this._sourceFile.getFullText();
        const insertPos = this.getStart();
        const newText = sourceFileText.substring(insertPos, colonToken.getPos()) + sourceFileText.substring(initializer.getEnd(), this.getEnd());
        const parent = this.getParentSyntaxList() || this.getParentOrThrow();

        insertIntoParentTextRange({
            insertPos,
            newText,
            parent,
            replacing: {
                textLength: this.getWidth()
            }
        });

        return parent.getChildAtIndexIfKindOrThrow(childIndex, SyntaxKind.ShorthandPropertyAssignment) as ShorthandPropertyAssignment;
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / TypedNode.ts View on Github external
function getSeparatorSyntaxKindForNode(node: Node) {
    switch (node.getKind()) {
        case SyntaxKind.TypeAliasDeclaration:
            return SyntaxKind.EqualsToken;
        default:
            return SyntaxKind.ColonToken;
    }
}
github dsherret / ts-morph / packages / ts-morph / src / manipulation / helpers / getInsertPosFromIndex.ts View on Github external
export function getInsertPosFromIndex(index: number, syntaxList: SyntaxList, children: Node[]) {
    if (index === 0) {
        const parent = syntaxList.getParentOrThrow();
        if (TypeGuards.isSourceFile(parent))
            return 0;
        else if (TypeGuards.isCaseClause(parent) || TypeGuards.isDefaultClause(parent)) {
            const colonToken = parent.getFirstChildByKindOrThrow(SyntaxKind.ColonToken);
            return colonToken.getEnd();
        }

        const isInline = syntaxList !== parent.getChildSyntaxList();
        if (isInline)
            return syntaxList.getStart();

        const parentContainer = getParentContainer(parent);
        const openBraceToken = parentContainer.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
        return openBraceToken.getEnd();
    }
    else {
        return children[index - 1].getEnd();
    }
}
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / ExclamationTokenableNode.ts View on Github external
setHasExclamationToken(value: boolean) {
            const exclamationTokenNode = this.getExclamationTokenNode();
            const hasExclamationToken = exclamationTokenNode != null;

            if (value === hasExclamationToken)
                return this;

            if (value) {
                if (TypeGuards.isQuestionTokenableNode(this))
                    this.setHasQuestionToken(false);

                const colonNode = this.getFirstChildByKind(SyntaxKind.ColonToken);
                if (colonNode == null)
                    throw new errors.InvalidOperationError("Cannot add an exclamation token to a node that does not have a type.");

                insertIntoParentTextRange({
                    insertPos: colonNode.getStart(),
                    parent: this,
                    newText: "!"
                });
            }
            else {
                removeChildren({ children: [exclamationTokenNode!] });
            }

            return this;
        }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / ReturnTypedNode.ts View on Github external
removeReturnType() {
            const returnTypeNode = this.getReturnTypeNode();
            if (returnTypeNode == null)
                return this;

            const colonToken = returnTypeNode.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
            removeChildren({ children: [colonToken, returnTypeNode], removePrecedingSpaces: true });
            return this;
        }
github dsherret / ts-morph / packages / ts-morph / src / compiler / ast / base / QuestionTokenableNode.ts View on Github external
function getInsertPos(this: QuestionTokenableNode & Node) {
                if (TypeGuards.hasName(this))
                    return this.getNameNode().getEnd();

                const colonNode = this.getFirstChildByKind(SyntaxKind.ColonToken);
                if (colonNode != null)
                    return colonNode.getStart();

                const semicolonToken = this.getLastChildByKind(SyntaxKind.SemicolonToken);
                if (semicolonToken != null)
                    return semicolonToken.getStart();

                return this.getEnd();
            }
        }