UNPKG

eslint-plugin-green

Version:

ESLint plugin for evaluating and promoting green coding practices

53 lines (52 loc) 2.44 kB
"use strict"; const rule = { meta: { type: 'suggestion', docs: { description: 'Enforce efficient CSS selector usage', category: 'CSS', recommended: true, }, schema: [], // no options }, create(context) { return { CallExpression(node) { if (node.callee.type === 'MemberExpression') { const callee = node.callee; if (callee.property.type === 'Identifier') { const methodName = callee.property.name; // Check for inefficient querySelector usage if (methodName === 'querySelector' || methodName === 'querySelectorAll') { if (node.arguments.length > 0 && node.arguments[0].type === 'Literal') { const selector = node.arguments[0].value; // Check for inefficient universal selectors if (selector.includes('*')) { context.report({ node, message: 'Avoid using universal selectors (*) as they are performance intensive' }); } // Check for inefficient descendant selectors if (selector.includes(' > ') || /\s+(?!=)/.test(selector)) { context.report({ node, message: 'Consider using more specific selectors instead of descendant selectors for better performance' }); } // Check for inefficient ID selectors with unnecessary qualifiers if (/[a-zA-Z]+#/.test(selector)) { context.report({ node, message: 'Avoid qualifying ID selectors with tag names or classes' }); } } } } } } }; } }; module.exports = rule;