Skip to content

Vue の Pug テンプレートのための ESLint

<template lang="pug"> で vue/* rule を動かし、eslint --fix で Pug を整形し、すべてのテンプレートを壊さずに保ちます。各 fix は適用前に検証されます。

sh
npm i -D eslint-plugin-pug-vue
UserCard.vue
<template lang="pug">div.card(·class="shadow"·,·:title·=·"title"·)····h2(·v-if='title'·)·{{·title·}}····input(v-model="name"·,·type='text'·disabled)/</template>
15 件の問題波線にカーソルを合わせると警告を読めます。
eslint の警告 15 件すべて
  • 2:1 Omit `div` before a class or id shorthand vue-pug/implicit-div
  • 2:10 Unexpected space after '(' vue-pug/attribute-parens-spacing
  • 2:11 Use the '.shadow' shorthand instead of the class attribute vue-pug/prefer-shorthand
  • 2:26 Unexpected comma in attribute block vue-pug/attribute-commas
  • 2:34 Unexpected space around '=' vue-pug/attribute-equals-spacing
  • 2:36 Unexpected space around '=' vue-pug/attribute-equals-spacing
  • 2:44 Unexpected space before ')' vue-pug/attribute-parens-spacing
  • 3:1 Expected indentation of 2 spaces but found 4 spaces vue-pug/indent
  • 3:8 Unexpected space after '(' vue-pug/attribute-parens-spacing
  • 3:14 Expected double quotes vue-pug/attribute-quotes
  • 3:21 Unexpected space before ')' vue-pug/attribute-parens-spacing
  • 4:1 Expected indentation of 2 spaces but found 4 spaces vue-pug/indent
  • 4:26 Unexpected comma in attribute block vue-pug/attribute-commas
  • 4:33 Expected double quotes vue-pug/attribute-quotes
  • 4:49 Unexpected self-closing `/` vue-pug/self-closing

Pug 上で動く vue/* rule ​

eslint-plugin-vue の rule は、いつもの名前とオプションのまま Pug テンプレート上で動きます。key の欠落、未使用の変数、属性の順序、非推奨の構文などを検出します。Pug では動作しない一部の rule は、無効にするか、Pug テンプレートの中でだけ出力を抑えます。そのため HTML テンプレートに対するチェックは引き続き働きます。

Pug 上で動く rule

vue
<template lang="pug">
ul
  li(v-for="todo in todos") {{ todo.title }}
</template>
txt
3:3  error  Elements in iteration expect to have 'v-bind:key' directives  vue/require-v-for-key

eslint --fix による整形 ​

12 個の小さな vue-pug/* rule が Pug そのものを整形します。インデント、属性ブロックのスペース・カンマ・クォート・改行、そして .class / #id の省略記法です。どの rule にもオプションがあり、不要なものは無効にできます。Prettier は不要で、保存時に ESLint と競合する別のフォーマッターもありません。

すべてのルールを見る

js
// eslint.config.js
import pluginVue from 'eslint-plugin-vue'
import vuePug from 'eslint-plugin-pug-vue'

export default [
  ...pluginVue.configs['flat/recommended'],
  ...vuePug.configs.recommended,
]

テンプレートを壊さない fix ​

Pug ではインデントが構造そのものです。1 行ずらすだけで木構造が変わります。そのため ESLint が Pug テンプレートに fix を書き込む前に、どの rule 由来の fix であっても、プラグインが次の検証を行います。

  1. fix を適用した状態のテンプレートをパースします。
  2. タグ、そのネスト、クラス、属性、すべての JavaScript の式を元のテンプレートと比較します。
  3. 何も変わっていない場合にだけ fix を適用します。変わっている場合は fix を破棄して警告を残すので、その行を手作業で直せます。

fix ガードの仕組み

pug
//- the same template to the guard
p(class="intro lead") Hello
p.intro.lead Hello

//- a different one: a class is gone
p.intro Hello

そのため vue-pug/prefer-shorthand は 1 行目を 2 行目に書き換えることがありますが、クラスが失われる fix や、行を別の親に移す fix が適用されることはありません。

2 分で始める ​

ESLint 9.15 または 10 の flat config、eslint-plugin-vue 10、vue-eslint-parser 10 が必要です。 セットアップで、インストール、設定、VS Code での保存時 fix までを順に説明します。 eslint-plugin-vue-pug から移行する場合は移行ガイドを読んでください。

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