Skip to content

Commit c875424

Browse files
committed
refactor(@angular-devkit/schematics): cleanup call implementations
1 parent 3108ce3 commit c875424

File tree

1 file changed

+19
-21
lines changed
  • packages/angular_devkit/schematics/src/rules

1 file changed

+19
-21
lines changed

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

+19-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { BaseException, isObservable } from '@angular-devkit/core';
99
import { Observable, of as observableOf, throwError } from 'rxjs';
10-
import { last, mergeMap, tap } from 'rxjs/operators';
10+
import { defaultIfEmpty, last, mergeMap, tap } from 'rxjs/operators';
1111
import { Rule, SchematicContext, Source } from '../engine/interface';
1212
import { Tree, TreeSymbol } from '../tree/interface';
1313

@@ -51,55 +51,53 @@ export class InvalidSourceResultException extends BaseException {
5151

5252

5353
export function callSource(source: Source, context: SchematicContext): Observable<Tree> {
54-
const result = source(context) as object;
54+
const result = source(context);
5555

56-
if (result === undefined) {
57-
return throwError(new InvalidSourceResultException(result));
58-
} else if (TreeSymbol in result) {
59-
return observableOf(result as Tree);
60-
} else if (isObservable(result)) {
56+
if (isObservable(result)) {
6157
// Only return the last Tree, and make sure it's a Tree.
62-
return (result as Observable<Tree>).pipe(
58+
return result.pipe(
59+
defaultIfEmpty(),
6360
last(),
6461
tap(inner => {
65-
if (!(TreeSymbol in inner)) {
62+
if (!inner || !(TreeSymbol in inner)) {
6663
throw new InvalidSourceResultException(inner);
6764
}
6865
}),
6966
);
67+
} else if (result && TreeSymbol in result) {
68+
return observableOf(result);
7069
} else {
7170
return throwError(new InvalidSourceResultException(result));
7271
}
7372
}
7473

7574

76-
export function callRule(rule: Rule,
77-
input: Observable<Tree>,
78-
context: SchematicContext): Observable<Tree> {
75+
export function callRule(
76+
rule: Rule,
77+
input: Observable<Tree>,
78+
context: SchematicContext,
79+
): Observable<Tree> {
7980
return input.pipe(mergeMap(inputTree => {
80-
const result = rule(inputTree, context) as object;
81+
const result = rule(inputTree, context);
8182

8283
if (result === undefined) {
8384
return observableOf(inputTree);
84-
} else if (TreeSymbol in result) {
85-
return observableOf(result as Tree);
8685
} else if (typeof result == 'function') {
8786
// This is considered a Rule, chain the rule and return its output.
8887
return callRule(result, input, context);
8988
} else if (isObservable(result)) {
90-
const obs = result as Observable<Tree>;
91-
9289
// Only return the last Tree, and make sure it's a Tree.
93-
return obs.pipe(
90+
return result.pipe(
91+
defaultIfEmpty(),
9492
last(),
9593
tap(inner => {
96-
if (!(TreeSymbol in inner)) {
94+
if (!inner || !(TreeSymbol in inner)) {
9795
throw new InvalidRuleResultException(inner);
9896
}
9997
}),
10098
);
101-
} else if (result === undefined) {
102-
return observableOf(inputTree);
99+
} else if (TreeSymbol in result) {
100+
return observableOf(result);
103101
} else {
104102
return throwError(new InvalidRuleResultException(result));
105103
}

0 commit comments

Comments
 (0)