• Stars
    star
    152
  • Rank 244,685 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code writer for JavaScript and TypeScript code.

code-block-writer

npm version CI deno doc stable

Code writer for JavaScript and TypeScript code.

With Deno:

import CodeBlockWriter from "https://deno.land/x/code_block_writer/mod.ts";

Or with Node:

npm install --save code-block-writer

Example

import CodeBlockWriter from "https://deno.land/x/code_block_writer/mod.ts";

const writer = new CodeBlockWriter({
  // optional options
  newLine: "\r\n",         // default: "\n"
  indentNumberOfSpaces: 2, // default: 4
  useTabs: false,          // default: false
  useSingleQuote: true     // default: false
});

writer.write("class MyClass extends OtherClass").block(() => {
  writer.writeLine(`@MyDecorator(1, 2)`);
  writer.write(`myMethod(myParam: any)`).block(() => {
    writer.write("return this.post(").quote("myArgument").write(");");
  });
});

console.log(writer.toString());

Outputs (using "\r\n" for newlines):

class MyClass extends OtherClass {
  @MyDecorator(1, 2)
  myMethod(myParam: any) {
    return this.post('myArgument');
  }
}

Methods

  • block(block?: () => void) - Indents all the code written within and surrounds it in braces.
  • inlineBlock(block?: () => void) - Same as block, but doesn't add a space before the first brace and doesn't add a newline at the end.
  • getLength() - Get the current number of characters.
  • writeLine(text: string) - Writes some text and adds a newline.
  • newLine() - Writes a newline.
  • newLineIfLastNot() - Writes a newline if what was written last wasn't a newline.
  • blankLine() - Writes a blank line. Does not allow consecutive blank lines.
  • blankLineIfLastNot() - Writes a blank line if what was written last wasn't a blank line.
  • quote() - Writes a quote character.
  • quote(text: string) - Writes text surrounded in quotes.
  • indent(times?: number) - Indents the current line. Optionally indents multiple times when providing a number.
  • indent(block?: () => void) - Indents a block of code.
  • space(times?: number) - Writes a space. Optionally writes multiple spaces when providing a number.
  • spaceIfLastNot() - Writes a space if the last was not a space.
  • tab(times?: number) - Writes a tab. Optionally writes multiple tabs when providing a number.
  • tabIfLastNot() - Writes a tab if the last was not a tab.
  • write(text: string) - Writes some text.
  • conditionalNewLine(condition: boolean) - Writes a newline if the condition is matched.
  • conditionalBlankLine(condition: boolean) - Writes a blank line if the condition is matched.
  • conditionalWrite(condition: boolean, text: string) - Writes if the condition is matched.
  • conditionalWrite(condition: boolean, textFunc: () => string) - Writes if the condition is matched.
  • conditionalWriteLine(condition: boolean, text: string) - Writes some text and adds a newline if the condition is matched.
  • conditionalWriteLine(condition: boolean, textFunc: () => string) - Writes some text and adds a newline if the condition is matched.
  • setIndentationLevel(indentationLevel: number) - Sets the current indentation level.
  • setIndentationLevel(whitespaceText: string) - Sets the current indentation level based on the provided whitespace text.
  • withIndentationLevel(indentationLevel: number, action: () => void) - Sets the indentation level within the provided action.
  • withIndentationLevel(whitespaceText: string, action: () => void) - Sets the indentation level based on the provided whitespace text within the action.
  • getIndentationLevel() - Gets the current indentation level.
  • queueIndentationLevel(indentationLevel: number) - Queues an indentation level to be used once a new line is written.
  • queueIndentationLevel(whitespaceText: string) - Queues an indentation level to be used once a new line is written based on the provided whitespace text.
  • hangingIndent(action: () => void) - Writes the code within the action with hanging indentation.
  • hangingIndentUnlessBlock(action: () => void) - Writes the code within the action with hanging indentation unless a block is written going from the first line to the second.
  • closeComment() - Writes text to exit a comment if in a comment.
  • unsafeInsert(pos: number, text: string) - Inserts text into the writer. This will not update the writer's state. Read more in its jsdoc.
  • isInComment() - Gets if the writer is currently in a comment.
  • isAtStartOfFirstLineOfBlock() - Gets if the writer is currently at the start of the first line of the text, block, or indentation block.
  • isOnFirstLineOfBlock() - Gets if the writer is currently on the first line of the text, block, or indentation block.
  • isInString() - Gets if the writer is currently in a string.
  • isLastNewLine() - Gets if the writer last wrote a newline.
  • isLastBlankLine() - Gets if the writer last wrote a blank line.
  • isLastSpace() - Gets if the writer last wrote a space.
  • isLastTab() - Gets if the writer last wrote a tab.
  • getLastChar() - Gets the last character written.
  • endsWith(text: string) - Gets if the writer ends with the provided text.
  • iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined - Iterates over the writer's characters in reverse order, stopping once a non-null or undefined value is returned and returns that value.
  • iterateLastCharCodes<T>(action: (charCode: number, index: number) => T | undefined): T | undefined - A slightly faster version of iterateLastChars that doesn't allocate a string per character.
  • getOptions() - Gets the writer options.
  • toString() - Gets the string.

Other Features

  • Does not indent within strings.
  • Escapes newlines within double and single quotes created with .quote(text).

C# Version

See CodeBlockWriterSharp.

More Repositories

1

ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
TypeScript
4,452
star
2

ts-ast-viewer

TypeScript AST viewer.
TypeScript
1,126
star
3

dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
TypeScript
984
star
4

ts-nameof

nameof in TypeScript
TypeScript
486
star
5

conditional-type-checks

Types for testing TypeScript types.
TypeScript
448
star
6

ts-type-info

TypeScript AST and code generator [Deprecated]
TypeScript
93
star
7

ts-factory-code-generator-generator

Generates code that generates TypeScript Compiler API factory code from an AST.
TypeScript
86
star
8

using_statement

"Using statement" in JavaScript and TypeScript.
TypeScript
68
star
9

rust-toolchain-file

GitHub Action to install Rust's toolchain via rust-toolchain.toml
28
star
10

dts-minify

Minifier for TypeScript declaration files (.d.ts)
TypeScript
24
star
11

barrel-maintainer

Automated real-time maintenance of barrels in JavaScript and TypeScript.
TypeScript
19
star
12

deno-which

Finds the path to the specified command in Deno.
TypeScript
14
star
13

console_static_text

Logging for text that should stay in the same place in a console.
Rust
13
star
14

jsr-publish-on-tag

Publishes a package to JSR with a version based on the current git tag.
TypeScript
13
star
15

Units-of-Measure-Proposal-for-TypeScript

11
star
16

server-bridge

Code generation for a statically typed bridge between the client and server in TypeScript
TypeScript
10
star
17

npm_bridge

Proof of concept for a bridge between Deno and npm packages
TypeScript
7
star
18

faster_prettier_example

TypeScript
6
star
19

which-runtime

Deno module for checking which runtime the code is running in.
TypeScript
6
star
20

CodeBlockWriterSharp

Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code in c#.
C#
6
star
21

npm-to-deno-workspace-example

TypeScript
4
star
22

text_lines

Information about lines of text in a string.
Rust
2
star
23

ts-object-create

Code generation that writes functions for creating objects with their types.
TypeScript
2
star
24

npm_bridge_example

Example of using npm bridge
TypeScript
2
star
25

libpack

Module concatenator for large Deno libraries.
Rust
2
star
26

slow-wasm-example

Rust
1
star
27

tsconf-talk

Code and slides for my talk at tsconf 2018.
TypeScript
1
star
28

ts-state-test-generator

Generates test helper functions for checking the state of a class or interface [abandoned]
TypeScript
1
star
29

path

Path class for JavaScript built on top of Deno's standard library.
TypeScript
1
star