feat: Add meta.languages support to rules#20571
Conversation
|
Hi @Copilot!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
✅ Deploy Preview for docs-eslint canceled.
|
|
Hi @Copilot!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
Co-authored-by: nzakas <[email protected]>
|
Hi @Copilot!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
1 similar comment
|
Hi @Copilot!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
meta.languages support to rules for language-aware validation
|
Hi @Copilot!, thanks for the Pull Request The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.
To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page. Read more about contributing to ESLint here |
nzakas
left a comment
There was a problem hiding this comment.
@copilot The code looks good. We now need to update these docs pages to explain how to use this feature:
https://eslint.org/docs/latest/extend/custom-rules
https://eslint.org/docs/latest/extend/custom-rule-tutorial
meta.languages support to rules for language-aware validationmeta.languages support to rules
Co-authored-by: nzakas <[email protected]>
Done in commit 3c2483e. Updated both docs pages:
|
nzakas
left a comment
There was a problem hiding this comment.
LGTM. Would like another review before merging.
| } | ||
|
|
||
| const { pluginName: rulePluginPart, objectName: ruleLangPart } = | ||
| splitPluginIdentifier(langEntry); |
There was a problem hiding this comment.
The value for the language may be a non-string which would lead to a type error here as splitPluginIdentifier uses String#split.
There was a problem hiding this comment.
Fixed in commit 5645358. Added a typeof langEntry !== "string" guard in the loop that skips non-string entries. The pluginsSchema.validate now also throws at config construction time if any entry is not a string, so the runtime defense is a belt-and-suspenders safeguard for any code paths that might bypass schema validation.
mdjermanovic
left a comment
There was a problem hiding this comment.
For rules enabled inline (e.g., /* eslint my-plugin/my-rule: 2 */), the logic in Linter cuts off the "The following rules do not support the language..." part, leaving a lint error message that doesn't really explain why the config is invalid:
1:1 error Inline configuration for rule "my-plugin/my-rule" is invalid:
- "my-plugin/my-rule"
my-plugin/my-rule
|
|
||
| if (unsupportedLanguageRules.length > 0) { | ||
| throw new TypeError( | ||
| `Key "rules": The following rules do not support the language "${normalizedLanguageName}":\n${unsupportedLanguageRules.map(ruleId => `\t- "${ruleId}"`).join("\n")}`, |
There was a problem hiding this comment.
The main problem is likely that users activate the JavaScript rules for all files and then will be struck with this error when using an additional language (e.g. JSON).
So users reading this error message should know to solve this problem (e.g. by turning off the rule or using files to only activate the rule for matching files).
|
@copilot address the concerns brought up by @mdjermanovic and @DMartens. For the error message, we can use an error message template file to create a more verbose and helpful error message to display to the user. |
Done in commit 239a0a7:
Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
mdjermanovic
left a comment
There was a problem hiding this comment.
To fix formatting errors.
Co-authored-by: Milos Djermanovic <[email protected]>
Co-authored-by: Milos Djermanovic <[email protected]>
mdjermanovic
left a comment
There was a problem hiding this comment.
LGTM. Leaving open for @fasttime to verify changes after his approval.
There was a problem hiding this comment.
We should add a test for this message template, similar to the existing ones in tests/messages. @copilot
There was a problem hiding this comment.
Added in commit bcfe9f9. Added a describe("rule-unsupported-language", ...) block to tests/messages/messages.js with two tests: one for a single rule ID (verifying the language name, rule ID, files example, and help URL appear in the output) and one for multiple rule IDs (verifying both IDs appear).
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| }, | ||
| fixable: "code", | ||
| schema: [], | ||
| languages: ["js/js"], |
Implements RFC 2025-rule-languages: adds a
meta.languagesarray to rule metadata, validated at config time against the active language.Changes
lib/config/config.jsdoesRuleSupportLanguage(ruleLangs, configLanguageName, validPluginNames)helper with full matching logicArray.isArray()andtypeof === "string"guards for invalidmeta.languagesvaluesvalidateRulesConfig()to collect rules whosemeta.languagesdon't match the config language and throw a descriptiveTypeErrorlisting all offenders@/tojs/in the language name shown in error messages (e.g."js/js"instead of"@/js")meta.languagesstructure (must be an array of strings) insidevalidateRulesConfig(), only for enabled rulesmessageTemplate = "rule-unsupported-language"andmessageDatato the thrown error, enabling both verbose terminal output and clear inline-config error messageslib/config/flat-config-schema.jsmeta.languagesatConfigconstruction time; validation is deferred tovalidateRulesConfig()so rules in plugins with invalidmeta.languagesdo not cause errors unless the rule is actually enabledlib/linter/linter.jsrule-unsupported-languagemessage template: when a rule enabled via inline config (e.g./* eslint my-plugin/my-rule: 2 */) doesn't support the active language, the lint error now clearly explains why and tells the user to scope the rule withfilesor disable it, instead of showing an incomplete messagemessages/rule-unsupported-language.js(new)filesscoping or"off")tests/lib/config/config.js— Extendedmeta.languagesvalidation test suite covering all matching paths:js/*wildcard matching@pluginplugin/*wildcard@/js→js/jsnormalization in error messagesmeta.languages(non-array or non-string entries) throws atvalidateRulesConfig()time only when the rule is enableddocs/src/extend/custom-rules.md— Addedmeta.languagesproperty documentation with usage examples and matching semantics (removed internal@/jsimplementation detail)docs/src/extend/custom-rule-tutorial.mdanddocs/_examples/custom-rule-tutorial-code/enforce-foo-bar.js— Updated tutorial example to includelanguages: ["js/js"]Matching semantics
Rules without
meta.languagescontinue to work with every language. Rules that declaremeta.languageswill causevalidateRulesConfig()to throw if the active language isn't in the list. Invalidmeta.languagesvalues (non-array or containing non-strings) are only validated when the rule is actually enabled — consistent with how ESLint skips other rule validation (e.g. rule existence checks) when a rule is set to"off".Error messages
When a rule doesn't support the active language, the error message now advises the user to either scope the rule with the
filesoption or disable it, for example:For rules enabled inline (e.g.
/* eslint no-var: 2 */), the lint error message is similarly clear:📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.