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

Add suggestion for 'no-same-line-conditional' #339

Merged
merged 4 commits into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ Code Smells, or maintainability issues, are raised for places of code which migh
* Template literals should not be nested ([`no-nested-template-literals`])
* Boolean literals should not be redundant ([`no-redundant-boolean`])
* Jump statements should not be redundant ([`no-redundant-jump`]) (:wrench: *fixable*)
* Conditionals should start on new lines ([`no-same-line-conditional`])
* Conditionals should start on new lines ([`no-same-line-conditional`]) (:wrench: *fixable*)
* "switch" statements should have at least 3 "case" clauses ([`no-small-switch`])
* Collection and array contents should be used ([`no-unused-collection`])
* "catch" clauses should do more than rethrow ([`no-useless-catch`])
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-same-line-conditional.md
@@ -1,5 +1,7 @@
# no-same-line-conditional

:wrench: *fixable*

Code is clearest when each statement has its own line. Nonetheless, it is a common pattern to combine on the same line an `if` and its resulting *then* statement. However, when an `if` is placed on the same line as the closing `}` from a preceding *then*, *else* or *else if* part, it is either an error - `else` is missing - or the invitation to a future error as maintainers fail to understand that the two statements are unconnected.

## Noncompliant Code Example
Expand Down
17 changes: 17 additions & 0 deletions src/rules/no-same-line-conditional.ts
Expand Up @@ -35,8 +35,11 @@ const rule: TSESLint.RuleModule<string, string[]> = {
messages: {
sameLineCondition: message,
sonarRuntime: '{{sonarRuntimeData}}',
suggestAddingElse: 'Add "else" keyword',
suggestAddingNewline: 'Move this "if" to a new line',
},
type: 'problem',
hasSuggestions: true,
docs: {
description: 'Conditionals should start on new lines',
recommended: 'error',
Expand Down Expand Up @@ -70,6 +73,20 @@ const rule: TSESLint.RuleModule<string, string[]> = {
{
messageId: 'sameLineCondition',
loc: followingIfToken.loc,
suggest: [
{
messageId: 'suggestAddingElse',
fix: fixer => fixer.insertTextBefore(followingIfToken, 'else '),
},
{
messageId: 'suggestAddingNewline',
fix: fixer =>
fixer.insertTextBefore(
followingIf,
'\n' + ' '.repeat(precedingIf.loc.start.column),
Copy link
Contributor

Choose a reason for hiding this comment

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

add test

),
},
],
},
[issueLocation(precedingIfLastToken.loc)],
message,
Expand Down
17 changes: 17 additions & 0 deletions tests/rules/no-same-line-conditional.test.ts
Expand Up @@ -100,6 +100,23 @@ ruleTester.run('Conditionals should start on new lines', rule, {
endLine: 3,
column: 9,
endColumn: 11,
suggestions: [
{
messageId: 'suggestAddingElse',
output: `
if (cond1) {
} else if (cond2) {
}`,
},
{
messageId: 'suggestAddingNewline',
output: `
if (cond1) {
}
if (cond2) {
}`,
},
],
},
],
},
Expand Down