Skip to content

Commit

Permalink
Add suggestion for 'no-same-line-conditional' (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Mar 24, 2022
1 parent 35ea28d commit 5756a52
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
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),
),
},
],
},
[issueLocation(precedingIfLastToken.loc)],
message,
Expand Down
66 changes: 66 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 Expand Up @@ -250,5 +267,54 @@ ruleTester.run('Conditionals should start on new lines', rule, {
},
],
},
{
code: `
function myFunc() {
foo(); if (cond1) {
} if (cond2) {
}
}`,
options: ['sonar-runtime'],
errors: [
{
messageId: 'sonarRuntime',
data: {
sonarRuntimeData: JSON.stringify({
secondaryLocations: [
{
line: 4,
column: 8,
endLine: 4,
endColumn: 9,
message: '',
},
],
message: 'Move this "if" to a new line or add the missing "else".',
}),
},
suggestions: [
{
messageId: 'suggestAddingElse',
output: `
function myFunc() {
foo(); if (cond1) {
} else if (cond2) {
}
}`,
},
{
messageId: 'suggestAddingNewline',
output: `
function myFunc() {
foo(); if (cond1) {
}
if (cond2) {
}
}`,
},
],
},
],
},
],
});

0 comments on commit 5756a52

Please sign in to comment.