|
1 | 1 | import { LogSeverity, LogType } from '../../Parser'; |
2 | | -import { Comment, CommentFileStructure, CommentStructure } from '../@types/CommentTypes'; |
| 2 | +import { Comment, CommentStructure } from '../@types/CommentTypes'; |
3 | 3 | import { MessageUtil } from './message.util'; |
4 | 4 |
|
5 | | -export function groupComments(logs: LogType[]): Comment[] { |
6 | | - const commentMap = logs.reduce((map: CommentStructure, log) => { |
7 | | - const { source: file, line, severity, msg } = log; |
8 | | - const text = MessageUtil.createMessageWithEmoji(msg, severity); |
| 5 | +export function groupComments(logs: LogType[], suppressRules: Set<string>): Comment[] { |
| 6 | + const commentMap = logs.reduce((state: CommentStructure, log) => { |
| 7 | + const { source: file, line } = log; |
9 | 8 |
|
10 | | - if (!line) return map; |
| 9 | + if (!line) return state; |
11 | 10 |
|
12 | | - const currentWarnings = map?.[file]?.[line]?.warnings ?? 0; |
13 | | - const currentErrors = map?.[file]?.[line]?.errors ?? 0; |
14 | | - const currentText = map?.[file]?.[line]?.text ?? ''; |
| 11 | + const currentComment = getOrInitComment(state, file, line); |
| 12 | + const updatedComment = updateComment(currentComment, log, suppressRules); |
| 13 | + return updateCommentStructure(state, updatedComment); |
| 14 | + }, {}); |
| 15 | + |
| 16 | + return Object.values(commentMap).flatMap((file) => Object.values(file)); |
| 17 | +} |
15 | 18 |
|
16 | | - const nextObject: Comment = { |
17 | | - text: `${currentText}${text} \n`, |
18 | | - errors: currentErrors + (severity === LogSeverity.error ? 1 : 0), |
19 | | - warnings: currentWarnings + (severity === LogSeverity.warning ? 1 : 0), |
| 19 | +function getOrInitComment(map: CommentStructure, file: string, line: number): Comment { |
| 20 | + return ( |
| 21 | + map?.[file]?.[line] ?? { |
| 22 | + text: '', |
| 23 | + errors: 0, |
| 24 | + warnings: 0, |
| 25 | + suppresses: 0, |
20 | 26 | file, |
21 | 27 | line, |
22 | | - }; |
| 28 | + } |
| 29 | + ); |
| 30 | +} |
23 | 31 |
|
24 | | - const fileCommentUpdater: CommentFileStructure = { [line]: nextObject }; |
25 | | - const updatedFileComment = Object.assign({}, map?.[file], fileCommentUpdater); |
| 32 | +function buildText(currentComment: Comment, log: LogType, isSuppressed: boolean): string { |
| 33 | + const { severity, msg } = log; |
| 34 | + const { text: currentText } = currentComment; |
| 35 | + const msgWithSuppression = isSuppressed ? `(SUPPRESSED) ${msg}` : msg; |
| 36 | + const text = MessageUtil.createMessageWithEmoji(msgWithSuppression, severity); |
| 37 | + return `${currentText}${text} \n`; |
| 38 | +} |
26 | 39 |
|
27 | | - const mapUpdater: CommentStructure = { [file]: updatedFileComment }; |
28 | | - return Object.assign({}, map, mapUpdater); |
29 | | - }, {}); |
| 40 | +function calculateErrors( |
| 41 | + currentComment: Comment, |
| 42 | + log: LogType, |
| 43 | + isSuppressed: boolean, |
| 44 | +): number { |
| 45 | + if (isSuppressed) return currentComment.errors; |
| 46 | + const { severity } = log; |
| 47 | + return currentComment.errors + (severity === LogSeverity.error ? 1 : 0); |
| 48 | +} |
30 | 49 |
|
31 | | - return Object.values(commentMap).flatMap((file) => Object.values(file)); |
| 50 | +function calculateWarnings( |
| 51 | + currentComment: Comment, |
| 52 | + log: LogType, |
| 53 | + isSuppressed: boolean, |
| 54 | +): number { |
| 55 | + if (isSuppressed) return currentComment.warnings; |
| 56 | + const { severity } = log; |
| 57 | + return currentComment.warnings + (severity === LogSeverity.warning ? 1 : 0); |
| 58 | +} |
| 59 | + |
| 60 | +function calculateSuppresses(currentComment: Comment, isSuppressed: boolean): number { |
| 61 | + return currentComment.suppresses + (isSuppressed ? 1 : 0); |
| 62 | +} |
| 63 | + |
| 64 | +function updateComment( |
| 65 | + currentComment: Comment, |
| 66 | + log: LogType, |
| 67 | + suppressRules: Set<string>, |
| 68 | +): Comment { |
| 69 | + const isSuppressed = suppressRules.has(log.ruleId); |
| 70 | + return { |
| 71 | + text: buildText(currentComment, log, isSuppressed), |
| 72 | + errors: calculateErrors(currentComment, log, isSuppressed), |
| 73 | + warnings: calculateWarnings(currentComment, log, isSuppressed), |
| 74 | + suppresses: calculateSuppresses(currentComment, isSuppressed), |
| 75 | + file: currentComment.file, |
| 76 | + line: currentComment.line, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function updateCommentStructure( |
| 81 | + currentStructure: CommentStructure, |
| 82 | + updatedComment: Comment, |
| 83 | +): CommentStructure { |
| 84 | + return { |
| 85 | + ...currentStructure, |
| 86 | + [updatedComment.file]: { |
| 87 | + ...currentStructure?.[updatedComment.file], |
| 88 | + [updatedComment.line]: updatedComment, |
| 89 | + }, |
| 90 | + }; |
32 | 91 | } |
0 commit comments