Skip to content

Commit 668265c

Browse files
Use ClassRelationshipDTO in package cycles
Use ClassRelationshipDTO in package cycles to indicate what class relationships need to be removed
1 parent e16134f commit 668265c

7 files changed

Lines changed: 1194 additions & 29 deletions

File tree

‎assets/refactor-first-report.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@
584584
<td class="rf-text-right">{{cycleCount}}</td>
585585
<td class="rf-text-right">{{effortRank}}</td>
586586
<td class="rf-text-left">{{#classRelationshipsToBreakPackage}}
587-
{{{.}}}<br>{{/classRelationshipsToBreakPackage}}</td>
587+
{{{safeRenderedLabel}}}<br>{{/classRelationshipsToBreakPackage}}</td>
588588
</tr>
589589
{{/packageRelationshipsToRemove.relationships}}
590590
</tbody>

‎lib/renderer.js‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,57 @@ 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+
* Normalizes one "class relationship to break a package cycle" entry.
72+
* Reports serialize ClassRelationshipDTO objects; entries of any other
73+
* shape (including pre-DTO rendered-label strings) are dropped.
74+
*
75+
* @param {*} entry - Raw class-break entry.
76+
* @returns {object|null} Entry extended with a sanitized label, or null when
77+
* the entry is not a valid ClassRelationshipDTO object.
78+
*/
79+
function prepareClassBreakEntry(entry) {
80+
if (!entry || typeof entry !== 'object') return null;
81+
return {
82+
...entry,
83+
safeRenderedLabel: sanitizeRelationshipLabel(entry.renderedLabel)
84+
};
85+
}
86+
87+
/**
88+
* Copies a relationship with label markup limited to source links and markers,
89+
* and normalizes its class-break entries the same way. Never trust a
90+
* precomputed value from JSON.
91+
*
5892
* @param {object} relationship - Raw relationship data.
59-
* @returns {object} Relationship with a restricted HTML label.
93+
* @returns {object} Relationship with restricted HTML labels.
6094
*/
6195
function prepareRelationship(relationship) {
6296
registerLinkSafetyHook();
6397
return {
6498
...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-
})
99+
safeRenderedLabel: sanitizeRelationshipLabel(relationship.renderedLabel),
100+
classRelationshipsToBreakPackage: Array.isArray(relationship.classRelationshipsToBreakPackage)
101+
? relationship.classRelationshipsToBreakPackage
102+
.map(prepareClassBreakEntry)
103+
.filter(entry => entry !== null)
104+
: []
71105
};
72106
}
73107

‎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
},

‎public/assets/refactor-first-report.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@
584584
<td class="rf-text-right">{{cycleCount}}</td>
585585
<td class="rf-text-right">{{effortRank}}</td>
586586
<td class="rf-text-left">{{#classRelationshipsToBreakPackage}}
587-
{{{.}}}<br>{{/classRelationshipsToBreakPackage}}</td>
587+
{{{safeRenderedLabel}}}<br>{{/classRelationshipsToBreakPackage}}</td>
588588
</tr>
589589
{{/packageRelationshipsToRemove.relationships}}
590590
</tbody>

0 commit comments

Comments
 (0)