2019-07-18 16:05:32 -04:00
|
|
|
/**
|
|
|
|
* @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';
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
import {AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
2019-07-18 16:05:32 -04:00
|
|
|
import {MetadataReader} from '../../../src/ngtsc/metadata';
|
|
|
|
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
|
|
|
import {ClassDeclaration, Decorator} from '../../../src/ngtsc/reflection';
|
2020-01-06 17:12:19 -05:00
|
|
|
import {HandlerFlags, TraitState} from '../../../src/ngtsc/transform';
|
2019-07-18 16:05:32 -04:00
|
|
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
|
|
|
import {MigrationHost} from '../migrations/migration';
|
2019-10-20 14:40:48 -04:00
|
|
|
|
2020-01-06 17:12:19 -05:00
|
|
|
import {NgccTraitCompiler} from './ngcc_trait_compiler';
|
|
|
|
import {isWithinPackage} from './util';
|
2019-07-18 16:05:32 -04:00
|
|
|
|
|
|
|
/**
|
2020-01-06 17:12:19 -05:00
|
|
|
* The standard implementation of `MigrationHost`, which is created by the `DecorationAnalyzer`.
|
2019-07-18 16:05:32 -04:00
|
|
|
*/
|
|
|
|
export class DefaultMigrationHost implements MigrationHost {
|
|
|
|
constructor(
|
|
|
|
readonly reflectionHost: NgccReflectionHost, readonly metadata: MetadataReader,
|
2020-01-06 17:12:19 -05:00
|
|
|
readonly evaluator: PartialEvaluator, private compiler: NgccTraitCompiler,
|
|
|
|
private entryPointPath: AbsoluteFsPath) {}
|
2019-07-18 16:05:32 -04:00
|
|
|
|
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
2019-10-23 15:00:49 -04:00
|
|
|
injectSyntheticDecorator(clazz: ClassDeclaration, decorator: Decorator, flags?: HandlerFlags):
|
|
|
|
void {
|
2020-01-06 17:12:19 -05:00
|
|
|
const migratedTraits = this.compiler.injectSyntheticDecorator(clazz, decorator, flags);
|
2019-07-18 16:05:32 -04:00
|
|
|
|
2020-01-06 17:12:19 -05:00
|
|
|
for (const trait of migratedTraits) {
|
|
|
|
if (trait.state === TraitState.ERRORED) {
|
|
|
|
trait.diagnostics =
|
|
|
|
trait.diagnostics.map(diag => createMigrationDiagnostic(diag, clazz, decorator));
|
2019-11-23 12:54:07 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-18 16:05:32 -04:00
|
|
|
}
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
getAllDecorators(clazz: ClassDeclaration): Decorator[]|null {
|
2020-01-06 17:12:19 -05:00
|
|
|
return this.compiler.getAllDecorators(clazz);
|
2019-10-20 14:40:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isInScope(clazz: ClassDeclaration): boolean {
|
|
|
|
return isWithinPackage(this.entryPointPath, clazz.getSourceFile());
|
|
|
|
}
|
2019-07-18 16:05:32 -04:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:54:07 -05:00
|
|
|
/**
|
|
|
|
* Creates a diagnostic from another one, containing additional information about the synthetic
|
|
|
|
* decorator.
|
|
|
|
*/
|
|
|
|
function createMigrationDiagnostic(
|
|
|
|
diagnostic: ts.Diagnostic, source: ts.Node, decorator: Decorator): ts.Diagnostic {
|
|
|
|
const clone = {...diagnostic};
|
|
|
|
|
|
|
|
const chain: ts.DiagnosticMessageChain[] = [{
|
|
|
|
messageText: `Occurs for @${decorator.name} decorator inserted by an automatic migration`,
|
|
|
|
category: ts.DiagnosticCategory.Message,
|
|
|
|
code: 0,
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (decorator.args !== null) {
|
|
|
|
const args = decorator.args.map(arg => arg.getText()).join(', ');
|
|
|
|
chain.push({
|
|
|
|
messageText: `@${decorator.name}(${args})`,
|
|
|
|
category: ts.DiagnosticCategory.Message,
|
|
|
|
code: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof clone.messageText === 'string') {
|
|
|
|
clone.messageText = {
|
|
|
|
messageText: clone.messageText,
|
|
|
|
category: diagnostic.category,
|
|
|
|
code: diagnostic.code,
|
|
|
|
next: chain,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
if (clone.messageText.next === undefined) {
|
|
|
|
clone.messageText.next = chain;
|
|
|
|
} else {
|
|
|
|
clone.messageText.next.push(...chain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clone;
|
|
|
|
}
|