From 08fe118c758c095ce2825a223308f9d3aa96f9ce Mon Sep 17 00:00:00 2001 From: Shawn Allen Date: Mon, 14 Oct 2019 16:24:34 -0700 Subject: [PATCH] add TODO(version) tests --- lib/stylelint-todo.js | 34 ++++++++++++++++++++++++++++++++++ script/test-todos.js | 25 +++++++++++++++++++++++++ stylelint.config.js | 6 +++++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 lib/stylelint-todo.js create mode 100755 script/test-todos.js diff --git a/lib/stylelint-todo.js b/lib/stylelint-todo.js new file mode 100644 index 00000000..25669ce0 --- /dev/null +++ b/lib/stylelint-todo.js @@ -0,0 +1,34 @@ +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) { + console.warn(`No "currentVersion" supplied to ${ruleName}; bailing`) + return () => null + } + + let match + return (root, result) => { + root.walkComments(node => { + if ((match = node.text.match(pattern))) { + const [substr, todoVersion, message] = match + if (semver.lte(todoVersion, currentVersion)) { + stylelint.utils.report({ + message: messages.rejected(`Unresolved TODO comment: "${message}" (expected to be resolved in "${todoVersion}")`), + node, + result, + ruleName + }) + } + } + }) + } +}) diff --git a/script/test-todos.js b/script/test-todos.js new file mode 100755 index 00000000..2106a187 --- /dev/null +++ b/script/test-todos.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node +const stylelint = require('stylelint') +const {red} = require('colorette') + +const ruleName = 'primer-css/TODO' +const cwd = process.cwd() + +stylelint + .lint({files: 'src/**/*.scss'}) + .then(data => { + let fail = false + for (const {source, warnings} of data.results) { + if (warnings.some(w => w.rule === ruleName)) { + console.warn('\n' + source.substr(cwd.length + 1)) + } + for (const warning of warnings) { + if (warning.rule === ruleName) { + console.warn(`${red('✖')} ${warning.text}`) + fail = true + } + } + } + + process.exit(fail ? 1 : 0) + }) diff --git a/stylelint.config.js b/stylelint.config.js index 2ee89ebd..0b48eb1e 100644 --- a/stylelint.config.js +++ b/stylelint.config.js @@ -1,7 +1,11 @@ +const currentVersion = process.env.PRIMER_VERSION || require('./package.json').version + module.exports = { extends: ['stylelint-config-primer'], + plugins: ['./lib/stylelint-todo'], syntax: 'scss', rules: { - 'primer/no-override': false + 'primer/no-override': false, + 'primer-css/TODO': [true, {currentVersion, severity: 'error'}] } }