Skip to content

Commit c656236

Browse files
clydinhansl
authored andcommitted
fix(@angular-devkit/schematics): improve tree type checking
Fixes #11683
1 parent 4208b2c commit c656236

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/angular_devkit/schematics/src/rules/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ export function noop(): Rule {
9696
export function filter(predicate: FilePredicate<boolean>): Rule {
9797
return ((tree: Tree) => {
9898
// TODO: Remove VirtualTree usage in 7.0
99-
if (tree instanceof VirtualTree) {
99+
if (VirtualTree.isVirtualTree(tree)) {
100100
return new FilteredTree(tree, predicate);
101-
} else if (tree instanceof HostTree) {
101+
} else if (HostTree.isHostTree(tree)) {
102102
return new FilterHostTree(tree, predicate);
103103
} else {
104104
throw new SchematicsException('Tree type is not supported.');

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ export class HostTree implements Tree {
109109
return this;
110110
}
111111

112+
static isHostTree(tree: Tree): tree is HostTree {
113+
if (tree instanceof HostTree) {
114+
return true;
115+
}
116+
117+
if (typeof tree === 'object' && typeof (tree as HostTree)._ancestry === 'object') {
118+
return true;
119+
}
120+
121+
return false;
122+
}
123+
112124
constructor(protected _backend: virtualFs.ReadonlyHost<{}> = new virtualFs.Empty()) {
113125
this._record = new virtualFs.CordHost(new virtualFs.SafeReadonlyHost(_backend));
114126
this._recordSync = new virtualFs.SyncDelegateHost(this._record);

packages/angular_devkit/schematics/src/tree/virtual.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export class VirtualDirEntry implements DirEntry {
104104
}
105105
}
106106

107-
108107
/**
109108
* The root class of most trees.
110109
*/
@@ -114,6 +113,18 @@ export class VirtualTree implements Tree {
114113
protected _root = new VirtualDirEntry(this);
115114
protected _tree = new Map<Path, FileEntry>();
116115

116+
static isVirtualTree(tree: Tree): tree is VirtualTree {
117+
if (tree instanceof VirtualTree) {
118+
return true;
119+
}
120+
121+
if (typeof tree === 'object' && typeof (tree as VirtualTree)._copyTo === 'function') {
122+
return true;
123+
}
124+
125+
return false;
126+
}
127+
117128
/**
118129
* Normalize the path. Made available to subclasses to overload.
119130
* @param path The path to normalize.

0 commit comments

Comments
 (0)