Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngcc): add a migration for undecorated child classes (#33362)
In Angular View Engine, there are two kinds of decorator inheritance: 1) both the parent and child classes have decorators This case is supported by InheritDefinitionFeature, which merges some fields of the definitions (such as the inputs or queries). 2) only the parent class has a decorator If the child class is missing a decorator, the compiler effectively behaves as if the parent class' decorator is applied to the child class as well. This is the "undecorated child" scenario, and this commit adds a migration to ngcc to support this pattern in Ivy. This migration has 2 phases. First, the NgModules of the application are scanned for classes in 'declarations' which are missing decorators, but whose base classes do have decorators. These classes are the undecorated children. This scan is performed recursively, so even if a declared class has a base class that itself inherits a decorator, this case is handled. Next, a synthetic decorator (either @component or @directive) is created on the child class. This decorator copies some critical information such as 'selector' and 'exportAs', as well as supports any decorated fields (@input, etc). A flag is passed to the decorator compiler which causes a special feature `CopyDefinitionFeature` to be included on the compiled definition. This feature copies at runtime the remaining aspects of the parent definition which `InheritDefinitionFeature` does not handle, completing the "full" inheritance of the child class' decorator from its parent class. PR Close #33362
- Loading branch information
1 parent
818c514
commit b381497
Showing
16 changed files
with
451 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/compiler-cli/ngcc/src/migrations/undecorated_child_migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import {readBaseClass} from '../../../src/ngtsc/annotations/src/util'; | ||
import {Reference} from '../../../src/ngtsc/imports'; | ||
import {ClassDeclaration} from '../../../src/ngtsc/reflection'; | ||
import {HandlerFlags} from '../../../src/ngtsc/transform'; | ||
|
||
import {Migration, MigrationHost} from './migration'; | ||
import {createComponentDecorator, createDirectiveDecorator, hasDirectiveDecorator, hasPipeDecorator} from './utils'; | ||
|
||
export class UndecoratedChildMigration implements Migration { | ||
apply(clazz: ClassDeclaration, host: MigrationHost): ts.Diagnostic|null { | ||
// This migration looks at NgModules and considers the directives (and pipes) it declares. | ||
// It verifies that these classes have decorators. | ||
const moduleMeta = host.metadata.getNgModuleMetadata(new Reference(clazz)); | ||
if (moduleMeta === null) { | ||
// Not an NgModule; don't care. | ||
return null; | ||
} | ||
|
||
// Examine each of the declarations to see if it needs to be migrated. | ||
for (const decl of moduleMeta.declarations) { | ||
this.maybeMigrate(decl, host); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
maybeMigrate(ref: Reference<ClassDeclaration>, host: MigrationHost): void { | ||
if (hasDirectiveDecorator(host, ref.node) || hasPipeDecorator(host, ref.node)) { | ||
// Stop if one of the classes in the chain is actually decorated with @Directive, @Component, | ||
// or @Pipe. | ||
return; | ||
} | ||
|
||
const baseRef = readBaseClass(ref.node, host.reflectionHost, host.evaluator); | ||
if (baseRef === null) { | ||
// Stop: can't migrate a class with no parent. | ||
return; | ||
} else if (baseRef === 'dynamic') { | ||
// Stop: can't migrate a class with an indeterminate parent. | ||
return; | ||
} | ||
|
||
// Apply the migration recursively, to handle inheritance chains. | ||
this.maybeMigrate(baseRef, host); | ||
|
||
// After the above call, `host.metadata` should have metadata for the base class, if indeed this | ||
// is a directive inheritance chain. | ||
const baseMeta = host.metadata.getDirectiveMetadata(baseRef); | ||
if (baseMeta === null) { | ||
// Stop: this isn't a directive inheritance chain after all. | ||
return; | ||
} | ||
|
||
// Otherwise, decorate the class with @Component() or @Directive(), as appropriate. | ||
if (baseMeta.isComponent) { | ||
host.injectSyntheticDecorator( | ||
ref.node, createComponentDecorator(ref.node, baseMeta), HandlerFlags.FULL_INHERITANCE); | ||
} else { | ||
host.injectSyntheticDecorator( | ||
ref.node, createDirectiveDecorator(ref.node, baseMeta), HandlerFlags.FULL_INHERITANCE); | ||
} | ||
|
||
// Success! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.