Skip to content

Commit 97a7a86

Browse files
Use class relationship dto in package cycles (#18)
* Use ClassRelationshipDTO in package cycles Use ClassRelationshipDTO in package cycles to indicate what class relationships need to be removed * Applying Coderabbit suggestions * Document package cycle renderer test helpers with JSDoc --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent e16134f commit 97a7a86

5 files changed

Lines changed: 1201 additions & 27 deletions

File tree

‎lib/renderer.js‎

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,56 @@ function registerLinkSafetyHook() {
5151
}
5252

5353
/**
54-
* Copies a relationship with label markup limited to source links and markers.
54+
* Sanitizes a relationship label down to links and removal markers.
5555
* The report-wide sanitizer permits template styles, so labels need their own
5656
* allow-list before interpolation. Never trust a precomputed value from JSON.
5757
*
58+
* @param {string} label - Raw label markup from the report JSON.
59+
* @returns {string} Label restricted to anchor and marker markup.
60+
*/
61+
function sanitizeRelationshipLabel(label) {
62+
return DOMPurify.sanitize(label ?? '', {
63+
ALLOWED_TAGS: ['a', 'strong'],
64+
ALLOWED_ATTR: ['href', 'target', 'rel'],
65+
ALLOW_DATA_ATTR: false,
66+
ALLOW_ARIA_ATTR: false
67+
});
68+
}
69+
70+
/**
71+
* Adapts one "class relationship to break a package cycle" entry for the
72+
* template. Reports serialize structured ClassRelationshipDTO objects, and
73+
* the template renders each entry as a raw string, so the entry is replaced
74+
* by its rendered label, sanitized to links and removal markers. Entries
75+
* of any other shape (including pre-DTO rendered-label strings) are dropped.
76+
*
77+
* @param {*} entry - Raw class-break entry.
78+
* @returns {string|null} Sanitized label markup, or null when the entry is
79+
* not a valid ClassRelationshipDTO object.
80+
*/
81+
function prepareClassBreakEntry(entry) {
82+
if (!entry || typeof entry !== 'object') return null;
83+
return sanitizeRelationshipLabel(entry.renderedLabel);
84+
}
85+
86+
/**
87+
* Copies a relationship with label markup limited to source links and markers,
88+
* and normalizes its class-break entries the same way. Never trust a
89+
* precomputed value from JSON.
90+
*
5891
* @param {object} relationship - Raw relationship data.
59-
* @returns {object} Relationship with a restricted HTML label.
92+
* @returns {object} Relationship with restricted HTML labels.
6093
*/
6194
function prepareRelationship(relationship) {
6295
registerLinkSafetyHook();
6396
return {
6497
...relationship,
65-
safeRenderedLabel: DOMPurify.sanitize(relationship.renderedLabel ?? '', {
66-
ALLOWED_TAGS: ['a', 'strong'],
67-
ALLOWED_ATTR: ['href', 'target', 'rel'],
68-
ALLOW_DATA_ATTR: false,
69-
ALLOW_ARIA_ATTR: false
70-
})
98+
safeRenderedLabel: sanitizeRelationshipLabel(relationship.renderedLabel),
99+
classRelationshipsToBreakPackage: Array.isArray(relationship.classRelationshipsToBreakPackage)
100+
? relationship.classRelationshipsToBreakPackage
101+
.map(prepareClassBreakEntry)
102+
.filter(entry => entry !== null)
103+
: []
71104
};
72105
}
73106

‎lib/table-operations.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ export const REPORT_TABLES = {
384384
{
385385
key: 'classRelationshipsToBreakPackage',
386386
label: 'Class Relationships to Remove To Break Package Relationship',
387-
value: row => (row.classRelationshipsToBreakPackage ?? []).map(stripHtml).join('; ')
387+
value: row => (row.classRelationshipsToBreakPackage ?? [])
388+
.map(entry => stripHtml(entry?.renderedLabel ?? ''))
389+
.join('; ')
388390
}
389391
]
390392
},

0 commit comments

Comments
 (0)