Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): improve tree type checking
Browse files Browse the repository at this point in the history
Fixes #11683
  • Loading branch information
clydin authored and hansl committed Aug 17, 2018
1 parent 4208b2c commit c656236
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/angular_devkit/schematics/src/rules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export function noop(): Rule {
export function filter(predicate: FilePredicate<boolean>): Rule {
return ((tree: Tree) => {
// TODO: Remove VirtualTree usage in 7.0
if (tree instanceof VirtualTree) {
if (VirtualTree.isVirtualTree(tree)) {
return new FilteredTree(tree, predicate);
} else if (tree instanceof HostTree) {
} else if (HostTree.isHostTree(tree)) {
return new FilterHostTree(tree, predicate);
} else {
throw new SchematicsException('Tree type is not supported.');
Expand Down
12 changes: 12 additions & 0 deletions packages/angular_devkit/schematics/src/tree/host-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ export class HostTree implements Tree {
return this;
}

static isHostTree(tree: Tree): tree is HostTree {
if (tree instanceof HostTree) {
return true;
}

if (typeof tree === 'object' && typeof (tree as HostTree)._ancestry === 'object') {
return true;
}

return false;
}

constructor(protected _backend: virtualFs.ReadonlyHost<{}> = new virtualFs.Empty()) {
this._record = new virtualFs.CordHost(new virtualFs.SafeReadonlyHost(_backend));
this._recordSync = new virtualFs.SyncDelegateHost(this._record);
Expand Down
13 changes: 12 additions & 1 deletion packages/angular_devkit/schematics/src/tree/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class VirtualDirEntry implements DirEntry {
}
}


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

static isVirtualTree(tree: Tree): tree is VirtualTree {
if (tree instanceof VirtualTree) {
return true;
}

if (typeof tree === 'object' && typeof (tree as VirtualTree)._copyTo === 'function') {
return true;
}

return false;
}

/**
* Normalize the path. Made available to subclasses to overload.
* @param path The path to normalize.
Expand Down

0 comments on commit c656236

Please sign in to comment.