ts

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ts provides utilities for generating TypeScript code in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Identifier

type Identifier string

Identifier is a TypeScript identifier string.

It can be used as Source, and since is of string kind it can also be useful as an identifier in code working with TypeScript.

func (Identifier) String

func (i Identifier) String() string

type Property

type Property struct {
	Name  string
	Value Source
}

Property is a named value for use with Object.

func (Property) AsSource

func (p Property) AsSource() Source

AsSource represents the property as a `name: value` pair.

type Source

type Source interface {
	// String renders the source as TypeScript, complete with import statements.
	String() string
	// contains filtered or unexported methods
}

Source represents TypeScript source code.

Source tracks imports separately for the rest of the source text so that imports of combined Source values can be deduplicated and hoisted to the top of the source. It is indentation-aware, so that nested Sources produce human-friendly output.

func Array

func Array(elements ...Source) Source

Array outputs the given elements surrounded by `[` and `]` and interspersed with `,`. It gives reasonable line-breaking, whitespace and indentation.

func AsSource

func AsSource(str string) Source

func DocComment

func DocComment(comment string) Source

DocComment renders the given string as a multiline `/** … */`-style doc-comment. It is indentation-aware.

func ImportedName

func ImportedName(module string, name Identifier) Source

ImportedName returns a Source representing a name that has been imported from a module.

func InvokeFunction

func InvokeFunction(function Source, arguments ...Source) Source

InvokeFunction follows the function by a parenthesized comma-separated list of arguments. It gives reasonable line-breaking, whitespace and indentation.

func InvokeMethod

func InvokeMethod(receiver Source, name Identifier, arguments ...Source) Source

InvokeMethod follows the receiver by a `.`, the method name and parenthesized comma-separated list of arguments. It gives reasonable line-breaking, whitespace and indentation.

func NumberLiteral

func NumberLiteral[N constraints.Integer | constraints.Float](value N) Source

NumberLiteral returns literal Source for the given value.

func Object

func Object(properties ...Property) Source

Object outputs the given properties as `name: value`-pairs surrounded by `{` and `}` and interspersed with `,`. It gives reasonable line-breaking, whitespace and indentation.

func RegexLiteral

func RegexLiteral(re *regexp.Regexp) Source

RegexLiteral represents the given regexp as a TypeScript regex literal. TODO there is missing escaping.

func Sourcef

func Sourcef(format string, a ...Source) Source

Sourcef turns format into indentation-aware source, replacing `%s` placeholders in it with the given arguments in an indentation-aware way.

func StatementGroups

func StatementGroups(blankLinesBetweenGroups int, groups ...Source) Source

StatementGroups chains groups of statements with the given number of blank lines in between them.

func Statements

func Statements(statements ...Source) Source

Statements chains the given statements together with indentation-aware newlines.

func StringEscape

func StringEscape(str string) Source

StringEscape escapes the given string for inclusion in a TypeScript string literal. TODO there is missing escaping.

func StringLiteral

func StringLiteral(str string) Source

StringLiteral represents the given string as a TypeScript string literal. TODO there is missing escaping.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL