简体   繁体   English

TSLint 错误“超过最大行长 120”

[英]TSLint Error "Exceeds maximum line length of 120"

In my Angular2 App I'm importing one component like this:在我的 Angular2 应用程序中,我正在导入一个这样的组件:

import { PersonsSingleAccountComponent} from 
   '../persons-information/fragments/persons-single-account/persons-single-account-bookings/persons-single-account-bookings.component'

It is giving me the lint error "Exceeds maximum line character".它给了我皮棉错误“超过最大行字符”。 If I try to give the statement in ``(backtick) it is throwing an error.如果我尝试在 ``(backtick) 中给出语句,则会引发错误。

How can I solve this lint error?如何解决此 lint 错误?

It's not really something you can change, not related to your code.这不是您可以更改的东西,与您的代码无关。

You should simply disable the rule for this import by adding a comment before :您应该通过在之前添加评论来禁用此导入的规则:

// tslint:disable-next-line:max-line-length
import { PersonsSingleAccountComponent} from '../persons-information/fragments/persons-single-account/persons-single-account-bookings/persons-single-account-bookings.component'

There is another way of solving this problem.还有另一种方法可以解决这个问题。

Since version 5.9.0 TSLint max-line-length rule provides support for ignore patterns.从 5.9.0 版开始, TSLint 最大行长度规则提供了对忽略模式的支持。

tslint.json: tslint.json:

{
  "rules": {
    "max-line-length": [
      true, 
      {
        "limit": 120, 
        "ignore-pattern": "^import [^,]+ from |^export | implements"
      }
    ],
  } 
}

This rule will ignore the following lines:此规则将忽略以下行:

import { SomeLongInterfaceName } from '../../../nested/directory/structure/target';
class MyClass implements SomeLongInterfaceName, OnInit, OnDestroy, OnViewChange {}
export { MyClass, SomeLongInterfaceName };

Credits go to DanielKucal .归功于 DanielKucal

For the question of the OP, using ^import [^,]+ from would be enought to ignore long imports.对于 OP 的问题,使用^import [^,]+ from足以忽略长导入。

IMHO this is an better approach, since it is less intrusive than modifing the TSLint rule for the whole project and has no code smell as if you disable TSLint rules in each file with a comment.恕我直言,这是一种更好的方法,因为它比修改整个项目的 TSLint 规则的侵入性更小,并且没有代码气味,就像您在每个带有注释的文件中禁用 TSLint 规则一样。

There's another way to get rid of this error - modify tslint rules for the whole project .还有另一种方法可以摆脱这个错误——修改整个项目的 tslint 规则

In my case I had an existing project with hundreds of lines exceeding the limit.就我而言,我有一个现有项目,其中数百行超出了限制。 In fact, the code was more clear that way, because this was basically an array of objects.事实上,这样代码更清晰,因为这基本上是一个对象数组。 But VS Code drew a red underline over the whole file making it hard to read.但是 VS Code 在整个文件上画了一条红色下划线,使其难以阅读。

What I did was: "max-line-length": [ false ] .我所做的是: "max-line-length": [ false ]

You can also change the length by writing "max-line-length": [ true, 240 ] , which will produce the same result.您还可以通过编写"max-line-length": [ true, 240 ]来更改长度,这将产生相同的结果。

Here's an example of tslint.json I have right now:这是我现在拥有的tslint.json示例:

{
    "extends": "../tslint.json",
    "rules": {
        "directive-selector": [
            true,
            "attribute",
            "app",
            "camelCase"
        ],
        "component-selector": [
            true,
            "element",
            "app",
            "kebab-case"
        ],
        "max-line-length": [ false ],
    }
}

Also, please, checkout this link for more advanced settings.另外,请查看此链接以获取更多高级设置。

Their are couple of ways to deal with tslint - max-line-length warnings.它们是处理tslint - max-line-length警告的几种方法。 3 ways are mentioned below.下面介绍3种方式。

  1. By provide rule in tslint.json file that will ignore specific statements.通过在tslint.json文件中提供将忽略特定语句的规则。
 "rules": {
   "max-line-length": [
     true, 
     {
       "limit": 120, 
       **"ignore-pattern": "^import [^,]+ from |^export | implements"**
     }
   ],
 } 
}
  1. By Changing default max-line-length character value in tslint.json file.通过更改tslint.json文件中的默认最大行长度字符值。
            true,
            {
                "limit": **300**,
                "ignore-pattern": "^import |^export | implements"
            }
        ],
  1. By adding comment above targeted code line in targeted file.通过在目标文件的目标代码行上方添加注释。

// tslint:disable-next-line:max-line-length

// Your code goes here.

tslint.json file will be located in root scope of project directory. tslint.json文件将位于项目目录的根范围内。

I would rename the files and remove the redundant naming.我会重命名文件并删除多余的命名。

And adding a path to the tsconfig if the persons are in a deep folder structure and used in other modules too:如果人员位于深层文件夹结构中并且也在其他模块中使用,则添加到 tsconfig 的路径:

{
    "compilerOptions": {
        "baseUrl": "./src",
        "paths": {
            "@persons/*": [
                "app/shared/foo/bar/persons/*"
            ]
        }
    }
}

Result:结果:

import { PersonsSingleAccountComponent} from 
   '@persons/information/fragments/account/bookings/single-account-bookings.component'

you can use您可以使用

import {进口 {

PersonsSingleAccountComponent PersonsSingleAccount 组件

} from '../persons-information/fragments/persons-single-account/persons-single-account-bookings/persons-single-account-bookings.component'` } 来自'../persons-information/fragments/persons-single-account/persons-single-account-bookings/persons-single-account-bookings.component'`

you can disable, it in case you want to ignore lint max lines for a file.您可以禁用它,以防您想忽略文件的 lint max 行。 Just add it to the top of your class.只需将其添加到班级顶部即可。

/* eslint-disable max-lines */ /* eslint-disable max-lines */

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

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