Node.js · API
DON
Clase con un único método estático, parse,
que convierte texto DON en un árbol de
Directive.
class DON {
static parse(text: string, options?: DONParseOptions): Directive;
}
interface DONParseOptions {
plugins?: DonPlugin[];
} Valor devuelto
-
Con varias directivas de primer nivel, una
raíz sintética cuyo
namees el símboloROOT_DIRECTIVE_NAMEy que las contiene enchildren. - Con una sola, esa misma directiva.
- Con texto vacío o solo comentarios, una raíz sin hijos.
import { DON } from "donly";
const root = DON.parse(`
name "my-app"
server { host "example.com" }
`);
String(root.name); // 'Symbol(root)' (raíz sintética)
root.children.length; // 2
root.children[0].args; // [ 'my-app' ] DON.parse('name "x"').name; // 'name' (la única directiva)
DON.parse("").children; // [] (raíz vacía)
DON.parse("# comentario").children; // [] Opciones
plugins es una lista de
DonPlugin. Se ejecutan sobre cada directiva mientras se parsea el documento.
import { DON } from "donly";
import { scopedVariablesPlugin } from "donly/plugins/scoped-variables";
const root = DON.parse(text, { plugins: [scopedVariablesPlugin] }); Errores
El texto mal formado (un string sin cerrar, un bloque sin cerrar, tokens
después de }…) lanza un
DonSyntaxError, subclase de
SyntaxError que se importa desde
donly/common/errors. Permite distinguir un
fallo de DON de cualquier otro error.
import { DON } from "donly";
import { DonSyntaxError } from "donly/common/errors";
try {
DON.parse('a "x');
} catch (error) {
error instanceof DonSyntaxError; // true
error.name; // 'DonSyntaxError'
error.message; // 'Unclosed string at Ln 0, Col 2 ("")'
}