Skip to content
Merged
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
9 changes: 3 additions & 6 deletions __tests__/acceptance/eslint-with-todo-formatter-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,9 @@ describe('eslint with todo formatter', function () {
},
});

expect(readTodoStorageFile(getTodoStorageFilePath(project.baseDir)))
.toMatchInlineSnapshot(`
Array [
"add|eslint|no-unused-vars|1|10|1|16|50f2c7b9dac0a4af1cde42fe5be7963201d0504d|1638316800000|1640908800000|1643500800000|src/with-fixable-error.js",
]
`);
expect(
readTodoStorageFile(getTodoStorageFilePath(project.baseDir))
).toMatchInlineSnapshot(`Array []`);

expect(result.exitCode).toEqual(0);
});
Expand Down
48 changes: 40 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"test:jest": "npm run build && jest --no-cache"
},
"dependencies": {
"@lint-todo/utils": "^12.0.0",
"@lint-todo/utils": "^13.1.0",
"chalk": "^4.1.0",
"ci-info": "^3.3.0",
"fs-extra": "^10.0.0",
Expand Down
38 changes: 2 additions & 36 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
TodoConfig,
TodoData,
todoStorageFileExists,
Range,
validateConfig,
WriteTodoOptions,
writeTodos,
getSourceForRange,
} from '@lint-todo/utils';
import { relative, join } from 'path';
import hasFlag from 'has-flag';
Expand All @@ -23,8 +23,6 @@ import { getBaseDir } from './get-base-dir';
import type { ESLint, Linter } from 'eslint';
import type { TodoFormatterOptions } from './types';

const LINES_PATTERN = /(.*?(?:\r\n?|\n|$))/gm;

export async function formatter(results: ESLint.LintResult[]): Promise<string> {
const baseDir = getBaseDir();
const todoConfigResult = validateConfig(baseDir);
Expand Down Expand Up @@ -221,10 +219,7 @@ export function buildMaybeTodos(
ruleId: message.ruleId || '',
range,
source: lintResult.source
? getSourceForRange(
lintResult.source.match(LINES_PATTERN) || [],
range
)
? getSourceForRange(lintResult.source, range)
: '',
originalLintResult: message,
},
Expand All @@ -240,35 +235,6 @@ export function buildMaybeTodos(
return todoData;
}

function getSourceForRange(source: string[], range: Range) {
const firstLine = range.start.line - 1;
const lastLine = range.end.line - 1;
let currentLine = firstLine - 1;
const firstColumn = range.start.column - 1;
const lastColumn = range.end.column - 1;
const src = [];
let line;

while (currentLine < lastLine) {
currentLine++;
line = source[currentLine];

if (currentLine === firstLine) {
if (firstLine === lastLine) {
src.push(line.slice(firstColumn, lastColumn));
} else {
src.push(line.slice(firstColumn));
}
} else if (currentLine === lastLine) {
src.push(line.slice(0, lastColumn));
} else {
src.push(line);
}
}

return src.join('');
}

function pushResult(results: ESLint.LintResult[], todo: TodoData) {
const resultForFile = findResult(results, todo);

Expand Down