How disable eslint warning for a specific line in a template in a .vue file in VS Code - vue.js

I would like to dismiss this error on my vue file
I am trying to add this processor line
<!-- eslint-disable-next-line vue/no-use-v-if-with-v-for -->
and
<!-- eslint-disable-next-line vue/no-confusing-v-for-v-if -->
but neither
Nor
dismiss the eslint warning
[eslint-plugin-vue] [vue/no-use-v-if-with-v-for]
The 'value' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.
I'm using the vetur extension for VSCode.
I added the precessor line fallowing this sample but eslint still warns about the next line.
PS. This solution is not the best one, but I needed it like this due the transition animation.

See Vetur's documentation:
Install ESLint plugin for the best linting experience. Vetur's template linting is only for quick start and does not support rule configuration.
So, you have to:
Install ESLint plugin
Enable vue plugin and disable Vetur's linting (add to VS Code config):
"vetur.validation.template": false,
"eslint.validate": [
"javascript",
"javascriptreact",
"vue"
]
If you don't have eslint and/or eslint-plugin-vue already installed, you should do that:
npm i eslint babel-eslint eslint-plugin-vue --save-dev
Simple config for ESLint:
{
"root": true,
"env": {
"node": true
},
"extends": ["plugin:vue/essential", "eslint:recommended"],
"rules": {
},
"parserOptions": {
"parser": "babel-eslint"
}
}
You should save it either to .eslintrc file or to package.json under eslintConfig name.
And, it works:

If you really want to disable it, try the solution below (it works for me). Since you are specific about the rule it doesn't disable other warnings:
<!-- eslint-disable vue/no-v-html -->
<textarea
type="email"
name="message"
required
aria-required="true"
v-html="form.inputs.name.placeholder"
/>
<!-- eslint-enable -->

Try {{! template-lint-disable }}

Related

Auto Sorting Tailwind CSS class in React Native project

We are using Visual Studio Code to develop a react native project using Tailwind CSS with this npm package: https://www.npmjs.com/package/twrnc. We had installed Prettier in our vscode and postcss, prettier, prettier-plugin-tailwindcss in our package.json's devDependencies section.
The auto complete function work fine but the auto sorting cannot work.
twrnc with style class Attribute not working
But when we switch the style keyword to class, it works.twrnc with class class Attribute work
We try to follow the config setting suggestion here: https://github.com/tailwindlabs/tailwindcss/issues/7553#issuecomment-735915659
Here is our VSCode setting json:
"editor.formatOnSave": true,
"tailwindCSS.classAttributes": ["class", "className", "ngClass", "style"],
"scss.validate": false,
"editor.quickSuggestions": {
"strings": true
},
"editor.autoClosingQuotes": "always",
"tailwindCSS.experimental.classRegex": [
"tw`([^`]*)", // tw`...`
"tw=\"([^\"]*)", // <div tw="..." />
"tw={\"([^\"}]*)", // <div tw={"..."} />
"tw\\.\\w+`([^`]*)", // tw.xxx`...`
"tw\\(.*?\\)`([^`]*)" // tw(Component)`...`
],
"tailwindCSS.includeLanguages": {
"typescript": "javascript",
"typescriptreact": "javascript"
}
Can anyone give me some suggesion on how to fix it? Many Thanks.

Vue 2 - ESLint + Standard + Prettier

How do I create a Vue 2 project that uses ESLint + StandardJS + Prettier?
StandardJS's rules should naturally take precedence over Prettier's.
vue create only provides the following options:
ESLint + Standard
ESLint + Prettier
I tried 2 things:
I mixed the eslint configurations of both of the above options. It resulted in a dependency hell, and once that was solved it didn't really work as expected.
I added the prettier-standard package to my eslintrc.js, it didn't work as expected either. It's worth mentioning that prettier-standard works well when manually executing it from the command line.
I'm of course looking to set this up at the project config level and not at the IDE level.
Can you try this repo I've just created? Looks like it's working great from what I've tested.
https://github.com/kissu/so-eslint-standard-prettier-config
Notes
I created 2 projects and dumped the configuration of the standard one into the Prettier one, the changes can be seen in this commit
CLI's current version of #vue/eslint-config-standard is throwing an error (Environment key "es2021" is unknown) because it requires ESlint 7 to work, as shown in this changelog
bumping ESlint to the latest version 7.29.0, fixed the issue
to check your project's current version of ESlint, you can run npx eslint --version
of course, you need to have the ESlint extension enabled and Prettier one disabled (if using VScode), otherwise it may conflict
I've tried to remove #vue/prettier from
extends: ['plugin:vue/essential', 'eslint:recommended', '#vue/standard', '#vue/prettier']
and see if it's successfully removes any ; and it does!
The errors are indeed coming from ESlint (if we do remove #vue/prettier), and they're fixed by ESlint only upon save (after an ESlint server + VScode restart)!
Putting Prettier back works fine of course.
Luckly, I had a new PC, hence had the opportunity to try a whole fresh config with VScode.
I had to install ESlint only and have those settings into my settings.json
{
"editor.codeActionsOnSave": {
"source.organizeImports": false,
"source.fixAll": true,
"source.fixAll.eslint": true,
"source.fixAll.stylelint": true
}
}
The formatting works perfectly and nothing more is required.
I have eslint 7.8.1 with Vue Prettier on and i don't have any problem, maybe the version of eslint that you have is not compatible with Prettier or maybe your eslint have some errors?
In each way i will put my eslint configuration and maybe it will help you!
module.exports = {
env: {
'browser': true,
'es6': true,
'node': true
},
parserOptions: {
'parser': 'babel-eslint'
},
extends: [
'#vue/standard',
'plugin:vue/recommended'
],
rules: {
"vue/html-indent": ["error", 4, {
"attribute": 1,
"closeBracket": 0,
"switchCase": 0,
"ignores": []
}],
"vue/max-attributes-per-line": [2, {
"singleline": 10,
"multiline": {
"max": 1,
"allowFirstLine": false
}
}],
'indent': ['error', 4],
'vue/component-name-in-template-casing': ['error', 'kebab-case'],
'vue/script-indent': [
'error',
4,
{ 'baseIndent': 1 }
],
'space-before-function-paren': ['error', 'never'],
'semi': [2, "never"],
'vue/singleline-html-element-content-newline': 'off',
'vue/multiline-html-element-content-newline': 'off'
},
overrides: [
{
'files': ['*.vue'],
'rules': {
'indent': 'off'
}
}
]
}
Also maybe you have forgot some of the devDependecies on package.json, those are mine
"eslint": "^7.8.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-vue": "^6.2.2"
Hope that those will help you !

Use different linter configurations for class based vue components (template and typescript)

How can I properly setup linting with prettier and the vue linters with running into conflicts in vue files with and typescript code?
An example:
Default: closing bracket (in this case --> closing li) is in next line.
<template>
<div>
<li
class="o-playtime__head__info__tags__item"
v-if="playtimeItemObject.event.ageRecommendation"
>
...
</div>
</div>
</template>
I want the closing bracket in the same line:
<template>
<div>
<li
class="o-playtime__head__info__tags__item"
v-if="playtimeItemObject.event.ageRecommendation">
...
</div>
</div>
</template>
Eslint configuration:
module.exports = {
root: true,
env: {
node: true
},
extends: [
'plugin:#typescript-eslint/recommended',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
"plugin:vue/essential",
"#vue/prettier",
"#vue/typescript",
],
parserOptions: {
parser: "#typescript-eslint/parser"
},
};
Prettier configuration:
module.exports = {
semi: true,
trailingComma: 'all',
tabWidth: 2,
};
Adding the vue linting rule to change the closing bracket:
rules: {
"vue/html-closing-bracket-newline": ["error", {
"singleline": "never",
"multiline": "never"
}]
}
This causes a conflict between vue/html-closing-bracket-newline and prettier/prettier.
Is there a way to overrule prettier for this case? Or do I have to configure both vue and prettier?
Or is there a general better way for combining the linters in vue development? I do not like the combination between two different formatters at all.
Is there a way to let different linters handle the different segments of a vue file?
<template> --> Linted by vue/recommended
<script lang="ts"> --> Linted by prettier/recommended // prettier/#typescript-eslint
After digging deeper into all the mentioned linter configurations:
Currently, there is no proper way to combine prettier with vue linting rules without running into conflicts, as prettier does not allow such configurations at the time of writing. It works out of the box, but as soon as there will be other rules defined for vue template handling, both formatters run into conflicts (vue formats after the defined rules, prettier tries to overwrite it again).
In my opinion, prettier does a good job, but is not a good partner to run beside other formatters due to missing configuration / skipping options. So in my case I removed prettier as a formatter and restrict linting to pure eslint with eslint-typescript and eslint-vue rules. This is a bit more work in configuration, but allows more customized formatting / linting without any conflicts.
eslint conf:
extends: [
"plugin:vue/recommended",
"eslint:recommended",
"#vue/typescript/recommended"
],

Vetur/Eslint/VS Code - can't set space between parenthesis for .vue files

I can't figure out how to set the configuration in for the space between function parentheses. I've set it everywhere to true, but when I save a .vue file, the space is removed - after it is removed it is highlighted as error (Missing space between function parentheses). It happens in script section. In .js files spaces are added, but also highlighted as error, this time... Unexpected space between function parentheses?! There was some configuration of settings (which I'm not able to recreate now) when on save the space was added for a moment and then removed again in .vue files.
my settings.json
"vetur.format.defaultFormatter.js": "prettier", // tried both prettier and typescript
// "vetur.format.defaultFormatter.js": "vscode-typescript", // tried both prettier and typescript
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"typescript.format.insertSpaceBeforeFunctionParenthesis": true,
"vetur.format.defaultFormatterOptions": {
"prettier": {
"singleQuote": true,
"spaceBeforeFunctionParen": true,
"eslintIntegration": true,
},
"vscode-typescript": {
"singleQuote": true,
"spaceBeforeFunctionParen": true,
"eslintIntegration": true,
}
},
.eslintrc.js
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'#vue/standard'
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
"space-before-function-paren": ["error", "always"], //setting this to 'never' removes the error highlight in vue files, not js files
},
parserOptions: {
parser: 'babel-eslint',
sourceType: "module"
}
}
I've read a zillion questions and set the space-between-function-parentheses in every possible setting that I found in the answers. Still the linting process finds a way to ignore all those settings and implement a different one. Not to mention that it highlights errors not consistent with the auto-formatting. Is there any other setting that I am still missing?
Try this:
npm install prettier#v1.19.1 --save-dev --save-exact
and then restart VS Code.
Prettier just recently updated to v2 and if your project doesn't have prettier installed locally it will use VS Code's version, which is most probably the latest version. In prettier v2 the space-before-function-paren has become a default and hence will be applied on all your projects that don't have a local version of prettier pre v2 installed. For me using any config combination didn't seem to work - it's like prettier just ignored all of them. Hope this helps.
Prior to Prettier v2, It seems to not support space-before-function-paren rule. So We should turn off the rule above to resolve conflict.
Try this
module.exports = {
rules: {
'space-before-function-paren': 'off'
}
}
in an ESLint configuration file(such as .eslintrc.js) located in root directory of project.
Then we should add following to settings.json in VS Code.
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
},
Last but not least, Disabling Vetur extension in VS Code might be a better choice.
I also had same exact issue with vetur and ESLint extns. Following in settings.json fixed it. By default it was prettier.
"vetur.format.defaultFormatter.js": "prettier-eslint",

use vue/nuxt linting rules in vscode

I created a new nuxt app using npx create-nuxt-app <project-name> and chose to use eslint and prettier.
I opened the project's directory using vscode and installed the ESLint and Prettier - Code formatter, and Vetur extensions.
When I save a .vue file vscode formats the code, but in a way that breaks the settings in the nuxt project.
For example vscode transforms
<div
class="test"
style="background: red">
test
</div>
to
<div class="test" style="background: red">test</div>
but this breaks the vue/max-attributes-per-line rule.
How do I set up vscode to use the nuxt project's linting and prettyfying rules?
When starting a new nuxt project using npx create-nuxt-app, check both ESLint and Prettier for linting options and choose the recommended jsconfig.json option.
Then do the following:
install additional npm dev packages
npm install --save-dev babel-eslint eslint eslint-config-prettier eslint-loader eslint-plugin-vue eslint-plugin-prettier prettier
Install VS Code extensions
Prettier
ESLint
Vetur
Formatting Toggle (optional)
Change your workplace settings (.vscode/settings.json) to the following:
{
"eslint.format.enable": true,
"vetur.format.defaultFormatter.html": "prettier"
}
You can toggle auto fixing using the Formatting Toggle extension, or if you didn't install it by changing your user settings:
{
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true
}
Change your .prettierrc file (optional)
{
"semi": false,
"arrowParens": "always",
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"endOfLine": "lf"
}
Install the extensions:
Vue
Vue 2 Snippets
Vue Peek
Vetur
ESLint
Go to File > Preferences > Settings and edit the User Settings file, adding the following configuration:
{
...... ,
"vetur.format.defaultFormatter.js": "vscode-typescript",
"vetur.format.defaultFormatter.html": "js-beautify-html",
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"eslint.autoFixOnSave": true,
"eslint.validate": [
{
"language": "vue",
"autoFix": true
},
{
"language": "html",
"autoFix": true
},
{
"language": "javascript",
"autoFix": true
}
],
}
With this configuration, VSCode will perform validation for these three file types: vue, HTML and JavaScript. Now go back to the src/App.vue file and press ctrl+alt+f on Windows or ctrl+shift+i on Linux or ctrl+options+f on Mac OS to perform the formatting of the code. ESLint will validate the code and display some errors on the screen.
Any errors can be corrected automatically, and it’s not necessary to correct each error manually. To do this, you can
press
ctrl+shift+p
and select
ESLint: Fix all problems