fix(ngcc): use the correct identifiers when updating typings files (#34254)

Previously the identifiers used in the typings files were the same as
those used in the source files.

When the typings files and the source files do not match exactly, e.g.
when one of them is flattened, while the other is a deep tree, it is
possible for identifiers to be renamed.

This commit ensures that the correct identifier is used in typings files
when the typings file does not export the same name as the source file.

Fixes https://github.com/angular/ngcc-validation/pull/608

PR Close #34254
This commit is contained in:
Pete Bacon Darwin
2019-12-18 14:03:05 +00:00
committed by Kara Erickson
parent f22a6eb00e
commit 31be29a9f3
14 changed files with 94 additions and 39 deletions

View File

@ -15,7 +15,7 @@ import * as o from '../output/output_ast';
import {Identifiers as R3} from '../render3/r3_identifiers';
import {OutputContext} from '../util';
import {typeWithParameters} from './util';
import {R3Reference, typeWithParameters} from './util';
import {unsupported} from './view/util';
@ -32,7 +32,7 @@ export interface R3ConstructorFactoryMetadata {
/**
* An expression representing the interface type being constructed.
*/
type: o.Expression;
type: R3Reference;
/**
* An expression representing the constructor type, intended for use within a class definition
@ -270,7 +270,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn {
`${meta.name}_Factory`),
statements,
type: o.expressionType(
o.importExpr(R3.FactoryDef, [typeWithParameters(meta.type, meta.typeArgumentCount)]))
o.importExpr(R3.FactoryDef, [typeWithParameters(meta.type.type, meta.typeArgumentCount)]))
};
}

View File

@ -29,7 +29,7 @@ export interface R3NgModuleMetadata {
/**
* An expression representing the module type being compiled.
*/
type: o.Expression;
type: R3Reference;
/**
* An expression representing the module type being compiled, intended for use within a class
@ -160,7 +160,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
const expression = o.importExpr(R3.defineNgModule).callFn([mapToMapExpression(definitionMap)]);
const type = new o.ExpressionType(o.importExpr(R3.NgModuleDefWithMeta, [
new o.ExpressionType(moduleType), tupleTypeOf(declarations), tupleTypeOf(imports),
new o.ExpressionType(moduleType.type), tupleTypeOf(declarations), tupleTypeOf(imports),
tupleTypeOf(exports)
]));
@ -228,7 +228,7 @@ export interface R3InjectorDef {
export interface R3InjectorMetadata {
name: string;
type: o.Expression;
type: R3Reference;
internalType: o.Expression;
deps: R3DependencyMetadata[]|null;
providers: o.Expression|null;
@ -259,7 +259,7 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef {
const expression = o.importExpr(R3.defineInjector).callFn([mapToMapExpression(definitionMap)]);
const type =
new o.ExpressionType(o.importExpr(R3.InjectorDef, [new o.ExpressionType(meta.type)]));
new o.ExpressionType(o.importExpr(R3.InjectorDef, [new o.ExpressionType(meta.type.type)]));
return {expression, type, statements: result.statements};
}

View File

@ -14,7 +14,7 @@ import {OutputContext, error} from '../util';
import {R3DependencyMetadata, R3FactoryTarget, compileFactoryFunction, dependenciesFromGlobalMetadata} from './r3_factory';
import {Identifiers as R3} from './r3_identifiers';
import {typeWithParameters} from './util';
import {R3Reference, typeWithParameters, wrapReference} from './util';
export interface R3PipeMetadata {
/**
@ -25,7 +25,7 @@ export interface R3PipeMetadata {
/**
* An expression representing a reference to the pipe itself.
*/
type: o.Expression;
type: R3Reference;
/**
* An expression representing the pipe being compiled, intended for use within a class definition
@ -64,14 +64,14 @@ export function compilePipeFromMetadata(metadata: R3PipeMetadata) {
definitionMapValues.push({key: 'name', value: o.literal(metadata.pipeName), quoted: false});
// e.g. `type: MyPipe`
definitionMapValues.push({key: 'type', value: metadata.type, quoted: false});
definitionMapValues.push({key: 'type', value: metadata.type.value, quoted: false});
// e.g. `pure: true`
definitionMapValues.push({key: 'pure', value: o.literal(metadata.pure), quoted: false});
const expression = o.importExpr(R3.definePipe).callFn([o.literalMap(definitionMapValues)]);
const type = new o.ExpressionType(o.importExpr(R3.PipeDefWithMeta, [
typeWithParameters(metadata.type, metadata.typeArgumentCount),
typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
new o.ExpressionType(new o.LiteralExpr(metadata.pipeName)),
]));
@ -92,7 +92,7 @@ export function compilePipeFromRender2(
const type = outputCtx.importExpr(pipe.type.reference);
const metadata: R3PipeMetadata = {
name,
type,
type: wrapReference(type),
internalType: type,
pipeName: pipe.name,
typeArgumentCount: 0,

View File

@ -98,3 +98,8 @@ export function jitOnlyGuardedExpression(expr: o.Expression): o.Expression {
/* sourceSpan */ undefined, true);
return new o.BinaryOperatorExpr(o.BinaryOperator.And, jitFlagUndefinedOrTrue, expr);
}
export function wrapReference(value: any): R3Reference {
const wrapped = new o.WrappedNodeExpr(value);
return {value: wrapped, type: wrapped};
}

View File

@ -12,6 +12,7 @@ import * as o from '../../output/output_ast';
import {ParseSourceSpan} from '../../parse_util';
import * as t from '../r3_ast';
import {R3DependencyMetadata} from '../r3_factory';
import {R3Reference} from '../util';
/**
@ -26,7 +27,7 @@ export interface R3DirectiveMetadata {
/**
* An expression representing a reference to the directive itself.
*/
type: o.Expression;
type: R3Reference;
/**
* An expression representing a reference to the directive being compiled, intended for use within

View File

@ -481,7 +481,7 @@ function createTypeForDef(meta: R3DirectiveMetadata, typeBase: o.ExternalReferen
const selectorForType = meta.selector !== null ? meta.selector.replace(/\n/g, '') : null;
return o.expressionType(o.importExpr(typeBase, [
typeWithParameters(meta.type, meta.typeArgumentCount),
typeWithParameters(meta.type.type, meta.typeArgumentCount),
selectorForType !== null ? stringAsType(selectorForType) : o.NONE_TYPE,
meta.exportAs !== null ? stringArrayAsType(meta.exportAs) : o.NONE_TYPE,
stringMapAsType(meta.inputs),