1
1
mirror of https://github.com/primer/css.git synced 2024-12-15 07:23:54 +03:00
css/lib/stylelint-todo.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-10-15 02:24:34 +03:00
const semver = require('semver')
const stylelint = require('stylelint')
const ruleName = 'primer-css/TODO'
const pattern = /\bTODO@([^:]+):\s+(.+)$/
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: message => message
})
module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}) => {
const {currentVersion} = options
if (!currentVersion) {
2019-10-15 02:31:51 +03:00
// eslint-disable-next-line no-console
2019-10-15 02:24:34 +03:00
console.warn(`No "currentVersion" supplied to ${ruleName}; bailing`)
return () => null
}
let match
return (root, result) => {
root.walkComments(node => {
if ((match = node.text.match(pattern))) {
2019-10-15 02:31:51 +03:00
// eslint-disable-next-line no-unused-vars
2019-10-15 02:24:34 +03:00
const [substr, todoVersion, message] = match
if (semver.lte(todoVersion, currentVersion)) {
stylelint.utils.report({
2019-10-15 02:31:51 +03:00
message: messages.rejected(
`Unresolved TODO comment: "${message}" (expected to be resolved in "${todoVersion}")`
),
2019-10-15 02:24:34 +03:00
node,
result,
ruleName
})
}
}
})
}
})