UNPKG

@ec0lint/plugin-css

Version:

ec0lint plugin that provides rules to verify CSS definition objects

69 lines (68 loc) 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../utils"); const color_1 = require("../utils/color"); exports.default = (0, utils_1.createRule)("no-invalid-color-hex", { meta: { docs: { description: "disallow invalid hex colors", category: "Possible Errors", recommended: true, stylelint: "color-no-invalid-hex", }, schema: [], messages: { invalid: "Unexpected invalid hex color '{{hex}}'.", }, type: "problem", }, create(context) { function createVisitor(_cssContext) { return { onProperty(property) { const value = property.getValue(); if (!value) { return; } const parsedValue = value.parsed; parsedValue.walk(({ value: textValue, type, sourceIndex }) => { if (type === "function" && textValue.toLowerCase() === "url") return false; if (type !== "word") return undefined; const hexMatch = /^#[\dA-Za-z]+/u.exec(textValue); if (!hexMatch) return undefined; const hexValue = hexMatch[0]; if ((0, color_1.parseHexColor)(hexValue).isComplete()) return undefined; const sourceCode = context.getSourceCode(); const startIndex = value.expression.range[0] + sourceIndex + 1; const endIndex = startIndex + textValue.length; const loc = value.directExpression ? { start: sourceCode.getLocFromIndex(startIndex), end: sourceCode.getLocFromIndex(endIndex), } : undefined; context.report({ node: value.expression, loc, messageId: "invalid", data: { hex: hexValue, }, }); return undefined; }); }, }; } return (0, utils_1.defineCSSVisitor)(context, { createVisitor, }); }, });