Skip to content

Commit 9374dd0

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

File tree

6 files changed

+176
-0
lines changed

6 files changed

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

tools/update-fixtures.ts

+28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
selectorAstToJson,
1616
styleContextToJson,
1717
} from "../tests/src/parser/test-utils.js";
18+
import { extractSelectorLocations } from "../tests/src/parser/style-selector-location-converter-utils.js";
1819
import type ts from "typescript";
1920
import type ESTree from "estree";
2021
import globals from "globals";
@@ -38,6 +39,10 @@ const SELECTOR_PARSING_FIXTURE_ROOT = path.resolve(
3839
dirname,
3940
"../tests/fixtures/parser/selector-parsing",
4041
);
42+
const SELECTOR_CONVERTER_FIXTURE_ROOT = path.resolve(
43+
dirname,
44+
"../tests/fixtures/parser/style-selector-location-converter",
45+
);
4146

4247
const RULES = [
4348
"no-unused-labels",
@@ -232,6 +237,29 @@ for (const {
232237
);
233238
}
234239

240+
for (const {
241+
input,
242+
inputFileName,
243+
outputFileName,
244+
config,
245+
meetRequirements,
246+
} of listupFixtures(SELECTOR_CONVERTER_FIXTURE_ROOT)) {
247+
if (!meetRequirements("parse")) {
248+
continue;
249+
}
250+
const services = parse(input, inputFileName, config).services;
251+
const styleContext = services.getStyleContext();
252+
if (styleContext.status !== "success") {
253+
continue;
254+
}
255+
const locations = extractSelectorLocations(services, styleContext.sourceAst);
256+
fs.writeFileSync(
257+
outputFileName,
258+
`${JSON.stringify(locations, undefined, 2)}\n`,
259+
"utf8",
260+
);
261+
}
262+
235263
function buildTypes(
236264
input: string,
237265
result: {

0 commit comments

Comments
 (0)