1
1
mirror of https://github.com/primer/css.git synced 2024-11-30 01:04:04 +03:00
css/tools/stylelint-selector-no-utility/index.js
2018-11-08 09:58:56 -08:00

44 lines
1.1 KiB
JavaScript

const stylelint = require('stylelint')
const ruleName = 'primer/selector-no-utility'
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: classname => `Avoid styling the utilty class "${classname}"`
})
const match = require('./match')
const utilityClasses = require('./classes.json')
module.exports = stylelint.createPlugin(ruleName, enabled => {
return (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: enabled,
possible: [true, false]
})
if (!validOptions) {
return
}
root.walkRules(rule => {
const matches = match(rule.selector)
if (!matches) {
return
}
for (const ruleClass of Array.from(matches)) {
if (utilityClasses.indexOf(ruleClass) >= 0) {
stylelint.utils.report({
message: messages.rejected(ruleClass),
node: rule,
result: result,
ruleName: ruleName
})
}
}
})
}
})
module.exports.ruleName = ruleName
module.exports.messages = messages