2019-07-18 21:05:32 +01:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-07-18 21:05:32 +01:00
|
|
|
*
|
|
|
|
|
* 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 20:40:48 +02:00
|
|
|
|
2020-06-23 22:12:32 +02:00
|
|
|
import {absoluteFromSourceFile, AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
2019-07-18 21:05:32 +01:00
|
|
|
import {MetadataReader} from '../../../src/ngtsc/metadata';
|
|
|
|
|
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
|
|
|
|
import {ClassDeclaration, Decorator} from '../../../src/ngtsc/reflection';
|
2020-01-06 23:12:19 +01:00
|
|
|
import {HandlerFlags, TraitState} from '../../../src/ngtsc/transform';
|
2019-07-18 21:05:32 +01:00
|
|
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
|
|
|
|
import {MigrationHost} from '../migrations/migration';
|
2019-10-20 20:40:48 +02:00
|
|
|
|
2020-01-06 23:12:19 +01:00
|
|
|
import {NgccTraitCompiler} from './ngcc_trait_compiler';
|
|
|
|
|
import {isWithinPackage} from './util';
|
2019-07-18 21:05:32 +01:00
|
|
|
|
|
|
|
|
/**
|
2020-01-06 23:12:19 +01:00
|
|
|
* The standard implementation of `MigrationHost`, which is created by the `DecorationAnalyzer`.
|
2019-07-18 21:05:32 +01:00
|
|
|
*/
|
|
|
|
|
export class DefaultMigrationHost implements MigrationHost {
|
|
|
|
|
constructor(
|
|
|
|
|
readonly reflectionHost: NgccReflectionHost, readonly metadata: MetadataReader,
|
2020-01-06 23:12:19 +01:00
|
|
|
readonly evaluator: PartialEvaluator, private compiler: NgccTraitCompiler,
|
|
|
|
|
private entryPointPath: AbsoluteFsPath) {}
|
2019-07-18 21:05:32 +01: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 12:00:49 -07:00
|
|
|
injectSyntheticDecorator(clazz: ClassDeclaration, decorator: Decorator, flags?: HandlerFlags):
|
|
|
|
|
void {
|
2020-01-06 23:12:19 +01:00
|
|
|
const migratedTraits = this.compiler.injectSyntheticDecorator(clazz, decorator, flags);
|
2019-07-18 21:05:32 +01:00
|
|
|
|
2020-01-06 23:12:19 +01:00
|
|
|
for (const trait of migratedTraits) {
|
fix(compiler-cli): remove the concept of an errored trait (#39923)
Previously, if a trait's analysis step resulted in diagnostics, the trait
would be considered "errored" and no further operations, including register,
would be performed. Effectively, this meant that the compiler would pretend
the class in question was actually undecorated.
However, this behavior is problematic for several reasons:
1. It leads to inaccurate diagnostics being reported downstream.
For example, if a component is put into the error state, for example due to
a template error, the NgModule which declares the component would produce a
diagnostic claiming that the declaration is neither a directive nor a pipe.
This happened because the compiler wouldn't register() the component trait,
so the component would not be recorded as actually being a directive.
2. It can cause incorrect behavior on incremental builds.
This bug is more complex, but the general issue is that if the compiler
fails to associate a component and its module, then incremental builds will
not correctly re-analyze the module when the component's template changes.
Failing to register the component as such is one link in the larger chain of
issues that result in these kinds of issues.
3. It lumps together diagnostics produced during analysis and resolve steps.
This is not causing issues currently as the dependency graph ensures the
right classes are re-analyzed when needed, instead of showing stale
diagnostics. However, the dependency graph was not intended to serve this
role, and could potentially be optimized in ways that would break this
functionality.
This commit removes the concept of an "errored" trait entirely from the
trait system. Instead, analyzed and resolved traits have corresponding (and
separate) diagnostics, in addition to potentially `null` analysis results.
Analysis (but not resolution) diagnostics are carried forward during
incremental build operations. Compilation (emit) is only performed when
a trait reaches the resolved state with no diagnostics.
This change is functionally different than before as the `register` step is
now performed even in the presence of analysis errors, as long as analysis
results are also produced. This fixes problem 1 above, and is part of the
larger solution to problem 2.
PR Close #39923
2020-11-25 15:01:24 -08:00
|
|
|
if ((trait.state === TraitState.Analyzed || trait.state === TraitState.Resolved) &&
|
|
|
|
|
trait.analysisDiagnostics !== null) {
|
|
|
|
|
trait.analysisDiagnostics = trait.analysisDiagnostics.map(
|
|
|
|
|
diag => createMigrationDiagnostic(diag, clazz, decorator));
|
|
|
|
|
}
|
|
|
|
|
if (trait.state === TraitState.Resolved && trait.resolveDiagnostics !== null) {
|
|
|
|
|
trait.resolveDiagnostics =
|
|
|
|
|
trait.resolveDiagnostics.map(diag => createMigrationDiagnostic(diag, clazz, decorator));
|
2019-11-23 18:54:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-18 21:05:32 +01:00
|
|
|
}
|
2019-10-20 20:40:48 +02:00
|
|
|
|
|
|
|
|
getAllDecorators(clazz: ClassDeclaration): Decorator[]|null {
|
2020-01-06 23:12:19 +01:00
|
|
|
return this.compiler.getAllDecorators(clazz);
|
2019-10-20 20:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isInScope(clazz: ClassDeclaration): boolean {
|
2020-06-23 22:12:32 +02:00
|
|
|
return isWithinPackage(this.entryPointPath, absoluteFromSourceFile(clazz.getSourceFile()));
|
2019-10-20 20:40:48 +02:00
|
|
|
}
|
2019-07-18 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-23 18:54:07 +01: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;
|
|
|
|
|
}
|