Skip to content

Commit 8688de4

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/schematics): check merge ancestry of delegate & scoped trees
This change ensures correct merge behavior when a `HostTree` is wrapped in a `ScopedTree` or `DelegateTree`. Previously the ancestry of the HostTree was not available to the merge.
1 parent 33ee976 commit 8688de4

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/angular_devkit/schematics/src/tree/host-tree.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
OverwriteFileAction,
3333
RenameFileAction,
3434
} from './action';
35+
import { DelegateTree } from './delegate';
3536
import { LazyFileEntry } from './entry';
3637
import {
3738
DirEntry,
@@ -45,6 +46,7 @@ import {
4546
UpdateRecorder,
4647
} from './interface';
4748
import { UpdateRecorderBase } from './recorder';
49+
import { ScopedTree } from './scoped';
4850

4951

5052
let _uniqueId = 0;
@@ -156,13 +158,27 @@ export class HostTree implements Tree {
156158
return branchedTree;
157159
}
158160

161+
private isAncestorOf(tree: Tree): boolean {
162+
if (tree instanceof HostTree) {
163+
return tree._ancestry.has(this._id);
164+
}
165+
if (tree instanceof DelegateTree) {
166+
return this.isAncestorOf((tree as unknown as { _other: Tree})._other);
167+
}
168+
if (tree instanceof ScopedTree) {
169+
return this.isAncestorOf((tree as unknown as { _base: Tree})._base);
170+
}
171+
172+
return false;
173+
}
174+
159175
merge(other: Tree, strategy: MergeStrategy = MergeStrategy.Default): void {
160176
if (other === this) {
161177
// Merging with yourself? Tsk tsk. Nothing to do at least.
162178
return;
163179
}
164180

165-
if (other instanceof HostTree && other._ancestry.has(this._id)) {
181+
if (this.isAncestorOf(other)) {
166182
// Workaround for merging a branch back into one of its ancestors
167183
// More complete branch point tracking is required to avoid
168184
strategy |= MergeStrategy.Overwrite;
@@ -173,7 +189,7 @@ export class HostTree implements Tree {
173189
const overwriteConflictAllowed =
174190
(strategy & MergeStrategy.AllowOverwriteConflict) == MergeStrategy.AllowOverwriteConflict;
175191
const deleteConflictAllowed =
176-
(strategy & MergeStrategy.AllowOverwriteConflict) == MergeStrategy.AllowDeleteConflict;
192+
(strategy & MergeStrategy.AllowDeleteConflict) == MergeStrategy.AllowDeleteConflict;
177193

178194
other.actions.forEach(action => {
179195
switch (action.kind) {

0 commit comments

Comments
 (0)