Custom ESLint/TypeScript indentation rules for parameters of method declaration

Viewed 314

I'd like to be able to indent parameters in a method declaration so that they line up, but not be forced all the way into having to put each parameter on a separate line.

enter image description here

ESLint doesn't seem to have a rule for that, and I think I'll just have to give up having any indentation rules enforced at all within the list of my method's parameters. I can live with that.

I'm having a hard time figuring out the correct AST node syntax to express this, however. I'm using AST Explorer, but I can't figure out the correct node syntax to describe what I'm seeing there.

enter image description here

I can use MethodDefinition by itself, but that only controls the indentation of the start of the method itself, not anything about the parameters.

I can us MethodDefinition > :expression *, but then no indentation rules are enforced within the entire body of the method. I don't want to disable that much linting.

I've tried syntax like MethodDefinition > params > Identifier, but that doesn't do anything. Other variations on that theme crash ESLint.

Can anyone clue me in about the correct ESLint syntax for what I'm trying to do? Much appreciated!

2 Answers

I looks like I gave up too soon before posting here. The correct syntax is MethodDefinition Identifier. I guess the trick is to ignore any of the lowercase steps in the node hierarchy, and only specify the uppercase node types.

I think this should be the solution for the behavior you are looking for in the .eslintrc.js:

module.exports = {
  //...
  rules: {
    'indent': 'off',
    '@typescript-eslint/indent': ['warn', 2, {
      FunctionDeclaration: {
        parameters: 'first',
      },
    }],
  }
}
Related