From dd2d202d1b65c58ec0ea394ae2372e68af5438af Mon Sep 17 00:00:00 2001 From: Duncan Beevers Date: Wed, 27 Oct 2021 16:33:02 -0700 Subject: [PATCH] Handle no-init let-declared dependency identifier Closes #4 --- __tests__/require-usememo.ts | 8 ++++++++ common.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/__tests__/require-usememo.ts b/__tests__/require-usememo.ts index cbe60de..19268c8 100644 --- a/__tests__/require-usememo.ts +++ b/__tests__/require-usememo.ts @@ -142,5 +142,13 @@ ruleTester.run("useMemo", rule, { options: [{ strict: true }], errors: [{ messageId: "unknown-usememo-props" }], }, + { + code: `const Component = () => { + let myObject; + myObject = {}; + return ; + }`, + errors: [{ messageId: "usememo-const" }], + }, ], }); diff --git a/common.ts b/common.ts index 4512d15..16b9698 100644 --- a/common.ts +++ b/common.ts @@ -53,6 +53,10 @@ function getIdentifierMemoStatus( if (node.type !== "VariableDeclarator") return MemoStatus.Memoized; if (node.parent.kind === "let") { context.report({ node, messageId: "usememo-const" }); + if (!node.init) { + // Rely on usememo-const reported error to fail this identifier + return MemoStatus.Memoized; + } } return getExpressionMemoStatus(context, node.init); }