Skip to content

Commit 1ff38c5

Browse files
committed
feat: implement resultModifier logic on defaultParser
1 parent da6b182 commit 1ff38c5

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

src/parsers/default-parser.ts

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,69 @@
1-
import { Parser } from 'src/blueprints/Parser';
1+
import { type ParseResult, Parser } from 'src/blueprints/Parser';
2+
3+
const KEYS = [
4+
'file',
5+
'line',
6+
'column',
7+
'errorCode',
8+
'message',
9+
'source',
10+
'sourceClean'
11+
] as const;
212

313
/**
414
* Default `Parser` instance.
515
*/
616
export const defaultParser = new Parser(
7-
/^(?:(?:(.*?)[:(](\d+)[:,](\d+)[)]? ?[:-] ?)|(?:error ?))(?:error ?)?(TS\d+)?(?:(?:: )|(?: - )|(?: ))(.*(?:\r?\n {2,}.*)*)(?:(?:\r?\n){2,}(\d+\s+(.*)\r?\n\s+~+))?$/gm,
8-
[
9-
'file',
10-
'line',
11-
'column',
12-
'errorCode',
13-
'message',
14-
'source',
15-
'sourceClean'
16-
] as const,
17+
/^(?:(?:(.*?)[:(](\d+)[:,](\d+)[)]? ?[:-] ?)|(?:error ?))(?:error ?)?(TS\d+)?(?:(?:: )|(?: - )|(?: ))(.*(?:\r?\n {2,}.*)*)(?:(?:\r?\n){2,}((?:\d+\s+.*\r?\n\s+~+(?:\r?\n)?)*))?$/gm,
18+
KEYS,
1719
(input) => {
1820
// biome-ignore lint/suspicious/noControlCharactersInRegex: needed for removing colored text
1921
return input.replaceAll(/\x1b\[[0-9;]*m/g, '');
22+
},
23+
(result) => {
24+
result._match = result._match.trimEnd();
25+
26+
if (result.source) {
27+
result.source = result.source.trim();
28+
29+
const matches = Array.from(
30+
result.source.trim().matchAll(/^(?:\d+)(\s.*)$/gm)
31+
);
32+
33+
let minIndent: number;
34+
35+
const codeLines = matches.reduce<string[]>((acc, curr) => {
36+
if (curr?.[1]) {
37+
const indentLength = (curr[1].match(/^(\s+).*$/)?.[1] || '').length;
38+
if (minIndent === undefined) {
39+
minIndent = indentLength;
40+
} else {
41+
minIndent = indentLength < minIndent ? indentLength : minIndent;
42+
}
43+
acc.push(curr[1]);
44+
}
45+
return acc;
46+
}, []);
47+
48+
if (codeLines.length > 0) {
49+
result.sourceClean = codeLines.reduce((acc, curr, currIndex) => {
50+
let res = acc;
51+
res += curr.slice(minIndent);
52+
53+
if (currIndex !== codeLines.length - 1) {
54+
res += '\n';
55+
}
56+
57+
return res;
58+
}, '');
59+
}
60+
}
61+
62+
return result;
2063
}
2164
);
65+
66+
/**
67+
* Default `Parser` result type.
68+
*/
69+
export type DefaultParserResult = ParseResult<typeof KEYS>;

0 commit comments

Comments
 (0)