Skip to content

Commit 8999a53

Browse files
authored
Use source utils from @lint-todo/utils (#399)
1 parent 16ea3e5 commit 8999a53

File tree

4 files changed

+46
-51
lines changed

4 files changed

+46
-51
lines changed

__tests__/acceptance/eslint-with-todo-formatter-test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,9 @@ describe('eslint with todo formatter', function () {
513513
},
514514
});
515515

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

523520
expect(result.exitCode).toEqual(0);
524521
});

package-lock.json

Lines changed: 40 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"test:jest": "npm run build && jest --no-cache"
2424
},
2525
"dependencies": {
26-
"@lint-todo/utils": "^12.0.0",
26+
"@lint-todo/utils": "^13.1.0",
2727
"chalk": "^4.1.0",
2828
"ci-info": "^3.3.0",
2929
"fs-extra": "^10.0.0",

src/formatter.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
TodoConfig,
1010
TodoData,
1111
todoStorageFileExists,
12-
Range,
1312
validateConfig,
1413
WriteTodoOptions,
1514
writeTodos,
15+
getSourceForRange,
1616
} from '@lint-todo/utils';
1717
import { relative, join } from 'path';
1818
import hasFlag from 'has-flag';
@@ -23,8 +23,6 @@ import { getBaseDir } from './get-base-dir';
2323
import type { ESLint, Linter } from 'eslint';
2424
import type { TodoFormatterOptions } from './types';
2525

26-
const LINES_PATTERN = /(.*?(?:\r\n?|\n|$))/gm;
27-
2826
export async function formatter(results: ESLint.LintResult[]): Promise<string> {
2927
const baseDir = getBaseDir();
3028
const todoConfigResult = validateConfig(baseDir);
@@ -221,10 +219,7 @@ export function buildMaybeTodos(
221219
ruleId: message.ruleId || '',
222220
range,
223221
source: lintResult.source
224-
? getSourceForRange(
225-
lintResult.source.match(LINES_PATTERN) || [],
226-
range
227-
)
222+
? getSourceForRange(lintResult.source, range)
228223
: '',
229224
originalLintResult: message,
230225
},
@@ -240,35 +235,6 @@ export function buildMaybeTodos(
240235
return todoData;
241236
}
242237

243-
function getSourceForRange(source: string[], range: Range) {
244-
const firstLine = range.start.line - 1;
245-
const lastLine = range.end.line - 1;
246-
let currentLine = firstLine - 1;
247-
const firstColumn = range.start.column - 1;
248-
const lastColumn = range.end.column - 1;
249-
const src = [];
250-
let line;
251-
252-
while (currentLine < lastLine) {
253-
currentLine++;
254-
line = source[currentLine];
255-
256-
if (currentLine === firstLine) {
257-
if (firstLine === lastLine) {
258-
src.push(line.slice(firstColumn, lastColumn));
259-
} else {
260-
src.push(line.slice(firstColumn));
261-
}
262-
} else if (currentLine === lastLine) {
263-
src.push(line.slice(0, lastColumn));
264-
} else {
265-
src.push(line);
266-
}
267-
}
268-
269-
return src.join('');
270-
}
271-
272238
function pushResult(results: ESLint.LintResult[], todo: TodoData) {
273239
const resultForFile = findResult(results, todo);
274240

0 commit comments

Comments
 (0)