Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
}
}
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;
function getSeparatorSyntaxKindForNode(node: Node) {
switch (node.getKind()) {
case SyntaxKind.TypeAliasDeclaration:
return SyntaxKind.EqualsToken;
default:
return SyntaxKind.ColonToken;
}
}
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();
}
}
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;
}
removeReturnType() {
const returnTypeNode = this.getReturnTypeNode();
if (returnTypeNode == null)
return this;
const colonToken = returnTypeNode.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
removeChildren({ children: [colonToken, returnTypeNode], removePrecedingSpaces: true });
return this;
}
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();
}
}