Skip to content

Commit 2fe1850

Browse files
authored
ESLINTJS-17 Allow functions function names for prefer-immediate-return (#5581)
1 parent 5c72a7c commit 2fe1850

File tree

11 files changed

+78
-42
lines changed

11 files changed

+78
-42
lines changed

.cirrus.template.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ js_ts_ruling_task:
415415
- npm run build:fast
416416
- npm run ruling
417417
on_failure:
418-
debug_script: diff -rq its/ruling/src/test/expected/jsts packages/ruling/actual/jsts
418+
debug_script:
419+
- sh tools/ruling-debug-script.sh packages/ruling/actual/jsts
419420

420421
ruling_task:
421422
depends_on:
@@ -435,8 +436,8 @@ ruling_task:
435436
- mvn test -Dtest=JsTsRulingTest -DskipTests=false -Dsonar.runtimeVersion=LATEST_RELEASE -Dmaven.test.redirectTestOutputToFile=false -Djunit.jupiter.execution.parallel.config.dynamic.factor=1 -B -e -V
436437
cleanup_before_cache_script: cleanup_maven_repository
437438
on_failure:
438-
diff_artifacts:
439-
path: '**/target/actual/**/*'
439+
debug_script:
440+
- sh tools/ruling-debug-script.sh its/ruling/target/actual/jsts
440441

441442
css_ruling_task:
442443
depends_on:

.cirrus.yml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/DEV.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ into the directory with the expected issues `its/ruling/src/test/resources/expec
5252

5353
From the project root, run: `npm run ruling-sync`
5454

55-
You can review the Ruling difference by running `diff -rq its/ruling/src/test/expected/jsts packages/ruling/actual/jsts`.
55+
You can review the Ruling difference by running `sh tools/ruling-debug-script.sh`.
5656
For CSS, run `diff -rq its/ruling/src/test/expected/css `
5757

5858
#### CSS (and old way for JS/TS)

its/ruling/src/test/expected/jsts/ace/javascript-S1488.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
"ace:experiments/animate_folding.html": [
66
241
77
],
8-
"ace:lib/ace/mode/css/csslint.js": [
9-
4287
10-
],
11-
"ace:lib/ace/mode/html/saxparser.js": [
12-
5907
13-
],
148
"ace:src/editor.js": [
159
1598
1610
],

its/ruling/src/test/expected/jsts/ant-design/typescript-S1488.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22
"ant-design:components/breadcrumb/Breadcrumb.tsx": [
33
38
44
],
5-
"ant-design:components/calendar/generateCalendar.tsx": [
6-
82
7-
],
85
"ant-design:components/card/Card.tsx": [
96
46
107
],
11-
"ant-design:components/message/hooks/useMessage.tsx": [
12-
20
13-
],
14-
"ant-design:components/notification/hooks/useNotification.tsx": [
15-
19
16-
],
178
"ant-design:components/steps/index.tsx": [
189
83
1910
],

its/ruling/src/test/expected/jsts/p5.js/javascript-S1488.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
],
1010
"p5.js:lib/addons/p5.sound.js": [
1111
1237,
12-
1527,
1312
1803,
1413
3823,
1514
3832,
1615
3912,
1716
4401,
1817
5264,
1918
7879,
20-
8000,
21-
12876
19+
8000
2220
],
2321
"p5.js:src/dom/dom.js": [
2422
1795,

its/ruling/src/test/expected/jsts/qunit/javascript-S1488.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

its/ruling/src/test/expected/jsts/rxjs/typescript-S1488.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/jsts/src/rules/S1488/rule.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
// https://sonarsource.github.io/rspec/#/rspec/S1488
1818

1919
import type { TSESTree } from '@typescript-eslint/utils';
20-
import { generateMeta, isIdentifier, isRequiredParserServices } from '../helpers/index.js';
20+
import {
21+
generateMeta,
22+
isFunction,
23+
isIdentifier,
24+
isRequiredParserServices,
25+
} from '../helpers/index.js';
2126
import type { Rule } from 'eslint';
2227
import type estree from 'estree';
2328
import * as meta from './generated-meta.js';
@@ -59,8 +64,10 @@ export const rule: Rule.RuleModule = {
5964
);
6065
});
6166

62-
if (hasJSDoc((lastButOne as estree.VariableDeclaration).declarations[0].id)) {
63-
// The temporary object is probably due to adding the jsdoc. Skip in this case.
67+
const id = (lastButOne as estree.VariableDeclaration).declarations[0].id;
68+
if (hasJSDoc(id) || isNamedFunctionExpression(id)) {
69+
// 1) The temporary variable might be due to adding the jsdoc. Skip in this case.
70+
// 2) The temporary variable might be a name of a function, which helps with recognizing in a call stack.
6471
return;
6572
}
6673

@@ -135,5 +142,13 @@ export const rule: Rule.RuleModule = {
135142
}
136143
return !!getJSDocType(services.esTreeNodeToTSNodeMap.get(node as TSESTree.Node));
137144
}
145+
146+
function isNamedFunctionExpression(node: estree.Node) {
147+
const services = context.sourceCode.parserServices;
148+
if (!isRequiredParserServices(services)) {
149+
return false;
150+
}
151+
return isFunction(node, services);
152+
}
138153
},
139154
};

packages/jsts/src/rules/S1488/unit.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ describe('S1488', () => {
2727
valid: [
2828
{
2929
code: `const schemas = schemaFileNames.map((schemaFileName) => {
30-
/** @type {import('ajv').AnySchema}
30+
/** @type {import('ajv').AnySchema}
3131
**/
3232
const result = parseJson(fs.readFileSync(path.join(schemaDir, schemaFileName), {encoding: 'utf8'}));
3333
return result;
3434
});`,
3535
},
36+
{
37+
code: `const makeFooHandler = () => {
38+
const fooHandler = () => {
39+
//
40+
}
41+
return fooHandler
42+
}`,
43+
},
3644
],
3745
invalid: [
3846
{
@@ -609,6 +617,20 @@ describe('S1488', () => {
609617
return parseJson(fs.readFileSync(path.join(schemaDir, schemaFileName), {encoding: 'utf8'}));
610618
});`,
611619
},
620+
{
621+
code: `const makeFooHandler = () => {
622+
const fooHandler = () => {
623+
//
624+
}
625+
return fooHandler
626+
}`,
627+
errors: 1,
628+
output: `const makeFooHandler = () => {
629+
return () => {
630+
//
631+
}
632+
}`,
633+
},
612634
],
613635
});
614636
});

0 commit comments

Comments
 (0)