简体   繁体   English

哪个 ESLint 规则适用于 Promise/Arrow 函数缩进?

[英]Which ESLint rule applies to Promise/Arrow function indent?

I have the following code snippet:我有以下代码片段:

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

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

ESLint complains about the line marked above as follows: ESLint 抱怨上面标记的行如下:

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?哪个indent规则对象选项在此处适用(必须更改),因为我想保持缩进不变?

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.我不想使用eslint-disable-line来抑制 ESLint 错误,因为这对我来说是一个全局(适用于我所有被 lint 的代码)问题。

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.如果您像我一样来到这里寻找 Promise Indent 答案(见标题)... ESLint Indent 规则的 MemberExpression 部分可能会有所帮助。 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.您可以通过在.eslintrc.js文件.eslintrc.js "MemberExpression"设置为"Off""Off"对链接方法的缩进的检查。

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

With MemberExpression set to "off", you can write your promises indented, or lined-up.将 MemberExpression 设置为“关闭”,您可以将承诺缩进或排列。

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 complains 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.由于您给.then两个处理程序,这样做可以使它们保持相同的缩进级别。

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

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