Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private printHeader(writer: CodeBlockWriter, structure: OptionalKind) {
this.factory.forModifierableNode().printText(writer, structure);
writer.write(`class`);
// can be null, ex. `export default class { ... }`
if (!StringUtils.isNullOrWhitespace(structure.name))
writer.space().write(structure.name);
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
writer.space();
writer.hangingIndent(() => {
if (structure.extends != null) {
const extendsText = this.getText(writer, structure.extends);
if (!StringUtils.isNullOrWhitespace(extendsText))
writer.write(`extends ${extendsText} `);
}
if (structure.implements != null) {
const implementsText = structure.implements instanceof Array
? structure.implements.map(i => this.getText(writer, i)).join(", ")
: this.getText(writer, structure.implements);
renameAlias(alias: string) {
if (StringUtils.isNullOrWhitespace(alias)) {
this.removeAliasWithRename();
return this;
}
let aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null) {
// trick is to insert an alias with the same name, then rename the alias. TS compiler will take care of the rest.
this.setAlias(this.getName());
aliasIdentifier = this.getAliasNode()!;
}
aliasIdentifier.rename(alias);
return this;
}
printText(writer: CodeBlockWriter, structure: ReturnTypedNodeStructure) {
let { returnType } = structure;
if (returnType == null && this.alwaysWrite === false)
return;
returnType = returnType ?? "void";
const returnTypeText = this.getText(writer, returnType);
if (!StringUtils.isNullOrWhitespace(returnTypeText)) {
writer.hangingIndent(() => {
writer.write(`: ${returnTypeText}`);
});
}
}
}
setAlias(alias: string) {
if (StringUtils.isNullOrWhitespace(alias)) {
this.removeAlias();
return this;
}
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null) {
insertIntoParentTextRange({
insertPos: this.getNameNode().getEnd(),
parent: this,
newText: ` as ${alias}`
});
}
else {
aliasIdentifier.replaceWithText(alias);
}
function getDefaultExtractedName(name: string | undefined, classDec: ClassDeclaration) {
name = StringUtils.isNullOrWhitespace(name) ? undefined : name;
return name || classDec.getName() || classDec.getSourceFile().getBaseNameWithoutExtension().replace(/[^a-zA-Z0-9_$]/g, "");
}
setAlias(alias: string) {
if (StringUtils.isNullOrWhitespace(alias)) {
this.removeAlias();
return this;
}
const aliasIdentifier = this.getAliasNode();
if (aliasIdentifier == null) {
insertIntoParentTextRange({
insertPos: this.getNameNode().getEnd(),
parent: this,
newText: ` as ${alias}`
});
}
else {
aliasIdentifier.replaceWithText(alias);
}
printText(writer: CodeBlockWriter, structure: TypedNodeStructure) {
let { type } = structure;
if (type == null && this.alwaysWrite === false)
return;
type = type ?? "any";
const typeText = this.getText(writer, type);
if (!StringUtils.isNullOrWhitespace(typeText)) {
writer.hangingIndent(() => {
writer.write(`${this.separator} ${typeText}`);
});
}
}
}
setInitializer(textOrWriterFunction: string | WriterFunction) {
const text = getTextFromStringOrWriter(this._getWriterWithQueuedIndentation(), textOrWriterFunction);
if (StringUtils.isNullOrWhitespace(text)) {
this.removeInitializer();
return this;
}
const initializer = this.getInitializer();
if (initializer != null) {
initializer.replaceWithText(text);
return this;
}
insertIntoParentTextRange({
insertPos: this.getNameNode().getEnd(),
parent: this,
newText: `=${text}`
});