Skip to content

Commit

Permalink
Adds new readTodoData API for convenience reading of TodoDataV2 (#270)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Jackson <[email protected]>
  • Loading branch information
scalvert and rwjblue authored Jul 26, 2021
1 parent f8cf08a commit 4f01444
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Those utilities are:

<dl>
<dt><a href="#buildTodoData">buildTodoData(baseDir, lintResults, todoConfig)</a> ⇒</dt>
<dd><p>Adapts a list of <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32">LintResult</a> to a map of <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L35">TodoFileHash</a>, <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36">TodoData</a>.</p>
<dd><p>Adapts an array of <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31">LintResult</a> to a set of <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61">TodoDataV2</a>.</p>
</dd>
<dt><a href="#buildTodoDatum">buildTodoDatum(lintResult, lintMessage, todoConfig)</a> ⇒</dt>
<dd><p>Adapts an <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32">LintResult</a> to a <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36">TodoData</a>. FilePaths are absolute
<dd><p>Adapts a <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31">LintResult</a> to a <a href="https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61">TodoDataV2</a>. FilePaths are absolute
when received from a lint result, so they&#39;re converted to relative paths for stability in
serializing the contents to disc.</p>
</dd>
Expand Down Expand Up @@ -54,6 +54,9 @@ have a todo lint violation.</p>
<dt><a href="#readTodosForFilePath">readTodosForFilePath(todoStorageDir, filePath)</a> ⇒</dt>
<dd><p>Reads todo files in the .lint-todo directory for a specific filePath.</p>
</dd>
<dt><a href="#readTodoData">readTodoData(baseDir)</a> ⇒</dt>
<dd><p>Reads todo files in the .lint-todo directory and returns Todo data in an array.</p>
</dd>
<dt><a href="#getTodoBatches">getTodoBatches(lintResults, existing)</a> ⇒</dt>
<dd><p>Gets 4 maps containing todo items to add, remove, those that are expired, or those that are stable (not to be modified).</p>
</dd>
Expand Down Expand Up @@ -88,26 +91,26 @@ Config values can be present in</p>
<a name="buildTodoData"></a>

## buildTodoData(baseDir, lintResults, todoConfig) ⇒
Adapts a list of [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32) to a map of [TodoFileHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L35), [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36).
Adapts an array of [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31) to a set of [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61).

**Kind**: global function
**Returns**: - A Promise resolving to a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) of [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36).
**Returns**: - A Promise resolving to a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) of [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61).

| Param | Description |
| --- | --- |
| baseDir | The base directory that contains the .lint-todo storage directory. |
| lintResults | A list of [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32) objects to convert to [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36) objects. |
| lintResults | An array of [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31) objects to convert to [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61) objects. |
| todoConfig | An object containing the warn or error days, in integers. |

<a name="buildTodoDatum"></a>

## buildTodoDatum(lintResult, lintMessage, todoConfig) ⇒
Adapts an [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32) to a [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36). FilePaths are absolute
Adapts a [LintResult](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31) to a [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61). FilePaths are absolute
when received from a lint result, so they're converted to relative paths for stability in
serializing the contents to disc.

**Kind**: global function
**Returns**: - A [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36) object.
**Returns**: - A [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61) object.

| Param | Description |
| --- | --- |
Expand Down Expand Up @@ -155,7 +158,7 @@ Creates, or ensures the creation of, the .lint-todo directory.
Creates a file path from the linting data. Excludes extension.

**Kind**: global function
**Returns**: - The todo file path for a [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36) object.
**Returns**: - The todo file path for a [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61) object.

| Param | Description |
| --- | --- |
Expand Down Expand Up @@ -184,7 +187,7 @@ Creates a short hash for the todo's file path.
Generates a unique filename for a todo lint data.

**Kind**: global function
**Returns**: - The todo file name for a [TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36) object.
**Returns**: - The todo file name for a [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61) object.

| Param | Description |
| --- | --- |
Expand Down Expand Up @@ -214,7 +217,7 @@ have a todo lint violation.
Reads all todo files in the .lint-todo directory.

**Kind**: global function
**Returns**: - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of [TodoFilePathHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L51)/[TodoMatcher](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4).
**Returns**: - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of [TodoFilePathHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L26)/[TodoMatcher](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4).

| Param | Description |
| --- | --- |
Expand All @@ -226,20 +229,32 @@ Reads all todo files in the .lint-todo directory.
Reads todo files in the .lint-todo directory for a specific filePath.

**Kind**: global function
**Returns**: - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of [TodoFilePathHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L51)/[TodoMatcher](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4).
**Returns**: - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of [TodoFilePathHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L26)/[TodoMatcher](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4).

| Param | Description |
| --- | --- |
| todoStorageDir | The .lint-todo storage directory. |
| filePath | The relative file path of the file to return todo items for. |

<a name="readTodoData"></a>

## readTodoData(baseDir) ⇒
Reads todo files in the .lint-todo directory and returns Todo data in an array.

**Kind**: global function
**Returns**: An array of [TodoDataV2](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61)

| Param | Description |
| --- | --- |
| baseDir | The base directory that contains the .lint-todo storage directory. |

<a name="getTodoBatches"></a>

## getTodoBatches(lintResults, existing) ⇒
Gets 4 maps containing todo items to add, remove, those that are expired, or those that are stable (not to be modified).

**Kind**: global function
**Returns**: - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of [TodoFileHash](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L35)/[TodoData](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36).
**Returns**: - An object of [TodoBatches](https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L36).

| Param | Description |
| --- | --- |
Expand Down
10 changes: 5 additions & 5 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { getDatePart } from './date-utils';
const LINES_PATTERN = /(.*?(?:\r\n?|\n|$))/gm;

/**
* Adapts a list of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32|LintResult} to a map of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L35|TodoFileHash}, {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData}.
* Adapts an array of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31|LintResult} to a set of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2}.
*
* @param baseDir - The base directory that contains the .lint-todo storage directory.
* @param lintResults - A list of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32|LintResult} objects to convert to {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData} objects.
* @param lintResults - An array of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31|LintResult} objects to convert to {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2} objects.
* @param todoConfig - An object containing the warn or error days, in integers.
* @returns - A Promise resolving to a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set|Set} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData}.
* @returns - A Promise resolving to a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set|Set} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2}.
*/
export function buildTodoData(
baseDir: string,
Expand All @@ -48,14 +48,14 @@ export function buildTodoData(
}

/**
* Adapts an {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L32|LintResult} to a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData}. FilePaths are absolute
* Adapts a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/lint.ts#L31|LintResult} to a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2}. FilePaths are absolute
* when received from a lint result, so they're converted to relative paths for stability in
* serializing the contents to disc.
*
* @param lintResult - The lint result object.
* @param lintMessage - A lint message object representing a specific violation for a file.
* @param todoConfig - An object containing the warn or error days, in integers.
* @returns - A {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData} object.
* @returns - A {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2} object.
*/
export function buildTodoDatum(
baseDir: string,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
getTodoStorageDirPath,
readTodos,
readTodosForFilePath,
readTodoData,
todoStorageDirExists,
todoDirFor,
todoFileNameFor,
Expand Down
25 changes: 20 additions & 5 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getTodoStorageDirPath(baseDir: string): string {
*
* @param baseDir - The base directory that contains the .lint-todo storage directory.
* @param todoData - The linting data for an individual violation.
* @returns - The todo file path for a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData} object.
* @returns - The todo file path for a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2} object.
*/
export function todoFilePathFor(todoData: TodoDataV2): string {
return posix.join(todoDirFor(todoData.filePath), todoFileNameFor(todoData));
Expand All @@ -82,7 +82,7 @@ export function todoDirFor(filePath: string): string {
* Generates a unique filename for a todo lint data.
*
* @param todoData - The linting data for an individual violation.
* @returns - The todo file name for a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData} object.
* @returns - The todo file name for a {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2} object.
*/
export function todoFileNameFor(todoData: TodoDataV2): string {
const fileContentsHash = `${todoData.engine}${todoData.ruleId}${todoData.range.start.line}${todoData.range.start.column}`;
Expand Down Expand Up @@ -124,7 +124,7 @@ export function writeTodos(
* Reads all todo files in the .lint-todo directory.
*
* @param baseDir - The base directory that contains the .lint-todo storage directory.
* @returns - A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L51|TodoFilePathHash}/{@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4|TodoMatcher}.
* @returns - A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L26|TodoFilePathHash}/{@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4|TodoMatcher}.
*/
export function readTodos(baseDir: string): Map<TodoFilePathHash, TodoMatcher> {
const existingTodos = new Map<TodoFilePathHash, TodoMatcher>();
Expand Down Expand Up @@ -154,7 +154,7 @@ export function readTodos(baseDir: string): Map<TodoFilePathHash, TodoMatcher> {
*
* @param todoStorageDir - The .lint-todo storage directory.
* @param filePath - The relative file path of the file to return todo items for.
* @returns - A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L51|TodoFilePathHash}/{@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4|TodoMatcher}.
* @returns - A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L26|TodoFilePathHash}/{@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/todo-matcher.ts#L4|TodoMatcher}.
*/
export function readTodosForFilePath(
baseDir: string,
Expand Down Expand Up @@ -188,12 +188,27 @@ export function readTodosForFilePath(
return existingTodos;
}

/**
* Reads todo files in the .lint-todo directory and returns Todo data in an array.
*
* @param baseDir - The base directory that contains the .lint-todo storage directory.
* @returns An array of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2}
*/
export function readTodoData(baseDir: string): TodoDataV2[] {
return [...readTodos(baseDir).values()].reduce(
(matcherResults: TodoDataV2[], matcher: TodoMatcher) => {
return [...matcherResults, ...matcher.unprocessed];
},
[]
);
}

/**
* Gets 4 maps containing todo items to add, remove, those that are expired, or those that are stable (not to be modified).
*
* @param lintResults - The linting data for all violations.
* @param existing - Existing todo lint data.
* @returns - A {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L35|TodoFileHash}/{@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData}.
* @returns - An object of {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L36|TodoBatches}.
*/
export function getTodoBatches(
baseDir: string,
Expand Down
2 changes: 1 addition & 1 deletion src/todo-batch-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class TodoBatchGenerator {
constructor(private baseDir: string, private options?: Partial<WriteTodoOptions>) {}

/**
* Matches todos to their associated {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/index.ts#L36|TodoData} object.
* Matches todos to their associated {@link https://github.com/ember-template-lint/ember-template-lint-todo-utils/blob/master/src/types/todo.ts#L61|TodoDataV2} object.
*
* The matching algorithm uses the following logic:
*
Expand Down
2 changes: 1 addition & 1 deletion src/todo-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ExactMatchers = new Map<TodoFileFormat, TodoExactMatcher>([
]);

export default class TodoMatcher {
private unprocessed: Set<TodoDataV2>;
unprocessed: Set<TodoDataV2>;

constructor() {
this.unprocessed = new Set();
Expand Down

0 comments on commit 4f01444

Please sign in to comment.