Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sawyerh committed Jul 17, 2020
0 parents commit f0b11ac
Show file tree
Hide file tree
Showing 12 changed files with 3,349 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# eslint-plugin-todo-plz

Enforce consistent and maintainable TODO comments.

## Installation

You'll first need to install [ESLint](http://eslint.org):

```
$ npm i eslint --save-dev
```

Next, install `eslint-plugin-todo-plz`:

```
$ npm install eslint-plugin-todo-plz --save-dev
```

## Usage

Add `todo-plz` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:

```json
{
"plugins": ["todo-plz"]
}
```

Then configure the rules you want to use under the rules section.

```json
{
"rules": {
"todo-plz/ticket-ref": ["error", { "pattern": "ABC-[0-9]+" }]
}
}
```

## Supported Rules

- [`ticket-ref`](docs/rules/ticket-ref.md)

## Inspiration

- Shoutout [`expiring-todo-comments`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/expiring-todo-comments.md) for showing me how to build my first ESLint rule.
35 changes: 35 additions & 0 deletions docs/rules/ticket-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Require a ticket reference in the TODO comment (ticket-ref)

Adding a `TODO` comment that will be addressed in the future should have a corresponding ticket (AKA issue) in the project backlog so the team doesn't lose track of the pending work.

## Fail

Examples of **incorrect** code for this rule:

```js
// TODO: Unmock this API request
```

## Pass

Examples of **correct** code for this rule:

```js
// TODO (ABC-123): Unmock this API request
```

## Options

### pattern

This option controls what the ticket pattern is to match against. Expects a regex string.

For example, let's say you're using Jira and your ticket IDs are prefixed with `ABC` followed by a number (e.g `ABC-123`), you would configure this rule like:

```json
{
"rules": {
"todo-plz/ticket-ref": ["error", { "pattern": "ABC-[0-9]+" }]
}
}
```
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports.rules = {
"ticket-ref": require("./rules/ticket-ref"),
};
74 changes: 74 additions & 0 deletions lib/rules/ticket-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @fileoverview Require a ticket reference in the TODO comment
* @author Sawyer
*/
"use strict";

const messages = {
missingTicket:
"TODO comment is missing a ticket reference matching pattern: {{ pattern }}",
};

const schema = [
{
type: "object",
properties: {
pattern: {
type: "string",
},
terms: {
type: "array",
items: {
type: "string",
},
},
},
},
];

function create(context) {
const { pattern, terms } = {
terms: ["TODO"],
...context.options[0],
};
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();

function validate(comment) {
const value = comment.value;
const hasTerm = terms.some((term) => value.includes(term));
if (!hasTerm) {
return;
}

terms.forEach((term) => {
const searchPattern = new RegExp(`${term}\\s?\\(${pattern}\\)`, "i");
if (searchPattern.test(value)) return;

context.report({
loc: comment.loc,
messageId: "missingTicket",
data: { pattern },
});
});
}

comments.forEach(validate);

return {};
}

module.exports = {
meta: {
docs: {
description: "Require a ticket reference in the TODO comment",
category: "Fill me in",
recommended: false,
},
fixable: null, // or "code" or "whitespace"
messages,
schema,
type: "suggestion",
},
create,
};
Loading

0 comments on commit f0b11ac

Please sign in to comment.