1
1
mirror of https://github.com/primer/css.git synced 2024-12-30 09:36:07 +03:00
css/index.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-05-28 17:55:56 +03:00
var stylelint = require("stylelint")
var primerUtilities = require("primer-utilities")
2016-05-28 17:55:56 +03:00
var ruleName = "primer/selector-no-utility"
var messages = stylelint.utils.ruleMessages(ruleName, {
rejected: function(classname) { return "Avoid styling the utilty class `" + classname + "`" }
2016-05-28 17:55:56 +03:00
})
module.exports = stylelint.createPlugin(ruleName, function(enabled) {
2016-05-28 17:55:56 +03:00
return function(root, result) {
2016-05-29 04:49:20 +03:00
var validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: enabled,
possible: [true, false]
})
if (!validOptions) { return }
var utilityClasses = primerUtilities.cssstats.selectors.values
2016-05-28 17:55:56 +03:00
if (utilityClasses.length == 0) {
return
}
root.walkRules(function(rule) {
var ruleClasses = rule.selector.match(/\.[a-z\-_0-9]+/ig)
if (ruleClasses != null) {
for (var i = 0; i < ruleClasses.length; i++) {
var ruleClass = ruleClasses[i]
if (utilityClasses.indexOf(ruleClass) >= 0) {
stylelint.utils.report({
message: messages.rejected(ruleClass),
node: rule,
result: result,
ruleName: ruleName
})
}
2016-05-28 17:55:56 +03:00
}
}
})
}
})
module.exports.ruleName = ruleName
module.exports.messages = messages