UNPKG

@stylistic/stylelint-plugin

Version:
71 lines (55 loc) 1.78 kB
import stylelint from "stylelint" import { addNamespace } from "../../utils/addNamespace/index.js" import { getRuleDocUrl } from "../../utils/getRuleDocUrl/index.js" import { hasBlock } from "../../utils/hasBlock/index.js" import { isStandardSyntaxAtRule } from "../../utils/isStandardSyntaxAtRule/index.js" import { rawNodeString } from "../../utils/rawNodeString/index.js" import { whitespaceChecker } from "../../utils/whitespaceChecker/index.js" let { utils: { report, ruleMessages, validateOptions } } = stylelint let shortName = `at-rule-semicolon-space-before` export let ruleName = addNamespace(shortName) export let messages = ruleMessages(ruleName, { expectedBefore: () => `Expected single space before ";"`, rejectedBefore: () => `Unexpected whitespace before ";"`, }) export let meta = { url: getRuleDocUrl(shortName), } /** * Requires or disallows whitespace before the semicolons of at-rules. * @type {import('stylelint').Rule} */ function rule (primary) { let checker = whitespaceChecker(`space`, primary, messages) return (root, result) => { let validOptions = validateOptions(result, ruleName, { actual: primary, possible: [`always`, `never`], }) if (!validOptions) return root.walkAtRules((atRule) => { if (hasBlock(atRule)) return if (!isStandardSyntaxAtRule(atRule)) return const nodeString = rawNodeString(atRule) const problemIndex = nodeString.length - 1 checker.before({ source: nodeString, index: nodeString.length, err: (m) => { report({ message: m, node: atRule, index: problemIndex, endIndex: problemIndex, result, ruleName, }) }, }) }) } } rule.ruleName = ruleName rule.messages = messages rule.meta = meta export default rule