Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions __tests__/__utils__/build-todo-data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { buildTodoDatum, generateHash } from '../../src/builders';
import { todoDirFor } from '../../src/io';
import TodoMatcher from '../../src/todo-matcher';
import {
FilePath,
LintMessage,
LintResult,
Location,
Range,
TodoConfig,
TodoDataV2,
TodoFilePathHash,
} from '../../src/types';
import { getFixture } from './get-fixture';
import { updatePaths } from './update-path';
Expand Down Expand Up @@ -58,7 +57,7 @@ export function buildExistingTodos(
baseDir: string,
lintResults: LintResult[],
todoConfig?: TodoConfig
): Map<TodoFilePathHash, TodoMatcher> {
): Map<FilePath, TodoMatcher> {
const results = updatePaths(baseDir, lintResults).filter((result) => result.messages.length > 0);

const todoData = results.reduce((converted, lintResult) => {
Expand All @@ -77,28 +76,27 @@ export function buildExistingTodos(
},
todoConfig
);
const todoFilePathHash = todoDirFor(todoDatum.filePath);

if (!converted.has(todoFilePathHash)) {
converted.set(todoFilePathHash, new TodoMatcher());
if (!converted.has(todoDatum.filePath)) {
converted.set(todoDatum.filePath, new TodoMatcher());
}

const matcher = converted.get(todoFilePathHash);
const matcher = converted.get(todoDatum.filePath);

matcher?.add(todoDatum);
}
});

return converted;
}, new Map<TodoFilePathHash, TodoMatcher>());
}, new Map<FilePath, TodoMatcher>());

return todoData;
}

export function buildExistingTodosFromFixture(
baseDir: string,
fixtureName: string
): Map<TodoFilePathHash, TodoMatcher> {
): Map<FilePath, TodoMatcher> {
const fixture = getFixture(fixtureName, baseDir, false);
return buildExistingTodos(baseDir, fixture);
}
Expand Down
181 changes: 118 additions & 63 deletions __tests__/builders-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { differenceInDays } from 'date-fns';
import { getDatePart } from '../src/date-utils';
import { TodoDataV1, TodoDataV2 } from '../src/types';
import { buildTodoDatum, normalizeToV2 } from '../src/builders';
import { buildFromTodoOperations, buildTodoDatum } from '../src/builders';
import { createTmpDir } from './__utils__/tmp-dir';

describe('builders', () => {
Expand Down Expand Up @@ -223,80 +222,136 @@ describe('builders', () => {
});
});

describe('normalizeToV2', () => {
it('returns a v2 todo when a v1 is provided', () => {
const todoDatum: TodoDataV1 = {
engine: 'eslint',
filePath: 'app/controllers/settings.js',
ruleId: 'no-prototype-builtins',
line: 25,
column: 21,
createdDate: getDatePart(new Date('2021-01-01')).getTime(),
fileFormat: 1,
};
describe('buildFromOperations', () => {
it('builds single todo from single add', () => {
const todoOperations: string[] = [
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
];

expect(normalizeToV2(todoDatum)).toMatchInlineSnapshot(`
Object {
"createdDate": 1609459200000,
"engine": "eslint",
"fileFormat": 1,
"filePath": "app/controllers/settings.js",
"range": Object {
"end": Object {
"column": 21,
"line": 25,
},
"start": Object {
"column": 21,
"line": 25,
const todos = buildFromTodoOperations(todoOperations);

expect(todos.size).toEqual(1);
expect(todos).toMatchInlineSnapshot(`
Map {
"addon/templates/components/foo.hbs" => TodoMatcher {
"unprocessed": Set {
Object {
"createdDate": 1629331200000,
"engine": "ember-template-lint",
"errorDate": 2493334800000,
"fileFormat": 2,
"filePath": "addon/templates/components/foo.hbs",
"range": Object {
"end": Object {
"column": 8,
"line": 174,
},
"start": Object {
"column": 8,
"line": 174,
},
},
"ruleId": "no-implicit-this",
"source": "864e3ef2438ac413d96a032cdd141e567fcc04b3",
"warnDate": 2493248400000,
},
},
},
"ruleId": "no-prototype-builtins",
"source": "",
}
`);
});

it('returns a v2 todo when a v2 is provided', () => {
const todoDatum: TodoDataV2 = {
engine: 'eslint',
filePath: 'app/controllers/settings.js',
ruleId: 'no-prototype-builtins',
range: {
start: {
line: 25,
column: 21,
},
end: {
line: 25,
column: 29,
},
},
source: '',
createdDate: getDatePart(new Date('2021-01-01')).getTime(),
fileFormat: 2,
};
it('builds single todo from single add with pipes in filePath', () => {
const todoOperations: string[] = [
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/fo|o.hbs',
];

expect(normalizeToV2(todoDatum)).toMatchInlineSnapshot(`
Object {
"createdDate": 1609459200000,
"engine": "eslint",
"fileFormat": 2,
"filePath": "app/controllers/settings.js",
"range": Object {
"end": Object {
"column": 29,
"line": 25,
const todos = buildFromTodoOperations(todoOperations);

expect(todos.size).toEqual(1);
expect(todos).toMatchInlineSnapshot(`
Map {
"addon/templates/components/fo|o.hbs" => TodoMatcher {
"unprocessed": Set {
Object {
"createdDate": 1629331200000,
"engine": "ember-template-lint",
"errorDate": 2493334800000,
"fileFormat": 2,
"filePath": "addon/templates/components/fo|o.hbs",
"range": Object {
"end": Object {
"column": 8,
"line": 174,
},
"start": Object {
"column": 8,
"line": 174,
},
},
"ruleId": "no-implicit-this",
"source": "864e3ef2438ac413d96a032cdd141e567fcc04b3",
"warnDate": 2493248400000,
},
},
"start": Object {
"column": 21,
"line": 25,
},
}
`);
});

it('builds single todo from multiple identical adds', () => {
const todoOperations: string[] = [
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
];

expect(buildFromTodoOperations(todoOperations)).toMatchInlineSnapshot(`
Map {
"addon/templates/components/foo.hbs" => TodoMatcher {
"unprocessed": Set {
Object {
"createdDate": 1629331200000,
"engine": "ember-template-lint",
"errorDate": 2493334800000,
"fileFormat": 2,
"filePath": "addon/templates/components/foo.hbs",
"range": Object {
"end": Object {
"column": 8,
"line": 174,
},
"start": Object {
"column": 8,
"line": 174,
},
},
"ruleId": "no-implicit-this",
"source": "864e3ef2438ac413d96a032cdd141e567fcc04b3",
"warnDate": 2493248400000,
},
},
},
"ruleId": "no-prototype-builtins",
"source": "",
}
`);
});

it('builds empty todos from single add and remove', () => {
const todoOperations: string[] = [
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
'remove|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
];

expect(buildFromTodoOperations(todoOperations)).toMatchInlineSnapshot(`Map {}`);
});

it('builds empty todos from single add and multiple identical removes', () => {
const todoOperations: string[] = [
'add|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
'remove|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
'remove|ember-template-lint|no-implicit-this|174|8|174|8|864e3ef2438ac413d96a032cdd141e567fcc04b3|2|1629331200000|2493248400000|2493334800000|addon/templates/components/foo.hbs',
];

expect(buildFromTodoOperations(todoOperations)).toMatchInlineSnapshot(`Map {}`);
});
});
});
Loading