|
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; |
2 | 12 |
|
3 | 13 | /** |
4 | 14 | * Default `Parser` instance. |
5 | 15 | */ |
6 | 16 | 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+\S.*\r?\n^\s+~+$(?:\r?\n){0,1})*)){0,1}$/gm, |
| 18 | + KEYS, |
17 | 19 | (input) => { |
18 | 20 | // biome-ignore lint/suspicious/noControlCharactersInRegex: needed for removing colored text |
19 | 21 | 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; |
20 | 63 | } |
21 | 64 | ); |
| 65 | + |
| 66 | +/** |
| 67 | + * Default `Parser` result type. |
| 68 | + */ |
| 69 | +export type DefaultParserResult = ParseResult<typeof KEYS>; |
0 commit comments