Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Improve S1488 ('prefer-immediate-return'): ignore when returned varia…
Browse files Browse the repository at this point in the history
…ble has a declared type (#277)

Co-authored-by: Oleksandr T <oleksandr.tarasiuk@outlook.com>
  • Loading branch information
yassin-kammoun-sonarsource and a-tarasyuk authored Aug 10, 2021
1 parent b2b1abf commit 536fdd9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rules/prefer-immediate-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const rule: Rule.RuleModule = {
function getOnlyDeclaredVariable(node: TSESTree.Statement) {
if (isVariableDeclaration(node) && node.declarations.length === 1) {
const { id, init } = node.declarations[0];
if (isIdentifier(id) && init) {
if (isIdentifier(id) && init && !id.typeAnnotation) {
return { id, init };
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/rules/prefer-immediate-return.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ ruleTester.run('prefer-immediate-return', rule, {
let arrow_function_no_block = (a, b) => a + b;
`,
},
{
code: `
function variable_has_type_annotation() {
let foo: number = 1;
return foo;
}
`,
},
{
code: `
function variable_is_used() {
Expand Down

0 comments on commit 536fdd9

Please # to comment.