-
-
Save mike-solomon/1f191393b5bef696937409491c8c559b to your computer and use it in GitHub Desktop.
Example of a custom ESLint rule
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
/** | |
* @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