You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.5 KiB
JavaScript

"use strict";
const wrapError = require("@validatem/wrap-error");
const hasShape = require("@validatem/has-shape");
const isFunction = require("@validatem/is-function");
const isPlainObject = require("@validatem/is-plain-object");
const allowExtraProperties = require("@validatem/allow-extra-properties");
const either = require("@validatem/either");
const required = require("@validatem/required");
const ValidationError = require("@validatem/error");
// NOTE: We use `hasShape` explicitly here, since the implicit `hasShape` wrapper you get when specifying an object literal will also enforce that the value *itself* is a plain object. But that is not the case for PostCSS plugins, which are functions that happen to have additional properties specified on them.
function hasPostCSSProperty(object) {
// This is custom logic that only checks whether the property *exists*, and is guaranteed not to attempt to get its *value* as on PostCSS 7, the property is a getter that lazily constructs an instance. We don't want to trigger that logic just by checking the shape of the object, which might happen if we specify the property in the `hasShape` rule instead.
if (!("postcss" in object)) {
throw new ValidationError(`Must have a 'postcss' property`);
}
}
let plugin = [
isFunction,
hasPostCSSProperty,
];
let pluginFunction = [
isPlainObject,
allowExtraProperties(hasShape({
postcssPlugin: [ required ],
}))
];
module.exports = wrapError("Must be a PostCSS plugin", either([ plugin, pluginFunction ]));