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-redundant-jump' #338

Merged
merged 1 commit into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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