fix(ivy): use 'typeof' and 'never' for type metadata (#24862)
Previously ngtsc would use a tuple of class types for listing metadata in .d.ts files. For example, an @NgModule's declarations might be represented with the type: [NgIf, NgForOf, NgClass] If the module had no declarations, an empty tuple [] would be produced. This has two problems. 1. If the class type has generic type parameters, TypeScript will complain that they're not provided. 2. The empty tuple type is not actually legal. This commit addresses both problems. 1. Class types are now represented using the `typeof` operator, so the above declarations would be represented as: [typeof NgIf, typeof NgForOf, typeof NgClass]. Since typeof operates on a value, it doesn't require generic type arguments. 2. Instead of an empty tuple, `never` is used to indicate no metadata. PR Close #24862
This commit is contained in:

committed by
Victor Berchet

parent
d3594fc1c5
commit
ed1db40322
@ -315,6 +315,10 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex
|
||||
visitWrappedNodeExpr(ast: o.WrappedNodeExpr<any>, ctx: EmitterVisitorContext): any {
|
||||
throw new Error('Abstract emitter cannot visit WrappedNodeExpr.');
|
||||
}
|
||||
visitTypeofExpr(expr: o.TypeofExpr, ctx: EmitterVisitorContext): any {
|
||||
ctx.print(expr, 'typeof ');
|
||||
expr.expr.visitExpression(this, ctx);
|
||||
}
|
||||
visitReadVarExpr(ast: o.ReadVarExpr, ctx: EmitterVisitorContext): any {
|
||||
let varName = ast.name !;
|
||||
if (ast.builtin != null) {
|
||||
|
@ -33,7 +33,8 @@ export enum BuiltinTypeName {
|
||||
Int,
|
||||
Number,
|
||||
Function,
|
||||
Inferred
|
||||
Inferred,
|
||||
None,
|
||||
}
|
||||
|
||||
export class BuiltinType extends Type {
|
||||
@ -77,6 +78,7 @@ export const INT_TYPE = new BuiltinType(BuiltinTypeName.Int);
|
||||
export const NUMBER_TYPE = new BuiltinType(BuiltinTypeName.Number);
|
||||
export const STRING_TYPE = new BuiltinType(BuiltinTypeName.String);
|
||||
export const FUNCTION_TYPE = new BuiltinType(BuiltinTypeName.Function);
|
||||
export const NONE_TYPE = new BuiltinType(BuiltinTypeName.None);
|
||||
|
||||
export interface TypeVisitor {
|
||||
visitBuiltinType(type: BuiltinType, context: any): any;
|
||||
@ -279,6 +281,22 @@ export class ReadVarExpr extends Expression {
|
||||
}
|
||||
}
|
||||
|
||||
export class TypeofExpr extends Expression {
|
||||
constructor(public expr: Expression, type?: Type|null, sourceSpan?: ParseSourceSpan|null) {
|
||||
super(type, sourceSpan);
|
||||
}
|
||||
|
||||
visitExpression(visitor: ExpressionVisitor, context: any) {
|
||||
return visitor.visitTypeofExpr(this, context);
|
||||
}
|
||||
|
||||
isEquivalent(e: Expression): boolean {
|
||||
return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr);
|
||||
}
|
||||
|
||||
isConstant(): boolean { return this.expr.isConstant(); }
|
||||
}
|
||||
|
||||
export class WrappedNodeExpr<T> extends Expression {
|
||||
constructor(public node: T, type?: Type|null, sourceSpan?: ParseSourceSpan|null) {
|
||||
super(type, sourceSpan);
|
||||
@ -738,6 +756,7 @@ export interface ExpressionVisitor {
|
||||
visitLiteralMapExpr(ast: LiteralMapExpr, context: any): any;
|
||||
visitCommaExpr(ast: CommaExpr, context: any): any;
|
||||
visitWrappedNodeExpr(ast: WrappedNodeExpr<any>, context: any): any;
|
||||
visitTypeofExpr(ast: TypeofExpr, context: any): any;
|
||||
}
|
||||
|
||||
export const THIS_EXPR = new ReadVarExpr(BuiltinVar.This, null, null);
|
||||
@ -993,6 +1012,12 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor {
|
||||
return this.transformExpr(ast, context);
|
||||
}
|
||||
|
||||
visitTypeofExpr(expr: TypeofExpr, context: any): any {
|
||||
return this.transformExpr(
|
||||
new TypeofExpr(expr.expr.visitExpression(this, context), expr.type, expr.sourceSpan),
|
||||
context);
|
||||
}
|
||||
|
||||
visitWriteVarExpr(expr: WriteVarExpr, context: any): any {
|
||||
return this.transformExpr(
|
||||
new WriteVarExpr(
|
||||
@ -1220,6 +1245,7 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor
|
||||
visitArrayType(type: ArrayType, context: any): any { return this.visitType(type, context); }
|
||||
visitMapType(type: MapType, context: any): any { return this.visitType(type, context); }
|
||||
visitWrappedNodeExpr(ast: WrappedNodeExpr<any>, context: any): any { return ast; }
|
||||
visitTypeofExpr(ast: TypeofExpr, context: any): any { return this.visitExpression(ast, context); }
|
||||
visitReadVarExpr(ast: ReadVarExpr, context: any): any {
|
||||
return this.visitExpression(ast, context);
|
||||
}
|
||||
@ -1474,6 +1500,10 @@ export function expressionType(
|
||||
return new ExpressionType(expr, typeModifiers);
|
||||
}
|
||||
|
||||
export function typeofExpr(expr: Expression) {
|
||||
return new TypeofExpr(expr);
|
||||
}
|
||||
|
||||
export function literalArr(
|
||||
values: Expression[], type?: Type | null,
|
||||
sourceSpan?: ParseSourceSpan | null): LiteralArrayExpr {
|
||||
|
@ -117,6 +117,9 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor {
|
||||
visitWrappedNodeExpr(ast: o.WrappedNodeExpr<any>, ctx: _ExecutionContext): never {
|
||||
throw new Error('Cannot interpret a WrappedNodeExpr.');
|
||||
}
|
||||
visitTypeofExpr(ast: o.TypeofExpr, ctx: _ExecutionContext): never {
|
||||
throw new Error('Cannot interpret a TypeofExpr');
|
||||
}
|
||||
visitReadVarExpr(ast: o.ReadVarExpr, ctx: _ExecutionContext): any {
|
||||
let varName = ast.name !;
|
||||
if (ast.builtin != null) {
|
||||
|
@ -349,6 +349,9 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor
|
||||
case o.BuiltinTypeName.String:
|
||||
typeStr = 'string';
|
||||
break;
|
||||
case o.BuiltinTypeName.None:
|
||||
typeStr = 'never';
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unsupported builtin type ${type.name}`);
|
||||
}
|
||||
|
Reference in New Issue
Block a user