Skip to content

Esta página es una traducción. Si difiere de la página en inglés, prevalece la página en inglés. English

Árbol de sintaxis ​

parsePug devuelve un árbol de sintaxis concreto: además de lo que significa una plantilla, conserva cómo está escrita. Cada atajo, paréntesis, coma, comilla y línea tiene un rango, así que una herramienta puede señalar cualquiera de ellos, o reescribir uno sin tocar el resto.

PugDocument ​

ts
interface PugDocument {
  type: 'Document'
  text: string             // the text passed to parsePug
  range: Range             // [start, end) that was parsed
  children: PugNode[]      // the top-level nodes
  errors: PugParseError[]  // see Errors
  lines: PugLine[]         // one entry per source line, see Lines
  eol: '\n' | '\r\n'       // the first line ending found; '\n' if there is none
  indentUnit: string       // the indentation of the first nested line, relative to its parent; '  ' if none
}

eol e indentUnit indican a un formateador qué final de línea e indentación usa ya el archivo.

Nodos ​

ts
type PugNode = Tag | MixinCall | Text | Comment | Code | Control | Filter

El range de un nodo abarca su(s) propia(s) línea(s), no las líneas anidadas bajo él; esas están en children. Los nodos que admiten líneas anidadas son Tag, MixinCall, Code y Control.

Tag ​

a.btn#go(href="/x" :title="t") Go se analiza como:

js
{
  type: 'Tag',
  range: [0, 33],                                   // the whole head line
  name: { value: 'a', range: [0, 1] },              // null for an implicit div: .card
  shorthands: [
    { kind: 'class', value: 'btn', range: [1, 5] }, // range includes the '.'
    { kind: 'id', value: 'go', range: [5, 8] },     // and the '#'
  ],
  attributeBlocks: [{ range: [8, 30], attributes: [/* see Attributes */], commas: [] }],
  andAttributes: [],                                // &attributes(obj): { range, expression }
  selfClose: null,                                  // the '/' of img/
  dot: null,                                        // the '.' of p. (text block)
  expansion: null,                                  // the ':' of li: a
  children: [{ type: 'Text', form: 'inline', range: [31, 33], content: [31, 33], parts: [/* … */] }],
}

El texto en línea (p Hi), el código con salida (p= msg), el hijo de una expansión de bloque (li: a) y las líneas anidadas van todos a children.

MixinCall ​

+card(title, 2).wide(x="1"): como una etiqueta, con name (card, sin el +) y args, el rango de los argumentos con sus paréntesis, (title, 2) (null sin ellos). Tiene shorthands, attributeBlocks, andAttributes y children igual que una etiqueta.

Text ​

ts
interface Text {
  type: 'Text'
  form: 'inline' | 'pipe' | 'block' | 'html'
  range: Range
  content: Range       // the text itself, after `| ` or the tag
  parts: TextPart[]
}
formCódigo fuente
inlinep Hello (texto tras una etiqueta en la misma línea)
pipe| Hello (un nodo por línea)
blocklas líneas bajo p., script., …
htmluna línea de HTML literal, <b>Hello</b>

parts divide el texto en texto sin procesar e interpolaciones. Para p Hello #{name}, #[b bold] and {{ x }}:

ParteCódigo fuente
{ type: 'Raw' }Hello
{ type: 'Interpolation', escaped, expression }#{name} (!{…} tiene escaped: false)
{ type: 'Raw' },
{ type: 'TagInterpolation', tag }#[b bold], con tag un nodo Tag completo
{ type: 'Raw' } and {{ x }}

{{ }} no se divide: pertenece a Vue y permanece en el texto sin procesar.

Comment, Code, Control, Filter ​

NodoCódigo fuenteCampos
Comment// shown, //- hidden y las líneas anidadas bajo ellosbuffered (// lo es, //- no), content
Code- const x = 1, = value, != html y p= value como hijo de una etiquetabuffered, escaped, expression, children
Controlif, else if, else, unless, each, for, while, case, when, default, mixin, block, append, prepend, include, extends, doctype, yieldkeyword, head (el resto de la línea), children
Filter:markdown-it y sus líneas de textoname, attributeBlocks, children (nodos Text)

Atributos ​

ts
interface AttributeBlock {
  range: Range        // includes '(' and ')'
  attributes: Attribute[]
  commas: Range[]     // the commas between attributes, if any
}

interface Attribute {
  range: Range
  key: { raw: string; range: Range }                  // href, :title, @click, v-model.trim
  operator: { value: '=' | '!='; range: Range } | null // null for a boolean attribute: disabled
  value: AttributeValue | null
}

interface AttributeValue {
  range: Range
  raw: string
  kind: 'string' | 'template' | 'expression'
  quote: '"' | "'" | '`' | null
  inner: Range | null       // between the quotes; null for an expression
  hasSubstitution: boolean  // a template literal with ${…}
}

Una etiqueta puede tener más de un bloque (a(x)(y) es Pug válido), así que attributeBlocks es una lista; normalmente está vacía o tiene una entrada.

kindCuándoEjemplo
stringel valor completo es un único literal '…' o "…"href="/x"
templateel valor completo es un único literal `…`title=`Hi ${name}`
expressioncualquier otra cosa: Pug la compila como JavaScripthref=url, title="a" + b

Para Vue, un valor string es lo que recibe la directiva o el atributo (:title="t" enlaza t); un valor expression lo evalúa Pug en tiempo de compilación, antes de que Vue lo vea.

Líneas ​

doc.lines tiene una entrada por cada línea de código fuente del rango analizado. Responde a «qué hay en esta línea y a qué profundidad debe indentarse», algo que un formateador necesita y que el árbol por sí solo no dice.

ts
interface PugLine {
  range: Range          // the line, without its line ending
  indent: Range         // its leading whitespace
  kind: 'node' | 'attribute' | 'paren-close' | 'raw' | 'continuation' | 'blank'
  depth: number
  anchor: number | null // index into lines; see below
}

Para esta plantilla:

pug
ul#list
  li(
    v-for="x in xs"
    :key="x"
  ) {{ x }}
  p.
    some text
Líneakinddepth
ul#listnode0
li(node1
v-for="x in xs"attribute2
:key="x"attribute2
) {{ x }}paren-close1
p.node1
some textraw2
(tras el último salto de línea)blank0
kindLíneadepth
nodeinicia un nodosu profundidad de anidamiento
attributedentro de un bloque de atributos multilíneala profundidad de la etiqueta + 1
paren-closeempieza con el ) que cierra un bloquela profundidad de la etiqueta
rawdentro de un bloque de texto, comentario, bloque de código o filtrola profundidad del propietario + 1
continuationcontinúa una línea anterior (un valor multilínea), o se omite tras un errorla profundidad de esa línea + 1
blankvacía o solo con espacios en blanco0

anchor se establece en las líneas que deben mantener su columna relativa a otra línea en lugar de seguir depth: las líneas de un bloque de texto posteriores a la primera (ancladas a esa primera línea) y las líneas de continuación (ancladas a la línea que continúan). En

pug
p.
  one
    two
p(title=`a
  b`) x

la línea two está anclada a la línea one, y la segunda línea del valor de title, a la línea anterior. Cuando un formateador mueve la línea ancla, mueve estas en la misma cantidad.

Publicado bajo la licencia MIT.