Documentation
¶
Overview ¶
Package parse turns TypeScript source text into the canonical AST.
Frontend is the contract; two implementations exist:
- TypeFrontend, here, parses the full type grammar plus interface, type alias, and enum declarations with no dependencies.
- github.com/lilybw/go-solid-compiler/tsx.Frontend wraps the real TypeScript compiler and handles the entire language including TSX.
Use the tsx frontend by default. Use TypeFrontend when the type grammar is all you need and the compiler dependency is not worth it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func File ¶
func File(name string, src []byte) (*ast.SourceFile, error)
File parses a module with the default frontend.
Types ¶
type Capability ¶
type Capability uint32
Capability reports what a frontend supports.
const ( // CapTypes covers the type grammar. CapTypes Capability = 1 << iota // CapDeclarations covers interface, type alias, enum, class, and function // declarations. CapDeclarations // CapExpressions covers the full expression and statement grammar. CapExpressions // CapJSX covers .tsx syntax. CapJSX // CapComments preserves comments as attached documentation. CapComments // CapPositions records accurate source positions on every node. CapPositions )
type Capable ¶
type Capable interface {
Capabilities() Capability
}
Capable is implemented by frontends that can describe their own coverage.
type ErrorList ¶
type ErrorList []Error
ErrorList is a collection of syntax errors.
type Frontend ¶
type Frontend interface {
// ParseFile parses a whole module.
ParseFile(name string, src []byte) (*ast.SourceFile, error)
// ParseType parses a single type expression, the operation a code
// generator needs when accepting a type as configuration.
ParseType(src []byte) (ast.Type, error)
}
Frontend converts TypeScript source into the canonical AST.
Implementations return the most complete tree they can alongside any errors, so that partial results remain useful.
var Default Frontend = TypeFrontend{}
Default is the frontend used by the package-level helpers.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner turns TypeScript source into tokens.
func NewScanner ¶
NewScanner returns a Scanner over src.
func (*Scanner) ScanTemplateContinue ¶
ScanTemplateContinue resumes scanning a template literal at a closing brace. The parser calls it after consuming a substitution expression.
type Token ¶
type Token struct {
Kind Tok
Text string
Span token.Span
// Raw is the undecoded source text, retained for literals so that a
// numeric type literal round-trips its original spelling.
Raw string
// NewlineBefore records whether a line terminator preceded this token,
// which TypeScript's grammar is sensitive to in several places.
NewlineBefore bool
// Docs holds JSDoc comment blocks immediately preceding this token.
Docs []string
}
Token is a lexed token. Text is the source spelling for identifiers and punctuation, and the decoded value for string and template literals.
type TypeFrontend ¶
type TypeFrontend struct{}
TypeFrontend parses the TypeScript type grammar plus interface, type alias, enum, import, and export declarations.
It does not parse the expression and statement grammar; anything it does not recognize is preserved verbatim as an ast.RawStmt and reported.
func (TypeFrontend) Capabilities ¶
func (TypeFrontend) Capabilities() Capability
Capabilities reports what this frontend covers.
func (TypeFrontend) ParseFile ¶
func (TypeFrontend) ParseFile(name string, src []byte) (*ast.SourceFile, error)
ParseFile parses a module.