UNPKG

@gitlab-formatters/eslint-formatter-gitlab

Version:

Formatter that transforms ESLint reports into a format suitable for use with GitLab widgets.

77 lines (72 loc) 2.64 kB
'use strict'; var node_path = require('node:path'); var node_crypto = require('node:crypto'); const getRelativePath = (path, context) => { // const root = process.env.CI_PROJECT_DIR ?? context.cwd; return node_path.relative(context.cwd, path); }; const generateFingerprint = (data, hashes) => { const hash = node_crypto.createHash('md5'); for (const part of data) { if (!part) continue; hash.update(part.toString()); } // Hash collisions should not happen, but if they do, a random hash will be generated. const hashCopy = hash.copy(); let digest = hash.digest('hex'); if (hashes.has(digest)) { hashCopy.update(Math.random().toString()); digest = hashCopy.digest('hex'); } hashes.add(digest); return digest; }; const determineSeverity = (severityCode, isFatal) => { if (isFatal === true) return 'critical'; switch (severityCode) { case 1: return 'minor'; case 2: return 'major'; default: return 'info'; } }; const gitlabCodeQualityFormatter = (results, context) => { const hashes = new Set(); const issues = results.flatMap((result) => result.messages.map(message => { var _a, _b, _c, _d; return ({ type: 'issue', check_name: (_a = message.ruleId) !== null && _a !== void 0 ? _a : 'unknown_rule', description: message.message, content: { body: `Error found in ${message.ruleId}` }, categories: ['Style'], location: { path: getRelativePath(result.filePath, context), lines: { begin: message.line, end: (_b = message.endLine) !== null && _b !== void 0 ? _b : message.line }, positions: { begin: { line: message.line, column: message.column }, end: { line: (_c = message.endLine) !== null && _c !== void 0 ? _c : message.line, column: (_d = message.endColumn) !== null && _d !== void 0 ? _d : message.column } } }, severity: determineSeverity(message.severity, message.fatal), fingerprint: generateFingerprint([result.filePath, message.ruleId, message.message, `${message.line}`, `${message.column}`], hashes), }); })); return JSON.stringify(issues); }; module.exports = gitlabCodeQualityFormatter;