eslint-plugin-green
Version:
ESLint plugin for evaluating and promoting green coding practices
51 lines (50 loc) • 2.19 kB
JavaScript
;
const rule = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce efficient animation practices',
category: 'Performance',
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 setInterval usage in animations
if (methodName === 'setInterval') {
const parent = context.getAncestors().pop();
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'CallExpression' &&
parent.callee.type === 'MemberExpression' &&
parent.callee.property.type === 'Identifier' &&
parent.callee.property.name === 'style') {
context.report({
node,
message: 'Use requestAnimationFrame instead of setInterval for animations to reduce CPU usage'
});
}
}
}
}
},
JSXOpeningElement(node) {
if (node.name.name === 'style') {
const hasTransform = node.attributes.some((attr) => attr.name.name === 'transform');
const hasWillChange = node.attributes.some((attr) => attr.name.name === 'willChange');
if (hasTransform && !hasWillChange) {
context.report({
node,
message: 'Add will-change property for elements with transform animations to optimize GPU usage'
});
}
}
}
};
}
};
module.exports = rule;