diff --git a/package.json b/package.json index efa8c72..9699403 100644 --- a/package.json +++ b/package.json @@ -84,16 +84,16 @@ ], "license": "Apache-2.0", "dependencies": { - "@eslint/core": "^0.15.2", + "@eslint/core": "^0.16.0", "@eslint/css-tree": "^3.6.5", - "@eslint/plugin-kit": "^0.3.5" + "@eslint/plugin-kit": "^0.4.0" }, "devDependencies": { "@eslint/json": "^0.13.1", "c8": "^10.1.3", "compute-baseline": "^0.4.0", "dedent": "^1.5.3", - "eslint": "^9.35.0", + "eslint": "^9.36.0", "eslint-config-eslint": "^13.0.0", "eslint-plugin-eslint-plugin": "^6.3.2", "got": "^14.4.2", diff --git a/tests/languages/css-source-code.test.js b/tests/languages/css-source-code.test.js index 635cd41..37e5de9 100644 --- a/tests/languages/css-source-code.test.js +++ b/tests/languages/css-source-code.test.js @@ -104,6 +104,236 @@ describe("CSSSourceCode", () => { }); }); + describe("getLocFromIndex()", () => { + it("should convert index to location correctly", () => { + const file = { + body: "a {\n /*test*/\r\n}", + path: "test.css", + }; + const language = new CSSLanguage(); + const parseResult = language.parse(file); + const sourceCode = new CSSSourceCode({ + text: file.body, + ast: parseResult.ast, + }); + + assert.deepStrictEqual(sourceCode.getLocFromIndex(0), { + line: 1, + column: 1, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(1), { + line: 1, + column: 2, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(2), { + line: 1, + column: 3, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(3), { + line: 1, + column: 4, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(4), { + line: 2, + column: 1, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(5), { + line: 2, + column: 2, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(6), { + line: 2, + column: 3, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(7), { + line: 2, + column: 4, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(8), { + line: 2, + column: 5, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(9), { + line: 2, + column: 6, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(10), { + line: 2, + column: 7, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(11), { + line: 2, + column: 8, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(12), { + line: 2, + column: 9, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(13), { + line: 2, + column: 10, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(14), { + line: 2, + column: 11, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(15), { + line: 2, + column: 12, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(16), { + line: 3, + column: 1, + }); + assert.deepStrictEqual(sourceCode.getLocFromIndex(17), { + line: 3, + column: 2, + }); + }); + }); + + describe("getIndexFromLoc()", () => { + it("should convert location to index correctly", () => { + const file = { + body: "a {\n /*test*/\r\n}", + path: "test.css", + }; + const language = new CSSLanguage(); + const parseResult = language.parse(file); + const sourceCode = new CSSSourceCode({ + text: file.body, + ast: parseResult.ast, + }); + + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 1, + column: 1, + }), + 0, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 1, + column: 2, + }), + 1, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 1, + column: 3, + }), + 2, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 1, + column: 4, + }), + 3, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 1, + }), + 4, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 2, + }), + 5, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 3, + }), + 6, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 4, + }), + 7, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 5, + }), + 8, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 6, + }), + 9, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 7, + }), + 10, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 8, + }), + 11, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 9, + }), + 12, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 10, + }), + 13, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 11, + }), + 14, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 2, + column: 12, + }), + 15, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 3, + column: 1, + }), + 16, + ); + assert.strictEqual( + sourceCode.getIndexFromLoc({ + line: 3, + column: 2, + }), + 17, + ); + }); + }); + describe("getRange()", () => { it("should return the range property of a node", () => { const loc = { diff --git a/tests/types/types.test.ts b/tests/types/types.test.ts index 921e3b7..776de11 100644 --- a/tests/types/types.test.ts +++ b/tests/types/types.test.ts @@ -1,5 +1,5 @@ import css, { CSSSourceCode } from "@eslint/css"; -import { ESLint } from "eslint"; +import type { ESLint } from "eslint"; import type { SourceLocation, SourceRange } from "@eslint/core"; import type { AnPlusB, @@ -82,6 +82,11 @@ css.configs.recommended.plugins satisfies object; function testVisitor(node: NodeType) { sourceCode.getLoc(node) satisfies SourceLocation; + sourceCode.getLocFromIndex(0) satisfies { + line: number; + column: number; + }; + sourceCode.getIndexFromLoc({ line: 1, column: 1 }) satisfies number; sourceCode.getRange(node) satisfies SourceRange; sourceCode.getParent(node) satisfies CssNodePlain | undefined; sourceCode.getAncestors(node) satisfies CssNodePlain[];