eslint 缩进规则缩进装饰成员

use*_*686 12 eslintrc typescript-eslint

为了启用链式方法的缩进:

await PostModel
  .findOne({
    author: user.user,
    _id: id,
  })
  .populate('tickets', 'title status');
Run Code Online (Sandbox Code Playgroud)

MemberExpression根据eslint 文档,我已将以下内容添加到我的 eslintrc 中

indent": ["error", "tab", { "MemberExpression": 1 }],
Run Code Online (Sandbox Code Playgroud)

但现在我遇到了装饰器的问题,尽管我更喜欢让它们与成员对齐,但装饰器会缩进。

@prop({ type: String, required: true })
  password: string;
Run Code Online (Sandbox Code Playgroud)

有没有办法在不冲突的情况下解决这两种情况?

小智 21

我遇到了和你一样的问题,发现这个评论有帮助,试试看?

{
  rules: {
    // ...
    "indent": ["error", 2, { "ignoredNodes": ["PropertyDefinition"] }]
  }
}
Run Code Online (Sandbox Code Playgroud)


buj*_*hmt 8

根据这个问题,您可以部分禁用装饰器的缩进规则:

indent: [
        'error',
        'tab',
        {
            MemberExpression: 1,
            ignoredNodes: [
                'FunctionExpression > .params[decorators.length > 0]',
                'FunctionExpression > .params > :matches(Decorator, :not(:first-child))',
                'ClassBody.body > PropertyDefinition[decorators.length > 0] > .key',
            ],
        },
    ],
Run Code Online (Sandbox Code Playgroud)

这对我有用。