feat(compiler): generate type parameters for generic type references (#14104)
This commit is contained in:

committed by
Miško Hevery

parent
1079b9381c
commit
69e14b500b
@ -77,7 +77,8 @@ export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCom
|
||||
const importResolver = {
|
||||
getImportAs: (symbol: StaticSymbol) => symbolResolver.getImportAs(symbol),
|
||||
fileNameToModuleName: (fileName: string, containingFilePath: string) =>
|
||||
compilerHost.fileNameToModuleName(fileName, containingFilePath)
|
||||
compilerHost.fileNameToModuleName(fileName, containingFilePath),
|
||||
getTypeArity: (symbol: StaticSymbol) => symbolResolver.getTypeArity(symbol)
|
||||
};
|
||||
const compiler = new AotCompiler(
|
||||
compilerHost, resolver, tmplParser, new StyleCompiler(urlResolver),
|
||||
|
@ -10,6 +10,7 @@ import {SummaryResolver} from '../summary_resolver';
|
||||
import {ValueTransformer, visitValue} from '../util';
|
||||
|
||||
import {StaticSymbol, StaticSymbolCache} from './static_symbol';
|
||||
import {isNgFactoryFile} from './util';
|
||||
|
||||
export class ResolvedStaticSymbol {
|
||||
constructor(public symbol: StaticSymbol, public metadata: any) {}
|
||||
@ -83,6 +84,15 @@ export class StaticSymbolResolver {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* getImportAs produces a symbol that can be used to import the given symbol.
|
||||
* The import might be different than the symbol if the symbol is exported from
|
||||
* a library with a summary; in which case we want to import the symbol from the
|
||||
* ngfactory re-export instead of directly to avoid introducing a direct dependency
|
||||
* on an otherwise indirect dependency.
|
||||
*
|
||||
* @param staticSymbol the symbol for which to generate a import symbol
|
||||
*/
|
||||
getImportAs(staticSymbol: StaticSymbol): StaticSymbol {
|
||||
if (staticSymbol.members.length) {
|
||||
const baseSymbol = this.getStaticSymbol(staticSymbol.filePath, staticSymbol.name);
|
||||
@ -98,6 +108,25 @@ export class StaticSymbolResolver {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTypeArity returns the number of generic type parameters the given symbol
|
||||
* has. If the symbol is not a type the result is null.
|
||||
*/
|
||||
getTypeArity(staticSymbol: StaticSymbol): number /*|null*/ {
|
||||
// If the file is a factory file, don't resolve the symbol as doing so would
|
||||
// cause the metadata for an factory file to be loaded which doesn't exist.
|
||||
// All references to generated classes must include the correct arity whenever
|
||||
// generating code.
|
||||
if (isNgFactoryFile(staticSymbol.filePath)) {
|
||||
return null;
|
||||
}
|
||||
let resolvedSymbol = this.resolveSymbol(staticSymbol);
|
||||
while (resolvedSymbol && resolvedSymbol.metadata instanceof StaticSymbol) {
|
||||
resolvedSymbol = this.resolveSymbol(resolvedSymbol.metadata);
|
||||
}
|
||||
return (resolvedSymbol && resolvedSymbol.metadata && resolvedSymbol.metadata.arity) || null;
|
||||
}
|
||||
|
||||
private _resolveSymbolMembers(staticSymbol: StaticSymbol): ResolvedStaticSymbol {
|
||||
const members = staticSymbol.members;
|
||||
const baseResolvedSymbol =
|
||||
@ -134,6 +163,7 @@ export class StaticSymbolResolver {
|
||||
*
|
||||
* @param declarationFile the absolute path of the file where the symbol is declared
|
||||
* @param name the name of the type.
|
||||
* @param members a symbol for a static member of the named type
|
||||
*/
|
||||
getStaticSymbol(declarationFile: string, name: string, members?: string[]): StaticSymbol {
|
||||
return this.staticSymbolCache.get(declarationFile, name, members);
|
||||
|
@ -94,10 +94,10 @@ class Serializer extends ValueTransformer {
|
||||
addOrMergeSummary(summary: Summary<StaticSymbol>) {
|
||||
let symbolMeta = summary.metadata;
|
||||
if (symbolMeta && symbolMeta.__symbolic === 'class') {
|
||||
// For classes, we only keep their statics, but not the metadata
|
||||
// For classes, we only keep their statics and arity, but not the metadata
|
||||
// of the class itself as that has been captured already via other summaries
|
||||
// (e.g. DirectiveSummary, ...).
|
||||
symbolMeta = {__symbolic: 'class', statics: symbolMeta.statics};
|
||||
symbolMeta = {__symbolic: 'class', statics: symbolMeta.statics, arity: symbolMeta.arity};
|
||||
}
|
||||
|
||||
let processedSummary = this.processedSummaryBySymbol.get(summary.symbol);
|
||||
@ -106,11 +106,11 @@ class Serializer extends ValueTransformer {
|
||||
this.processedSummaries.push(processedSummary);
|
||||
this.processedSummaryBySymbol.set(summary.symbol, processedSummary);
|
||||
}
|
||||
// Note: == by purpose to compare with undefined!
|
||||
// Note: == on purpose to compare with undefined!
|
||||
if (processedSummary.metadata == null && symbolMeta != null) {
|
||||
processedSummary.metadata = this.processValue(symbolMeta);
|
||||
}
|
||||
// Note: == by purpose to compare with undefined!
|
||||
// Note: == on purpose to compare with undefined!
|
||||
if (processedSummary.type == null && summary.type != null) {
|
||||
processedSummary.type = this.processValue(summary.type);
|
||||
}
|
||||
@ -147,7 +147,7 @@ class Serializer extends ValueTransformer {
|
||||
if (value instanceof StaticSymbol) {
|
||||
const baseSymbol = this.symbolResolver.getStaticSymbol(value.filePath, value.name);
|
||||
let index = this.indexBySymbol.get(baseSymbol);
|
||||
// Note: == by purpose to compare with undefined!
|
||||
// Note: == on purpose to compare with undefined!
|
||||
if (index == null) {
|
||||
index = this.indexBySymbol.size;
|
||||
this.indexBySymbol.set(baseSymbol, index);
|
||||
|
@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
const STRIP_SRC_FILE_SUFFIXES = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
|
||||
const NG_FACTORY = /\.ngfactory\./;
|
||||
|
||||
export function ngfactoryFilePath(filePath: string): string {
|
||||
const urlWithSuffix = splitTypescriptSuffix(filePath);
|
||||
@ -14,7 +15,11 @@ export function ngfactoryFilePath(filePath: string): string {
|
||||
}
|
||||
|
||||
export function stripNgFactory(filePath: string): string {
|
||||
return filePath.replace(/\.ngfactory\./, '.');
|
||||
return filePath.replace(NG_FACTORY, '.');
|
||||
}
|
||||
|
||||
export function isNgFactoryFile(filePath: string): boolean {
|
||||
return NG_FACTORY.test(filePath);
|
||||
}
|
||||
|
||||
export function splitTypescriptSuffix(path: string): string[] {
|
||||
|
Reference in New Issue
Block a user