Vue ESLint Parser Pug Template Tokenizer
This package, `vue-eslint-parser-template-tokenizer-pug`, serves as an internal tokenizer specifically designed for `vue-eslint-parser` to handle Pug (formerly Jade) templates within Vue Single File Components. Its primary function is to convert Pug syntax within `<template lang="pug">` blocks into a format that `vue-eslint-parser` can then process to generate an Abstract Syntax Tree (AST) for linting. It is currently at version 1.0.0. While actively maintained, it is *not* intended for direct end-user consumption. Users who wish to lint Pug templates in Vue applications should instead use `eslint-plugin-vue-pug`, which leverages this library internally. Its release cadence is tied to the needs of `eslint-plugin-vue-pug` and `vue-eslint-parser`, meaning updates are driven by functional requirements rather than a fixed schedule.
Common errors
-
ESLint: Cannot find module 'vue-eslint-parser-template-tokenizer-pug'
cause The package is not installed or the path in `parserOptions.templateTokenizer` is incorrect.fixEnsure `vue-eslint-parser-template-tokenizer-pug` is listed in your project's `devDependencies` and installed (`npm install` or `yarn install`). Verify the import path is correct if used directly, or that `eslint-plugin-vue-pug` is correctly configured. -
Error: Cannot read properties of undefined (reading 'templateBody')
cause Attempting to lint a `.vue` file without `vue-eslint-parser` configured as the main parser, or providing an invalid `templateTokenizer` object.fixEnsure `vue-eslint-parser` is correctly set as the `parser` in your ESLint configuration and that `parserOptions.templateTokenizer.pug` points to this package (usually handled by `eslint-plugin-vue-pug`).
Warnings
- gotcha This is an internal library and is not intended for direct use by end-user applications. For linting Pug templates in Vue components, use `eslint-plugin-vue-pug` instead.
- gotcha The README mentions that adding errors to `templateBody.errors` might not affect linting, suggesting potential limitations in how parsing errors from the tokenizer are surfaced by `eslint-plugin-vue`'s `no-parsing-error` rule.
- breaking `vue-eslint-parser`'s `parserOptions.templateTokenizer` is an experimental feature and may change or be removed in minor versions of `vue-eslint-parser` without notice. This could impact how this tokenizer is configured and used.
Install
-
npm install vue-eslint-parser-template-tokenizer-pug -
yarn add vue-eslint-parser-template-tokenizer-pug -
pnpm add vue-eslint-parser-template-tokenizer-pug
Imports
- parseTemplate
const { parseTemplate } = require('vue-eslint-parser-template-tokenizer-pug');import { parseTemplate } from 'vue-eslint-parser-template-tokenizer-pug'; - TemplateTokenizerFactory
import type { TemplateTokenizerFactory } from 'vue-eslint-parser-template-tokenizer-pug';
Quickstart
import { ESLint } from 'eslint';
import vueParser from 'vue-eslint-parser';
import pugTokenizer from 'vue-eslint-parser-template-tokenizer-pug';
const cli = new ESLint({
useEslintrc: false,
overrideConfig: {
parser: vueParser,
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
templateTokenizer: {
pug: pugTokenizer
}
},
rules: {
'vue/no-unused-vars': 'error'
}
}
});
async function lintPugVue() {
const code = `<template lang="pug">\n div.my-component\n p Hello, Pug in Vue!\n span {{ someVar }}\n</template>\n<script>\nexport default { data: () => ({ someVar: 'test' }) }\n</script>`;
const results = await cli.lintText(code, { filePath: 'test.vue' });
console.log('Linting Results:');
results.forEach(result => {
console.log(`File: ${result.filePath}`);
result.messages.forEach(message => {
console.log(` Line ${message.line}, Col ${message.column}: ${message.message} (${message.ruleId})`);
});
if (result.errorCount > 0) {
console.error(` Errors: ${result.errorCount}`);
}
});
}
lintPugVue().catch(console.error);