Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created January 19, 2017 16:02
Show Gist options
  • Save rafaelrinaldi/b50c08567f492336b32dc3485d9083f4 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/b50c08567f492336b32dc3485d9083f4 to your computer and use it in GitHub Desktop.
ESLint custom rule to ensure TODO comments to follow a specific format
module.exports = {
meta: {
docs: {
description: 'requires a TODO to be specified either with a task number or URL and a message',
category: 'Best Practices',
recommended: true
},
schema: []
},
create: function(context) {
// If comment starts with `TODO` it is considered worth parsing
function isValidTodo(node) {
var value = node.value.replace(/\/|\\|\*/gm, '').trim();
var isValid = /^todo/i.test(value);
return isValid;
}
// Check if todo has both ticket id/url and a message
function isVagueTodo(node) {
var valueWithoutTodo = node.value.replace(/^todo\s/i, '');
var isVague = /\((.+)\)\s?(\w+)/m.test(valueWithoutTodo);
return !isVague;
}
function checkForVagueTodos(node) {
if (isValidTodo(node)) {
if (isVagueTodo(node)) {
context.report(
node,
'You must specify either a task number or URL and a message for every TODO'
);
}
}
}
return {
LineComment: checkForVagueTodos,
BlockComment: checkForVagueTodos
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment