diff --git a/test/helpers/ast-checker.js b/test/helpers/ast-checker.js new file mode 100644 index 0000000..2bb78be --- /dev/null +++ b/test/helpers/ast-checker.js @@ -0,0 +1,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 diff --git a/test/helpers/check-fixture.js b/test/helpers/check-fixture.js index 849588f..aa8a5d7 100644 --- a/test/helpers/check-fixture.js +++ b/test/helpers/check-fixture.js @@ -8,6 +8,7 @@ const postcss = require("postcss") // plugin const atImport = require("../..") +const astCheckerPlugin = require("./ast-checker") function read(name, ext) { ext = ext || ".css" @@ -20,7 +21,7 @@ module.exports = function (t, file, opts, postcssOpts, warnings) { if (typeof file === "string") file = { name: file, ext: ".css" } const { name, ext } = file - return postcss(atImport(opts)) + return postcss([atImport(opts), astCheckerPlugin()]) .process(read(name, ext), postcssOpts || {}) .then(result => { const actual = result.css