-
Notifications
You must be signed in to change notification settings - Fork 116
/
ast-checker.js
48 lines (40 loc) · 1.23 KB
/
ast-checker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use strict"
const astCheckerPlugin = () => {
return {
postcssPlugin: "ast-checker-plugin",
OnceExit(root) {
root.walkAtRules(node => {
if (typeof node.params !== "string") {
throw node.error(
`Params must be of type 'string', found '${typeof node.params}' instead`,
)
}
if (typeof node.type !== "string") {
throw node.error(
`Type must be of type 'string', found '${typeof node.type}' instead`,
)
}
if (node.type !== "atrule") {
throw node.error(
`Type must be 'atrule', found '${node.type}' instead`,
)
}
if (typeof node.name !== "string") {
throw node.error(
`Name must be of type 'string', found '${typeof node.name}' instead`,
)
}
if (node.nodes && !Array.isArray(node.nodes)) {
throw node.error(
`Nodes must be of type 'Array' when it is present, found '${typeof node.nodes}' instead`,
)
}
if (!("parent" in node)) {
throw node.error("AtRule must have a 'parent' property")
}
})
},
}
}
astCheckerPlugin.postcss = true
module.exports = astCheckerPlugin