From 4f995f497bb90bd222d2c73ea66ea7b74d512ab9 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Tue, 11 Nov 2025 23:59:02 +0900 Subject: [PATCH 1/6] refactor: use plugin-kit native method --- .../lib/languages/html-language.js | 6 +- .../lib/languages/html-source-code.js | 96 ++----------------- packages/eslint-plugin/package.json | 2 +- .../tests/languages/html-source-code.test.js | 6 +- yarn.lock | 21 +++- 5 files changed, 37 insertions(+), 94 deletions(-) diff --git a/packages/eslint-plugin/lib/languages/html-language.js b/packages/eslint-plugin/lib/languages/html-language.js index 7467d189..b8d8dfa3 100644 --- a/packages/eslint-plugin/lib/languages/html-language.js +++ b/packages/eslint-plugin/lib/languages/html-language.js @@ -5,10 +5,10 @@ */ const { visitorKeys, parseForESLint } = require("@html-eslint/parser"); -const { HTMLSourceCode } = require("./html-source-code"); +const { createHTMLSourceCode } = require("./html-source-code"); /** - * @implements {Language<{ LangOptions: ParserOptions; Code: HTMLSourceCode; RootNode: AST.Program; Node: {}}>} + * @implements {Language<{ LangOptions: ParserOptions; Code: ReturnType; RootNode: AST.Program; Node: {}}>} */ class HTMLLanguage { constructor() { @@ -104,7 +104,7 @@ class HTMLLanguage { * @param {OkParseResult} parseResult */ createSourceCode(file, parseResult) { - return new HTMLSourceCode({ + return createHTMLSourceCode({ text: /** @type {string} */ (file.body), ast: parseResult.ast, comments: parseResult.comments, diff --git a/packages/eslint-plugin/lib/languages/html-source-code.js b/packages/eslint-plugin/lib/languages/html-source-code.js index b3d0281a..3aa19fc9 100644 --- a/packages/eslint-plugin/lib/languages/html-source-code.js +++ b/packages/eslint-plugin/lib/languages/html-source-code.js @@ -1,11 +1,10 @@ /** * @import {AST} from 'eslint'; * @import {SourceLocation, DirectiveType} from '@eslint/plugin-kit'; - * @import {TraversalStep, Position} from '@eslint/core'; + * @import {TraversalStep, SourceCode} from '@eslint/core'; * @import {CommentContent, AnyHTMLNode} from '@html-eslint/types'; * @import {BaseNode} from '../types'; */ - const { TextSourceCodeBase, ConfigCommentParser, @@ -71,90 +70,6 @@ class HTMLSourceCode extends TextSourceCodeBase { return this.lines; } - // Copied from eslint source code - /** - * @see https://github.com/eslint/eslint/blob/f60f2764971a33e252be13e560dccf21f554dbf1/lib/languages/js/source-code/source-code.js#L745 - * @param {Position} loc - * @returns {number} - */ - getIndexFromLoc(loc) { - if ( - typeof loc !== "object" || - typeof loc.line !== "number" || - typeof loc.column !== "number" - ) { - throw new TypeError( - "Expected `loc` to be an object with numeric `line` and `column` properties." - ); - } - - if (loc.line <= 0) { - throw new RangeError( - `Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.` - ); - } - - if (loc.line > this.lineStartIndices.length) { - throw new RangeError( - `Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).` - ); - } - - const lineStartIndex = this.lineStartIndices[loc.line - 1]; - const lineEndIndex = - loc.line === this.lineStartIndices.length - ? this.text.length - : this.lineStartIndices[loc.line]; - const positionIndex = lineStartIndex + loc.column; - if ( - (loc.line === this.lineStartIndices.length && - positionIndex > lineEndIndex) || - (loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex) - ) { - throw new RangeError( - `Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).` - ); - } - - return positionIndex; - } - - // Copied from eslint source code - /** - * @see https://github.com/eslint/eslint/blob/f60f2764971a33e252be13e560dccf21f554dbf1/lib/languages/js/source-code/source-code.js#L694 - * @param {number} index - * @returns {Position} - */ - getLocFromIndex(index) { - if (typeof index !== "number") { - throw new TypeError("Expected `index` to be a number."); - } - - if (index < 0 || index > this.text.length) { - throw new RangeError( - `Index out of range (requested index ${index}, but source text has length ${this.text.length}).` - ); - } - if (index === this.text.length) { - return { - line: this.lines.length, - // @ts-ignore - column: this.lines.at(-1).length, - }; - } - - const lineNumber = - // @ts-ignore - index >= this.lineStartIndices.at(-1) - ? this.lineStartIndices.length - : this.lineStartIndices.findIndex((el) => index < el); - - return { - line: lineNumber, - column: index - this.lineStartIndices[lineNumber - 1], - }; - } - getInlineConfigNodes() { return this.comments.filter((comment) => INLINE_CONFIG.test(comment.value)); } @@ -267,7 +182,14 @@ class HTMLSourceCode extends TextSourceCodeBase { return this.parentsMap.get(node); } } +/** + * @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config + * @returns {SourceCode} + */ +function createHTMLSourceCode(config) { + return new HTMLSourceCode(config); +} module.exports = { - HTMLSourceCode, + createHTMLSourceCode, }; diff --git a/packages/eslint-plugin/package.json b/packages/eslint-plugin/package.json index 93bf7349..5a03824d 100644 --- a/packages/eslint-plugin/package.json +++ b/packages/eslint-plugin/package.json @@ -39,7 +39,7 @@ "accessibility" ], "dependencies": { - "@eslint/plugin-kit": "^0.3.1", + "@eslint/plugin-kit": "^0.4.1", "@html-eslint/parser": "^0.48.0", "@html-eslint/template-parser": "^0.48.0", "@html-eslint/template-syntax-parser": "^0.48.0", diff --git a/packages/eslint-plugin/tests/languages/html-source-code.test.js b/packages/eslint-plugin/tests/languages/html-source-code.test.js index b5e54343..4a7b1a22 100644 --- a/packages/eslint-plugin/tests/languages/html-source-code.test.js +++ b/packages/eslint-plugin/tests/languages/html-source-code.test.js @@ -3,7 +3,9 @@ */ const { HTMLLanguage } = require("../../lib/languages/html-language"); -const { HTMLSourceCode } = require("../../lib/languages/html-source-code"); +const { + createHTMLSourceCode, +} = require("../../lib/languages/html-source-code"); /** * @param {string} text @@ -25,7 +27,7 @@ const createSourceCode = (text) => { const language = new HTMLLanguage(); const file = createFile(text); const parsed = language.parse(file); - const sourceCode = new HTMLSourceCode({ + const sourceCode = createHTMLSourceCode({ text, // @ts-ignore ast: parsed.ast, diff --git a/yarn.lock b/yarn.lock index 3f7def74..8912812e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1046,6 +1046,15 @@ __metadata: languageName: node linkType: hard +"@eslint/core@npm:^0.17.0": + version: 0.17.0 + resolution: "@eslint/core@npm:0.17.0" + dependencies: + "@types/json-schema": "npm:^7.0.15" + checksum: 10c0/9a580f2246633bc752298e7440dd942ec421860d1946d0801f0423830e67887e4aeba10ab9a23d281727a978eb93d053d1922a587d502942a713607f40ed704e + languageName: node + linkType: hard + "@eslint/eslintrc@npm:^2.1.4": version: 2.1.4 resolution: "@eslint/eslintrc@npm:2.1.4" @@ -1111,6 +1120,16 @@ __metadata: languageName: node linkType: hard +"@eslint/plugin-kit@npm:^0.4.1": + version: 0.4.1 + resolution: "@eslint/plugin-kit@npm:0.4.1" + dependencies: + "@eslint/core": "npm:^0.17.0" + levn: "npm:^0.4.1" + checksum: 10c0/51600f78b798f172a9915dffb295e2ffb44840d583427bc732baf12ecb963eb841b253300e657da91d890f4b323d10a1bd12934bf293e3018d8bb66fdce5217b + languageName: node + linkType: hard + "@google-cloud/cloud-sql-connector@npm:^1.3.3": version: 1.4.0 resolution: "@google-cloud/cloud-sql-connector@npm:1.4.0" @@ -1229,7 +1248,7 @@ __metadata: resolution: "@html-eslint/eslint-plugin@workspace:packages/eslint-plugin" dependencies: "@eslint/core": "npm:^0.14.0" - "@eslint/plugin-kit": "npm:^0.3.1" + "@eslint/plugin-kit": "npm:^0.4.1" "@html-eslint/parser": "npm:^0.48.0" "@html-eslint/template-parser": "npm:^0.48.0" "@html-eslint/template-syntax-parser": "npm:^0.48.0" From 3b2d7f7429a65f10c3583341438cec0afb2ecbdb Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 12 Nov 2025 00:05:01 +0900 Subject: [PATCH 2/6] Update html-source-code.test.js --- .../tests/languages/html-source-code.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/eslint-plugin/tests/languages/html-source-code.test.js b/packages/eslint-plugin/tests/languages/html-source-code.test.js index 4a7b1a22..c2255c4e 100644 --- a/packages/eslint-plugin/tests/languages/html-source-code.test.js +++ b/packages/eslint-plugin/tests/languages/html-source-code.test.js @@ -2,6 +2,8 @@ * @import {File} from "@eslint/core"; */ +import { SourceCode } from "eslint"; + const { HTMLLanguage } = require("../../lib/languages/html-language"); const { createHTMLSourceCode, @@ -34,9 +36,21 @@ const createSourceCode = (text) => { // @ts-ignore comments: parsed.comments, }); + return sourceCode; }; +/** + * @template T + * @param {T | null | undefined} value + * @returns {asserts value is T} + */ +function nonNullish(value) { + if (value === undefined && value === null) { + throw new TypeError("Value must not be null or undefined"); + } +} + describe("HTMLSourceCode", () => { const code = ` @@ -47,6 +61,7 @@ describe("HTMLSourceCode", () => { describe("getInlineConfigNodes()", () => { it("should return inline config nodes", () => { const sourceCode = createSourceCode(code); + nonNullish(sourceCode.getInlineConfigNodes); expect(sourceCode.getInlineConfigNodes().length).toBe(5); }); }); @@ -54,6 +69,8 @@ describe("HTMLSourceCode", () => { describe("getDisableDirectives", () => { it("should return directives", () => { const sourceCode = createSourceCode(code); + nonNullish(sourceCode.getDisableDirectives); + expect(sourceCode.getDisableDirectives().directives.length).toBe(5); expect(sourceCode.getDisableDirectives().problems.length).toBe(0); }); @@ -64,6 +81,8 @@ describe("HTMLSourceCode", () => { --> `; const sourceCode = createSourceCode(code); + + nonNullish(sourceCode.getDisableDirectives); expect(sourceCode.getDisableDirectives().problems.length).toBe(1); }); }); From 26c5a3c7b614588e74ec2d10ea15dffc56873215 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 12 Nov 2025 00:05:31 +0900 Subject: [PATCH 3/6] Update html-source-code.test.js --- packages/eslint-plugin/tests/languages/html-source-code.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/eslint-plugin/tests/languages/html-source-code.test.js b/packages/eslint-plugin/tests/languages/html-source-code.test.js index c2255c4e..21a63a2c 100644 --- a/packages/eslint-plugin/tests/languages/html-source-code.test.js +++ b/packages/eslint-plugin/tests/languages/html-source-code.test.js @@ -2,8 +2,6 @@ * @import {File} from "@eslint/core"; */ -import { SourceCode } from "eslint"; - const { HTMLLanguage } = require("../../lib/languages/html-language"); const { createHTMLSourceCode, From d2232fc97996c2fea0d305ef6eba000c10d0aaee Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 12 Nov 2025 00:12:50 +0900 Subject: [PATCH 4/6] Update html-source-code.js --- packages/eslint-plugin/lib/languages/html-source-code.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/lib/languages/html-source-code.js b/packages/eslint-plugin/lib/languages/html-source-code.js index 3aa19fc9..b739add2 100644 --- a/packages/eslint-plugin/lib/languages/html-source-code.js +++ b/packages/eslint-plugin/lib/languages/html-source-code.js @@ -4,6 +4,7 @@ * @import {TraversalStep, SourceCode} from '@eslint/core'; * @import {CommentContent, AnyHTMLNode} from '@html-eslint/types'; * @import {BaseNode} from '../types'; + * */ const { TextSourceCodeBase, @@ -184,7 +185,7 @@ class HTMLSourceCode extends TextSourceCodeBase { } /** * @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config - * @returns {SourceCode} + * @returns {TextSourceCodeBase} */ function createHTMLSourceCode(config) { return new HTMLSourceCode(config); From e5e460328df1f8163bc1b38f7551cd3076951192 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 12 Nov 2025 00:16:02 +0900 Subject: [PATCH 5/6] update --- packages/eslint-plugin/lib/languages/html-source-code.js | 5 ++++- .../eslint-plugin/tests/languages/html-source-code.test.js | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/lib/languages/html-source-code.js b/packages/eslint-plugin/lib/languages/html-source-code.js index b739add2..017ec777 100644 --- a/packages/eslint-plugin/lib/languages/html-source-code.js +++ b/packages/eslint-plugin/lib/languages/html-source-code.js @@ -185,7 +185,10 @@ class HTMLSourceCode extends TextSourceCodeBase { } /** * @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config - * @returns {TextSourceCodeBase} + * @returns {TextSourceCodeBase & { + * getDisableDirectives(): { problems: {ruleId: null | string, message: string; loc: SourceLocation}[]; directives: Directive[]} + * getInlineConfigNodes(): CommentContent[] + * }} */ function createHTMLSourceCode(config) { return new HTMLSourceCode(config); diff --git a/packages/eslint-plugin/tests/languages/html-source-code.test.js b/packages/eslint-plugin/tests/languages/html-source-code.test.js index 21a63a2c..700fa50b 100644 --- a/packages/eslint-plugin/tests/languages/html-source-code.test.js +++ b/packages/eslint-plugin/tests/languages/html-source-code.test.js @@ -59,7 +59,6 @@ describe("HTMLSourceCode", () => { describe("getInlineConfigNodes()", () => { it("should return inline config nodes", () => { const sourceCode = createSourceCode(code); - nonNullish(sourceCode.getInlineConfigNodes); expect(sourceCode.getInlineConfigNodes().length).toBe(5); }); }); @@ -80,7 +79,6 @@ describe("HTMLSourceCode", () => { `; const sourceCode = createSourceCode(code); - nonNullish(sourceCode.getDisableDirectives); expect(sourceCode.getDisableDirectives().problems.length).toBe(1); }); }); From e43159eb6fb2974d7e27f31c3d162006411bd00b Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Mon, 17 Nov 2025 23:12:15 +0900 Subject: [PATCH 6/6] Update html-source-code.test.js --- .../tests/languages/html-source-code.test.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/eslint-plugin/tests/languages/html-source-code.test.js b/packages/eslint-plugin/tests/languages/html-source-code.test.js index 700fa50b..825b4fc8 100644 --- a/packages/eslint-plugin/tests/languages/html-source-code.test.js +++ b/packages/eslint-plugin/tests/languages/html-source-code.test.js @@ -38,17 +38,6 @@ const createSourceCode = (text) => { return sourceCode; }; -/** - * @template T - * @param {T | null | undefined} value - * @returns {asserts value is T} - */ -function nonNullish(value) { - if (value === undefined && value === null) { - throw new TypeError("Value must not be null or undefined"); - } -} - describe("HTMLSourceCode", () => { const code = ` @@ -66,7 +55,6 @@ describe("HTMLSourceCode", () => { describe("getDisableDirectives", () => { it("should return directives", () => { const sourceCode = createSourceCode(code); - nonNullish(sourceCode.getDisableDirectives); expect(sourceCode.getDisableDirectives().directives.length).toBe(5); expect(sourceCode.getDisableDirectives().problems.length).toBe(0);