Skip to content

Commit

Permalink
Add suggestion for 'no-redundant-jump' (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Mar 24, 2022
1 parent 9e30611 commit aac249c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -36,7 +36,7 @@ Code Smells, or maintainability issues, are raised for places of code which migh
* "switch" statements should not be nested ([`no-nested-switch`])
* 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`])
* Jump statements should not be redundant ([`no-redundant-jump`]) (:wrench: *fixable*)
* Conditionals should start on new lines ([`no-same-line-conditional`])
* "switch" statements should have at least 3 "case" clauses ([`no-small-switch`])
* Collection and array contents should be used ([`no-unused-collection`])
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-redundant-jump.md
@@ -1,5 +1,7 @@
# no-redundant-jump

:wrench: *fixable*

Jump statements, such as `return`, `break` and `continue` let you change the default flow of program execution, but jump statements that direct the control flow to the original direction are just a waste of keystrokes.

## Noncompliant Code Example
Expand Down
3 changes: 3 additions & 0 deletions src/rules/no-redundant-jump.ts
Expand Up @@ -28,9 +28,11 @@ const rule: TSESLint.RuleModule<string, string[]> = {
meta: {
messages: {
removeRedundantJump: 'Remove this redundant jump.',
suggestJumpRemoval: 'Remove this redundant jump',
},
schema: [],
type: 'suggestion',
hasSuggestions: true,
docs: {
description: 'Jump statements should not be redundant',
recommended: 'error',
Expand All @@ -46,6 +48,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
context.report({
messageId: 'removeRedundantJump',
node,
suggest: [{ messageId: 'suggestJumpRemoval', fix: fixer => fixer.remove(node) }],
});
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/rules/no-redundant-jump.test.ts
Expand Up @@ -94,6 +94,17 @@ ruleTester.run('Jump statements should not be redundant', rule, {
return; // Noncompliant
}`,
),
{
code: `function foo(x) { console.log(x); return; }`,
errors: [
{
messageId: 'removeRedundantJump',
suggestions: [
{ messageId: 'suggestJumpRemoval', output: `function foo(x) { console.log(x); }` },
],
},
],
},
],
valid: [
{
Expand Down

0 comments on commit aac249c

Please sign in to comment.