Skip to content

Vue में Pug templates के लिए ESLint

अपने vue/* rules को <template lang="pug"> पर चलाएँ, eslint --fix से Pug को format करें, और हर template को सुरक्षित रखें: हर 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

आपके vue/* rules, Pug पर ​

eslint-plugin-vue के rules Pug templates पर अपने सामान्य नामों और options के साथ चलते हैं: गायब keys, अप्रयुक्त variables, attribute का क्रम, deprecated syntax। जो थोड़े-से rules Pug पर काम नहीं कर सकते, उन्हें बंद कर दिया जाता है, या केवल Pug templates के अंदर चुप कराया जाता है, ताकि वे आपके HTML templates को जाँचते रहें।

कौन-से rules 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

eslint --fix से formatting ​

बारह छोटे vue-pug/* rules खुद Pug को format करते हैं: indentation, attribute blocks की spacing, commas, quotes और line breaks, तथा .class / #id shorthands। हर rule के options हैं, और आप किसी को भी बंद कर सकते हैं। न Prettier, और न कोई दूसरा formatter जो save करते समय ESLint से टकराए।

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

ऐसे fixes जो template को नहीं तोड़ सकते ​

Pug में indentation ही structure है: एक line हिलाइए और tree बदल जाता है। इसलिए ESLint किसी Pug template में fix लिखे, उससे पहले plugin उसे जाँचता है, चाहे fix किसी भी rule से आया हो:

  1. वह fix लागू करके template को parse करता है;
  2. tags, उनकी nesting, classes, attributes और हर JavaScript expression की तुलना मूल template से करता है;
  3. fix तभी लागू करता है जब कुछ भी न बदला हो। वरना fix हटा देता है और चेतावनी रखता है, ताकि आप वह line हाथ से ठीक कर सकें।

Fix guard कैसे काम करता है

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 पहली line को दूसरी में बदल सकता है, लेकिन जो fix कोई class खो दे, या किसी line को दूसरे parent में ले जाए, वह कभी लागू नहीं होता।

दो मिनट में शुरू करें ​

इसे flat config वाला ESLint 9.15 या 10, eslint-plugin-vue 10 और vue-eslint-parser 10 चाहिए। शुरुआत करें में install, config और VS Code में fix-on-save के चरण हैं। eslint-plugin-vue-pug से आ रहे हैं? माइग्रेशन गाइड पढ़ें।

MIT License के अंतर्गत जारी।