Skip to content

Commit

Permalink
improve suggestion for 'no-redundant-jump' (SonarSource#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilchik-elena authored Mar 28, 2022
1 parent 4f7f4db commit 94005e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/rules/no-redundant-jump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,21 @@ const rule: TSESLint.RuleModule<string, string[]> = {
if (!withArgument) {
const block = node.parent as TSESTree.BlockStatement;
if (block.body[block.body.length - 1] === node && block.body.length > 1) {
const previousComments = context.getSourceCode().getCommentsBefore(node);
const previousToken =
previousComments.length === 0
? context.getSourceCode().getTokenBefore(node)!
: previousComments[previousComments.length - 1];

context.report({
messageId: 'removeRedundantJump',
node,
suggest: [{ messageId: 'suggestJumpRemoval', fix: fixer => fixer.remove(node) }],
suggest: [
{
messageId: 'suggestJumpRemoval',
fix: fixer => fixer.removeRange([previousToken.range[1], node.range[1]]),
},
],
});
}
}
Expand Down
27 changes: 26 additions & 1 deletion tests/rules/no-redundant-jump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,32 @@ ruleTester.run('Jump statements should not be redundant', rule, {
{
messageId: 'removeRedundantJump',
suggestions: [
{ messageId: 'suggestJumpRemoval', output: `function foo(x) { console.log(x); }` },
{ messageId: 'suggestJumpRemoval', output: `function foo(x) { console.log(x); }` },
],
},
],
},
{
code: `
function foo(x) {
console.log(x);
// comment1
// comment2
return;
}`,
errors: [
{
messageId: 'removeRedundantJump',
suggestions: [
{
messageId: 'suggestJumpRemoval',
output: `
function foo(x) {
console.log(x);
// comment1
// comment2
}`,
},
],
},
],
Expand Down

0 comments on commit 94005e9

Please sign in to comment.