@stylistic/stylelint-plugin
Version:
A collection of stylistic/formatting Stylelint rules
74 lines (58 loc) • 1.73 kB
JavaScript
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),
}
/** @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