Skip to content

Commit cdeed7c

Browse files
committed
Added tests for converter functions for ESTree-style range and location for PostCSS nodes
1 parent 3397a63 commit cdeed7c

File tree

5 files changed

+284
-0
lines changed

5 files changed

+284
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
</style>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
[
3+
{
4+
"start": {
5+
"line": 9,
6+
"column": 7
7+
}
8+
},
9+
[
10+
89,
11+
null
12+
]
13+
],
14+
[
15+
{
16+
"start": {
17+
"line": 10,
18+
"column": 2
19+
},
20+
"end": {
21+
"line": 12,
22+
"column": 3
23+
}
24+
},
25+
[
26+
92,
27+
122
28+
]
29+
],
30+
[
31+
{
32+
"start": {
33+
"line": 11,
34+
"column": 4
35+
},
36+
"end": {
37+
"line": 11,
38+
"column": 15
39+
}
40+
},
41+
[
42+
107,
43+
118
44+
]
45+
],
46+
[
47+
{
48+
"start": {
49+
"line": 14,
50+
"column": 2
51+
},
52+
"end": {
53+
"line": 16,
54+
"column": 3
55+
}
56+
},
57+
[
58+
126,
59+
158
60+
]
61+
],
62+
[
63+
{
64+
"start": {
65+
"line": 15,
66+
"column": 4
67+
},
68+
"end": {
69+
"line": 15,
70+
"column": 24
71+
}
72+
},
73+
[
74+
134,
75+
154
76+
]
77+
]
78+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
</style>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[
2+
[
3+
{
4+
"start": {
5+
"line": 7,
6+
"column": 19
7+
}
8+
},
9+
[
10+
130,
11+
null
12+
]
13+
],
14+
[
15+
{
16+
"start": {
17+
"line": 8,
18+
"column": 2
19+
},
20+
"end": {
21+
"line": 17,
22+
"column": 3
23+
}
24+
},
25+
[
26+
133,
27+
275
28+
]
29+
],
30+
[
31+
{
32+
"start": {
33+
"line": 9,
34+
"column": 4
35+
},
36+
"end": {
37+
"line": 12,
38+
"column": 5
39+
}
40+
},
41+
[
42+
150,
43+
221
44+
]
45+
],
46+
[
47+
{
48+
"start": {
49+
"line": 10,
50+
"column": 6
51+
},
52+
"end": {
53+
"line": 10,
54+
"column": 34
55+
}
56+
},
57+
[
58+
169,
59+
197
60+
]
61+
],
62+
[
63+
{
64+
"start": {
65+
"line": 11,
66+
"column": 6
67+
},
68+
"end": {
69+
"line": 11,
70+
"column": 17
71+
}
72+
},
73+
[
74+
204,
75+
215
76+
]
77+
],
78+
[
79+
{
80+
"start": {
81+
"line": 14,
82+
"column": 4
83+
},
84+
"end": {
85+
"line": 16,
86+
"column": 5
87+
}
88+
},
89+
[
90+
227,
91+
271
92+
]
93+
],
94+
[
95+
{
96+
"start": {
97+
"line": 15,
98+
"column": 6
99+
},
100+
"end": {
101+
"line": 15,
102+
"column": 24
103+
}
104+
},
105+
[
106+
247,
107+
265
108+
]
109+
]
110+
]
Lines changed: 61 additions & 0 deletions
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";
7+
import type { SourceLocation } from "../../../src/ast";
8+
import {
9+
styleNodeLoc,
10+
styleNodeRange,
11+
} from "../../../src/parser/style-context";
12+
import { generateParserOptions, listupFixtures } from "./test-utils";
13+
14+
const STYLE_CONTEXT_FIXTURE_ROOT = path.resolve(
15+
__dirname,
16+
"../../fixtures/parser/style-location-converter"
17+
);
18+
19+
function parse(code: string, filePath: string, config: any) {
20+
return parseForESLint(code, generateParserOptions({ filePath }, config));
21+
}
22+
23+
describe("Check for AST.", () => {
24+
for (const {
25+
input,
26+
inputFileName,
27+
outputFileName,
28+
config,
29+
meetRequirements,
30+
} of listupFixtures(STYLE_CONTEXT_FIXTURE_ROOT)) {
31+
describe(inputFileName, () => {
32+
let result: any;
33+
34+
it("most to generate the expected style context.", () => {
35+
result = parse(input, inputFileName, config);
36+
if (!meetRequirements("test")) {
37+
return;
38+
}
39+
const styleContext = result.services.getStyleContext();
40+
assert.strictEqual(styleContext.status, "success");
41+
const locations: [
42+
Partial<SourceLocation>,
43+
[number | undefined, number | undefined]
44+
][] = [
45+
[
46+
styleNodeLoc(styleContext.sourceAst),
47+
styleNodeRange(styleContext.sourceAst),
48+
],
49+
];
50+
styleContext.sourceAst.walk((node: Node) => {
51+
locations.push([styleNodeLoc(node), styleNodeRange(node)]);
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)