Skip to content

Commit 874d916

Browse files
authored
Fix: 24 | additional null check for initial call arguments (#25)
* add null check for args on proxy member expressions, for any calls that do not have any args * fix snapshot for no deps on useEffect additional test case for snapshot changes where the useEffect might not have any deps and still needs to error out
1 parent f9e1b71 commit 874d916

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/StateSnapshot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ function isUsedInUseProxy(node, scope) {
307307
(init.type === 'CallExpression' && init.callee.name === 'useSnapshot')
308308
) {
309309
if (
310-
(init.arguments[0] &&
310+
(init.arguments.length > 0 &&
311+
init.arguments[0] &&
311312
init.arguments[0]._babelType === 'MemberExpression' &&
312313
node._babelType === 'MemberExpression') ||
313314
(init.arguments[0] &&

src/lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ export function isInHookDeps(node) {
205205
}
206206

207207
const flatDepPaths = []
208-
allDepExpressions.elements.forEach((exprNode) => {
208+
209+
// Handle cases where the elements might not exist at all ,
210+
// aka useEffect(()=>{}) without a dep array
211+
;(allDepExpressions.elements || []).forEach((exprNode) => {
209212
let exprPath
210213
if (exprNode.type === 'MemberExpression') {
211214
exprPath = flattenMemberExpression(exprNode)

tests/StateSnapshot.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,18 @@ ruleTester.run('state-snapshot-rule', rule, {
317317
}`,
318318
errors: [SNAPSHOT_CALLBACK_MESSAGE],
319319
},
320+
{
321+
code: `
322+
function useExample2(s) {
323+
const {b: {c} } = useSnapshot(s.a1);
324+
325+
useEffect(() => {
326+
if (c === 'a1c') {
327+
state.a1.b.c = 'example';
328+
}
329+
});
330+
}`,
331+
errors: [SNAPSHOT_CALLBACK_MESSAGE],
332+
},
320333
],
321334
})

0 commit comments

Comments
 (0)