Last active
February 5, 2025 17:51
-
-
Save stephenway/08d1a93f1d8c21be1e599c26cd21dc20 to your computer and use it in GitHub Desktop.
Example of rule customization in ESLint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
// ... other configurations ... | |
plugins: [ | |
'custom', // Assuming 'custom' is the name of your plugin | |
], | |
rules: { | |
'custom/pascal-case-component': 'warn', | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
rules: { | |
'pascal-case-component': require('./lib/rules/pascal-case-component'), | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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