Skip to content

Commit

Permalink
fix(eslint-plugin): [explicit-function-return-type] fix checking wron…
Browse files Browse the repository at this point in the history
…g ancestor's return type (#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 <61150013+auvred@users.noreply.github.com>
  • Loading branch information
yeonjuan and auvred committed Apr 20, 2024
1 parent 5f38325 commit 9e0d9f5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 18 deletions.
13 changes: 5 additions & 8 deletions packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,12 @@ function ancestorHasReturnType(node: FunctionNode): boolean {
// const x: Foo = () => {};
// Assume that a typed variable types the function expression
case AST_NODE_TYPES.VariableDeclarator:
if (ancestor.id.typeAnnotation) {
return true;
}
break;
return !!ancestor.id.typeAnnotation;

case AST_NODE_TYPES.PropertyDefinition:
if (ancestor.typeAnnotation) {
return true;
}
break;
return !!ancestor.typeAnnotation;
case AST_NODE_TYPES.ExpressionStatement:
return false;
}

ancestor = ancestor.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,74 @@ class Foo {
},
{
code: `
function foo(): any {
const bar = () => () => console.log('aa');
}
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 3,
endLine: 3,
column: 24,
endColumn: 26,
},
],
},
{
code: `
let anyValue: any;
function foo(): any {
anyValue = () => () => console.log('aa');
}
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 4,
endLine: 4,
column: 23,
endColumn: 25,
},
],
},
{
code: `
class Foo {
foo(): any {
const bar = () => () => {
return console.log('foo');
};
}
}
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 4,
endLine: 4,
column: 26,
endColumn: 28,
},
],
},
{
code: `
var funcExpr = function () {
return 'test';
};
Expand Down Expand Up @@ -1171,6 +1239,31 @@ const x: Foo = {
},
],
},
{
code: `
function foo(): any {
class Foo {
foo = () => () => {
return console.log('foo');
};
}
}
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 4,
endLine: 4,
column: 20,
endColumn: 22,
},
],
},
{
code: '() => () => {};',
options: [{ allowHigherOrderFunctions: true }],
Expand Down
20 changes: 11 additions & 9 deletions packages/website/src/components/editor/LoadedEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
monaco.editor.setModelMarkers(model, 'eslint', diagnostics);
updateMarkers();
});
return () => disposable();
return (): void => disposable();
}, [webLinter, monaco, codeActions, updateMarkers]);

useEffect(() => {
const disposable = webLinter.onParse((uri, model) => {
onASTChange(model);
});
return () => disposable();
return (): void => disposable();
}, [webLinter, onASTChange]);

useEffect(() => {
Expand Down Expand Up @@ -178,7 +178,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
'typescript',
createProvideCodeActions(codeActions),
);
return () => disposable.dispose();
return (): void => disposable.dispose();
}, [codeActions, monaco]);

useEffect(() => {
Expand All @@ -191,7 +191,9 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
}
}
});
return () => disposable.dispose();
return (): void => {
disposable.dispose();
};
}, [editor, tabs.eslintrc]);

useEffect(() => {
Expand All @@ -204,7 +206,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
}
}, 150),
);
return () => disposable.dispose();
return (): void => disposable.dispose();
}, [onSelect, editor, tabs.code]);

useEffect(() => {
Expand All @@ -227,7 +229,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
}
},
});
return () => disposable.dispose();
return (): void => disposable.dispose();
}, [editor, monaco, webLinter]);

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

return () => {
return (): void => {
closable.forEach(c => c.close());
};
}, [system, onChange]);
Expand All @@ -255,14 +257,14 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
system.writeFile(model.uri.path, model.getValue());
}
});
return () => disposable.dispose();
return (): void => disposable.dispose();
}, [editor, system]);

useEffect(() => {
const disposable = monaco.editor.onDidChangeMarkers(() => {
updateMarkers();
});
return () => disposable.dispose();
return (): void => disposable.dispose();
}, [monaco.editor, updateMarkers]);

const resize = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/hooks/useMediaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const useMediaQuery = (mediaQuery: string): boolean => {
}

documentChangeHandler();
return () => {
return (): void => {
try {
mediaQueryList.removeEventListener('change', documentChangeHandler);
} catch {
Expand Down

0 comments on commit 9e0d9f5

Please sign in to comment.