Skip to content

Commit 2f55209

Browse files
committedJan 14, 2025·
fix(@angular-devkit/schematics): update Rule type to support returning a Promise of Tree
The `Rule` type has been updated to align with its intended functionality, allowing it to return a `Promise<Tree>`. Previously, this behavior was supported but not properly typed. Closes #22783 (cherry picked from commit 4f80308)
1 parent 8e110bb commit 2f55209

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed
 

‎goldens/public-api/angular_devkit/schematics/index.api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ interface RequiredWorkflowExecutionContext {
722722
}
723723

724724
// @public (undocumented)
725-
export type Rule = (tree: Tree_2, context: SchematicContext) => Tree_2 | Observable<Tree_2> | Rule | Promise<void | Rule> | void;
725+
export type Rule = (tree: Tree_2, context: SchematicContext) => Tree_2 | Observable<Tree_2> | Rule | Promise<void | Tree_2 | Rule> | void;
726726

727727
// @public
728728
export type RuleFactory<T extends object> = (options: T) => Rule;

‎packages/angular_devkit/schematics/src/engine/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,4 @@ export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
236236
export type Rule = (
237237
tree: Tree,
238238
context: SchematicContext,
239-
) => Tree | Observable<Tree> | Rule | Promise<void | Rule> | void;
239+
) => Tree | Observable<Tree> | Rule | Promise<void | Tree | Rule> | void;

‎packages/angular_devkit/schematics/src/rules/base_spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ describe('chain', () => {
4747
.then(done, done.fail);
4848
});
4949

50+
it('works with async rules', (done) => {
51+
const rulesCalled: Tree[] = [];
52+
53+
const tree0 = empty();
54+
const tree1 = empty();
55+
const tree2 = empty();
56+
const tree3 = empty();
57+
58+
const rule0: Rule = async (tree: Tree) => ((rulesCalled[0] = tree), tree1);
59+
const rule1: Rule = async (tree: Tree) => ((rulesCalled[1] = tree), tree2);
60+
const rule2: Rule = async (tree: Tree) => ((rulesCalled[2] = tree), tree3);
61+
62+
lastValueFrom(callRule(chain([rule0, rule1, rule2]), tree0, context))
63+
.then((result) => {
64+
expect(result).not.toBe(tree0);
65+
expect(rulesCalled[0]).toBe(tree0);
66+
expect(rulesCalled[1]).toBe(tree1);
67+
expect(rulesCalled[2]).toBe(tree2);
68+
expect(result).toBe(tree3);
69+
})
70+
.then(done, done.fail);
71+
});
72+
5073
it('works with a sync generator of rules', async () => {
5174
const rulesCalled: Tree[] = [];
5275

0 commit comments

Comments
 (0)
Please sign in to comment.