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

Commit 2bca2e4

Browse files
FN prefer-immediate-return: consider hoisted variables (#83)
1 parent 23ba8e2 commit 2bca2e4

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

ruling/snapshots/prefer-immediate-return

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ src/jest/packages/jest-resolve/src/index.js: 378
1717
src/react-native/jest/mockComponent.js: 13
1818
src/react-native/Libraries/Animated/src/nodes/AnimatedInterpolation.js: 359
1919
src/react-native/Libraries/Components/ScrollResponder.js: 385
20-
src/react-native/Libraries/Renderer/ReactFabric-dev.js: 3139,10554,10585,13532
21-
src/react-native/Libraries/Renderer/ReactNativeRenderer-dev.js: 3589,10924,10955
20+
src/react-native/Libraries/Renderer/ReactFabric-dev.js: 3139,10554,10585,12291,13532
21+
src/react-native/Libraries/Renderer/ReactNativeRenderer-dev.js: 3589,10924,10955,12661
2222
src/react-native/local-cli/bundle/assetPathUtils.js: 52
2323
src/react-native/local-cli/util/PackageManager.js: 40
2424
src/react-native/RNTester/js/ScrollViewSimpleExample.js: 63

src/rules/prefer-immediate-return.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const rule: Rule.RuleModule = {
4646
const declaredIdentifier = getOnlyDeclaredVariable(lastButOne);
4747

4848
if (returnedIdentifier && declaredIdentifier) {
49-
const sameVariable = context.getScope().variables.find(variable => {
49+
const sameVariable = getVariables(context).find(variable => {
5050
return (
5151
variable.references.find(ref => ref.identifier === returnedIdentifier) !== undefined &&
5252
variable.references.find(ref => ref.identifier === declaredIdentifier.id) !== undefined
@@ -111,6 +111,15 @@ const rule: Rule.RuleModule = {
111111
const action = isReturnStatement(node) ? "return" : "throw";
112112
return `Immediately ${action} this expression instead of assigning it to the temporary variable "${variable}".`;
113113
}
114+
115+
function getVariables(context: Rule.RuleContext) {
116+
const { variableScope, variables: currentScopeVariables } = context.getScope();
117+
if (variableScope === context.getScope()) {
118+
return currentScopeVariables;
119+
} else {
120+
return currentScopeVariables.concat(variableScope.variables);
121+
}
122+
}
114123
},
115124
};
116125

tests/rules/prefer-immediate-return.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,21 @@ ruleTester.run("prefer-immediate-return", rule, {
303303
return 42
304304
}`,
305305
},
306+
{
307+
// hoisted variables
308+
code: `
309+
function foo() {
310+
if (cond) {
311+
var x = 42;
312+
return x;
313+
}
314+
}
315+
`,
316+
errors: [
317+
{
318+
message: 'Immediately return this expression instead of assigning it to the temporary variable "x".',
319+
},
320+
],
321+
},
306322
],
307323
});

0 commit comments

Comments
 (0)