tracey-cli
Version:
A markdown-based requirements tool
65 lines (64 loc) • 3.72 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateStrategy = void 0;
var unist_util_visit_1 = __importDefault(require("unist-util-visit"));
var common_1 = require("../common");
var parseTracelinkToAnnotation = function (requirement, tracelink) {
var _a = tracelink.children, file = _a[0].children[0].title, line = _a[1].children[0].value, description = _a[2].children[0].value;
return {
description: description,
file: file,
identifier: requirement.id,
line: parseInt(line),
};
};
var isEqualAnnotation = function (existingAnnotation) { return function (linkedAnnotation) {
var hasSameDescription = linkedAnnotation.description === existingAnnotation.description;
var hasSameFile = linkedAnnotation.file === existingAnnotation.file;
var hasSameIdentifier = linkedAnnotation.identifier === existingAnnotation.identifier;
return hasSameDescription && hasSameFile && hasSameIdentifier;
}; };
/** @requirement #[ TracelinkTable.Update ]# #( Update tracelinks of a requirement if annotations and tracelinks exist )# */
exports.updateStrategy = {
shouldExecute: function (requirement, linkedAnnotations) { return linkedAnnotations.length > 0 && common_1.requirementHasTracelinks(requirement); },
execute: function (requirement, annotations) {
var linkedAnnotations = __spreadArrays(annotations);
unist_util_visit_1.default(requirement.ast, 'html', function (blockStart, index, parent) {
var _a;
if (blockStart.value === '<div class="tracey tracey-plugin-tracelinktable">' && parent) {
// Start with an empty list of update annotations
var updateAnnotations_1 = [];
unist_util_visit_1.default(parent.children[index + 1], 'tableRow', function (tableRow, index) {
if (index > 0) {
// Parse existing tracelink into annotation
var existingAnnotation = parseTracelinkToAnnotation(requirement, tableRow);
var linkedAnnotationIndex = linkedAnnotations.findIndex(isEqualAnnotation(existingAnnotation));
// If it is included in the linked annotations
if (linkedAnnotationIndex > -1) {
// Add it to the list of update annotations
updateAnnotations_1.push(linkedAnnotations[linkedAnnotationIndex]);
// Remove it from the list of linked annotations
linkedAnnotations.splice(linkedAnnotationIndex, 1);
}
}
});
// Add remaining linked annotations to the update annotations
updateAnnotations_1.push.apply(updateAnnotations_1, linkedAnnotations);
// Create a tracey block from the update annotations
var tracelinkBlock = common_1.createTracelinkBlock(updateAnnotations_1, requirement);
// write the tracey block into the requirement
(_a = parent.children).splice.apply(_a, __spreadArrays([index, tracelinkBlock.length], tracelinkBlock));
}
});
},
};