Description
Which packages would you like to change?
-
@eslint/compat
-
@eslint/config-array
-
@eslint/config-helpers
-
@eslint/core
-
@eslint/mcp
-
@eslint/migrate-config
-
@eslint/object-schema
-
@eslint/plugin-kit
What problem do you want to solve?
Hello,
In this issue, I’d like to propose adding a language
property to the RuleContext
interface and its actual implementation:
https://github.com/eslint/rewrite/blob/main/packages/core/src/types.ts#L296-L382
Currently, a rule’s context
argument does not expose the language
property directly.
For example, accessing languageOptions
is valid:
create(context) {
context.languageOptions; // ✅ Valid
}
However, trying to access context.language
results in an error:
create(context) {
context.language; // ❌ Invalid
}
This means users cannot determine the language of the file when writing rules.
Why is this useful?
The ESLint ecosystem is expanding to support a variety of languages and syntaxes via plugins. However, some rules may need to behave differently depending on the language being parsed. For instance, when linting Markdown, some rules account for whether the flavor is CommonMark or GFM.
Take eslint/markdown#294 as an example — this has been an unresolved issue for a while. If we expose the language
property through context
, the rule could adapt its behavior depending on whether the content is written in commonmark
or gfm
. So, the !TIP
and !NOTE
directives can be ignored by default only when in GFM mode.
This proposal might also relate to broader use cases such as eslint/eslint#19805, where rules might need to behave differently across different languages or file types.
I believe that adding the language
property would allow rules to be implemented with parser-specific behavior.
What do you think is the correct solution?
Add language
property to the RuleContext
interface and its actual implementation.
If users set a configuration like the one below:
// eslint.config.js
import { defineConfig } from "eslint/config";
import markdown from "@eslint/markdown";
export default defineConfig([
{
files: ["**/*.md"],
plugins: {
markdown
},
language: "markdown/commonmark",
rules: {
"markdown/no-html": "error"
}
}
]);
The value of context.language
would be commonmark
.
If users set a configuration like the one below:
{
files: ["**/*.jsonc", ".vscode/*.json"],
language: "json/jsonc",
rules: {
"json/no-duplicate-keys": "error",
},
},
The value of context.language
would be jsonc
.
Participation
- I am willing to submit a pull request for this change.
Additional comments
I tried to find where the actual implementation of RuleContext
lives, but I haven’t been able to locate it. I’d really appreciate it if someone could point me to where the real implementation is.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status