Skip to content

Commit b0d6844

Browse files
committed
test(selector-node-loc-fixing): added tests
1 parent 577eec0 commit b0d6844

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 25 additions & 0 deletions
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>
Lines changed: 20 additions & 0 deletions
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>
Lines changed: 26 additions & 0 deletions
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>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import assert from "assert";
2+
import fs from "fs";
3+
import path from "path";
4+
import type { AnyNode } from "postcss";
5+
import type { Node as SelectorNode } from "postcss-selector-parser";
6+
7+
import { parseForESLint } from "../../../src/index.js";
8+
import { generateParserOptions, listupFixtures } from "./test-utils.js";
9+
import type { SourceLocation } from "../../../src/ast/common.js";
10+
11+
const dirname = path.dirname(new URL(import.meta.url).pathname);
12+
const STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT = path.resolve(
13+
dirname,
14+
"../../fixtures/parser/style-selector-location-converter",
15+
);
16+
17+
function parse(code: string, filePath: string, config: any) {
18+
return parseForESLint(code, generateParserOptions({ filePath }, config));
19+
}
20+
21+
describe("Check for AST.", () => {
22+
for (const {
23+
input,
24+
inputFileName,
25+
outputFileName,
26+
config,
27+
meetRequirements,
28+
} of listupFixtures(STYLE_SELECTOR_CONTEXT_FIXTURE_ROOT)) {
29+
describe(inputFileName, () => {
30+
let services: any;
31+
32+
it("most to generate the expected style context.", () => {
33+
services = parse(input, inputFileName, config).services;
34+
if (!meetRequirements("test")) {
35+
return;
36+
}
37+
const locations = extractLocations(services);
38+
const output = fs.readFileSync(outputFileName, "utf8");
39+
assert.strictEqual(
40+
`${JSON.stringify(locations, undefined, 2)}\n`,
41+
output,
42+
);
43+
});
44+
});
45+
}
46+
});
47+
48+
function extractLocations(
49+
services: Record<string, any>,
50+
): [string, Partial<SourceLocation>][][] {
51+
const locations: [string, Partial<SourceLocation>][][] = [];
52+
const styleContext = services.getStyleContext();
53+
assert.strictEqual(styleContext.status, "success");
54+
styleContext.sourceAst.walk((node: AnyNode) => {
55+
if (node.type === "rule") {
56+
const selectorAst = services.getStyleSelectorAST(node);
57+
const selectorLocations: [string, Partial<SourceLocation>][] = [];
58+
selectorAst.walk((selectorNode: SelectorNode) => {
59+
selectorLocations.push([
60+
selectorNode.type,
61+
services.styleSelectorNodeLoc(selectorNode, node),
62+
]);
63+
});
64+
locations.push(selectorLocations);
65+
}
66+
});
67+
return locations;
68+
}

0 commit comments

Comments
 (0)