From 5d0489f7f5a51303ae2b515f57151e4fd6a9a36b Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sun, 31 Mar 2024 22:13:37 +0900 Subject: [PATCH 1/3] fix(eslint-plugin): [explicit-function-return-type] fix checking wrong ancestor's return type --- .../src/util/explicitReturnTypeUtils.ts | 13 ++-- .../explicit-function-return-type.test.ts | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index 1cfc834c1c1b..2604e6185cc0 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -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; diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index 2036927e0ca9..8ab7fd538f05 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -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'; }; From 00b1e57526f97c5ca8412b6734c136b804532f48 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Mon, 1 Apr 2024 22:30:57 +0900 Subject: [PATCH 2/3] fix lint errors --- .../src/components/editor/LoadedEditor.tsx | 20 ++++++++++--------- packages/website/src/hooks/useMediaQuery.ts | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/website/src/components/editor/LoadedEditor.tsx b/packages/website/src/components/editor/LoadedEditor.tsx index 01434408db7d..6dab67a39278 100644 --- a/packages/website/src/components/editor/LoadedEditor.tsx +++ b/packages/website/src/components/editor/LoadedEditor.tsx @@ -135,14 +135,14 @@ export const LoadedEditor: React.FC = ({ 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(() => { @@ -178,7 +178,7 @@ export const LoadedEditor: React.FC = ({ 'typescript', createProvideCodeActions(codeActions), ); - return () => disposable.dispose(); + return (): void => disposable.dispose(); }, [codeActions, monaco]); useEffect(() => { @@ -191,7 +191,9 @@ export const LoadedEditor: React.FC = ({ } } }); - return () => disposable.dispose(); + return (): void => { + disposable.dispose(); + }; }, [editor, tabs.eslintrc]); useEffect(() => { @@ -204,7 +206,7 @@ export const LoadedEditor: React.FC = ({ } }, 150), ); - return () => disposable.dispose(); + return (): void => disposable.dispose(); }, [onSelect, editor, tabs.code]); useEffect(() => { @@ -227,7 +229,7 @@ export const LoadedEditor: React.FC = ({ } }, }); - return () => disposable.dispose(); + return (): void => disposable.dispose(); }, [editor, monaco, webLinter]); useEffect(() => { @@ -243,7 +245,7 @@ export const LoadedEditor: React.FC = ({ }), ]; - return () => { + return (): void => { closable.forEach(c => c.close()); }; }, [system, onChange]); @@ -255,14 +257,14 @@ export const LoadedEditor: React.FC = ({ 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(() => { diff --git a/packages/website/src/hooks/useMediaQuery.ts b/packages/website/src/hooks/useMediaQuery.ts index a092478ac882..422570aee8ef 100644 --- a/packages/website/src/hooks/useMediaQuery.ts +++ b/packages/website/src/hooks/useMediaQuery.ts @@ -29,7 +29,7 @@ const useMediaQuery = (mediaQuery: string): boolean => { } documentChangeHandler(); - return () => { + return (): void => { try { mediaQueryList.removeEventListener('change', documentChangeHandler); } catch { From 7a6dd1a90f9244b05dd6023fd1d77f9cc5d77b04 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sat, 20 Apr 2024 15:14:50 +0900 Subject: [PATCH 3/3] add test cases --- .../explicit-function-return-type.test.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index 8ab7fd538f05..7556cee3ce77 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -1239,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 }],