Skip to content

ESLint cho template Pug trong Vue

Chạy các rule vue/* của bạn trên <template lang="pug">, format Pug bằng eslint --fix, và giữ nguyên mọi template: mỗi bản fix đều được kiểm tra trước khi áp dụng.

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 vấn đềRê chuột lên đường lượn sóng để đọc cảnh báo.
Toàn bộ 15 cảnh báo từ eslint
  • 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

Rule vue/* của bạn, trên Pug ​

Các rule của eslint-plugin-vue chạy trên template Pug với tên và option quen thuộc: thiếu key, biến không dùng, thứ tự attribute, cú pháp deprecated. Vài rule không thể hoạt động trên Pug thì bị tắt, hoặc chỉ bị im lặng bên trong template Pug, nên chúng vẫn kiểm tra các template HTML của bạn.

Những rule nào chạy trên Pug

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

Format bằng eslint --fix ​

Mười hai rule vue-pug/* nhỏ format chính phần Pug: thụt lề, khoảng trắng, dấu phẩy, dấu nháy và ngắt dòng của attribute block, cùng shorthand .class / #id. Mỗi rule có option riêng, và bạn có thể tắt bất kỳ rule nào. Không Prettier, và không có formatter thứ hai tranh với ESLint khi lưu file.

Xem mọi rule

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

Bản fix không thể làm hỏng template ​

Trong Pug, thụt lề là cấu trúc: dời một dòng là cây đổi theo. Vì vậy trước khi ESLint ghi một bản fix vào template Pug, plugin kiểm tra nó, bất kể bản fix đến từ rule nào:

  1. nó parse template với bản fix đã áp dụng;
  2. nó so sánh các tag, cách chúng lồng nhau, class, attribute và mọi biểu thức JavaScript với bản gốc;
  3. nó chỉ áp dụng bản fix nếu không có gì thay đổi. Nếu không, nó bỏ bản fix và giữ cảnh báo, để bạn tự sửa dòng đó bằng tay.

Cách fix guard hoạt động

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

Vì vậy vue-pug/prefer-shorthand có thể viết lại dòng đầu thành dòng thứ hai, nhưng một bản fix làm mất class, hoặc chuyển một dòng sang node cha khác, sẽ không bao giờ được áp dụng.

Bắt đầu trong hai phút ​

Cần ESLint 9.15 hoặc 10 với flat config, eslint-plugin-vue 10 và vue-eslint-parser 10. Bắt đầu hướng dẫn từ cài đặt, config đến fix-on-save trong VS Code. Đang dùng eslint-plugin-vue-pug? Đọc hướng dẫn chuyển đổi.

Phát hành theo giấy phép MIT.