Skip to content

Error when checking non-const dependency expression #4

Description

@duncanbeevers

I added this test-case that demonstrates the error. When examining a useCallback or useMemo dependency defined not as a const, but as a let with no init value, the getIdentifierMemoStatus method fails.

diff --git a/__tests__/require-usememo.ts b/__tests__/require-usememo.ts
index cbe60de..fe24d0c 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 <Child prop={myObject} />;
+      }`,
+      errors: [{ messageId: "object-usememo-props" }],
+    },
   ],
 });

OUTPUT

  ● useMemo › invalid › const Component = () => {
        let myObject;
        myObject = {};
        return <Child prop={myObject} />;
      }

    TypeError: Cannot read property 'type' of null
    Occurred while linting <input>:4

      62 |   expression: TSESTree.Expression
      63 | ): MemoStatus {
    > 64 |   switch (expression.type) {
         |                      ^
      65 |     case "ObjectExpression":
      66 |       return MemoStatus.UnmemoizedObject;
      67 |     case "ArrayExpression":

      at getExpressionMemoStatus (common.ts:64:22)
      at getIdentifierMemoStatus (common.ts:57:10)
      at Object.getExpressionMemoStatus (common.ts:85:14)
      at JSXAttribute (require-usememo.ts:81:21)
      at node_modules/eslint/lib/linter/safe-emitter.js:45:58
          at Array.forEach (<anonymous>)
      at Object.emit (node_modules/eslint/lib/linter/safe-emitter.js:45:38)
      at NodeEventGenerator.applySelector (node_modules/eslint/lib/linter/node-event-generator.js:256:26)
      at NodeEventGenerator.applySelectors (node_modules/eslint/lib/linter/node-event-generator.js:285:22)
      at NodeEventGenerator.enterNode (node_modules/eslint/lib/linter/node-event-generator.js:299:14)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions