Skip to content

Instantly share code, notes, and snippets.

@EduardoSimon
Created January 27, 2025 14:49
Show Gist options
  • Save EduardoSimon/b98174d9f02265e193d80af6e2efea28 to your computer and use it in GitHub Desktop.
Save EduardoSimon/b98174d9f02265e193d80af6e2efea28 to your computer and use it in GitHub Desktop.
Draft eslint plugin to prevent css classes to be removed from the code
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow removal of specific CSS classes',
category: 'Best Practices',
},
schema: [], // No options
},
create(context) {
const classesToKeep = ['specific-class-to-keep', /^regex-to-keep-classes$/];
return {
Literal(node) {
if (typeof node.value === 'string') {
classesToKeep.forEach((cls) => {
const regex = cls instanceof RegExp ? cls : new RegExp(`\\b${cls}\\b`);
if (regex.test(node.value)) {
context.report({
node,
message: `The class "${cls}" should not be removed.`,
});
}
});
}
},
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment