admin管理员组

文章数量:1292870

VSCode is showing this warning: Warning: This line has a length of 82. Maximum allowed is 80

The affected line is this:

function getThatNewPath (myParam, myFirstParam, mySecondParam, anotherParam) {

How can I fix it?

VSCode is showing this warning: Warning: This line has a length of 82. Maximum allowed is 80

The affected line is this:

function getThatNewPath (myParam, myFirstParam, mySecondParam, anotherParam) {

How can I fix it?

Share Improve this question edited Feb 25, 2020 at 10:48 user1941537 asked Feb 25, 2020 at 10:32 user1941537user1941537 6,69517 gold badges67 silver badges114 bronze badges 3
  • 3 Are you asking how to insert new-lines between the parameters? Put the cursor next to the ma, hit Delete, and hit Enter, repeat on every ma, finally add a new-line just before the closing parenthese. VSCode will take care of the correct indentation. – Teemu Commented Feb 25, 2020 at 10:32
  • In your mand line eslint --fix? Assuming you're using eslint. – evolutionxbox Commented Feb 25, 2020 at 10:33
  • "prettier.printwidth": 180 in User Settings JSON in VSCode, if you have installed the amazing Prettier extension. Or you could rather editorconfig – Roko C. Buljan Commented Feb 25, 2020 at 10:40
Add a ment  | 

5 Answers 5

Reset to default 2

Well, since it is a warning, you don't really need to fix it, but if you want anyway, I'd suggest making the line shorter, either by shortening your param names or by using multiple lines to achieve the same result.

Of course, if you split the line into multiple lines this depends on the coding guidelines you apply.

E.g.:

function getVideoAlbumResource (myParam, myFirstParam, mySecondParam,
                                anotherParam, ..., finalParam) {

Since you're using JavaScript you could also pass an object containing your params, which you could destructure later on inside the function.

E.g.:

function getVideoAlbumResource (params) {
    const { myParam, myFirstParam, mySecondParam, anotherParam } = params;

Or you could also destructure your passed object right away in the function header and make it so that you use a fresh line for each property you deconstruct

E.g.:

function getVideoAlbumResource ({
    myParam,
    myFirstParam,
    mySecondParam,
    anotherParam,
}) {
    // your code here

Of course you could refer to your params by using the dot notation (params.myParam, etc.) too.

You can do one of those 3 things :

  • Reformat your line into multiple line to not exceed 80
  • or change your eslint config of max-len to put more than 80
  • or add an es-lint ignore on top of your line to ignore this rule for this particular line

I like listing each parameter on their own line when there are 4 or more of them:

function getVideoalbumResource (
  myParam,
  myFirstParam,
  mySecondParam,
  anotherParam
) {
  // code
}

I think it is your tslint.json which is causing the warning. In tslint.json, change the value of max-line-length to your desired value. eg-

"max-line-length": [
      true,
      140
    ],

Add this to your rules:

"max-len": ["error", { code: 140 }]

本文标签: javascriptWarning This line has a length of 82 Maximum allowed is 80Stack Overflow