Skip to content

Commit fb8ce7d

Browse files
committed
test(selector-node-loc-fixing): added tests
1 parent 33d298b commit fb8ce7d

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script>
2+
let a = 10
3+
</script>
4+
5+
<span class="myClass">Hello!</span>
6+
7+
<b>{a}</b>
8+
9+
<style>
10+
.myClass {
11+
color: red;
12+
}
13+
14+
b {
15+
font-size: xx-large;
16+
}
17+
18+
a:active,
19+
a::before,
20+
b + a,
21+
b + .myClass,
22+
a[data-key="value"] {
23+
color: blue;
24+
}
25+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="container">
2+
<div class="div-class">Hello</div>
3+
4+
<span class="span-class">World!</span>
5+
</div>
6+
7+
<style lang="postcss">
8+
body {
9+
colour: white;
10+
background-colour: grey;
11+
}
12+
13+
a:active,
14+
a::before,
15+
b + a,
16+
b + .myClass,
17+
a[data-key="value"] {
18+
color: blue;
19+
}
20+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div class="container">
2+
<div class="div-class">Hello</div>
3+
4+
<span class="span-class">World!</span>
5+
</div>
6+
7+
<style lang="scss">
8+
.container {
9+
.div-class {
10+
// This is an inline comment
11+
color: red;
12+
}
13+
14+
.span-class {
15+
font-weight: bold;
16+
}
17+
18+
a:active,
19+
a::before,
20+
b + a,
21+
b + .myClass,
22+
a[data-key="value"] {
23+
color: blue;
24+
}
25+
}
26+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import assert from "assert";
2+
import fs from "fs";
3+
import path from "path";
4+
import type { Node } from "postcss";
5+
6+
import { parseForESLint } from "../../../src/index.js";
7+
import { generateParserOptions, listupFixtures } from "./test-utils.js";
8+
import type { SourceLocation } from "../../../src/ast/common.js";
9+
10+
const dirname = path.dirname(new URL(import.meta.url).pathname);
11+
const STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT = path.resolve(
12+
dirname,
13+
"../../fixtures/parser/style-selector-location-converter",
14+
);
15+
16+
function parse(code: string, filePath: string, config: any) {
17+
return parseForESLint(code, generateParserOptions({ filePath }, config));
18+
}
19+
20+
describe("Check for AST.", () => {
21+
for (const {
22+
input,
23+
inputFileName,
24+
outputFileName,
25+
config,
26+
meetRequirements,
27+
} of listupFixtures(STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT)) {
28+
describe(inputFileName, () => {
29+
let services: any;
30+
31+
it("most to generate the expected style context.", () => {
32+
services = parse(input, inputFileName, config).services;
33+
if (!meetRequirements("test")) {
34+
return;
35+
}
36+
const styleContext = services.getStyleContext();
37+
assert.strictEqual(styleContext.status, "success");
38+
const locations: [
39+
Partial<SourceLocation>,
40+
[number | undefined, number | undefined],
41+
][] = [
42+
[
43+
services.styleSelectorNodeLoc(styleContext.sourceAst),
44+
services.styleSelectorNodeRange(styleContext.sourceAst),
45+
],
46+
];
47+
styleContext.sourceAst.walk((node: Node) => {
48+
locations.push([
49+
services.styleSelectorNodeLoc(node),
50+
services.styleSelectorNodeRange(node),
51+
]);
52+
});
53+
const output = fs.readFileSync(outputFileName, "utf8");
54+
assert.strictEqual(
55+
`${JSON.stringify(locations, undefined, 2)}\n`,
56+
output,
57+
);
58+
});
59+
});
60+
}
61+
});

0 commit comments

Comments
 (0)