Files
672093f177 feat: Add meta.languages support to rules (#20571)
* Initial plan

* Add meta.languages validation to validateRulesConfig

Co-authored-by: nzakas <[email protected]>

* docs: update custom-rules and custom-rule-tutorial for meta.languages

Co-authored-by: nzakas <[email protected]>

* fix: address review feedback on meta.languages validation

Co-authored-by: nzakas <[email protected]>

* refactor: normalize @/ to js/ before calling doesRuleSupportLanguage, removing special cases

Co-authored-by: fasttime <[email protected]>

* refactor: precompute plugin namespace map to replace for-loops in doesRuleSupportLanguage

Co-authored-by: fasttime <[email protected]>

* refactor: simplify buildPluginNamespaceMap to include all plugins with name fallback

Co-authored-by: fasttime <[email protected]>

* refactor: compute pluginNamespaceMap once in validateRulesConfig and pass to doesRuleSupportLanguage

Co-authored-by: fasttime <[email protected]>

* refactor: simplify doesRuleSupportLanguage guard to !Array.isArray(ruleLangs)

Co-authored-by: fasttime <[email protected]>

* refactor: replace pluginNamespaceMap with validPluginNames in doesRuleSupportLanguage

Co-authored-by: fasttime <[email protected]>

* fix: replace undefined with void 0 (no-undefined rule), add direct-match rule to namespace test

Co-authored-by: fasttime <[email protected]>

* fix: only validate meta.languages when rule is enabled, not at Config construction time

Co-authored-by: nzakas <[email protected]>

* fix: simplify doesRuleSupportLanguage guard and add namespace-match doc example

Co-authored-by: fasttime <[email protected]>

* docs: use more familiar names in namespace-match example

Co-authored-by: fasttime <[email protected]>

* fix: convert inline comment to block comment to fix linting error in test

Co-authored-by: fasttime <[email protected]>

* fix: apply prettier formatting to config.js and tests/lib/config/config.js

Co-authored-by: fasttime <[email protected]>

* fix: improve error message for rules that don't support the current language

Co-authored-by: nzakas <[email protected]>
Agent-Logs-Url: https://github.com/eslint/eslint/sessions/fad3a342-bb5e-47f5-99f0-bd79c80f569b

* Update tests/lib/linter/linter.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update docs/src/extend/custom-rules.md

Co-authored-by: Milos Djermanovic <[email protected]>

* Formatting

* test: add test for rule-unsupported-language message template

Agent-Logs-Url: https://github.com/eslint/eslint/sessions/475e3c79-203c-408a-a5aa-cdc0a525a2f0

Co-authored-by: fasttime <[email protected]>

---------

Co-authored-by: nzakas <[email protected]>
Co-authored-by: fasttime <[email protected]>
Co-authored-by: Nicholas C. Zakas <[email protected]>
Co-authored-by: Milos Djermanovic <[email protected]>

Co-authored-by: Hanzo Dev <[email protected]>
2026-03-26 18:00:18 +01:00

65 lines
1.5 KiB
JavaScript

/**
* @fileoverview Rule to enforce that `const foo` is assigned "bar".
* @author Ben Perlmutter
*/
"use strict";
// The enforce-foo-bar rule definition
module.exports = {
meta: {
type: "problem",
docs: {
description:
"Enforce that a variable named `foo` can only be assigned a value of 'bar'.",
},
fixable: "code",
schema: [],
languages: ["js/js"],
},
create(context) {
return {
// Performs action in the function on every variable declarator
VariableDeclarator(node) {
// Check if a `const` variable declaration
if (node.parent.kind === "const") {
// Check if variable name is `foo`
if (
node.id.type === "Identifier" &&
node.id.name === "foo"
) {
// Check if value of variable is "bar"
if (
node.init &&
node.init.type === "Literal" &&
node.init.value !== "bar"
) {
/*
* Report error to ESLint. Error message uses
* a message placeholder to include the incorrect value
* in the error message.
* Also includes a `fix(fixer)` function that replaces
* any values assigned to `const foo` with "bar".
*/
context.report({
node,
message:
'Value other than "bar" assigned to `const foo`. Unexpected value: {{ notBar }}.',
data: {
notBar: node.init.value,
},
fix(fixer) {
return fixer.replaceText(
node.init,
'"bar"',
);
},
});
}
}
}
},
};
},
};