Skip to content

Commit d634f82

Browse files
authored
feat: add support for getLocFromIndex and getIndexFromLoc (#376)
* feat: add support for `getLocFromIndex` * wip: migration * wip: bump dependencies * wip * wip: add type test * wip: tests
1 parent 2739769 commit d634f82

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@
7373
"test:types": "tsc -p tests/types/tsconfig.json"
7474
},
7575
"devDependencies": {
76-
"@eslint/js": "^9.31.0",
77-
"@eslint/json": "^0.13.1",
76+
"@eslint/js": "^9.36.0",
77+
"@eslint/json": "^0.13.2",
7878
"c8": "^10.1.3",
7979
"dedent": "^1.5.3",
80-
"eslint": "^9.35.0",
80+
"eslint": "^9.36.0",
8181
"eslint-config-eslint": "^13.0.0",
8282
"eslint-plugin-eslint-plugin": "^6.3.2",
83-
"globals": "^16.0.0",
83+
"globals": "^16.4.0",
8484
"lint-staged": "^15.2.9",
8585
"mocha": "^11.6.0",
8686
"prettier": "^3.3.3",
8787
"typescript": "^5.9.2",
8888
"yorkie": "^2.0.0"
8989
},
9090
"dependencies": {
91-
"@eslint/core": "^0.15.2",
92-
"@eslint/plugin-kit": "^0.3.5",
91+
"@eslint/core": "^0.16.0",
92+
"@eslint/plugin-kit": "^0.4.0",
9393
"github-slugger": "^2.0.0",
9494
"mdast-util-from-markdown": "^2.0.2",
9595
"mdast-util-frontmatter": "^2.0.1",

tests/language/markdown-source-code.test.js

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

107+
describe("getLocFromIndex()", () => {
108+
it("should convert index to location correctly", () => {
109+
const text = "foo\nbar\r\nbaz";
110+
const markdownSourceCode = new MarkdownSourceCode({
111+
text,
112+
ast: fromMarkdown(text),
113+
});
114+
115+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(0), {
116+
line: 1,
117+
column: 1,
118+
});
119+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(1), {
120+
line: 1,
121+
column: 2,
122+
});
123+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(2), {
124+
line: 1,
125+
column: 3,
126+
});
127+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(3), {
128+
line: 1,
129+
column: 4,
130+
});
131+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(4), {
132+
line: 2,
133+
column: 1,
134+
});
135+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(5), {
136+
line: 2,
137+
column: 2,
138+
});
139+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(6), {
140+
line: 2,
141+
column: 3,
142+
});
143+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(7), {
144+
line: 2,
145+
column: 4,
146+
});
147+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(8), {
148+
line: 2,
149+
column: 5,
150+
});
151+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(9), {
152+
line: 3,
153+
column: 1,
154+
});
155+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(10), {
156+
line: 3,
157+
column: 2,
158+
});
159+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(11), {
160+
line: 3,
161+
column: 3,
162+
});
163+
assert.deepStrictEqual(markdownSourceCode.getLocFromIndex(12), {
164+
line: 3,
165+
column: 4,
166+
});
167+
});
168+
});
169+
170+
describe("getIndexFromLoc()", () => {
171+
it("should convert location to index correctly", () => {
172+
const text = "foo\nbar\r\nbaz";
173+
const markdownSourceCode = new MarkdownSourceCode({
174+
text,
175+
ast: fromMarkdown(text),
176+
});
177+
178+
assert.strictEqual(
179+
markdownSourceCode.getIndexFromLoc({ line: 1, column: 1 }),
180+
0,
181+
);
182+
assert.strictEqual(
183+
markdownSourceCode.getIndexFromLoc({ line: 1, column: 2 }),
184+
1,
185+
);
186+
assert.strictEqual(
187+
markdownSourceCode.getIndexFromLoc({ line: 1, column: 3 }),
188+
2,
189+
);
190+
assert.strictEqual(
191+
markdownSourceCode.getIndexFromLoc({ line: 1, column: 4 }),
192+
3,
193+
);
194+
assert.strictEqual(
195+
markdownSourceCode.getIndexFromLoc({ line: 2, column: 1 }),
196+
4,
197+
);
198+
assert.strictEqual(
199+
markdownSourceCode.getIndexFromLoc({ line: 2, column: 2 }),
200+
5,
201+
);
202+
assert.strictEqual(
203+
markdownSourceCode.getIndexFromLoc({ line: 2, column: 3 }),
204+
6,
205+
);
206+
assert.strictEqual(
207+
markdownSourceCode.getIndexFromLoc({ line: 2, column: 4 }),
208+
7,
209+
);
210+
assert.strictEqual(
211+
markdownSourceCode.getIndexFromLoc({ line: 2, column: 5 }),
212+
8,
213+
);
214+
assert.strictEqual(
215+
markdownSourceCode.getIndexFromLoc({ line: 3, column: 1 }),
216+
9,
217+
);
218+
assert.strictEqual(
219+
markdownSourceCode.getIndexFromLoc({ line: 3, column: 2 }),
220+
10,
221+
);
222+
assert.strictEqual(
223+
markdownSourceCode.getIndexFromLoc({ line: 3, column: 3 }),
224+
11,
225+
);
226+
assert.strictEqual(
227+
markdownSourceCode.getIndexFromLoc({ line: 3, column: 4 }),
228+
12,
229+
);
230+
});
231+
});
232+
107233
describe("getInlineConfigNodes()", () => {
108234
it("should return the inline config nodes", () => {
109235
const nodes = sourceCode.getInlineConfigNodes();

tests/types/types.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ typeof processorPlugins satisfies {};
136136
parent?: Parent | undefined,
137137
) {
138138
sourceCode.getLoc(node) satisfies SourceLocation;
139+
sourceCode.getLocFromIndex(0) satisfies {
140+
line: number;
141+
column: number;
142+
};
143+
sourceCode.getIndexFromLoc({ line: 1, column: 1 }) satisfies number;
139144
sourceCode.getRange(node) satisfies SourceRange;
140145
sourceCode.getParent(node) satisfies Node | undefined;
141146
sourceCode.getAncestors(node) satisfies Node[];

0 commit comments

Comments
 (0)