Skip to content

Errors ​

parsePug never throws. What it cannot parse becomes an entry in doc.errors, and the rest of the template is still parsed:

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
}

Match on code, not on message. To show a line and column, count from the offset:

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

Recovery ​

An error does not stop the parse. The parser skips to the end of the bad construct, or to the end of the line, and goes on with the next line. Lines that cannot be placed after an error, such as lines nested under a construct that cannot have children, are kept in doc.lines with the kind continuation, and no node is built for them.

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 },
// ]

The ESLint plugin applies no fix inside a template that has errors, since it cannot check them.

Codes ​

codeExampleMessage
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-indentationa tab-indented line among space-indented onesInvalid 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"

Each code covers a family of problems; unexpected-character, unbalanced-key, inconsistent-indentation and unsupported-syntax have several messages.

These match what the Pug compiler rejects, with one deliberate difference: a line break inside a '…' or "…" attribute value is unterminated-string, as in JavaScript. Use backticks for multi-line values.

unsupported-syntax ​

This code marks valid Pug that the parser does not handle, such as interpolated tag names (#{tag}) or text blocks without a tag. A few forms are also parsed without an error but differently from Pug. Both lists are in Unsupported Pug syntax.

Released under the MIT License.