eslint indent rule indents decorated members

Viewed 1803

In order to enable indents for chained methods:

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

I have added the following MemberExpression to my eslintrc, as per eslint docs

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

but now I am experiencing problem with decorators, which get indented although my preference is to have them aligned with the member.

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

Is there a way to address those two cases without a conflict?

3 Answers

I got the same problem as you did, and found this comment help, give it a try?

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

According to this issue, you can partially disable the indent rule for decorators:

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

This works for me.

@bujhmt's answer actually solved my problem, however, I needed to make some changes to it.

Before:

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

    ],

And the errors were gone.

Related