stylelint
Version:
Modern CSS linter
45 lines (40 loc) • 1.23 kB
JavaScript
import { isString } from "lodash"
import { vendor } from "postcss"
import valueParser from "postcss-value-parser"
import {
declarationValueIndex,
isStandardSyntaxFunction,
matchesStringOrRegExp,
report,
ruleMessages,
validateOptions,
} from "../../utils"
export const ruleName = "function-whitelist"
export const messages = ruleMessages(ruleName, {
rejected: (name) => `Unexpected function "${name}"`,
})
export default function (whitelistInput) {
const whitelist = [].concat(whitelistInput)
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: whitelist,
possible: [isString],
})
if (!validOptions) { return }
root.walkDecls(decl => {
const { value } = decl
valueParser(value).walk(function (node) {
if (node.type !== "function") { return }
if (!isStandardSyntaxFunction(node)) { return }
if (matchesStringOrRegExp(vendor.unprefixed(node.value).toLowerCase(), whitelist)) { return }
report({
message: messages.rejected(node.value),
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
})
})
})
}
}