Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: semi option omitLastInOneLineBlock does not work for one liner class blocks #17035

Closed
1 task done
martian17 opened this issue Mar 29, 2023 · 4 comments · Fixed by #17105
Closed
1 task done

Bug: semi option omitLastInOneLineBlock does not work for one liner class blocks #17035

martian17 opened this issue Mar 29, 2023 · 4 comments · Fixed by #17105
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes

Comments

@martian17
Copy link

martian17 commented Mar 29, 2023

Environment

Node version: v18.14.1
npm version: pnpm 7.29.1
Local ESLint version: 8.37.0
Global ESLint version: N/A
Operating System: Ubuntu

What parser are you using?

Default (Espree)

What did you do?

Configuration
// .eslintrc.cjs
module.exports = {
    extends: [
        "eslint:recommended"
    ],
    env: {
      browser: true,
      node: true,
    },
    parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module"
    },
    rules: {
        "semi": ["error", "always", {
            "omitLastInOneLineBlock": true
        }]
    }
}
// index.js
export class SomeClass{
    logType(){
        console.log(this.type);
    }
}

export class Variant1 extends SomeClass{type=1}
export class Variant2 extends SomeClass{type=2}
export class Variant3 extends SomeClass{type=3}
export class Variant4 extends SomeClass{type=4}
export class Variant5 extends SomeClass{type=5}

What did you expect to happen?

No errors

What actually happened?

Here is the error

root/index.js
   8:47  error  Missing semicolon  semi
   9:47  error  Missing semicolon  semi
  10:47  error  Missing semicolon  semi
  11:47  error  Missing semicolon  semi
  12:47  error  Missing semicolon  semi

✖ 5 problems (5 errors, 0 warnings)
  5 errors and 0 warnings potentially fixable with the `--fix` option.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

If this is the expected behavior, I'd still love to have an option to enable checks for single line classes.

@martian17 martian17 added bug ESLint is working incorrectly repro:needed labels Mar 29, 2023
@martian17
Copy link
Author

martian17 commented Mar 29, 2023

I confirmed that this error goes away after editing line 324 of /lib/rules/semi.js to match "ClassBody" in addition to "BlockStatement".
https://github.com/eslint/eslint/blob/main/lib/rules/semi.js#L324
before:

            if (parent.type === "BlockStatement") {

after:

            if (parent.type === "BlockStatement" || parent.type === "ClassBody") {

To elaborate on why the parent type should be "ClassBody", it is to make the behavior consistent with the existing semi rule implementation and match exactly the part enclosed by the opening and closing curly braces including the braces themselves. In the AST, class is represented by "ClassDeclaration" and its child "ClassBody". "ClassDeclaration" encompasses the entire class statement, and "ClassBody" represents the part enclosed by curly braces.

Should I make a pull request with this change? Or should this behavior be added as an option instead?

@mdjermanovic
Copy link
Member

Hi @martian17, thanks for the issue!

I just checked PR #14945 where the semi rule was updated for class fields, and there was no discussion about the omitLastInOneLineBlock option. I think we didn't consider updating this option because class bodies are not "blocks". On the other hand, function bodies aren't blocks either (although they're BlockStatement nodes in ESTree AST, they are generally not considered to be "blocks") but this option does apply to them, so it might make sense to apply it to class bodies as well. However, as the omitLastInOneLineBlock option not only allows the omission of semicolons but also disallows them, updating this option would now introduce new warnings so it looks like a breaking change to me. I would support adding a new option omitLastInOneLineClassBody for the sake of completeness of this rule. Curious what other team members think about this.

@nzakas
Copy link
Member

nzakas commented Mar 30, 2023

I would support adding a new option omitLastInOneLineClassBody for the sake of completeness of this rule. Curious what other team members think about this.

Makes sense to me. @martian17 do you want to submit a pull request for this?

@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion repro:yes and removed repro:needed labels Mar 30, 2023
@snitin315 snitin315 self-assigned this Apr 20, 2023
@nzakas
Copy link
Member

nzakas commented Apr 20, 2023

Looks like @snitin315 assigned this to himself. In the future, please leave a comment saying that you're working on it for clarity.

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Oct 25, 2023
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Oct 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants
@nzakas @martian17 @mdjermanovic @snitin315 and others