Skip to content

Commit 3baeacf

Browse files
authored
feat: add support for getLocFromIndex and getIndexFromLoc (#167)
* feat: add support for `getLocFromIndex` and `getIndexFromLoc` * wip * wip * wip: add type tests * wip * wip
1 parent b905054 commit 3baeacf

File tree

3 files changed

+239
-4
lines changed

3 files changed

+239
-4
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@
8484
],
8585
"license": "Apache-2.0",
8686
"dependencies": {
87-
"@eslint/core": "^0.15.2",
87+
"@eslint/core": "^0.16.0",
8888
"@eslint/css-tree": "^3.6.5",
89-
"@eslint/plugin-kit": "^0.3.5"
89+
"@eslint/plugin-kit": "^0.4.0"
9090
},
9191
"devDependencies": {
9292
"@eslint/json": "^0.13.1",
9393
"c8": "^10.1.3",
9494
"compute-baseline": "^0.4.0",
9595
"dedent": "^1.5.3",
96-
"eslint": "^9.35.0",
96+
"eslint": "^9.36.0",
9797
"eslint-config-eslint": "^13.0.0",
9898
"eslint-plugin-eslint-plugin": "^6.3.2",
9999
"lint-staged": "^15.2.7",

tests/languages/css-source-code.test.js

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,236 @@ describe("CSSSourceCode", () => {
104104
});
105105
});
106106

107+
describe("getLocFromIndex()", () => {
108+
it("should convert index to location correctly", () => {
109+
const file = {
110+
body: "a {\n /*test*/\r\n}",
111+
path: "test.css",
112+
};
113+
const language = new CSSLanguage();
114+
const parseResult = language.parse(file);
115+
const sourceCode = new CSSSourceCode({
116+
text: file.body,
117+
ast: parseResult.ast,
118+
});
119+
120+
assert.deepStrictEqual(sourceCode.getLocFromIndex(0), {
121+
line: 1,
122+
column: 1,
123+
});
124+
assert.deepStrictEqual(sourceCode.getLocFromIndex(1), {
125+
line: 1,
126+
column: 2,
127+
});
128+
assert.deepStrictEqual(sourceCode.getLocFromIndex(2), {
129+
line: 1,
130+
column: 3,
131+
});
132+
assert.deepStrictEqual(sourceCode.getLocFromIndex(3), {
133+
line: 1,
134+
column: 4,
135+
});
136+
assert.deepStrictEqual(sourceCode.getLocFromIndex(4), {
137+
line: 2,
138+
column: 1,
139+
});
140+
assert.deepStrictEqual(sourceCode.getLocFromIndex(5), {
141+
line: 2,
142+
column: 2,
143+
});
144+
assert.deepStrictEqual(sourceCode.getLocFromIndex(6), {
145+
line: 2,
146+
column: 3,
147+
});
148+
assert.deepStrictEqual(sourceCode.getLocFromIndex(7), {
149+
line: 2,
150+
column: 4,
151+
});
152+
assert.deepStrictEqual(sourceCode.getLocFromIndex(8), {
153+
line: 2,
154+
column: 5,
155+
});
156+
assert.deepStrictEqual(sourceCode.getLocFromIndex(9), {
157+
line: 2,
158+
column: 6,
159+
});
160+
assert.deepStrictEqual(sourceCode.getLocFromIndex(10), {
161+
line: 2,
162+
column: 7,
163+
});
164+
assert.deepStrictEqual(sourceCode.getLocFromIndex(11), {
165+
line: 2,
166+
column: 8,
167+
});
168+
assert.deepStrictEqual(sourceCode.getLocFromIndex(12), {
169+
line: 2,
170+
column: 9,
171+
});
172+
assert.deepStrictEqual(sourceCode.getLocFromIndex(13), {
173+
line: 2,
174+
column: 10,
175+
});
176+
assert.deepStrictEqual(sourceCode.getLocFromIndex(14), {
177+
line: 2,
178+
column: 11,
179+
});
180+
assert.deepStrictEqual(sourceCode.getLocFromIndex(15), {
181+
line: 2,
182+
column: 12,
183+
});
184+
assert.deepStrictEqual(sourceCode.getLocFromIndex(16), {
185+
line: 3,
186+
column: 1,
187+
});
188+
assert.deepStrictEqual(sourceCode.getLocFromIndex(17), {
189+
line: 3,
190+
column: 2,
191+
});
192+
});
193+
});
194+
195+
describe("getIndexFromLoc()", () => {
196+
it("should convert location to index correctly", () => {
197+
const file = {
198+
body: "a {\n /*test*/\r\n}",
199+
path: "test.css",
200+
};
201+
const language = new CSSLanguage();
202+
const parseResult = language.parse(file);
203+
const sourceCode = new CSSSourceCode({
204+
text: file.body,
205+
ast: parseResult.ast,
206+
});
207+
208+
assert.strictEqual(
209+
sourceCode.getIndexFromLoc({
210+
line: 1,
211+
column: 1,
212+
}),
213+
0,
214+
);
215+
assert.strictEqual(
216+
sourceCode.getIndexFromLoc({
217+
line: 1,
218+
column: 2,
219+
}),
220+
1,
221+
);
222+
assert.strictEqual(
223+
sourceCode.getIndexFromLoc({
224+
line: 1,
225+
column: 3,
226+
}),
227+
2,
228+
);
229+
assert.strictEqual(
230+
sourceCode.getIndexFromLoc({
231+
line: 1,
232+
column: 4,
233+
}),
234+
3,
235+
);
236+
assert.strictEqual(
237+
sourceCode.getIndexFromLoc({
238+
line: 2,
239+
column: 1,
240+
}),
241+
4,
242+
);
243+
assert.strictEqual(
244+
sourceCode.getIndexFromLoc({
245+
line: 2,
246+
column: 2,
247+
}),
248+
5,
249+
);
250+
assert.strictEqual(
251+
sourceCode.getIndexFromLoc({
252+
line: 2,
253+
column: 3,
254+
}),
255+
6,
256+
);
257+
assert.strictEqual(
258+
sourceCode.getIndexFromLoc({
259+
line: 2,
260+
column: 4,
261+
}),
262+
7,
263+
);
264+
assert.strictEqual(
265+
sourceCode.getIndexFromLoc({
266+
line: 2,
267+
column: 5,
268+
}),
269+
8,
270+
);
271+
assert.strictEqual(
272+
sourceCode.getIndexFromLoc({
273+
line: 2,
274+
column: 6,
275+
}),
276+
9,
277+
);
278+
assert.strictEqual(
279+
sourceCode.getIndexFromLoc({
280+
line: 2,
281+
column: 7,
282+
}),
283+
10,
284+
);
285+
assert.strictEqual(
286+
sourceCode.getIndexFromLoc({
287+
line: 2,
288+
column: 8,
289+
}),
290+
11,
291+
);
292+
assert.strictEqual(
293+
sourceCode.getIndexFromLoc({
294+
line: 2,
295+
column: 9,
296+
}),
297+
12,
298+
);
299+
assert.strictEqual(
300+
sourceCode.getIndexFromLoc({
301+
line: 2,
302+
column: 10,
303+
}),
304+
13,
305+
);
306+
assert.strictEqual(
307+
sourceCode.getIndexFromLoc({
308+
line: 2,
309+
column: 11,
310+
}),
311+
14,
312+
);
313+
assert.strictEqual(
314+
sourceCode.getIndexFromLoc({
315+
line: 2,
316+
column: 12,
317+
}),
318+
15,
319+
);
320+
assert.strictEqual(
321+
sourceCode.getIndexFromLoc({
322+
line: 3,
323+
column: 1,
324+
}),
325+
16,
326+
);
327+
assert.strictEqual(
328+
sourceCode.getIndexFromLoc({
329+
line: 3,
330+
column: 2,
331+
}),
332+
17,
333+
);
334+
});
335+
});
336+
107337
describe("getRange()", () => {
108338
it("should return the range property of a node", () => {
109339
const loc = {

tests/types/types.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import css, { CSSSourceCode } from "@eslint/css";
2-
import { ESLint } from "eslint";
2+
import type { ESLint } from "eslint";
33
import type { SourceLocation, SourceRange } from "@eslint/core";
44
import type {
55
AnPlusB,
@@ -82,6 +82,11 @@ css.configs.recommended.plugins satisfies object;
8282

8383
function testVisitor<NodeType extends CssNodePlain>(node: NodeType) {
8484
sourceCode.getLoc(node) satisfies SourceLocation;
85+
sourceCode.getLocFromIndex(0) satisfies {
86+
line: number;
87+
column: number;
88+
};
89+
sourceCode.getIndexFromLoc({ line: 1, column: 1 }) satisfies number;
8590
sourceCode.getRange(node) satisfies SourceRange;
8691
sourceCode.getParent(node) satisfies CssNodePlain | undefined;
8792
sourceCode.getAncestors(node) satisfies CssNodePlain[];

0 commit comments

Comments
 (0)