Skip to content

Commit 40eb4be

Browse files
Copilotota-meshi
andauthored
Add ESLint language plugin support (#532)
* Initial plan * Add ESLint language plugin support with YAMLLanguage implementation Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Add changeset for ESLint language plugin support Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Fix getTokensBetween signature for better type consistency Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Update add-language-plugin-support.md * Add test cases for language plugin support - Add tests/src/language/directive-comments.ts for ESLint directive comment functionality - Add tests/src/language/yaml-source-code.ts for YAMLSourceCode class methods - Tests cover eslint-disable, eslint-enable, eslint-disable-line, eslint-disable-next-line - Tests cover inline configuration comments - Tests cover YAMLSourceCode token and comment retrieval methods - All 45 new tests passing, total 5153 tests pass Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Add token-store and core-rules compatibility tests - Add tests/src/language/token-store.ts for comprehensive TokenStore testing - Add tests/src/language/yaml-source-code-with-core-rules.ts for ESLint core rules compatibility - TokenStore tests cover all methods: getFirstToken, getLastToken, getTokenBefore/After, getTokens, getTokensBetween, getComments - Core rules test ensures all ESLint built-in rules don't crash with yml/yaml language - 320 new tests added (28 TokenStore + 292 core rules tests) - Total: 5,473 tests passing Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Fix linting errors - remove trailing whitespace Fixed prettier/prettier errors in yaml-source-code.ts test file by removing trailing whitespace from 3 lines. Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Update add-language-plugin-support.md * Refactor token-store tests to remove getPair helper Removed getPair helper function and added explicit assertions to each test for navigating the AST structure (ast.body[0], doc.content, content.pairs). This makes tests more explicit and clear about what they're testing, as suggested in code review. Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Add language plugin documentation to README and user guide Added "Languages" section to both README.md and docs/user-guide/index.md documenting the yml/yaml language identifier and how to use it in ESLint configurations. Follows the same pattern as eslint-plugin-toml documentation. Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Fix test names to accurately reflect single-rule testing Updated test names in directive-comments.ts: - Changed "should disable multiple rules separated by commas" to "should disable a specific rule with eslint-disable-next-line" - Changed "should configure multiple rules in one comment" to "should configure a rule with inline config comment" These tests only test single rules, so the names now accurately reflect their behavior. Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> * Update tests to actually test multiple rules as originally intended Restored original test names and updated test implementations: - "should disable multiple rules separated by commas" now tests disabling both yml/no-empty-key and yml/no-empty-mapping-value with eslint-disable-next-line - "should configure multiple rules in one comment" now tests configuring both rules in a single inline config comment Tests now match their descriptions and validate multi-rule directive functionality. Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ota-meshi <16508807+ota-meshi@users.noreply.github.com> Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
1 parent 5af8943 commit 40eb4be

File tree

13 files changed

+2513
-12
lines changed

13 files changed

+2513
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-yml": major
3+
---
4+
5+
feat: add ESLint language plugin support

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@ eslint .
150150
eslint "src/**/*.{js,yaml,yml}"
151151
```
152152

153+
#### Languages
154+
155+
This plugin provides the following language identifiers for use in ESLint configurations:
156+
157+
- `yml/yaml` ... YAML files
158+
159+
For example, to apply settings specifically to YAML files, you can use the `language` field in your ESLint configuration:
160+
161+
```js
162+
import eslintPluginYml from 'eslint-plugin-yml';
163+
export default [
164+
{
165+
files: ["*.yaml", "*.yml", "**/*.yaml", "**/*.yml"],
166+
plugins: {
167+
yml: eslintPluginYml,
168+
},
169+
language: "yml/yaml",
170+
}
171+
]
172+
```
173+
174+
The configuration above is included in the shareable configs provided by this plugin, so using `configs` is generally recommended.
175+
176+
See also <https://eslint.org/docs/latest/use/configure/plugins#specify-a-language>
177+
153178
## :computer: Editor Integrations
154179

155180
### Visual Studio Code

docs/user-guide/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ eslint .
104104
eslint "src/**/*.{js,yaml,yml}"
105105
```
106106

107+
#### Languages
108+
109+
This plugin provides the following language identifiers for use in ESLint configurations:
110+
111+
- `yml/yaml` ... YAML files
112+
113+
For example, to apply settings specifically to YAML files, you can use the `language` field in your ESLint configuration:
114+
115+
```js
116+
import eslintPluginYml from 'eslint-plugin-yml';
117+
export default [
118+
{
119+
files: ["*.yaml", "*.yml", "**/*.yaml", "**/*.yml"],
120+
plugins: {
121+
yml: eslintPluginYml,
122+
},
123+
language: "yml/yaml",
124+
}
125+
]
126+
```
127+
128+
The configuration above is included in the shareable configs provided by this plugin, so using `configs` is generally recommended.
129+
130+
See also <https://eslint.org/docs/latest/use/configure/plugins#specify-a-language>
131+
107132
## :computer: Editor Integrations
108133

109134
### Visual Studio Code

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
},
6565
"homepage": "https://ota-meshi.github.io/eslint-plugin-yml/",
6666
"dependencies": {
67+
"@eslint/core": "^1.0.1",
68+
"@eslint/plugin-kit": "^0.5.1",
6769
"debug": "^4.3.2",
6870
"diff-sequences": "^27.5.1",
6971
"escape-string-regexp": "4.0.0",

src/index.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
import type { Linter } from "eslint";
2+
import type { RuleDefinition } from "@eslint/core";
13
import type { RuleModule } from "./types.js";
24
import { rules as ruleList } from "./utils/rules.js";
35
import base from "./configs/flat/base.js";
46
import recommended from "./configs/flat/recommended.js";
57
import standard from "./configs/flat/standard.js";
68
import prettier from "./configs/flat/prettier.js";
79
import * as meta from "./meta.js";
10+
import type { YAMLSourceCode, YAMLLanguageOptions } from "./language/index.js";
11+
import { YAMLLanguage } from "./language/index.js";
812

913
const configs = {
10-
base,
11-
recommended,
12-
standard,
13-
prettier,
14+
base: base as Linter.Config[],
15+
recommended: recommended as Linter.Config[],
16+
standard: standard as Linter.Config[],
17+
prettier: prettier as Linter.Config[],
1418
// Keep flat/* for backward compatibility
15-
"flat/base": base,
16-
"flat/recommended": recommended,
17-
"flat/standard": standard,
18-
"flat/prettier": prettier,
19+
"flat/base": base as Linter.Config[],
20+
"flat/recommended": recommended as Linter.Config[],
21+
"flat/standard": standard as Linter.Config[],
22+
"flat/prettier": prettier as Linter.Config[],
1923
};
2024

2125
const rules = ruleList.reduce(
@@ -24,9 +28,12 @@ const rules = ruleList.reduce(
2428
return obj;
2529
},
2630
{} as { [key: string]: RuleModule },
27-
);
31+
) as Record<string, RuleDefinition>;
2832

29-
const plugin = { meta, configs, rules };
33+
const languages = {
34+
yaml: new YAMLLanguage(),
35+
};
3036

31-
export { meta, configs, rules };
32-
export default plugin;
37+
export type { YAMLLanguageOptions, YAMLSourceCode };
38+
export { meta, configs, rules, languages };
39+
export default { meta, configs, rules, languages };

src/language/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @fileoverview YAML language plugin exports
3+
*/
4+
5+
export { YAMLLanguage } from "./yaml-language.js";
6+
export type { YAMLLanguageOptions } from "./yaml-language.js";
7+
export { YAMLSourceCode } from "./yaml-source-code.js";

0 commit comments

Comments
 (0)