Sub topic 17
Writing a Custom ESLint Rule
This chapter assumes you are comfortable with ES modules, have an eslint.config.js flat config working, and know how rules and severity levels behave. Most teams never need a custom rule, because shared configs and plugins cover most needs. When your project has a convention that no existing rule enforces, you can write a small rule of your own.
How a Rule Sees Your Code
ESLint parses each file into an AST (abstract syntax tree). An AST is a tree of nodes, and each node describes one piece of syntax, such as a function call or an identifier. ESLint walks this tree. A rule's create function returns an object whose keys are node types. ESLint calls the matching function each time it reaches a node of that type. Inside that function, the rule calls context.report() to record a problem.
To check the shape of a node, paste code into an AST explorer such as astexplorer.net and choose the espree parser. In that tree, console.log(x) is a CallExpression whose callee is a MemberExpression with object.name === "console" and property.name === "log".
The Rule Module
// rules/no-console-log.js
export default {
meta: {
type: "suggestion",
docs: { description: "Disallow console.log calls" },
messages: { noLog: "Unexpected console.log." },
schema: [],
},
create(context) {
return {
CallExpression(node) {
const callee = node.callee;
if (
callee.type === "MemberExpression" &&
callee.object.type === "Identifier" &&
callee.object.name === "console" &&
callee.property.type === "Identifier" &&
callee.property.name === "log"
) {
context.report({ node, messageId: "noLog" });
}
},
};
},
};The meta object holds metadata. messages defines reusable message IDs. schema: [] declares that the rule takes no options. ESLint will reject any options a user passes.
Registering It in Flat Config
Flat config has no special folder for rules. You wrap the rule in a plugin object and give that plugin a namespace:
// eslint.config.js
import noConsoleLog from "./rules/no-console-log.js";
export default [
{
files: ["**/*.js"],
plugins: { local: { rules: { "no-console-log": noConsoleLog } } },
rules: { "local/no-console-log": "error" },
},
];Run npx eslint . and every console.log call is reported as local/no-console-log.
Testing the Rule
ESLint ships RuleTester. If no test framework is loaded, it throws an error when a case fails, so plain Node can run it:
// rules/no-console-log.test.js
import { RuleTester } from "eslint";
import rule from "./no-console-log.js";
const tester = new RuleTester();
tester.run("no-console-log", rule, {
valid: ["console.error('x');", "log('x');"],
invalid: [
{ code: "console.log('x');", errors: [{ messageId: "noLog" }] },
],
});
console.log("All tests passed");Exercise
Your rule must also flag console["log"]("x"). What does that callee look like in the AST, and how would you change the check?
Show answer
The callee is a MemberExpression with computed: true, and its property is a Literal with value: "log" rather than an Identifier. Accept either form: (callee.property.type === "Identifier" && !callee.computed && callee.property.name === "log") || (callee.property.type === "Literal" && callee.property.value === "log"). Then add console["log"]("x"); as an invalid case in the RuleTester test.