Skip to content

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

エラー ​

parsePug は例外を投げません。パースできないものは doc.errors のエントリになり、テンプレートの残りは引き続きパースされます。

ts
interface PugParseError {
  code: string     // one of the codes below
  message: string  // for people; may change between versions
  offset: number   // where the problem is, as an offset into text
}

message ではなく code で判定してください。行と列を表示するには、オフセットから数えます。

js
const before = text.slice(0, error.offset)
const line = before.split('\n').length                    // 1-based
const column = error.offset - before.lastIndexOf('\n')    // 1-based

回復 ​

エラーが起きてもパースは止まりません。パーサーは不正な構文の終わりか行末まで読み飛ばし、次の行から続けます。エラーの後で配置できない行(子を持てない構文の下にネストされた行など)は、種類 continuation として doc.lines に残り、ノードは作られません。

js
parsePug('div\n  p(class="a)\n  span ok\n  li(a=1 a=2)\n').errors
// [
//   { code: 'unterminated-string', message: 'Unterminated string', offset: 14 },
//   { code: 'duplicate-attribute', message: 'Duplicate attribute "a"', offset: 37 },
// ]

ESLint プラグインは、エラーのあるテンプレートでは、それを検証できないため、その中で fix を一切適用しません。

コード ​

code例メッセージ
unterminated-stringp(title="hi) xUnterminated string
unclosed-attributesp(title="hi"Attribute block is not closed with ")"
unclosed-arguments+card(1, 2Mixin arguments are not closed with ")"
missing-attribute-valuep(title=) xExpected a value after the operator
duplicate-attributep(a="1" a="2")Duplicate attribute "a"
unexpected-characterp(a!b) xExpected "=" after "!"
unbalanced-keyp([a="1") xUnclosed "[" in an attribute key
bad-escapep(a="\x")Invalid escape sequence in a JavaScript string
inconsistent-indentationスペースでインデントされた行の中にタブでインデントされた行があるInvalid indentation, you can use tabs or spaces but not both
unterminated-interpolationp Hi #{nameInterpolation is not closed with "}"
unsupported-syntax#{tag} xUnsupported syntax: "#{tag} x"

各コードは問題のグループをカバーします。unexpected-character、unbalanced-key、inconsistent-indentation、unsupported-syntax には複数のメッセージがあります。

これらは Pug コンパイラが拒否するものと一致しますが、意図的な違いが 1 つあります。'…' や "…" の属性値の中の改行は、JavaScript と同じく unterminated-string になります。複数行の値にはバッククォートを使ってください。

unsupported-syntax ​

このコードは、パーサーが扱わない有効な Pug を示します。たとえば補間されたタグ名(#{tag})や、タグのないテキストブロックです。また、いくつかの書き方はエラーなしでパースされますが、Pug とは異なる解釈になります。どちらの一覧も未対応の Pug 構文にあります。

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