Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

1.3 KB
Raw Blame
/**
 * Rule: no-zustand-getstate-in-hooks
 *
 * Prevents calling .getState() on Zustand hooks.
 *
 * This rule catches patterns like:
 *   useStore.getState()
 *   useAppStore.getState()
 *
 * Instead, export a standalone action from the store.
 *
 * ESLint equivalent:
 * "no-restricted-syntax": [
 *   "error",
 *   {
 *     "selector": "CallExpression[callee.property.name='getState'][callee.object.name=/^use/]",
 *     "message": "Avoid calling .getState() directly. Export a standalone action from the store instead."
 *   }
 * ]
 */

export default {
	create(context) {
		return {
			CallExpression(node) {
				const callee = node.callee;

				// Check if it's a member expression (e.g., useStore.getState())
				if (callee.type !== 'MemberExpression') {
					return;
				}

				// Check if the property is 'getState'
				const property = callee.property;
				if (property.type !== 'Identifier' || property.name !== 'getState') {
					return;
				}

				// Check if the object name starts with 'use'
				const object = callee.object;
				if (object.type !== 'Identifier' || !object.name.startsWith('use')) {
					return;
				}

				context.report({
					node,
					message:
						'Avoid calling .getState() directly. Export a standalone action from the store instead.',
				});
			},
		};
	},
};