Skip to content

Commit 0b1787b

Browse files
authored
refactor: use plugin-kit native method (#447)
1 parent e88504c commit 0b1787b

File tree

5 files changed

+44
-94
lines changed

5 files changed

+44
-94
lines changed

packages/eslint-plugin/lib/languages/html-language.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
const { visitorKeys, parseForESLint } = require("@html-eslint/parser");
8-
const { HTMLSourceCode } = require("./html-source-code");
8+
const { createHTMLSourceCode } = require("./html-source-code");
99

1010
/**
11-
* @implements {Language<{ LangOptions: ParserOptions; Code: HTMLSourceCode; RootNode: AST.Program; Node: {}}>}
11+
* @implements {Language<{ LangOptions: ParserOptions; Code: ReturnType<typeof createHTMLSourceCode>; RootNode: AST.Program; Node: {}}>}
1212
*/
1313
class HTMLLanguage {
1414
constructor() {
@@ -104,7 +104,7 @@ class HTMLLanguage {
104104
* @param {OkParseResult<AST.Program>} parseResult
105105
*/
106106
createSourceCode(file, parseResult) {
107-
return new HTMLSourceCode({
107+
return createHTMLSourceCode({
108108
text: /** @type {string} */ (file.body),
109109
ast: parseResult.ast,
110110
comments: parseResult.comments,

packages/eslint-plugin/lib/languages/html-source-code.js

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* @import {AST} from 'eslint';
33
* @import {SourceLocation, DirectiveType} from '@eslint/plugin-kit';
4-
* @import {TraversalStep, Position} from '@eslint/core';
4+
* @import {TraversalStep, SourceCode} from '@eslint/core';
55
* @import {CommentContent, AnyHTMLNode} from '@html-eslint/types';
66
* @import {BaseNode} from '../types';
7+
*
78
*/
8-
99
const {
1010
TextSourceCodeBase,
1111
ConfigCommentParser,
@@ -71,90 +71,6 @@ class HTMLSourceCode extends TextSourceCodeBase {
7171
return this.lines;
7272
}
7373

74-
// Copied from eslint source code
75-
/**
76-
* @see https://github.com/eslint/eslint/blob/f60f2764971a33e252be13e560dccf21f554dbf1/lib/languages/js/source-code/source-code.js#L745
77-
* @param {Position} loc
78-
* @returns {number}
79-
*/
80-
getIndexFromLoc(loc) {
81-
if (
82-
typeof loc !== "object" ||
83-
typeof loc.line !== "number" ||
84-
typeof loc.column !== "number"
85-
) {
86-
throw new TypeError(
87-
"Expected `loc` to be an object with numeric `line` and `column` properties."
88-
);
89-
}
90-
91-
if (loc.line <= 0) {
92-
throw new RangeError(
93-
`Line number out of range (line ${loc.line} requested). Line numbers should be 1-based.`
94-
);
95-
}
96-
97-
if (loc.line > this.lineStartIndices.length) {
98-
throw new RangeError(
99-
`Line number out of range (line ${loc.line} requested, but only ${this.lineStartIndices.length} lines present).`
100-
);
101-
}
102-
103-
const lineStartIndex = this.lineStartIndices[loc.line - 1];
104-
const lineEndIndex =
105-
loc.line === this.lineStartIndices.length
106-
? this.text.length
107-
: this.lineStartIndices[loc.line];
108-
const positionIndex = lineStartIndex + loc.column;
109-
if (
110-
(loc.line === this.lineStartIndices.length &&
111-
positionIndex > lineEndIndex) ||
112-
(loc.line < this.lineStartIndices.length && positionIndex >= lineEndIndex)
113-
) {
114-
throw new RangeError(
115-
`Column number out of range (column ${loc.column} requested, but the length of line ${loc.line} is ${lineEndIndex - lineStartIndex}).`
116-
);
117-
}
118-
119-
return positionIndex;
120-
}
121-
122-
// Copied from eslint source code
123-
/**
124-
* @see https://github.com/eslint/eslint/blob/f60f2764971a33e252be13e560dccf21f554dbf1/lib/languages/js/source-code/source-code.js#L694
125-
* @param {number} index
126-
* @returns {Position}
127-
*/
128-
getLocFromIndex(index) {
129-
if (typeof index !== "number") {
130-
throw new TypeError("Expected `index` to be a number.");
131-
}
132-
133-
if (index < 0 || index > this.text.length) {
134-
throw new RangeError(
135-
`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`
136-
);
137-
}
138-
if (index === this.text.length) {
139-
return {
140-
line: this.lines.length,
141-
// @ts-ignore
142-
column: this.lines.at(-1).length,
143-
};
144-
}
145-
146-
const lineNumber =
147-
// @ts-ignore
148-
index >= this.lineStartIndices.at(-1)
149-
? this.lineStartIndices.length
150-
: this.lineStartIndices.findIndex((el) => index < el);
151-
152-
return {
153-
line: lineNumber,
154-
column: index - this.lineStartIndices[lineNumber - 1],
155-
};
156-
}
157-
15874
getInlineConfigNodes() {
15975
return this.comments.filter((comment) => INLINE_CONFIG.test(comment.value));
16076
}
@@ -267,7 +183,17 @@ class HTMLSourceCode extends TextSourceCodeBase {
267183
return this.parentsMap.get(node);
268184
}
269185
}
186+
/**
187+
* @param {{ast: AST.Program, text: string, comments: CommentContent[]}} config
188+
* @returns {TextSourceCodeBase<any> & {
189+
* getDisableDirectives(): { problems: {ruleId: null | string, message: string; loc: SourceLocation}[]; directives: Directive[]}
190+
* getInlineConfigNodes(): CommentContent[]
191+
* }}
192+
*/
193+
function createHTMLSourceCode(config) {
194+
return new HTMLSourceCode(config);
195+
}
270196

271197
module.exports = {
272-
HTMLSourceCode,
198+
createHTMLSourceCode,
273199
};

packages/eslint-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"accessibility"
4040
],
4141
"dependencies": {
42-
"@eslint/plugin-kit": "^0.3.1",
42+
"@eslint/plugin-kit": "^0.4.1",
4343
"@html-eslint/parser": "^0.48.0",
4444
"@html-eslint/template-parser": "^0.48.0",
4545
"@html-eslint/template-syntax-parser": "^0.48.0",

packages/eslint-plugin/tests/languages/html-source-code.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
*/
44

55
const { HTMLLanguage } = require("../../lib/languages/html-language");
6-
const { HTMLSourceCode } = require("../../lib/languages/html-source-code");
6+
const {
7+
createHTMLSourceCode,
8+
} = require("../../lib/languages/html-source-code");
79

810
/**
911
* @param {string} text
@@ -25,13 +27,14 @@ const createSourceCode = (text) => {
2527
const language = new HTMLLanguage();
2628
const file = createFile(text);
2729
const parsed = language.parse(file);
28-
const sourceCode = new HTMLSourceCode({
30+
const sourceCode = createHTMLSourceCode({
2931
text,
3032
// @ts-ignore
3133
ast: parsed.ast,
3234
// @ts-ignore
3335
comments: parsed.comments,
3436
});
37+
3538
return sourceCode;
3639
};
3740

@@ -52,6 +55,7 @@ describe("HTMLSourceCode", () => {
5255
describe("getDisableDirectives", () => {
5356
it("should return directives", () => {
5457
const sourceCode = createSourceCode(code);
58+
5559
expect(sourceCode.getDisableDirectives().directives.length).toBe(5);
5660
expect(sourceCode.getDisableDirectives().problems.length).toBe(0);
5761
});
@@ -62,6 +66,7 @@ describe("HTMLSourceCode", () => {
6266
-->
6367
`;
6468
const sourceCode = createSourceCode(code);
69+
6570
expect(sourceCode.getDisableDirectives().problems.length).toBe(1);
6671
});
6772
});

yarn.lock

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,15 @@ __metadata:
10461046
languageName: node
10471047
linkType: hard
10481048

1049+
"@eslint/core@npm:^0.17.0":
1050+
version: 0.17.0
1051+
resolution: "@eslint/core@npm:0.17.0"
1052+
dependencies:
1053+
"@types/json-schema": "npm:^7.0.15"
1054+
checksum: 10c0/9a580f2246633bc752298e7440dd942ec421860d1946d0801f0423830e67887e4aeba10ab9a23d281727a978eb93d053d1922a587d502942a713607f40ed704e
1055+
languageName: node
1056+
linkType: hard
1057+
10491058
"@eslint/eslintrc@npm:^2.1.4":
10501059
version: 2.1.4
10511060
resolution: "@eslint/eslintrc@npm:2.1.4"
@@ -1111,6 +1120,16 @@ __metadata:
11111120
languageName: node
11121121
linkType: hard
11131122

1123+
"@eslint/plugin-kit@npm:^0.4.1":
1124+
version: 0.4.1
1125+
resolution: "@eslint/plugin-kit@npm:0.4.1"
1126+
dependencies:
1127+
"@eslint/core": "npm:^0.17.0"
1128+
levn: "npm:^0.4.1"
1129+
checksum: 10c0/51600f78b798f172a9915dffb295e2ffb44840d583427bc732baf12ecb963eb841b253300e657da91d890f4b323d10a1bd12934bf293e3018d8bb66fdce5217b
1130+
languageName: node
1131+
linkType: hard
1132+
11141133
"@google-cloud/cloud-sql-connector@npm:^1.3.3":
11151134
version: 1.4.0
11161135
resolution: "@google-cloud/cloud-sql-connector@npm:1.4.0"
@@ -1229,7 +1248,7 @@ __metadata:
12291248
resolution: "@html-eslint/eslint-plugin@workspace:packages/eslint-plugin"
12301249
dependencies:
12311250
"@eslint/core": "npm:^0.14.0"
1232-
"@eslint/plugin-kit": "npm:^0.3.1"
1251+
"@eslint/plugin-kit": "npm:^0.4.1"
12331252
"@html-eslint/parser": "npm:^0.48.0"
12341253
"@html-eslint/template-parser": "npm:^0.48.0"
12351254
"@html-eslint/template-syntax-parser": "npm:^0.48.0"

0 commit comments

Comments
 (0)