Skip to content

Instantly share code, notes, and snippets.

@stephenway
Last active February 5, 2025 17:51
Show Gist options
  • Save stephenway/08d1a93f1d8c21be1e599c26cd21dc20 to your computer and use it in GitHub Desktop.
Save stephenway/08d1a93f1d8c21be1e599c26cd21dc20 to your computer and use it in GitHub Desktop.
Example of rule customization in ESLint
module.exports = {
// ... other configurations ...
plugins: [
'custom', // Assuming 'custom' is the name of your plugin
],
rules: {
'custom/pascal-case-component': 'warn',
},
};
module.exports = {
rules: {
'pascal-case-component': require('./lib/rules/pascal-case-component'),
},
};
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce PascalCase for React component names',
category: 'Stylistic Issues',
recommended: false,
},
schema: [], // no options
},
create(context) {
return {
FunctionDeclaration(node) {
if (
node.id &&
!/^[A-Z][A-Za-z0-9]*$/.test(node.id.name)
) {
context.report({
node: node.id,
message: 'React component name "{{name}}" should be in PascalCase.',
data: {
name: node.id.name,
},
});
}
},
VariableDeclarator(node) {
if (
node.id &&
node.init &&
(node.init.type === 'ArrowFunctionExpression' || node.init.type === 'FunctionExpression') &&
!/^[A-Z][A-Za-z0-9]*$/.test(node.id.name)
) {
context.report({
node: node.id,
message: 'React component name "{{name}}" should be in PascalCase.',
data: {
name: node.id.name,
},
});
}
},
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment