Skip to content

Error when checking non-const dependency expression #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
duncanbeevers opened this issue Oct 27, 2021 · 0 comments · May be fixed by #5
Open

Error when checking non-const dependency expression #4

duncanbeevers opened this issue Oct 27, 2021 · 0 comments · May be fixed by #5

Comments

@duncanbeevers
Copy link

duncanbeevers commented Oct 27, 2021

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)
duncanbeevers added a commit to duncanbeevers/eslint-plugin-react-memo that referenced this issue Oct 27, 2021
@duncanbeevers duncanbeevers linked a pull request Oct 27, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant