|
| 1 | +/** |
| 2 | + * @typedef {import('eslint').Rule.RuleModule} RuleModule |
| 3 | + */ |
| 4 | + |
| 5 | +/** @type {RuleModule} */ |
| 6 | +module.exports = { |
| 7 | + name: 'no-unstable-useOnyx-defaults', |
| 8 | + meta: { |
| 9 | + type: 'problem', |
| 10 | + docs: { |
| 11 | + description: 'Disallow inline array or object literals as default values in useOnyx. Use getEmptyArray(), getEmptyObject(), a constant, or a memoized value instead.', |
| 12 | + recommended: 'error', |
| 13 | + }, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + noEmptyArrayDefault: 'Avoid using empty array [] as default value in useOnyx. Use getEmptyArray<T>() instead.', |
| 17 | + noInlineArrayDefault: 'Avoid using array literal as default value in useOnyx. Use a memoized value or a constant instead.', |
| 18 | + noEmptyObjectDefault: 'Avoid using empty object {} as default value in useOnyx. Use getEmptyObject<T>() instead.', |
| 19 | + noInlineObjectDefault: 'Avoid using object literal as default value in useOnyx. Use a memoized value or a constant instead.', |
| 20 | + noUnstableIdentifierDefault: 'Default value must be memoized with useMemo or declared as a top-level constant.', |
| 21 | + }, |
| 22 | + }, |
| 23 | + create(context) { |
| 24 | + return { |
| 25 | + VariableDeclarator(node) { |
| 26 | + if ( |
| 27 | + !node.init |
| 28 | + || node.init.type !== 'CallExpression' |
| 29 | + || node.init.callee.name !== 'useOnyx' |
| 30 | + ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Check if the variable declaration is an array pattern (destructuring) |
| 35 | + if (node.id.type !== 'ArrayPattern') { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + node.id.elements.forEach((element) => { |
| 40 | + if (!element || element.type !== 'AssignmentPattern') { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const defaultValue = element.right; |
| 45 | + |
| 46 | + // Check for inline array default |
| 47 | + if (defaultValue.type === 'ArrayExpression') { |
| 48 | + const isEmpty = defaultValue.elements.length === 0; |
| 49 | + context.report({ |
| 50 | + node: defaultValue, |
| 51 | + messageId: isEmpty ? 'noEmptyArrayDefault' : 'noInlineArrayDefault', |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Check for inline object default |
| 56 | + if (defaultValue.type === 'ObjectExpression') { |
| 57 | + const isEmpty = defaultValue.properties.length === 0; |
| 58 | + context.report({ |
| 59 | + node: defaultValue, |
| 60 | + messageId: isEmpty ? 'noEmptyObjectDefault' : 'noInlineObjectDefault', |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // If it's an Identifier, verify it's memoized or top-level const |
| 65 | + if (defaultValue.type === 'Identifier') { |
| 66 | + const scope = context.getScope(); |
| 67 | + const variable = scope.set.get(defaultValue.name); |
| 68 | + |
| 69 | + if ( |
| 70 | + variable |
| 71 | + && variable.defs.length > 0 |
| 72 | + && variable.defs[0].node.type === 'VariableDeclarator' |
| 73 | + ) { |
| 74 | + const def = variable.defs[0].node; |
| 75 | + const init = def.init; |
| 76 | + |
| 77 | + const isMemoized = init |
| 78 | + && init.type === 'CallExpression' |
| 79 | + && init.callee.type === 'Identifier' |
| 80 | + && init.callee.name === 'useMemo'; |
| 81 | + |
| 82 | + const isTopLevelConst = variable.scope.type === 'module' |
| 83 | + && variable.defs[0].parent.kind === 'const'; |
| 84 | + |
| 85 | + if (!isMemoized && !isTopLevelConst) { |
| 86 | + context.report({ |
| 87 | + node: defaultValue, |
| 88 | + messageId: 'noUnstableIdentifierDefault', |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + }, |
| 95 | + }; |
| 96 | + }, |
| 97 | +}; |
0 commit comments