diff --git a/package.json b/package.json index 7e1ade88..67ca00a8 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ }, "dependencies": { "@eslint/core": "^0.14.0", - "@eslint/plugin-kit": "^0.3.1", + "@eslint/plugin-kit": "^0.4.0", "mdast-util-from-markdown": "^2.0.2", "mdast-util-frontmatter": "^2.0.1", "mdast-util-gfm": "^3.0.0", diff --git a/src/language/markdown-language.js b/src/language/markdown-language.js index 6b75b44c..d0536d1c 100644 --- a/src/language/markdown-language.js +++ b/src/language/markdown-language.js @@ -196,6 +196,8 @@ export class MarkdownLanguage { return new MarkdownSourceCode({ text: /** @type {string} */ (file.body), ast: parseResult.ast, + lineStart: this.lineStart, + columnStart: this.columnStart, }); } } diff --git a/tests/language/markdown-source-code.test.js b/tests/language/markdown-source-code.test.js index a92ca1ee..591536d1 100644 --- a/tests/language/markdown-source-code.test.js +++ b/tests/language/markdown-source-code.test.js @@ -104,6 +104,84 @@ describe("MarkdownSourceCode", () => { }); }); + describe("getLocFromIndex()", () => { + it("should throw a TypeError when `offset` is not a number", () => { + assert.throws(() => { + sourceCode.getLocFromIndex("0"); + }, TypeError); + assert.throws(() => { + sourceCode.getLocFromIndex(null); + }, TypeError); + assert.throws(() => { + sourceCode.getLocFromIndex(undefined); + }, TypeError); + assert.throws(() => { + sourceCode.getLocFromIndex(true); + }, TypeError); + assert.throws(() => { + sourceCode.getLocFromIndex(false); + }, TypeError); + }); + + it("should throw a RangeError when `offset` is less than `0`", () => { + assert.throws(() => { + sourceCode.getLocFromIndex(-1); + }, RangeError); + }); + + it("should throw a RangeError when `offset` is greater than `this.text.length`", () => { + assert.throws(() => { + sourceCode.getLocFromIndex(sourceCode.text.length + 1); + }, RangeError); + }); + + it("should return { line: 1, column: 1 } when `offset` is `0` and `text` is empty", () => { + const emptySourceCode = new MarkdownSourceCode({ + text: "", + ast: fromMarkdown(""), + }); + + assert.deepStrictEqual(emptySourceCode.getLocFromIndex(0), { + line: 1, + column: 1, + }); + }); + + it("should return the correct location when `offset` is `0`", () => { + assert.deepStrictEqual(sourceCode.getLocFromIndex(0), { + line: 1, + column: 1, + }); + }); + + it("should return the correct location when `offset` is `this.text.length`", () => { + const offset = 768; + + assert.deepStrictEqual(sourceCode.text.length, offset); + assert.deepStrictEqual(sourceCode.getLocFromIndex(768), { + line: 37, + column: 63, + }); + }); + + it("should return the correct location when `offset` is in the middle of a line", () => { + assert.deepStrictEqual(sourceCode.getLocFromIndex(15), { + line: 1, + column: 16, + }); + + assert.deepStrictEqual(sourceCode.getLocFromIndex(140), { + line: 13, + column: 1, + }); + + assert.deepStrictEqual(sourceCode.getLocFromIndex(427), { + line: 23, + column: 5, + }); + }); + }); + describe("getInlineConfigNodes()", () => { it("should return the inline config nodes", () => { const nodes = sourceCode.getInlineConfigNodes();