From 2f55209dd24602bdf8c27ef083f96b5f55166971 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 9 Jan 2025 15:05:50 +0000 Subject: [PATCH] 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`. Previously, this behavior was supported but not properly typed. Closes #22783 (cherry picked from commit 4f803083fc8940aed2aa30f46970273175ad6b37) --- .../angular_devkit/schematics/index.api.md | 2 +- .../schematics/src/engine/interface.ts | 2 +- .../schematics/src/rules/base_spec.ts | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/goldens/public-api/angular_devkit/schematics/index.api.md b/goldens/public-api/angular_devkit/schematics/index.api.md index de17a0462162..ff74a39c16eb 100644 --- a/goldens/public-api/angular_devkit/schematics/index.api.md +++ b/goldens/public-api/angular_devkit/schematics/index.api.md @@ -722,7 +722,7 @@ interface RequiredWorkflowExecutionContext { } // @public (undocumented) -export type Rule = (tree: Tree_2, context: SchematicContext) => Tree_2 | Observable | Rule | Promise | void; +export type Rule = (tree: Tree_2, context: SchematicContext) => Tree_2 | Observable | Rule | Promise | void; // @public export type RuleFactory = (options: T) => Rule; diff --git a/packages/angular_devkit/schematics/src/engine/interface.ts b/packages/angular_devkit/schematics/src/engine/interface.ts index bb4e3ccc33ac..41d63f631121 100644 --- a/packages/angular_devkit/schematics/src/engine/interface.ts +++ b/packages/angular_devkit/schematics/src/engine/interface.ts @@ -236,4 +236,4 @@ export type Source = (context: SchematicContext) => Tree | Observable; export type Rule = ( tree: Tree, context: SchematicContext, -) => Tree | Observable | Rule | Promise | void; +) => Tree | Observable | Rule | Promise | void; diff --git a/packages/angular_devkit/schematics/src/rules/base_spec.ts b/packages/angular_devkit/schematics/src/rules/base_spec.ts index ef28788d0959..9414174b0113 100644 --- a/packages/angular_devkit/schematics/src/rules/base_spec.ts +++ b/packages/angular_devkit/schematics/src/rules/base_spec.ts @@ -47,6 +47,29 @@ describe('chain', () => { .then(done, done.fail); }); + it('works with async rules', (done) => { + const rulesCalled: Tree[] = []; + + const tree0 = empty(); + const tree1 = empty(); + const tree2 = empty(); + const tree3 = empty(); + + const rule0: Rule = async (tree: Tree) => ((rulesCalled[0] = tree), tree1); + const rule1: Rule = async (tree: Tree) => ((rulesCalled[1] = tree), tree2); + const rule2: Rule = async (tree: Tree) => ((rulesCalled[2] = tree), tree3); + + lastValueFrom(callRule(chain([rule0, rule1, rule2]), tree0, context)) + .then((result) => { + expect(result).not.toBe(tree0); + expect(rulesCalled[0]).toBe(tree0); + expect(rulesCalled[1]).toBe(tree1); + expect(rulesCalled[2]).toBe(tree2); + expect(result).toBe(tree3); + }) + .then(done, done.fail); + }); + it('works with a sync generator of rules', async () => { const rulesCalled: Tree[] = [];