UNPKG

node-sarif-builder

Version:
140 lines (139 loc) 6.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SarifBuilder = void 0; const fs = __importStar(require("fs-extra")); const languages_1 = require("./languages"); const utils_1 = require("./utils"); // SARIF Builder class SarifBuilder { // Initialize SARIF Log builder constructor(options = {}) { // Default run value this.log = { $schema: 'http://json.schemastore.org/sarif-2.1.0.json', version: '2.1.0', runs: [], }; (0, utils_1.setOptionValues)(options, this.log); } addRun(sarifRunBuilder) { this.log.runs.push(sarifRunBuilder.run); } generateSarifFileSync(file) { const sarifJsonString = this.buildSarifJsonString(); fs.writeFileSync(file, sarifJsonString, 'utf8'); } async generateSarifFile(file) { const sarifJsonString = this.buildSarifJsonString(); await fs.writeFile(file, sarifJsonString, 'utf8'); } buildSarifOutput() { // Complete runs this.log.runs = this.log.runs.map((run) => this.completeRunFields(run)); return this.log; } // Build final sarif json, complete when possible buildSarifJsonString(options = { indent: false }) { this.buildSarifOutput(); const sarifJson = options.indent ? JSON.stringify(this.log, null, 2) : JSON.stringify(this.log); if (sarifJson.includes('SARIF_BUILDER_INVALID')) { throw new Error('Your SARIF log is invalid, please solve SARIF_BUILDER_INVALID messages'); } return sarifJson; } completeRunFields(run) { var _a, _b, _c, _d, _e, _f; // Collect all missing artifacts from results run.artifacts = run.artifacts || []; const existingArtifactUris = new Set(run.artifacts.map((a) => { var _a; return (_a = a === null || a === void 0 ? void 0 : a.location) === null || _a === void 0 ? void 0 : _a.uri; }).filter(Boolean)); for (const result of run.results || []) { for (const location of result.locations || []) { const uri = (_b = (_a = location === null || location === void 0 ? void 0 : location.physicalLocation) === null || _a === void 0 ? void 0 : _a.artifactLocation) === null || _b === void 0 ? void 0 : _b.uri; if (uri && !existingArtifactUris.has(uri)) { // Add result to driver artifact only if not existing const lastDot = uri.lastIndexOf('.'); const ext = lastDot !== -1 ? uri.slice(lastDot + 1) : ''; const language = languages_1.EXTENSIONS_LANGUAGES[ext] || 'unknown'; run.artifacts.push({ sourceLanguage: language, location: { uri }, }); existingArtifactUris.add(uri); } } } // Build artifact index map (uri -> index) for O(1) lookups const artifactIndexMap = new Map(); run.artifacts.forEach((artifact, index) => { var _a; const uri = (_a = artifact === null || artifact === void 0 ? void 0 : artifact.location) === null || _a === void 0 ? void 0 : _a.uri; if (uri) { artifactIndexMap.set(uri, index); } }); // Build rules index map (id -> index) for O(1) lookups const rulesIndexMap = new Map(); (((_d = (_c = run === null || run === void 0 ? void 0 : run.tool) === null || _c === void 0 ? void 0 : _c.driver) === null || _d === void 0 ? void 0 : _d.rules) || []).forEach((rule, index) => { rulesIndexMap.set(rule.id, index); }); // Update index in results with computed values for (const result of run.results || []) { // Set rule index in results if (result.ruleId) { const ruleIndex = rulesIndexMap.get(result.ruleId); if (ruleIndex !== undefined) { result.ruleIndex = ruleIndex; } } // Set artifact index in results if (result.locations) { for (const location of result.locations) { const uri = (_f = (_e = location === null || location === void 0 ? void 0 : location.physicalLocation) === null || _e === void 0 ? void 0 : _e.artifactLocation) === null || _f === void 0 ? void 0 : _f.uri; if (uri) { const artIndex = artifactIndexMap.get(uri); if (artIndex !== undefined) { location.physicalLocation.artifactLocation.index = artIndex; } } } } } return run; } } exports.SarifBuilder = SarifBuilder;