Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/eslint-plugin/lib/languages/html-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createHTMLSourceCode>; RootNode: AST.Program; Node: {}}>}
*/
class HTMLLanguage {
constructor() {
Expand Down Expand Up @@ -104,7 +104,7 @@ class HTMLLanguage {
* @param {OkParseResult<AST.Program>} parseResult
*/
createSourceCode(file, parseResult) {
return new HTMLSourceCode({
return createHTMLSourceCode({
text: /** @type {string} */ (file.body),
ast: parseResult.ast,
comments: parseResult.comments,
Expand Down
100 changes: 13 additions & 87 deletions packages/eslint-plugin/lib/languages/html-source-code.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -267,7 +183,17 @@ class HTMLSourceCode extends TextSourceCodeBase {
return this.parentsMap.get(node);
}
}
/**
* @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config
* @returns {TextSourceCodeBase<any> & {
* getDisableDirectives(): { problems: {ruleId: null | string, message: string; loc: SourceLocation}[]; directives: Directive[]}
* getInlineConfigNodes(): CommentContent[]
* }}
*/
function createHTMLSourceCode(config) {
return new HTMLSourceCode(config);
}

module.exports = {
HTMLSourceCode,
createHTMLSourceCode,
};
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
};

Expand All @@ -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);
});
Expand All @@ -62,6 +66,7 @@ describe("HTMLSourceCode", () => {
-->
`;
const sourceCode = createSourceCode(code);

expect(sourceCode.getDisableDirectives().problems.length).toBe(1);
});
});
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down