Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e0d9f5

Browse files
yeonjuanauvred
andauthored
fix(eslint-plugin): [explicit-function-return-type] fix checking wrong ancestor's return type (typescript-eslint#8809)
* fix(eslint-plugin): [explicit-function-return-type] fix checking wrong ancestor's return type * fix lint errors * add test cases --------- Co-authored-by: auvred <[email protected]>
1 parent 5f38325 commit 9e0d9f5

File tree

4 files changed

+110
-18
lines changed

4 files changed

+110
-18
lines changed

packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,12 @@ function ancestorHasReturnType(node: FunctionNode): boolean {
337337
// const x: Foo = () => {};
338338
// Assume that a typed variable types the function expression
339339
case AST_NODE_TYPES.VariableDeclarator:
340-
if (ancestor.id.typeAnnotation) {
341-
return true;
342-
}
343-
break;
340+
return !!ancestor.id.typeAnnotation;
341+
344342
case AST_NODE_TYPES.PropertyDefinition:
345-
if (ancestor.typeAnnotation) {
346-
return true;
347-
}
348-
break;
343+
return !!ancestor.typeAnnotation;
344+
case AST_NODE_TYPES.ExpressionStatement:
345+
return false;
349346
}
350347

351348
ancestor = ancestor.parent;

packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,74 @@ class Foo {
10161016
},
10171017
{
10181018
code: `
1019+
function foo(): any {
1020+
const bar = () => () => console.log('aa');
1021+
}
1022+
`,
1023+
options: [
1024+
{
1025+
allowTypedFunctionExpressions: true,
1026+
},
1027+
],
1028+
errors: [
1029+
{
1030+
messageId: 'missingReturnType',
1031+
line: 3,
1032+
endLine: 3,
1033+
column: 24,
1034+
endColumn: 26,
1035+
},
1036+
],
1037+
},
1038+
{
1039+
code: `
1040+
let anyValue: any;
1041+
function foo(): any {
1042+
anyValue = () => () => console.log('aa');
1043+
}
1044+
`,
1045+
options: [
1046+
{
1047+
allowTypedFunctionExpressions: true,
1048+
},
1049+
],
1050+
errors: [
1051+
{
1052+
messageId: 'missingReturnType',
1053+
line: 4,
1054+
endLine: 4,
1055+
column: 23,
1056+
endColumn: 25,
1057+
},
1058+
],
1059+
},
1060+
{
1061+
code: `
1062+
class Foo {
1063+
foo(): any {
1064+
const bar = () => () => {
1065+
return console.log('foo');
1066+
};
1067+
}
1068+
}
1069+
`,
1070+
options: [
1071+
{
1072+
allowTypedFunctionExpressions: true,
1073+
},
1074+
],
1075+
errors: [
1076+
{
1077+
messageId: 'missingReturnType',
1078+
line: 4,
1079+
endLine: 4,
1080+
column: 26,
1081+
endColumn: 28,
1082+
},
1083+
],
1084+
},
1085+
{
1086+
code: `
10191087
var funcExpr = function () {
10201088
return 'test';
10211089
};
@@ -1171,6 +1239,31 @@ const x: Foo = {
11711239
},
11721240
],
11731241
},
1242+
{
1243+
code: `
1244+
function foo(): any {
1245+
class Foo {
1246+
foo = () => () => {
1247+
return console.log('foo');
1248+
};
1249+
}
1250+
}
1251+
`,
1252+
options: [
1253+
{
1254+
allowTypedFunctionExpressions: true,
1255+
},
1256+
],
1257+
errors: [
1258+
{
1259+
messageId: 'missingReturnType',
1260+
line: 4,
1261+
endLine: 4,
1262+
column: 20,
1263+
endColumn: 22,
1264+
},
1265+
],
1266+
},
11741267
{
11751268
code: '() => () => {};',
11761269
options: [{ allowHigherOrderFunctions: true }],

packages/website/src/components/editor/LoadedEditor.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
135135
monaco.editor.setModelMarkers(model, 'eslint', diagnostics);
136136
updateMarkers();
137137
});
138-
return () => disposable();
138+
return (): void => disposable();
139139
}, [webLinter, monaco, codeActions, updateMarkers]);
140140

141141
useEffect(() => {
142142
const disposable = webLinter.onParse((uri, model) => {
143143
onASTChange(model);
144144
});
145-
return () => disposable();
145+
return (): void => disposable();
146146
}, [webLinter, onASTChange]);
147147

148148
useEffect(() => {
@@ -178,7 +178,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
178178
'typescript',
179179
createProvideCodeActions(codeActions),
180180
);
181-
return () => disposable.dispose();
181+
return (): void => disposable.dispose();
182182
}, [codeActions, monaco]);
183183

184184
useEffect(() => {
@@ -191,7 +191,9 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
191191
}
192192
}
193193
});
194-
return () => disposable.dispose();
194+
return (): void => {
195+
disposable.dispose();
196+
};
195197
}, [editor, tabs.eslintrc]);
196198

197199
useEffect(() => {
@@ -204,7 +206,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
204206
}
205207
}, 150),
206208
);
207-
return () => disposable.dispose();
209+
return (): void => disposable.dispose();
208210
}, [onSelect, editor, tabs.code]);
209211

210212
useEffect(() => {
@@ -227,7 +229,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
227229
}
228230
},
229231
});
230-
return () => disposable.dispose();
232+
return (): void => disposable.dispose();
231233
}, [editor, monaco, webLinter]);
232234

233235
useEffect(() => {
@@ -243,7 +245,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
243245
}),
244246
];
245247

246-
return () => {
248+
return (): void => {
247249
closable.forEach(c => c.close());
248250
};
249251
}, [system, onChange]);
@@ -255,14 +257,14 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
255257
system.writeFile(model.uri.path, model.getValue());
256258
}
257259
});
258-
return () => disposable.dispose();
260+
return (): void => disposable.dispose();
259261
}, [editor, system]);
260262

261263
useEffect(() => {
262264
const disposable = monaco.editor.onDidChangeMarkers(() => {
263265
updateMarkers();
264266
});
265-
return () => disposable.dispose();
267+
return (): void => disposable.dispose();
266268
}, [monaco.editor, updateMarkers]);
267269

268270
const resize = useMemo(() => {

packages/website/src/hooks/useMediaQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const useMediaQuery = (mediaQuery: string): boolean => {
2929
}
3030

3131
documentChangeHandler();
32-
return () => {
32+
return (): void => {
3333
try {
3434
mediaQueryList.removeEventListener('change', documentChangeHandler);
3535
} catch {

0 commit comments

Comments
 (0)