GitHub 0
Skip to content
glosslint is in preview and not on npm yet. Version 0.0.1 will be the first release.

JavaScript API

Reference for glosslint's JavaScript API: every package entry point, exported function signature, preset export, CLI binary, and TypeScript type.

glosslint ships five JavaScript package entry points plus a CLI binary. Use this reference when you call the matcher, formatters, glossary-table reader, or corpus tools from Node scripts, custom linters, or build pipelines, or when you wire up ESLint and x18n.

The main entry point exports corpus builders, the matcher, string kind classification, finding formatters, corpus utilities, GlosslintConfigError, CONFIG_ERRORS, and all core TypeScript types.

function defineCorpus(input: CorpusInput): Corpus
ts

Validates every field in input, merges corpora listed in extends from left to right before applying input.terms, and returns a frozen flat Corpus. Throws GlosslintConfigError on an invalid corpus or term. See The corpus for merge order and field details.

function fromGlossaryTable(path: string | URL, columns?: GlossaryTableColumns): Corpus
ts

Reads the file at path synchronously and turns the first Markdown table with a Use or Term column, or the custom header names in columns, into a Corpus. Throws GlosslintConfigError with GLOSSLINT_NO_GLOSSARY_TABLE if no matching table exists, or GLOSSLINT_DUPLICATE_TERM if the table lists the same term twice. See Glossary tables.

function createMatcher(corpus: Corpus, options?: MatcherOptions): Matcher
ts

Compiles corpus into a reusable Matcher with a readonly corpus property and a check(text, kind?) method. Pass { identifierAvoid: true } in options to report avoid words when checking text as kind "identifier".

function check(matcher: Matcher, text: string, kind?: TextKind): Finding[]
ts

Calls matcher.check(text, kind) and returns every finding in text, read as kind, which defaults to "unknown". See Findings format for the returned Finding shape.

function kindOf(site: KindSite): TextKind
ts

Returns glosslint's default TextKind for a string from its key, attribute, or element name, its closing punctuation, and, when shown is true, its length and capitalisation. Adapters use this function unless their custom kindOf option returns a kind first.

function formatFinding(finding: Finding | CliFinding, options?: FormatOptions): string
ts

Formats one Finding or CliFinding in the tree format, starting with the headline and followed by why, fix, term, and see branches when present. Set options.color to true for ANSI colors; color is off by default.

function formatFindings(findings: (Finding | CliFinding)[], options?: FormatOptions): string
ts

Formats every finding in the tree format separated by blank lines and appends a summary line. Pass options.files to include the file count in the summary, and options.color for ANSI colors.

function toJson(findings: CliFinding[], files: number): CliReport
ts

Builds the { findings, summary } object printed by glosslint check --format json. Counts total findings, total files, and findings per FindingCode.

function testCorpus(corpus: Corpus): CorpusTestFailure[]
ts

Checks every term's examples.good and examples.bad strings using createMatcher(corpus) and kindOf({ text, shown: true }). Returns a CorpusTestFailure for each good example flagged for its term and each bad example not flagged for its term.

function printCorpus(corpus: Corpus, format: "md" | "json"): string
ts

Formats corpus as a Markdown glossary ("md") or pretty-printed JSON containing { terms, allow } ("json"). In Markdown, terms with more than a spelling get their own ## section, and bare spelling-only terms appear together under ## Spelling.

function explain(corpus: Corpus, query: string): string
ts

Looks up what corpus says about a word or phrase, matching terms, abbreviations, locale forms, avoid words, matcher findings, or mentions, and formats each match with its description, nuance, avoid list, examples, links, and related terms. Returns No term in the corpus matches "<query>". when nothing matches.

function toX18nGlossary(corpus: Corpus): X18nGlossaryEntry[]
ts

Converts each term in corpus into an X18nGlossaryEntry with term, translations, forms, a note combining description and nuance, and doNotTranslate: true for exact terms without translations.

class GlosslintConfigError extends Error {
  readonly code: string;
  readonly why: string;
  readonly fix: string;
  readonly term?: string;
  constructor(options: { code: string; why: string; fix: string; term?: string });
}
ts

Error class thrown by defineCorpus and fromGlossaryTable on an invalid corpus or glossary table. See Config errors for every error code and fix.

const CONFIG_ERRORS: {
  readonly unknownField: "GLOSSLINT_C001";
  readonly missingTerm: "GLOSSLINT_C002";
  readonly badCase: "GLOSSLINT_C003";
  readonly avoidIsTerm: "GLOSSLINT_C004";
  readonly duplicateTerm: "GLOSSLINT_C005";
  readonly badSkip: "GLOSSLINT_C006";
  readonly badType: "GLOSSLINT_C007";
  readonly notCorpus: "GLOSSLINT_C008";
  readonly badCasing: "GLOSSLINT_C011";
};
ts

Maps corpus validation error names to their stable GlosslintConfigError codes.

Provides the ESLint flat config factory and its GlosslintEslintOptions type. Requires the optional peer dependency eslint (>=9.15.0). See ESLint for setup and rule behavior.

export default function glosslint(options: GlosslintEslintOptions): Linter.Config[]
ts

Returns a single-element array containing an ESLint flat config object named "glosslint" with glosslint/term, glosslint/avoid, and glosslint/comment set to "error" and glosslint/identifier set to "warn". It sets no parser, so your ESLint config supplies typescript-eslint, vue-eslint-parser, or yaml-eslint-parser for the files you lint.

Provides the x18n plugin factory and its GlosslintX18nOptions type. Requires the optional peer dependency @x18n/core (>=0.1.14). See x18n for setup and fix behavior.

export default function glosslint(options: GlosslintX18nOptions): Plugin
ts

Returns an @x18n/core plugin named "glosslint" that adds the checks glosslint-term ("error"), glosslint-avoid ("warning"), and glosslint-case ("error") over source-locale messages and hard-coded strings. Importing this entry point also augments @x18n/core's CheckIds interface with those three check IDs.

Exports a default Corpus containing 477 product and technology names generated from case-police 2.2.1 (each an exact term with skip: ["lower", "upper"]) and 7 joined spellings in allow. Pass it inside extends when calling defineCorpus. See Built-in presets.

declare const corpus: Corpus;
export default corpus;
ts

Exports a default Corpus containing 37 inclusive-wording terms with reasons on each avoid word, inline examples, and an empty allow list. Pass it inside extends when calling defineCorpus. See Built-in presets.

declare const corpus: Corpus;
export default corpus;
ts

The package registers one executable in package.json bin: glosslint (./dist/cli.mjs). It is not listed under package.json exports, so run it from your shell with npx glosslint or pnpm exec glosslint.

It provides glosslint [check] [...paths] (default command), glosslint test, glosslint print, and glosslint explain <query>. See CLI for all flags and exit codes.

Every type below is exported from glosslint, except GlosslintEslintOptions (from glosslint/eslint) and GlosslintX18nOptions (from glosslint/x18n).

  • Avoid: Object form of an avoid entry, { word: string; why?: string }.
  • CaseMode: How capitals are checked on a term: "exact" | "spelling" | "context".
  • CaseStyle: Case style for a context term: "title" | "sentence" | "lower" | "upper" | "as-written".
  • Casing: Partial<Record<CasingKind, CaseStyle>> mapping text kinds to case styles.
  • CasingKind: Text kinds that accept a case style: "label" | "header" | "sentence".
  • CliFinding: Finding returned by the CLI, adding file, line, column, fix.result, and TermSummary in place of the full Term.
  • CliReport: JSON structure printed by glosslint check --format json and returned by toJson, holding { findings, summary }.
  • Corpus: Validated, flat corpus returned by defineCorpus, with kind: "glosslint.corpus", terms, allow, optional casing, and optional wholeLabel.
  • CorpusInput: Configuration object accepted by defineCorpus, with optional extends, terms, allow, casing, and wholeLabel.
  • CorpusTestFailure: One failing term example from testCorpus, with term, example, expected ("finding" | "none"), and findings.
  • Finding: One match returned by Matcher.check, with code, start, end, text, message, full term, and optional why, fix, and see.
  • FindingCode: Union of the three finding codes: "GLOSSLINT_TERM" | "GLOSSLINT_AVOID" | "GLOSSLINT_CASE".
  • Fix: Replacement attached to a finding, { kind: FixKind; replace: string }.
  • FixKind: Action safety level of a fix: "autofix" | "suggestion" | "rename".
  • FormatOptions: Options for formatFinding and formatFindings: { color?: boolean; files?: number }.
  • GlossaryTableColumns: Optional custom column headers for fromGlossaryTable: { term?, avoid?, notes?, description?, links? }.
  • GlosslintEslintOptions: Options for glosslint/eslint: { corpus, files?, ignoreCallees?, identifierAvoid?, kindOf? }.
  • GlosslintX18nOptions: Options for glosslint/x18n: { corpus, sourceLocale?, kindOf? }.
  • KindOf: Custom kind classifier (site: KindSite) => TextKind | undefined.
  • KindSite: String context passed to kindOf: { text: string; name?: string; file?: string; shown?: boolean }.
  • Link: Reference link on a term, { title: string; url: string }.
  • Matcher: Compiled corpus returned by createMatcher, exposing readonly corpus and check(text, kind?).
  • MatcherOptions: Options for createMatcher: { identifierAvoid?: boolean }.
  • Term: Corpus entry with required term and optional description, nuance, abbr, avoid, examples, links, case, casing, skip, forms, and translations.
  • TermSummary: Pick<Term, "term" | "description" | "nuance">, carried on each CliFinding.
  • TextKind: Context of a checked string: "label" | "header" | "sentence" | "comment" | "identifier" | "unknown".
  • X18nGlossaryEntry: Entry produced by toX18nGlossary with term and optional translations, forms, note, and doNotTranslate.