stylelint
Version:
Modern CSS linter
47 lines (38 loc) • 1.18 kB
JavaScript
import { isString } from "lodash"
import valueParser from "postcss-value-parser"
import {
declarationValueIndexOffset,
report,
ruleMessages,
validateOptions,
} from "../../utils"
export const ruleName = "unit-blacklist"
export const messages = ruleMessages(ruleName, {
rejected: (u) => `Unexpected unit "${u}"`,
})
export default function (blacklistInput) {
const blacklist = [].concat(blacklistInput)
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: blacklist,
possible: [isString],
})
if (!validOptions) { return }
root.walkDecls(decl => {
const { value } = decl
valueParser(value).walk(function (node) {
if (node.type === "function" && node.value === "url") { return false }
const unit = valueParser.unit(node.value).unit
if (blacklist.indexOf(unit) !== -1 && node.type !== "string") {
report({
message: messages.rejected(unit),
node: decl,
index: declarationValueIndexOffset(decl) + node.sourceIndex,
result,
ruleName,
})
}
})
})
}
}