NullVoxPopuli / eslint-plugin-decorator-position

ESLint plugin for enforcing decorator position
MIT License
51 stars 10 forks source link

decorator-position affecting indent #701

Closed nunocunha closed 1 year ago

nunocunha commented 1 year ago

Description

No entirely sure on how to create a reproduction example, but mixing indent with decorator-position/decorator-position makes ESLint go crazy.

.eslintrc.js

module.exports = {
    extends: ['eslint:recommended'],
    root: true,
    overrides: [
        {
            files: ['*.js', '*.ts'],
            plugins: ['decorator-position'],
            rules: {
                'decorator-position/decorator-position': ['error', {
                    methods: 'above',
                    properties: 'above'
                }],
                indent: ['error', 2]
            }
        }
    ]
};

example.js

function Decorator() {
  console.log();
}

    @Decorator() // This line SHOULD have an indent error (has 4 spaces, should have 0), but does NOT 
    class Class { // This line has the correct indent error (has 4 spaces, should have 0)
@Decorator() // This line SHOULD have an indent error (has 0 spaces, should have 2), but does NOT
  prop1; // This line is correct

    @Decorator() // This line SHOULD have an indent error (has 4 spaces, should have 2), but does NOT
      prop2; // This line SHOULD have an indent error (has 6 spaces, should have 2), but does NOT
} // This line should NOT have an indent error (has 0 spaces, should have 0)

Expected (eslint ./example.js --fix)

function Decorator() {
  console.log();
}

@Decorator() 
class Class {
  @Decorator()
  prop1;

  @Decorator()
  prop2;
}

Actual (eslint ./example --fix)

function Decorator() {
  console.log();
}

    @Decorator() 
class Class {
@Decorator()
  prop1;

    @Decorator()
      prop2;
    }

Notes

It seems that decorator-position/decorator-position is affecting indent, as ESLint expects the line after the decorator to have one more indent level than the decorator.

NullVoxPopuli commented 1 year ago

I'm kind of surprised anything is happening at all with the class decorator, tbh.

https://github.com/NullVoxPopuli/eslint-plugin-decorator-position/blob/main/lib/rules/decorator-position.js#L171

The AST hasn't specified anything for the class decorator: image

So that's kinda goofy.

If you have time for a few failing tests in a PR, that'd be super helpful <3

nunocunha commented 1 year ago

Apparently this is an issue with vanilla ESLint and TypeScript. I was trying to use "indent" for both JS and TS, but it doesn't support TS. More info here. So I ended up separating "indent" for JS only and used "@typescript-eslint/indent" for TS, as one should do. Doing this solved my issue.

Sorry I took a while to get back to you.

P.S.: Disregard that commit reference, I was trying to figure everything out, while being tired after a long day of work. It isn't an issue with this plugin.