Appearance
Adding a New Lint Rule
How to add a new rule to the code-style linter.
Process
When the user identifies a new style rule to enforce:
Capture examples from the user. Ask for or collect concrete good and bad code examples. These become the test fixtures.
Create the rule folder. Each rule lives in its own directory under the appropriate language:
lint/{language}/{rule-name}/ {rule-name}.js — the rule {rule-name}.test.js — tests {rule-name}.test.good.{ext} — code that should pass {rule-name}.test.bad.{ext} — code that should failWrite the test fixtures first. Put the user's good examples in
{rule-name}.test.good.{ext}and bad examples in{rule-name}.test.bad.{ext}.Write the rule. Import
Rulefrom the engine. For AST-based rules, use.on(nodeType, handler). For line-based rules, use.onLine(handler).jsimport { Rule } from "../../engine.js"; const myRule = Rule(); myRule.on("node_type", (node, { file, lines }) => { return [{ file, line: ..., content: ... }]; }); export default myRule;Write the test. Run the linter CLI against the good and bad fixtures. Assert the good file passes and the bad file fails with the expected violation count and line numbers.
Register the rule. Add it to the language's
index.js:jsimport myRule from "./my-rule/my-rule.js"; javascript.use(myRule);For shared rules (tabs, line-length), put them in
common/and import from each language that needs them.Verify. Run all tests and lint the linter's own code:
shnode --test skills/code-style/lint/**/*.test.js