Skip to content

Esta página é uma tradução. Se ela divergir da página em inglês, vale a página em inglês. English

Árvore de sintaxe ​

O parsePug devolve uma árvore de sintaxe concreta: além do que um template significa, ela guarda como ele está escrito. Todo atalho, parêntese, vírgula, aspa e linha tem um range, então uma ferramenta pode apontar para qualquer um deles, ou reescrever um sem tocar no 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 dizem a um formatador qual quebra de linha e qual indentação o arquivo já usa.

Nós ​

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

O range de um nó cobre a(s) própria(s) linha(s) dele, não as linhas aninhadas sob ele; essas estão em children. Os nós que aceitam linhas aninhadas são Tag, MixinCall, Code e Control.

Tag ​

a.btn#go(href="/x" :title="t") Go é analisado 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: [/* … */] }],
}

O texto inline (p Hi), o código com buffer (p= msg), o filho de uma expansão de bloco (li: a) e as linhas aninhadas vão todos para children.

MixinCall ​

+card(title, 2).wide(x="1"): como uma tag, com name (card, sem o +) e args, o range dos argumentos com seus parênteses, (title, 2) (null sem eles). Tem shorthands, attributeBlocks, andAttributes e children como uma tag.

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-fonte
inlinep Hello (texto depois de uma tag, na mesma linha)
pipe| Hello (um nó por linha)
blockas linhas sob p., script., …
htmluma linha de HTML literal, <b>Hello</b>

parts divide o texto em texto bruto e interpolações. Para p Hello #{name}, #[b bold] and {{ x }}:

ParteCódigo-fonte
{ type: 'Raw' }Hello
{ type: 'Interpolation', escaped, expression }#{name} (!{…} tem escaped: false)
{ type: 'Raw' },
{ type: 'TagInterpolation', tag }#[b bold], com tag sendo um nó Tag completo
{ type: 'Raw' } and {{ x }}

{{ }} não é dividido: pertence ao Vue e fica no texto bruto.

Comment, Code, Control, Filter ​

NóCódigo-fonteCampos
Comment// shown, //- hidden, e as linhas aninhadas sob elesbuffered (// é, //- não é), content
Code- const x = 1, = value, != html, e p= value como filho de uma tagbuffered, escaped, expression, children
Controlif, else if, else, unless, each, for, while, case, when, default, mixin, block, append, prepend, include, extends, doctype, yieldkeyword, head (o resto da linha), children
Filter:markdown-it, e suas linhas de textoname, attributeBlocks, children (nós 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 ${…}
}

Uma tag pode ter mais de um bloco (a(x)(y) é Pug válido), então attributeBlocks é uma lista; normalmente ela está vazia ou tem uma entrada.

kindQuandoExemplo
stringo valor inteiro é um único literal '…' ou "…"href="/x"
templateo valor inteiro é um único literal `…`title=`Hi ${name}`
expressionqualquer outra coisa: o Pug a compila como JavaScripthref=url, title="a" + b

Para o Vue, um valor string é o que a diretiva ou o atributo recebe (:title="t" faz o bind de t); um valor expression é avaliado pelo Pug em tempo de compilação, antes de o Vue vê-lo.

Linhas ​

doc.lines tem uma entrada por linha do código-fonte do trecho analisado. Ela responde "o que há nesta linha, e com que profundidade ela deve ser indentada", o que um formatador precisa e a árvore sozinha não diz.

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 este template:

pug
ul#list
  li(
    v-for="x in xs"
    :key="x"
  ) {{ x }}
  p.
    some text
Linhakinddepth
ul#listnode0
li(node1
v-for="x in xs"attribute2
:key="x"attribute2
) {{ x }}paren-close1
p.node1
some textraw2
(depois da última quebra de linha)blank0
kindLinhadepth
nodeinicia um nóa profundidade de aninhamento dele
attributedentro de um bloco de atributos de várias linhasa profundidade da tag + 1
paren-closecomeça com o ) que fecha um blocoa profundidade da tag
rawdentro de um bloco de texto, comentário, bloco de código ou filtroa profundidade do dono + 1
continuationcontinua uma linha acima (um valor de várias linhas), ou é ignorada depois de um erroa profundidade dessa linha + 1
blankvazia ou só com espaços em branco0

anchor é definido em linhas que devem manter sua coluna relativa a outra linha em vez de seguir depth: as linhas de um bloco de texto depois da primeira (ancoradas a essa primeira linha) e as linhas de continuação (ancoradas à linha que continuam). Em

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

a linha two está ancorada à linha one, e a segunda linha do valor de title à linha acima dela. Quando um formatador move a linha-âncora, ele move estas pela mesma quantidade.

Publicado sob a licença MIT.