mirror of
https://github.com/antebudimir/eslint-plugin-vanilla-extract.git
synced 2026-01-01 09:23:31 +00:00
Add comprehensive rule to detect and prevent empty CSS style blocks: - Identify style objects with no properties - Flag empty style blocks as potential code quality issues - Provide auto-fix capability to remove empty blocks - Handle edge cases like comments-only blocks This rule helps maintain cleaner codebases by eliminating empty style definitions that often result from incomplete refactoring or forgotten implementations, reducing confusion and unnecessary code.
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import type { Rule } from 'eslint';
|
|
import { TSESTree } from '@typescript-eslint/utils';
|
|
import { isEmptyObject } from '../shared-utils/empty-object-processor.js';
|
|
import { reportEmptyDeclaration } from './fix-utils.js';
|
|
|
|
/**
|
|
* Handles conditional expressions with empty objects.
|
|
*/
|
|
export function processConditionalExpression(
|
|
context: Rule.RuleContext,
|
|
node: TSESTree.ConditionalExpression,
|
|
reportedNodes: Set<TSESTree.Node>,
|
|
callNode: TSESTree.CallExpression,
|
|
): void {
|
|
const isConsequentEmpty = node.consequent.type === 'ObjectExpression' && isEmptyObject(node.consequent);
|
|
const isAlternateEmpty = node.alternate.type === 'ObjectExpression' && isEmptyObject(node.alternate);
|
|
|
|
// If both branches are empty, report the entire declaration for removal
|
|
if (isConsequentEmpty && isAlternateEmpty) {
|
|
reportedNodes.add(node);
|
|
reportEmptyDeclaration(context, node, callNode);
|
|
return;
|
|
}
|
|
|
|
// Otherwise, handle individual empty branches
|
|
if (isConsequentEmpty || isAlternateEmpty) {
|
|
const emptyNode = isConsequentEmpty ? node.consequent : node.alternate;
|
|
reportedNodes.add(emptyNode);
|
|
|
|
// No fix provided, just flagging the issue
|
|
context.report({
|
|
node: emptyNode as Rule.Node,
|
|
messageId: 'emptyConditionalStyle',
|
|
});
|
|
}
|
|
}
|