Skip to content

Commit 68bd4aa

Browse files
Merge pull request #98 from contentstack/fix/dx-3566-deduplicate-references
Enhance processOverlappingPaths to collect all paths and remove redun…
2 parents f273028 + 06b8d6a commit 68bd4aa

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/stack.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,27 +1227,63 @@ export class Stack {
12271227
/**
12281228
* @private
12291229
* @method processOverlappingPaths
1230-
* @description Processes overlapping paths by including all paths from each group
1230+
* @description Processes overlapping paths using a chained approach
12311231
* @param {Object} pathAnalysis - Analysis result from analyzeReferencePaths
12321232
* @returns {this} - Returns stack instance
12331233
*/
12341234
private processOverlappingPaths(pathAnalysis: any): Stack {
1235-
// Start with independent paths (if any)
1235+
// Collect ALL paths first
1236+
const allPaths: string[] = []
1237+
1238+
// Add independent paths
12361239
if (pathAnalysis.independentPaths.length > 0) {
1237-
this.q.includeSpecificReferences = [...pathAnalysis.independentPaths]
1238-
} else {
1239-
this.q.includeSpecificReferences = []
1240+
allPaths.push(...pathAnalysis.independentPaths)
12401241
}
12411242

1242-
// Process each overlapping group by including ALL paths from each group
1243+
// Add all paths from overlapping groups
12431244
pathAnalysis.overlappingGroups.forEach((group: string[]) => {
1244-
// Add all paths from the group to includeSpecificReferences
1245-
this.q.includeSpecificReferences.push(...group)
1245+
allPaths.push(...group)
12461246
})
12471247

1248+
// Remove exact duplicates
1249+
const uniquePaths = [...new Set(allPaths)]
1250+
1251+
// Smart filtering: remove parent paths that are fully covered by child paths
1252+
const filteredPaths = this.removeRedundantPaths(uniquePaths)
1253+
1254+
this.q.includeSpecificReferences = filteredPaths
12481255
return this
12491256
}
12501257

1258+
/**
1259+
* @private
1260+
* @method removeRedundantPaths
1261+
* @description Removes parent paths that are fully covered by more specific child paths
1262+
* Example: ["content", "content.content"] → ["content.content"]
1263+
* But keeps: ["form", "form.fields", "form.fields.rules"] → all three (not redundant)
1264+
*/
1265+
private removeRedundantPaths(paths: string[]): string[] {
1266+
// Sort by length (longest first) to prioritize more specific paths
1267+
const sortedPaths = paths.sort((a, b) => b.length - a.length)
1268+
const result: string[] = []
1269+
1270+
for (const currentPath of sortedPaths) {
1271+
// Check if this path is made redundant by a more specific path already in result
1272+
const isRedundant = result.some(existingPath => {
1273+
// Check if currentPath is a direct parent of existingPath
1274+
return existingPath.startsWith(currentPath + '.') &&
1275+
existingPath.split('.').length === currentPath.split('.').length + 1
1276+
})
1277+
1278+
if (!isRedundant) {
1279+
result.push(currentPath)
1280+
}
1281+
}
1282+
1283+
return result
1284+
}
1285+
1286+
12511287
/**
12521288
* @private
12531289
* @method preProcess

typings/stack.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,19 @@ export declare class Stack {
513513
/**
514514
* @private
515515
* @method processOverlappingPaths
516-
* @description Processes overlapping paths by including all paths from each group
516+
* @description Processes overlapping paths using a chained approach
517517
* @param {Object} pathAnalysis - Analysis result from analyzeReferencePaths
518518
* @returns {this} - Returns stack instance
519519
*/
520520
private processOverlappingPaths;
521+
/**
522+
* @private
523+
* @method removeRedundantPaths
524+
* @description Removes parent paths that are fully covered by more specific child paths
525+
* Example: ["content", "content.content"] → ["content.content"]
526+
* But keeps: ["form", "form.fields", "form.fields.rules"] → all three (not redundant)
527+
*/
528+
private removeRedundantPaths;
521529
/**
522530
* @private
523531
* @method preProcess

0 commit comments

Comments
 (0)