ESLint缩进规则会缩进修饰成员吗?

11
为了启用链接方法的缩进:
await PostModel
  .findOne({
    author: user.user,
    _id: id,
  })
  .populate('tickets', 'title status');

根据 ESLint 文档,我已将以下 MemberExpression 添加到我的 eslintrc 文件中。

indent": ["error", "tab", { "MemberExpression": 1 }],

但现在我在使用装饰器时遇到了问题,虽然我的偏好是将它们与成员对齐,但它们却出现了缩进。

@prop({ type: String, required: true })
  password: string;

有没有一种方法可以在不冲突的情况下解决这两种情况?
4个回答

18

我遇到了和你一样的问题,找到了 这个评论 很有帮助,你可以试试看?

{
  rules: {
    // ...
    "indent": ["error", 2, { "ignoredNodes": ["PropertyDefinition"] }]
  }
}

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',
            ],
        },
    ],

这对我来说可行。

1
谢谢,但这对我完全没有用。似乎存在一个未解决的问题,缩进规则导致许多其他票被关闭。 - user776686

3
我可以为您翻译成中文:

@bujhmt的答案实际上解决了我的问题,但是我需要对其进行一些更改。

之前:

export abstract class MyAbstractClass extends AnotherClass {
    @Column()
    name!: string;
    ^^^^--> Expected indentation of 8 spaces, found 4

    @Column()
    address!: string;
    ^^^^--> Expected indentation of 8 spaces, found 4
}

Then I changed my .eslintrc rule to this:

    "indent": [
        `error`,
        4,
        {
            "ignoredNodes": [
                `FunctionExpression > .params[decorators.length > 0]`,
                `FunctionExpression > .params > :matches(Decorator, :not(:first-child))`,
                `ClassBody.body > PropertyDefinition[decorators.length > 0] > .key`,
            ],
        },

    ],

错误不见了。


0
如上所述,您可以使用ignoredNodes并使用抽象语法树选择器来忽略特定元素。这意味着该元素不再受规则约束。
例如:
Pass@prop({ type: String, required: true })
password: string;

Also Pass@prop({ type: String, required: true })
                      password: string;

你可以将代码粘贴到https://astexplorer.net/中(如果你使用的是typescript,请将类型设置为@typescript-eslint/parser - 参见这篇有用的文章

所以像这样的东西可能会有所帮助:ClassBody.body > PropertyDefinition[decorators.length>0] > Identifier

这意味着类体上有装饰器的任何属性都将被该规则忽略。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接