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

feat: add new omitLastInOneLineClassBody option to the semi rule #17105

Merged
merged 6 commits into from Apr 27, 2023

Conversation

snitin315
Copy link
Contributor

@snitin315 snitin315 commented Apr 22, 2023

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Resolve #17035

  1. This PR adds a new omitLastInOneLineClassBody option to the semi rule. The following code will not throw any error now:

    /*eslint semi: ["error", "always", { "omitLastInOneLineClassBody": true}] */
    
    export class SomeClass{
        logType(){
            console.log(this.type);
        }
    }
    
    export class Variant1 extends SomeClass{type=1}
    export class Variant2 extends SomeClass{type=2}
  2. Also fixed the order of options in the semi-rule documentation

    • omitLastInOneLineClassBody and omitLastInOneLineBlock under "always".
    • beforeStatementContinuationChars under "never"
    before after
    Screenshot 2023-04-22 at 7 32 55 AM Screenshot 2023-04-22 at 7 27 16 AM

Is there anything you'd like reviewers to focus on?

@snitin315 snitin315 requested a review from a team as a code owner April 22, 2023 02:03
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Apr 22, 2023
@netlify
Copy link

netlify bot commented Apr 22, 2023

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 9fe8d3a
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/644a87c40a0eb3000848ff78

@@ -353,7 +355,7 @@ module.exports = {
report(node);
}
} else {
const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node));
const oneLinerBlock = ((exceptOneLine || exceptOneLineClassBody) && isLastInOneLinerBlock(node));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per this condition, "omitLastInOneLineClassBody": true would also apply to blocks:

/* eslint semi: ["error", "always", { "omitLastInOneLineClassBody": true}] */

{ foo; } // false positive

It might be clearer to add a new function isLastInOneLinerClassBody.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdjermanovic Good catch. Fixed.

@snitin315 snitin315 added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Apr 23, 2023
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code and docs LGTM. I left a few suggestions about the tests.

`,
options: ["always", { omitLastInOneLineClassBody: false }],
parserOptions: { ecmaVersion: 2022, sourceType: "module" }
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a few tests to verify that this option applies only to one-line class bodies, for example:

{
    code: "class C {\nfoo;}",
    options: ["always", { omitLastInOneLineClassBody: true }],
    parserOptions: { ecmaVersion: 2022 }
},
{
    code: "class C {foo;\n}",
    options: ["always", { omitLastInOneLineClassBody: true }],
    parserOptions: { ecmaVersion: 2022 }
},
{
    code: "class C {foo;\nbar;}",
    options: ["always", { omitLastInOneLineClassBody: true }],
    parserOptions: { ecmaVersion: 2022 }
}

and a test to verify that the false positive from #17105 (comment) has been fixed:

{
    code: "{ foo; }",
    options: ["always", { omitLastInOneLineClassBody: true }],
    parserOptions: { ecmaVersion: 2022 }
}

and a test to verify that the option applies to one-line class bodies even if the class definition is multiline:

{
    code: "class C\n{ foo }",
    options: ["always", { omitLastInOneLineClassBody: true }],
    parserOptions: { ecmaVersion: 2022 }
}

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 559ff4e into main Apr 27, 2023
22 checks passed
@mdjermanovic mdjermanovic deleted the feat/semi-omitLastInOneLineClassBody branch April 27, 2023 17:57
@mdjermanovic
Copy link
Member

@snitin315 could you please submit this same change to docs/src/rules/semi.md again in a new PR, as we had to undo it due to #17225.

@snitin315
Copy link
Contributor Author

Sure. I'll resend.

@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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: semi option omitLastInOneLineBlock does not work for one liner class blocks
2 participants