Skip to content

Instantly share code, notes, and snippets.

@mike-solomon
Created September 2, 2022 18:10
Show Gist options
  • Save mike-solomon/1f191393b5bef696937409491c8c559b to your computer and use it in GitHub Desktop.
Save mike-solomon/1f191393b5bef696937409491c8c559b to your computer and use it in GitHub Desktop.
Example of a custom ESLint rule
/**
* @fileoverview Don't allow bacon in variable names
* @author Mike Solomon
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: "Don't allow bacon in identifiers",
recommended: false,
url: "https://eslint.org/docs/rules/no-bacon",
},
schema: [], // no options
messages: {
avoidBacon: "The word bacon was found in '{{identifier}}'"
}
},
create(context) {
return {
Identifier(node) {
const identifier = node.name;
if (identifier.toLowerCase().includes("bacon")) {
context.report({
node: node,
messageId: 'avoidBacon',
data: {
identifier
}
});
}
}
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment