Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
230 changes: 230 additions & 0 deletions tests/languages/css-source-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 6 additions & 1 deletion tests/types/types.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -82,6 +82,11 @@ css.configs.recommended.plugins satisfies object;

function testVisitor<NodeType extends CssNodePlain>(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[];
Expand Down
Loading