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
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
lib/rules/semi.js
Outdated
@@ -353,7 +355,7 @@ module.exports = { | |||
report(node); | |||
} | |||
} else { | |||
const oneLinerBlock = (exceptOneLine && isLastInOneLinerBlock(node)); | |||
const oneLinerBlock = ((exceptOneLine || exceptOneLineClassBody) && isLastInOneLinerBlock(node)); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mdjermanovic Good catch. Fixed.
There was a problem hiding this 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" } | ||
}, |
There was a problem hiding this comment.
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 }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
@snitin315 could you please submit this same change to |
Sure. I'll resend. |
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
This PR adds a new
omitLastInOneLineClassBody
option to thesemi
rule. The following code will not throw any error now:Also fixed the order of options in the semi-rule documentation
omitLastInOneLineClassBody
andomitLastInOneLineBlock
under"always"
.beforeStatementContinuationChars
under"never"
Is there anything you'd like reviewers to focus on?