vibator namespace
This document is the reference of the vibator namespace and all its
subnamespaces, properties, and functions as they are provided by the base
vibator framework.
Namespaces
- project: Navigate the files and folders in this project.
- ts: Parse and manipulate TypeScript files.
- json: Parse JSON files.
- text: Parse and manipulate files as plain text.
- ignore: Check ignore markers for a rule at line, node, or file level.
- git: Gateway to git functionality.
- shell: Gateway to the shell.
- package: Parse and manage
package.jsonfiles. - module: Resolve module specifiers to files.
- glob: Match paths against globs.
A plugin adds its own namespaces. See Extending the namespace.
project
Navigate the files and folders in this project, and write to them.
| Declaration | Description |
|---|---|
| root: string | The absolute path of the project root. |
| files: FileSet | Every file in scope for the current run. |
| folders: FolderSet | Every top-level folder in the project. |
| exists(path: string): boolean | Reports whether a path exists on disk, whether or not discovery lists it. |
| write(path: string, content: string): void | Writes content to an absolute path, creating the file when the path is new and overwriting it when the path exists. |
ts
Parse and manipulate TypeScript files.
| Declaration | Description |
|---|---|
| parse(file: File): Ast | Parses a TypeScript or JavaScript file into a syntax tree. |
| program(tsconfig: File): Program | Builds a type-checked program from a tsconfig file. |
json
Parse JSON files.
| Declaration | Description |
|---|---|
| parse(file: File): unknown | Parses a JSON file into its value. |
| keys(value: unknown): string[] | Flattens a value into its dotted key paths. |
text
Parse and manipulate files as plain text.
| Declaration | Description |
|---|---|
| lines(file: File): Line[] | Splits a file into numbered lines. |
| maskComments(file: File): string | Returns the file content with JavaScript and TypeScript comments blanked, keeping line positions. |
| maskCode(file: File): string | Returns the file content with Markdown code fences and spans blanked, keeping line positions. |
| positionAt(file: File, offset: number): Position | Resolves a character offset to a line and column. |
| matches(file: File, pattern: RegExp): Match[] | Returns every match of a pattern with its position. |
| search(files: FileSet, pattern: RegExp): Hit[] | Returns every match of a pattern across a set of files, each with the path of its file. |
| binary(file: File): boolean | Reports whether the file content is binary. |
ignore
Check whether an ignore marker silences a rule. A marker reads
vibator-ignore <rule-id>: <reason> on the line above the finding, or
vibator-ignore-file <rule-id>: <reason> anywhere in the file. The reason is
optional.
| Declaration | Description |
|---|---|
| line(file: File, line: number, rule: string): boolean | Reports whether a marker on the line above names the rule. |
| node(node: Node, rule: string): boolean | Reports whether a marker above the node or an enclosing class, function, method, interface, enum, or module names the rule. |
| file(file: File, rule: string): boolean | Reports whether a file-level marker names the rule. |
git
Gateway to git functionality.
| Declaration | Description |
|---|---|
| isRepo(): boolean | Reports whether the project root sits inside a git repository. |
| files(): string[] | The tracked and untracked-tracked-eligible paths git keeps. |
| untrackedFiles(): string[] | The untracked paths git keeps. |
| changedFiles(): string[] | The paths this working tree changed against HEAD, present on disk. |
| stagedFiles(): string[] | The paths staged for the next commit, present on disk. |
| changedSince(base: string): string[] | The paths this branch changed since it diverged from base. |
| status(paths: string[]): StatusEntry[] | The working-tree status of the given paths. |
| restore(paths: string[]): void | Restores the given tracked paths from the index. |
shell
Gateway to the shell.
| Declaration | Description |
|---|---|
| run(command: string, options?: ShellOptions): ShellResult | Runs a command and returns its outcome. Runs from the root with no timeout by default. |
package
Parse and manage package.json files.
| Declaration | Description |
|---|---|
| root: PackageManifest | The parsed root package.json. |
| parse(file: File): PackageManifest | Parses a package.json file into a manifest. |
module
Resolve module references and specifiers to files.
| Declaration | Description |
|---|---|
| resolve(specifier: string, from?: string): string | Resolves a reference to an absolute file path, from from. |
The three reference forms, in resolution order:
| Form | Example | Meaning |
|---|---|---|
| Absolute path | /abs/rules.ts |
The path as it is. |
Starts with . |
./rules.ts, .vibator/docs/rule.md |
A local file, joined onto the directory of from. |
| Anything else | zod, @acme/rules |
A package specifier resolved by Node, honoring the exports map. |
glob
Match paths against globs; prefix a glob with ! to exclude.
| Declaration | Description |
|---|---|
| matches(path: string, globs: string | string[]): boolean | Whether the path matches the globs and none of the exclusions. |
Extending the namespace
A plugin adds a namespace when its module is imported. Rules then read it as
vibator.<name>.
| Declaration | Description |
|---|---|
| defineNamespace(name: string, value: Value): Value | Adds a namespace to vibator and returns it. A name vibator already has is an error. |
| interface Vibator | The type of vibator. A plugin adds its namespace to it with declare module "vibator". |
import { defineNamespace } from "vibator";
export const audit = defineNamespace("audit", { count: () => 3 });
declare module "vibator" {
interface Vibator {
audit: typeof audit;
}
}
A rule's file sees the declared type once it imports the plugin's types, such
as import type {} from "@acme/audit";.
Types
The objects returned and accepted across the namespaces above.
FileSet
An ordered collection of files, with glob filtering and lookup by path. It is
iterable, so a rule can loop over it with for...of.
| Declaration | Description |
|---|---|
| match(glob: string | string[]): FileSet | Filters the set to files matching the glob or globs. |
| get(path: string): File | The file at an absolute path. |
| length: number | The count of files in the set. |
| forEach(visit: (file: File, index: number) => void): void | Runs a function over each file in order. |
| map(transform: (file: File, index: number) => T): T[] | Maps each file to a value. |
| flatMap(transform: (file: File, index: number) => T[]): T[] | Maps each file to a list of values and flattens the lists. |
| filter(keep: (file: File) => boolean): FileSet | Returns the files that satisfy a predicate. |
| find(match: (file: File) => boolean): File | undefined | Returns the first file that satisfies a predicate. |
| paths(): string[] | The absolute paths in the set, sorted. |
File
One file in the project.
| Declaration | Description |
|---|---|
| path: string | The absolute path, forward-slashed. |
| name: string | The file base name. |
| ext: string | The file extension, including the dot. |
| content: string | The file content decoded as UTF-8 text. |
| bytes: Buffer | The raw file content as bytes. |
FolderSet
An ordered collection of folders, with glob filtering and lookup by path.
| Declaration | Description |
|---|---|
| match(glob: string | string[]): FolderSet | Filters the set to folders matching the glob or globs. |
| get(path: string): Folder | undefined | The folder at an absolute path. |
| length: number | The count of folders in the set. |
| forEach(visit: (folder: Folder, index: number) => void): void | Runs a function over each folder in order. |
| map(transform: (folder: Folder, index: number) => T): T[] | Maps each folder to a value. |
| filter(keep: (folder: Folder) => boolean): FolderSet | Returns the folders that satisfy a predicate. |
| find(match: (folder: Folder) => boolean): Folder | undefined | Returns the first folder that satisfies a predicate. |
| paths(): string[] | The absolute paths in the set, sorted. |
Folder
One folder in the project.
| Declaration | Description |
|---|---|
| path: string | The absolute path, forward-slashed. |
| name: string | The folder base name. |
| files: FileSet | The files directly inside this folder. |
| folders: FolderSet | The folders directly inside this folder. |
Ast
The syntax tree of a parsed file.
| Declaration | Description |
|---|---|
| source: SourceFile | The TypeScript source file. |
| nodes: NodeCursor[] | Every node, flattened in source order. |
| lineAt(offset: number): number | The 1-based line a character offset falls on. |
NodeCursor
One node in a syntax tree with its position.
| Declaration | Description |
|---|---|
| node: Node | The TypeScript node. |
| line: number | The 1-based line the node starts on. |
Program
A type-checked TypeScript program.
| Declaration | Description |
|---|---|
| checker: TypeChecker | The program type checker. |
| files: FileSet | The files included in the program. |
| ast(file: File): Ast | The syntax tree of a file in the program. |
Line
One line of text with its number.
| Declaration | Description |
|---|---|
| number: number | The 1-based line number. |
| text: string | The line content. |
Position
A line and column in a file.
| Declaration | Description |
|---|---|
| line: number | The 1-based line. |
| column: number | The 1-based column. |
Match
One match of a pattern with its position.
| Declaration | Description |
|---|---|
| text: string | The matched text. |
| index: number | The character offset of the match. |
| line: number | The 1-based line the match starts on. |
| column: number | The 1-based column the match starts on. |
| groups: string[] | The captured groups. |
Hit
One match of a pattern in a file of a set. It has every field of Match and the path of its file.
| Declaration | Description |
|---|---|
| file: string | The absolute path of the file the match is in. |
StatusEntry
The git status of one path.
| Declaration | Description |
|---|---|
| path: string | The repo-relative path. |
| staged: boolean | True when the path holds staged changes. |
| unstaged: boolean | True when the path holds unstaged changes. |
| untracked: boolean | True when git tracks the path for the first time. |
ShellOptions
The options for a shell command.
| Declaration | Description |
|---|---|
| cwd: string | The working directory, relative to the project root. |
| timeoutMs: number | The time a command runs before it is stopped, in milliseconds. |
ShellResult
The outcome of a shell command.
| Declaration | Description |
|---|---|
| ok: boolean | True when the command exits with code 0. |
| stdout: string | The captured standard output. |
| stderr: string | The captured standard error. |
| code: number | The exit code. |
PackageManifest
A parsed package.json.
| Declaration | Description |
|---|---|
| name: string | The package name. |
| version: string | The package version. |
| scripts: Record<string, string> | The scripts, keyed by name. |
| dependencies: Record<string, string> | The runtime dependencies, keyed by name. |
| devDependencies: Record<string, string> | The development dependencies, keyed by name. |
| peerDependencies: Record<string, string> | The peer dependencies, keyed by name. |
External types
Types provided by other packages.
| Type | Source |
|---|---|
| Buffer | Node.js |
| SourceFile | TypeScript |
| Node | TypeScript |
| TypeChecker | TypeScript |