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..017ec777 100644 --- a/packages/eslint-plugin/lib/languages/html-source-code.js +++ b/packages/eslint-plugin/lib/languages/html-source-code.js @@ -1,11 +1,11 @@ /** * @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 +71,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 +183,17 @@ class HTMLSourceCode extends TextSourceCodeBase { return this.parentsMap.get(node); } } +/** + * @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config + * @returns {TextSourceCodeBase & { + * getDisableDirectives(): { problems: {ruleId: null | string, message: string; loc: SourceLocation}[]; directives: Directive[]} + * getInlineConfigNodes(): CommentContent[] + * }} + */ +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..825b4fc8 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,13 +27,14 @@ 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, // @ts-ignore comments: parsed.comments, }); + return sourceCode; }; @@ -52,6 +55,7 @@ describe("HTMLSourceCode", () => { describe("getDisableDirectives", () => { it("should return directives", () => { const sourceCode = createSourceCode(code); + expect(sourceCode.getDisableDirectives().directives.length).toBe(5); expect(sourceCode.getDisableDirectives().problems.length).toBe(0); }); @@ -62,6 +66,7 @@ describe("HTMLSourceCode", () => { --> `; const sourceCode = createSourceCode(code); + expect(sourceCode.getDisableDirectives().problems.length).toBe(1); }); }); 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"