Skip to main content
Module

x/deno2node/src/deps.deno.ts>CodeBlockWriter

Compile your Deno project to run on Node.js.
Go to Latest
class CodeBlockWriter
import { CodeBlockWriter } from "https://deno.land/x/deno2node@v1.7.1/src/deps.deno.ts";

Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.

Constructors

new
CodeBlockWriter(opts?: Partial<CodeBlockWriterOptions>)

Constructor.

Methods

blankLine(): this

Writes a blank line.

Writes a blank line if the last written text was not a blank line.

block(block?: () => void): this

Writes a block using braces.

closeComment(): this

Writes text to exit a comment if in a comment.

conditionalBlankLine(condition: boolean | undefined): this

Writes a blank line if the condition is true.

conditionalNewLine(condition: boolean | undefined): this

Writes a newline if the condition is true.

conditionalWrite(condition: boolean | undefined, textFunc: () => string): this

Conditionally writes text.

conditionalWrite(condition: boolean | undefined, text: string): this

Conditionally writes text.

conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this

Conditionally writes a line of text.

conditionalWriteLine(condition: boolean | undefined, text: string): this

Conditionally writes a line of text.

endsWith(text: string): boolean

Gets if the writer ends with the provided text.

Gets the current indentation level.

getLastChar(): string | undefined

Gets the last char written.

getLength(): number

Gets the length of the string in the writer.

Gets the options.

hangingIndent(action: () => void): this

Writes the text within the provided action with hanging indentation.

hangingIndentUnlessBlock(action: () => void): this

Writes the text within the provided action with hanging indentation unless writing a block.

indent(times?: number): this

Indents the code one level for the current line.

indent(block: () => void): this

Indents a block of code.

inlineBlock(block?: () => void): this

Writes an inline block with braces.

Gets if the writer is currently at the start of the first line of the text, block, or indentation block.

isInComment(): boolean

Gets if the writer is currently in a comment.

isInString(): boolean

Gets if the writer is currently in a string.

isLastBlankLine(): boolean

Gets if the last chars written were for a blank line.

isLastNewLine(): boolean

Gets if the last chars written were for a newline.

isLastSpace(): boolean

Gets if the last char written was a space.

isLastTab(): boolean

Gets if the last char written was a tab.

Gets if the writer is currently on the first line of the text, block, or indentation block.

iterateLastCharCodes<T>(action: (charCode: number, index: number) => T | undefined): T | undefined

Iterates over the writer character char codes in reverse order. The iteration stops when a non-null or undefined value is returned from the action. The returned value is then returned by the method.

iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined

Iterates over the writer characters in reverse order. The iteration stops when a non-null or undefined value is returned from the action. The returned value is then returned by the method.

newLine(): this

Writes a newline.

Writes a newline if the last line was not a newline.

queueIndentationLevel(indentationLevel: number): this

Queues the indentation level for the next lines written.

queueIndentationLevel(whitespaceText: string): this

Queues the indentation level for the next lines written using the provided indentation text.

quote(): this

Writes a quote character.

quote(text: string): this

Writes text surrounded in quotes.

setIndentationLevel(indentationLevel: number): this

Sets the current indentation level.

setIndentationLevel(whitespaceText: string): this

Sets the current indentation using the provided indentation text.

space(times?: number): this

Writes a space.

Writes a space if the last character was not a space.

tab(times?: number): this

Writes a tab.

tabIfLastNot(): this

Writes a tab if the last character was not a tab.

toString(): string

Gets the writer's text.

unsafeInsert(pos: number, text: string): this

Inserts text at the provided position.

This method is "unsafe" because it won't update the state of the writer unless inserting at the end position. It is biased towards being fast at inserting closer to the start or end, but slower to insert in the middle. Only use this if absolutely necessary.

withIndentationLevel(indentationLevel: number, action: () => void): this

Sets the indentation level within the provided action and restores the writer's indentation state afterwards.

withIndentationLevel(whitespaceText: string, action: () => void): this

Sets the indentation level with the provided indentation text within the provided action and restores the writer's indentation state afterwards.

write(text: string): this

Writes the provided text.

writeLine(text: string): this

Writes a line of text.