Skip to content

このページは翻訳です。英語版と内容が異なる場合は、英語版が正しい内容です。 English

構文木 ​

parsePug は具象構文木を返します。テンプレートの意味だけでなく、それがどう書かれているかも保持します。すべての省略記法、括弧、カンマ、クォート、行が範囲を持つので、ツールはそのどれでも指し示したり、ほかを変えずに 1 つだけ書き換えたりできます。

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 と indentUnit は、ファイルがすでにどの改行コードとインデントを使っているかをフォーマッターに伝えます。

ノード ​

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

ノードの range はそのノード自身の行を覆い、その下にネストされた行は含みません。ネストされた行は children にあります。ネストされた行を取れるノードは Tag、MixinCall、Code、Control です。

Tag ​

a.btn#go(href="/x" :title="t") Go は次のようにパースされます。

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: [/* … */] }],
}

インラインのテキスト(p Hi)、バッファ付きコード(p= msg)、ブロック展開の子(li: a)、ネストされた行は、すべて children に入ります。

MixinCall ​

+card(title, 2).wide(x="1"): タグと同様ですが、name(+ を除いた card)と args を持ちます。args は括弧を含む引数の範囲 (title, 2) です(括弧がなければ null)。タグと同じく shorthands、attributeBlocks、andAttributes、children を持ちます。

Text ​

ts
interface Text {
  type: 'Text'
  form: 'inline' | 'pipe' | 'block' | 'html'
  range: Range
  content: Range       // the text itself, after `| ` or the tag
  parts: TextPart[]
}
formソース
inlinep Hello(同じ行でタグの後ろにあるテキスト)
pipe| Hello(1 行につき 1 ノード)
blockp.、script. などの下の行
htmlリテラル HTML の行、<b>Hello</b>

parts は、テキストを生のテキストと補間に分割します。p Hello #{name}, #[b bold] and {{ x }} の場合は次のとおりです。

部品ソース
{ type: 'Raw' }Hello
{ type: 'Interpolation', escaped, expression }#{name}(!{…} は escaped: false)
{ type: 'Raw' },
{ type: 'TagInterpolation', tag }#[b bold]。tag は完全な Tag ノード
{ type: 'Raw' } and {{ x }}

{{ }} は分割されません。これは Vue のものなので、生のテキストの中に残ります。

Comment、Code、Control、Filter ​

ノードソースフィールド
Comment// shown、//- hidden、およびその下にネストされた行buffered(// は true、//- は false)、content
Code- const x = 1、= value、!= html、およびタグの子としての p= valuebuffered、escaped、expression、children
Controlif、else if、else、unless、each、for、while、case、when、default、mixin、block、append、prepend、include、extends、doctype、yieldkeyword、head(行の残り)、children
Filter:markdown-it と、そのテキスト行name、attributeBlocks、children(Text ノード)

属性 ​

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 ${…}
}

1 つのタグが複数のブロックを持つこともあります(a(x)(y) は有効な Pug です)。そのため attributeBlocks はリストで、通常は空か、1 つのエントリを持ちます。

kind条件例
string値の全体が 1 つの '…' または "…" リテラルhref="/x"
template値の全体が 1 つの `…` リテラルtitle=`Hi ${name}`
expressionそれ以外すべて。Pug が JavaScript としてコンパイルしますhref=url、title="a" + b

Vue にとって、string の値は、ディレクティブや属性が受け取るものです(:title="t" は t をバインドします)。expression の値は、Vue が見る前に、Pug がコンパイル時に評価します。

行 ​

doc.lines には、パースした範囲のソースの 1 行ごとに 1 つのエントリがあります。「この行には何があり、どの深さでインデントすべきか」に答えるもので、フォーマッターに必要ですが、木だけでは分かりません。

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
}

次のテンプレートの場合:

pug
ul#list
  li(
    v-for="x in xs"
    :key="x"
  ) {{ x }}
  p.
    some text
行kinddepth
ul#listnode0
li(node1
v-for="x in xs"attribute2
:key="x"attribute2
) {{ x }}paren-close1
p.node1
some textraw2
(最後の改行の後)blank0
kind行depth
nodeノードを始めるそのネストの深さ
attribute複数行の属性ブロックの中タグの深さ + 1
paren-closeブロックを閉じる ) で始まるタグの深さ
rawテキストブロック、コメント、コードブロック、フィルターの中所有者の深さ + 1
continuation上の行の続き(複数行の値)、またはエラーの後で読み飛ばされた行その行の深さ + 1
blank空、または空白だけ0

anchor は、depth に従うのではなく、別の行を基準にした列を保つべき行に設定されます。テキストブロックの 2 行目以降(最初の行を基準にします)と、continuation の行(続きとなる元の行を基準にします)です。次の例では、

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

two の行は one の行を基準にし、title の値の 2 行目はその上の行を基準にします。フォーマッターが基準の行を動かすときは、これらの行も同じ量だけ動かします。

MIT License のもとで公開されています。