admin管理员组

文章数量:1333487

I have the following code snippet:

const enclosing = () => {
  const setClaim = (userId, claim) => {
    client
      .setClaim({ userId, claim })
      .then(() => {
          // do something

          return resolve(true);
        }, // eslint plains about this line
        err => reject(err)
      );
  });
};

ESLint plains about the line marked above as follows:

139:9   error  Expected indentation of 6 spaces but found 8                  indent

Which indent rule object option does apply here (has to be changed) as I want to keep the indentation as is?

I don't want to suppress the ESLint error using eslint-disable-line as this is a global (applies to all of my code being linted) issue for me.

I have the following code snippet:

const enclosing = () => {
  const setClaim = (userId, claim) => {
    client
      .setClaim({ userId, claim })
      .then(() => {
          // do something

          return resolve(true);
        }, // eslint plains about this line
        err => reject(err)
      );
  });
};

ESLint plains about the line marked above as follows:

139:9   error  Expected indentation of 6 spaces but found 8                  indent

Which indent rule object option does apply here (has to be changed) as I want to keep the indentation as is?

I don't want to suppress the ESLint error using eslint-disable-line as this is a global (applies to all of my code being linted) issue for me.

Share Improve this question edited May 30, 2017 at 21:12 Alexander Zeitler asked May 30, 2017 at 20:41 Alexander ZeitlerAlexander Zeitler 13.1k14 gold badges89 silver badges142 bronze badges 4
  • 2 It is the indent rule. – Alexander Zeitler Commented May 30, 2017 at 20:55
  • 1 I would be more concerned about the mismatched parenthesis ) on the second-to-last line… – Bergi Commented May 30, 2017 at 22:04
  • One has to admit, that's a really weird indentation. Are you going to do this consistently for all multiline arguments, or only when it's a function, or only when there are multiple functions? – Bergi Commented May 30, 2017 at 22:05
  • Yes, I think this is the root cause (done by WebStorm) – Alexander Zeitler Commented May 30, 2017 at 22:06
Add a ment  | 

2 Answers 2

Reset to default 12

In case you arrived here looking for Promise Indent answers like I did (see title)... The MemberExpression section of ESLint Indent rules may be helpful. Member expression "enforces indentation level for multi-line property chains".

You can turn off checking for indentation of chained methods by setting "MemberExpression" to "Off" in your .eslintrc.js file.

rules: {
  'indent': ['error', 4, { 'MemberExpression': 'off'}]
}

With MemberExpression set to "off", you can write your promises indented, or lined-up.

Lined Up

client
.setClaim({ userId, claim })
.then(resolve(true), reject)

Indented

client
    .setClaim({ userId, claim })
    .then(resolve(true), reject)
const enclosing = () => {
  const setClaim = (userId, claim) => {
    client
      .setClaim({ userId, claim })
      .then(() => {
         // ^ insert a newline here
          // do something

          return resolve(true);
        }, // eslint plains about this line
        err => reject(err)
      );
  });
};

I suggest adding a new line before the fulfill handler. Since you give .then two handlers, doing so can keep them at the same indentation level.

本文标签: javascriptWhich ESLint rule applies to PromiseArrow function indentStack Overflow