Parsing and linting
parse(), lint() and format() from @zplkit/core, and what they return.
import { parse, lint, format } from "@zplkit/core";Parsing ZPL and Linting explain how to use them, and every rule.
parse
parse(zpl: string): ParseResultReads ZPL into command nodes. Supported commands become typed nodes, and everything else is kept as Raw nodes, so no input is lost. Returns a ParseResult:
| Property | Type | Description |
|---|---|---|
commands | Command[] | Typed nodes for supported commands, Raw nodes for everything else |
diagnostics | Diagnostic[] | Parameters that couldn't be read, and text the printer skips |
Diagnostic
| Property | Type | Description |
|---|---|---|
severity | "warning" | Parse problems are always warnings: the printer still prints |
rule | "ignored-text" | "invalid-parameter" | A parameter that couldn't be read, or text the printer skips |
message | string | What's wrong, in a sentence |
span | Span | Where in the text, as character offsets |
lint
lint(commands: readonly Command[], media?: LintMedia): LintDiagnostic[]Finds problems the printer wouldn't report in commands from parse() or label.commands, and returns a list of LintDiagnostics. media is the printer's media size, for labels that don't set ^PW or ^LL:
| Property | Type | Description |
|---|---|---|
width? | number | Media width in dots |
length? | number | Media length in dots |
LintDiagnostic
| Property | Type | Description |
|---|---|---|
rule | LintRule | Which check found it; the linting docs explain each one |
severity | "error" | "warning" | Errors stop the label from printing as intended, warnings change how it prints |
message | string | What's wrong, in a sentence |
index | number | Position of the command in the list |
span? | Span | Position in the source text, for parsed ZPL |
serialize
serialize(commands: Command[], options?: { minify?: boolean }): stringTurns command nodes back into ZPL. Pass { minify: false } to put each command on its own line.
format
format(zpl: string): stringLays out ZPL to read: one field or setting per line, and a blank line between labels. Unlike serialize(), it keeps the ZPL as written, only moving line breaks, so the labels print the same.
format("^XA^PW406^FO20,20^A0N,40^FDHi^FS^XZ");
// ^XA
// ^PW406
// ^FO20,20^A0N,40^FDHi^FS
// ^XZ