简体   繁体   English

如何禁用段落中的eslint规则最大行长<template> vue.js 的?</template>

[英]How to disable eslint rule max line length for paragraph in <template> of vue.js?

I am using airbnb eslint and currently I am getting error:我正在使用 airbnb eslint,目前出现错误:

error: Line 6 exceeds the maximum line length of 100 (max-len) at path/to/file.vue:6:1:错误:第 6 行在 path/to/file.vue:6:1 处超过了最大行长度 100 (max-len):

<template lang="pug">
  div
    .container
      .row
        .col-xl-10.mx-auto
          p Please let us know how you got here, and use the header buttons to navigate back to safe harbor.
</template>

Is there a way to disable lint for paragraph text like above?有没有办法像上面那样禁用段落文本的 lint?
Also, how to increase the line length from 100 to 120?另外,如何将行长从 100 增加到 120?

Update更新

There has been some updates to eslint-plugin-vue in the past 4 years.在过去的 4 年里,eslint-plugin-vue 有一些更新。 The comments in templates can now be used to override eslint settings.模板中的注释现在可用于覆盖 eslint 设置。

For next line only仅用于下一行

<!-- eslint-disable-next-line max-len -->
<my-reasonably-long-component>...</my-reasonably-long-component>

For multi-line purpose用于多线目的

<!-- eslint-disable max-len -->
<my-reasonably-long-component>
  ...
</my-reasonably-long-component>
<!-- eslint-enable max-len -->

In addition, as of eslint-plugin-vue v6.1.0 the vue/max-len rule was added, which ads more control about how the length rules此外,从eslint-plugin-vue v6.1.0 ,添加了vue/max-len规则,该规则广告对长度规则的控制方式进行了更多控制

{
    "vue/max-len": ["error", {
        "code": 80,
        "template": 80,
        "tabWidth": 2,
        "comments": 80,
        "ignorePattern": "",
        "ignoreComments": false,
        "ignoreTrailingComments": false,
        "ignoreUrls": false,
        "ignoreStrings": false,
        "ignoreTemplateLiterals": false,
        "ignoreRegExpLiterals": false,
        "ignoreHTMLAttributeValues": false,
        "ignoreHTMLTextContents": false,
    }]
}

If you have more than a couple outliers, tweaking the globals for templates-specifically might work better.如果您有多个异常值,则调整模板的全局变量可能会更好。


Original Answer原始答案

AFAIK, there is no way to apply eslint rules to the template, and specifically to one line in a template. AFAIK,没有办法将 eslint 规则应用于模板,特别是模板中的一行。 I hope to be proven wrong though.我希望被证明是错误的。

anyway, because I have a file with lots of text, to get around it, I've added this rule 'max-len': ["error", { "code": 120 }], in my .eslintrc.js file.无论如何,因为我有一个包含大量文本的文件,为了绕过它,我在我的.eslintrc.js文件中添加了这条规则'max-len': ["error", { "code": 120 }], .

here is the structure (with other settings removed)这是结构(删除了其他设置)

module.exports {
  rules: {
    'max-len': ["error", { "code": 120 }]
  }
}

This will disable the rule for the entire template tag.这将禁用整个模板标签的规则。 Official ES Lint docs on disabling rules 关于禁用规则的官方 ES Lint 文档

<template>
  <!-- eslint-disable max-len -->
  ...

EDIT: If you want to instead disable line length rule for all .vue files, then add this to .eslintrc.js (this will also disable the rule for <script> and <style> tags):编辑:如果您想禁用所有 .vue 文件的行长规则,请将其添加到.eslintrc.js (这也将禁用<script><style>标签的规则):

module.exports = {
  ...
  overrides: [
    {
      files: ["*.vue"],
      rules: {
        ...
        'max-len': 'off' // disables line length check
      }
    }
  ]
};

For eslint-plugin-vue >= 4.1.0 you can use directive comments to disable eslint.对于eslint-plugin-vue >= 4.1.0 ,您可以使用指令注释来禁用 eslint。

https://github.com/vuejs/eslint-plugin-vue/commit/ad84e0ba6d81f24583e65fc70b1d9803d73d3ed9 https://github.com/vuejs/eslint-plugin-vue/commit/ad84e0ba6d81f24583e65fc70b1d9803d73d3ed9

<template>
  <!-- eslint-disable-next-line vue/max-attributes-per-line -->
  <div a="1" b="2" c="3" d="4">
  </div>
</template>

You can disable max-len and use vue/max-len with something like "template": 9000 .您可以禁用 max-len 并将vue/max-len"template": 9000类的内容一起使用。 An example:一个例子:

"overrides": [
    {
      "files": [
        "*.vue"
      ],
      "rules": {
        "max-len": "off",
        "vue/max-len": [
          "error",
          {
            "code": 120,
            "template": 9000,
            "ignoreTemplateLiterals": true,
            "ignoreUrls": true,
            "ignoreStrings": true
          }
        ]
      }
    }
  ]

This way you disable the rule only for the <template></template> part of a component.这样,您只能为组件的<template></template>部分禁用规则。

You can add this to your ESLint rules:您可以将其添加到您的 ESLint 规则中:

rules: {
  "vue/max-attributes-per-line": "off"
}

This worked for me (even if I rather set it on for my projects).这对我有用(即使我宁愿为我的项目设置它)。
You can find more information here if you want.如果需要,您可以在此处找到更多信息。

the correct eslint config is this:正确的 eslint 配置是这样的:

rules: {
  'prettier/prettier': ['error', { printWidth: 120 }],
},

I am using Vue3, the structure has been changed, .eslintrc.js is being merged into package.json so that methods mentioned before is not work for me.我正在使用 Vue3,结构已更改, .eslintrc.js正在合并到package.json中,因此前面提到的方法对我不起作用。

First.第一的。

This is the way it works to me Commented the line in .editorconfig这就是它对我的工作方式在.editorconfig中评论了这一行

[*.{js,jsx,ts,tsx,vue}]
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
# max_line_length = 100

Seconds.秒。

Add the line in eslintConfig object in package.jsonpackage.jsoneslintConfig对象中添加该行

"rules": {
  "max-len": ["error", 120]
},

what was missing for me is the location of configuration file.我缺少的是配置文件的位置。 in my angular project it is.eslintrc.js在我的 angular 项目中它是.eslintrc.js

and the part of the configuration code looks like this:部分配置代码如下所示:

  rules: {
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
    "max-len": ["error", {"code": 120}],
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM