Skip to content

Commit

Permalink
Calculate removed and added lines separate and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Apr 30, 2024
1 parent 2e328df commit fbc62e3
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 45 deletions.
122 changes: 118 additions & 4 deletions __test__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { findTodos, generateComment } from '../src/tools'
import { Todo } from '../src/types'
import { excludeFilesPrDiff, todoPrDiff } from './test_data'
import {
excludeFilesDiff,
mixedTodoDiff,
newFileTodoDiff,
startWithRemovedLineTodoDiff,
updateTodoDiff
} from './test_data'

describe('extractTodos', () => {
it('should correctly extract todos', () => {
const fileTodos = findTodos(todoPrDiff)
it('should correctly extract mixed todos', () => {
const fileTodos = findTodos(mixedTodoDiff)

const expectedTodos: Todo[] = [
{
Expand Down Expand Up @@ -47,10 +53,118 @@ describe('extractTodos', () => {
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract updated todos', () => {
const fileTodos = findTodos(updateTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'README.md',
line: 29,
content: 'todo remved',
isAdded: false
},
{
filename: 'README.md',
line: 29,
content: 'todo updated',
isAdded: true
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract todos from a new file', () => {
const fileTodos = findTodos(newFileTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'first.js',
line: 3,
content: 'TODO first todo',
isAdded: true
},
{
filename: 'first.js',
line: 4,
content: 'TODO second todo',
isAdded: true
},
{
filename: 'first.js',
line: 5,
content: 'TODO third todo',
isAdded: true
},
{
filename: 'first.js',
line: 6,
content: 'TODO fourth todo',
isAdded: true
},
{
filename: 'first.js',
line: 14,
content: 'TODO: Implement methodA',
isAdded: true
},
{
filename: 'first.js',
line: 25,
content: 'TODO: Implement methodB',
isAdded: true
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract todos when diff starts with removed lines', () => {
const fileTodos = findTodos(startWithRemovedLineTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'first.js',
line: 4,
content: 'TODO first todo',
isAdded: false
},
{
filename: 'first.js',
line: 5,
content: 'TODO second todo',
isAdded: false
},
{
filename: 'first.js',
line: 6,
content: 'TODO third todo',
isAdded: false
},
{
filename: 'first.js',
line: 7,
content: 'TODO fourth todo',
isAdded: false
},
{
filename: 'first.js',
line: 15,
content: 'TODO: Implement methodA',
isAdded: true
},
{
filename: 'first.js',
line: 25,
content: 'TODO: Implement methodB',
isAdded: false
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly exclude files', () => {
const exclude = ['**/*.yml', '**/excluded/*']

const fileTodos = findTodos(excludeFilesPrDiff, exclude)
const fileTodos = findTodos(excludeFilesDiff, exclude)

const expectedTodos: Todo[] = [
{
Expand Down
117 changes: 110 additions & 7 deletions __test__/test_data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrDiff } from '../src/types'

export const todoPrDiff: PrDiff = [
export const mixedTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'README.md',
Expand All @@ -16,7 +16,6 @@ export const todoPrDiff: PrDiff = [
- [Tool_A](https://example.com)
- [Tool_B](https://example.com)
-- [Tool_C](https://example.net)
\\ No newline at end of file
+- [Tool_C](https://example.com)
+
+- // TODO here`
Expand Down Expand Up @@ -70,7 +69,111 @@ export const todoPrDiff: PrDiff = [
}
]

export const excludeFilesPrDiff: PrDiff = [
export const updateTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'README.md',
status: 'modified',
additions: 3,
deletions: 1,
changes: 4,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -26,4 +26,6 @@ TODO:
First line
- [Tool_A](https://example.com)
- [Tool_B](https://example.com)
-- // todo remved
+- // todo updated`
}
]

export const newFileTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.js',
status: 'added',
additions: 27,
deletions: 0,
changes: 0,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -0,0 +1,27 @@
+// import second.js
+
+// TODO first todo
+// TODO second todo
+// TODO third todo
+// TODO fourth todo
+// Dummy class A
+class A {
+ constructor() {
+ this.propertyA = 'Value A';
+ }
+
+ methodA() {
+ // TODO: Implement methodA
+ }
+}
+
+// Dummy class B
+class B {
+ constructor() {
+ this.propertyB = 'Value B';
+ }
+
+ methodB() {
+ // TODO: Implement methodB
+ }
+}`
}
]

export const startWithRemovedLineTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.js',
status: 'added',
additions: 27,
deletions: 0,
changes: 0,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -2,0 +8,27 @@
// import second.js
-
-// TODO first todo
-// TODO second todo
-// TODO third todo
-// TODO fourth todo
-// Dummy class A
class A {
constructor() {
this.propertyA = 'Value A';
}
methodA() {
+ // TODO: Implement methodA
}
}
// Dummy class B
-class B {
- constructor() {
- this.propertyB = 'Value B';
- }
-
- methodB() {
- // TODO: Implement methodB
- }
-}`
}
]

export const excludeFilesDiff: PrDiff = [
{
sha: 'sha',
filename: 'filename.js',
Expand All @@ -81,7 +184,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in filename js`,
previous_filename: undefined
},
Expand All @@ -95,7 +198,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in filename yml`,
previous_filename: undefined
},
Expand All @@ -109,7 +212,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in excluded directory`,
previous_filename: undefined
},
Expand All @@ -123,7 +226,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in included directory`,
previous_filename: undefined
}
Expand Down
61 changes: 45 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29482,7 +29482,8 @@ exports.generateComment = exports.findTodos = void 0;
const minimatch_1 = __nccwpck_require__(4501);
function findTodos(prDiff, exclude = []) {
// Find first number in string
const regex = /(\d+)/;
const firstAddedLineRegex = /\+(\d+)/;
const firstRemovedLineRegex = /-(\d+)/;
const todos = [];
for (const file of prDiff) {
const excluded = exclude.some(pattern => (0, minimatch_1.minimatch)(file.filename, pattern));
Expand All @@ -29494,36 +29495,64 @@ function findTodos(prDiff, exclude = []) {
continue;
// remove first line and get the line number where the patch starts
const firstLine = lines.shift();
const match = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(regex);
if (match === undefined || match === null || (match === null || match === void 0 ? void 0 : match.length) === 0)
let addedStartLineNumer;
const addedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstAddedLineRegex);
if (addedMatch !== undefined &&
addedMatch !== null &&
(addedMatch === null || addedMatch === void 0 ? void 0 : addedMatch.length) > 1) {
addedStartLineNumer = parseInt(addedMatch[1]);
}
let removedStartLineNumer;
const removedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstRemovedLineRegex);
if (removedMatch !== undefined &&
removedMatch !== null &&
(removedMatch === null || removedMatch === void 0 ? void 0 : removedMatch.length) > 1) {
removedStartLineNumer = parseInt(removedMatch[1]);
}
if (addedStartLineNumer === undefined ||
removedStartLineNumer === undefined) {
continue;
const startLineNumer = parseInt(match[0]);
}
// get all todos from the patch map them to the line number
let currentLine = startLineNumer;
let currentAddedLine = addedStartLineNumer;
let currentRemovedLine = removedStartLineNumer;
for (const line of lines) {
const isAdded = line.startsWith('+');
const isDeleted = line.startsWith('-');
const todo = getTodoIfFound(line);
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentLine,
content: todo,
isAdded: !isDeleted
});
}
if (isDeleted) {
currentLine -= 1;
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentRemovedLine,
content: todo,
isAdded: false
});
}
currentRemovedLine += 1;
}
else if (isAdded) {
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentAddedLine,
content: todo,
isAdded: true
});
}
currentAddedLine += 1;
}
else {
currentLine += 1;
currentAddedLine += 1;
currentRemovedLine += 1;
}
}
}
return todos;
}
exports.findTodos = findTodos;
function getTodoIfFound(line) {
const regex = /[/*#]+.*(TODO.*|FIXME.*)/i;
const regex = /[/*#]+.*?(TODO.*|FIXME.*)/i;
const match = line.match(regex);
if (match === undefined || match === null || (match === null || match === void 0 ? void 0 : match.length) === 0)
return;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit fbc62e3

Please sign in to comment.