mirror of
https://github.com/angular/angular.git
synced 2026-11-25 16:07:50 -05:00
feat(ivy): introduce the type check block compiler (#26203)
This commit introduces the main functionality of the type-check compiler: generation of type check blocks. Type check blocks are blocks of TypeScript code which can be inlined into source files, and when processed by the TypeChecker will give information about any typing errors in template expressions. PR Close #26203
This commit is contained in:
committed by
Jason Aden
parent
4c615f7de7
commit
355a7cae3c
@@ -10,6 +10,7 @@ ts_library(
|
||||
"//packages:types",
|
||||
"//packages/compiler",
|
||||
"//packages/compiler-cli/src/ngtsc/metadata",
|
||||
"//packages/compiler-cli/src/ngtsc/translator",
|
||||
"//packages/compiler-cli/src/ngtsc/util",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -6,9 +6,38 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {BoundTarget, DirectiveMeta} from '@angular/compiler';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {Reference} from '../../metadata';
|
||||
|
||||
/**
|
||||
* Metadata to generate a type constructor for a particular directive.
|
||||
*/
|
||||
* Extension of `DirectiveMeta` that includes additional information required to type-check the
|
||||
* usage of a particular directive.
|
||||
*/
|
||||
export interface TypeCheckableDirectiveMeta extends DirectiveMeta {
|
||||
ref: Reference<ts.ClassDeclaration>;
|
||||
queries: string[];
|
||||
ngTemplateGuards: string[];
|
||||
hasNgTemplateContextGuard: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata required in addition to a component class in order to generate a type check block (TCB)
|
||||
* for that component.
|
||||
*/
|
||||
export interface TypeCheckBlockMetadata {
|
||||
/**
|
||||
* Semantic information about the template of the component.
|
||||
*/
|
||||
boundTarget: BoundTarget<TypeCheckableDirectiveMeta>;
|
||||
|
||||
/**
|
||||
* The name of the requested type check block function.
|
||||
*/
|
||||
fnName: string;
|
||||
}
|
||||
|
||||
export interface TypeCtorMetadata {
|
||||
/**
|
||||
* The name of the requested type constructor function.
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @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 {AST, ASTWithSource, Binary, Conditional, Interpolation, LiteralPrimitive, MethodCall, PropertyRead} from '@angular/compiler';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
const BINARY_OPS = new Map<string, ts.SyntaxKind>([
|
||||
['+', ts.SyntaxKind.PlusToken],
|
||||
['-', ts.SyntaxKind.MinusToken],
|
||||
['<', ts.SyntaxKind.LessThanToken],
|
||||
['>', ts.SyntaxKind.GreaterThanToken],
|
||||
['<=', ts.SyntaxKind.LessThanEqualsToken],
|
||||
['>=', ts.SyntaxKind.GreaterThanEqualsToken],
|
||||
['==', ts.SyntaxKind.EqualsEqualsToken],
|
||||
['===', ts.SyntaxKind.EqualsEqualsEqualsToken],
|
||||
['*', ts.SyntaxKind.AsteriskToken],
|
||||
['/', ts.SyntaxKind.SlashToken],
|
||||
['%', ts.SyntaxKind.PercentToken],
|
||||
['!=', ts.SyntaxKind.ExclamationEqualsToken],
|
||||
['!==', ts.SyntaxKind.ExclamationEqualsEqualsToken],
|
||||
['||', ts.SyntaxKind.BarBarToken],
|
||||
['&&', ts.SyntaxKind.AmpersandAmpersandToken],
|
||||
['&', ts.SyntaxKind.AmpersandToken],
|
||||
['|', ts.SyntaxKind.BarToken],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Convert an `AST` to TypeScript code directly, without going through an intermediate `Expression`
|
||||
* AST.
|
||||
*/
|
||||
export function astToTypescript(
|
||||
ast: AST, maybeResolve: (ast: AST) => ts.Expression | null): ts.Expression {
|
||||
const resolved = maybeResolve(ast);
|
||||
if (resolved !== null) {
|
||||
return resolved;
|
||||
}
|
||||
// Branch based on the type of expression being processed.
|
||||
if (ast instanceof ASTWithSource) {
|
||||
// Fall through to the underlying AST.
|
||||
return astToTypescript(ast.ast, maybeResolve);
|
||||
} else if (ast instanceof PropertyRead) {
|
||||
// This is a normal property read - convert the receiver to an expression and emit the correct
|
||||
// TypeScript expression to read the property.
|
||||
const receiver = astToTypescript(ast.receiver, maybeResolve);
|
||||
return ts.createPropertyAccess(receiver, ast.name);
|
||||
} else if (ast instanceof Interpolation) {
|
||||
return astArrayToExpression(ast.expressions, maybeResolve);
|
||||
} else if (ast instanceof Binary) {
|
||||
const lhs = astToTypescript(ast.left, maybeResolve);
|
||||
const rhs = astToTypescript(ast.right, maybeResolve);
|
||||
const op = BINARY_OPS.get(ast.operation);
|
||||
if (op === undefined) {
|
||||
throw new Error(`Unsupported Binary.operation: ${ast.operation}`);
|
||||
}
|
||||
return ts.createBinary(lhs, op as any, rhs);
|
||||
} else if (ast instanceof LiteralPrimitive) {
|
||||
if (ast.value === undefined) {
|
||||
return ts.createIdentifier('undefined');
|
||||
} else if (ast.value === null) {
|
||||
return ts.createNull();
|
||||
} else {
|
||||
return ts.createLiteral(ast.value);
|
||||
}
|
||||
} else if (ast instanceof MethodCall) {
|
||||
const receiver = astToTypescript(ast.receiver, maybeResolve);
|
||||
const method = ts.createPropertyAccess(receiver, ast.name);
|
||||
const args = ast.args.map(expr => astToTypescript(expr, maybeResolve));
|
||||
return ts.createCall(method, undefined, args);
|
||||
} else if (ast instanceof Conditional) {
|
||||
const condExpr = astToTypescript(ast.condition, maybeResolve);
|
||||
const trueExpr = astToTypescript(ast.trueExp, maybeResolve);
|
||||
const falseExpr = astToTypescript(ast.falseExp, maybeResolve);
|
||||
return ts.createParen(ts.createConditional(condExpr, trueExpr, falseExpr));
|
||||
} else {
|
||||
throw new Error(`Unknown node type: ${Object.getPrototypeOf(ast).constructor}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an array of `AST` expressions into a single `ts.Expression`, by converting them all
|
||||
* and separating them with commas.
|
||||
*/
|
||||
function astArrayToExpression(
|
||||
astArray: AST[], maybeResolve: (ast: AST) => ts.Expression | null): ts.Expression {
|
||||
// Reduce the `asts` array into a `ts.Expression`. Multiple expressions are combined into a
|
||||
// `ts.BinaryExpression` with a comma separator. First make a copy of the input array, as
|
||||
// it will be modified during the reduction.
|
||||
const asts = astArray.slice();
|
||||
return asts.reduce(
|
||||
(lhs, ast) =>
|
||||
ts.createBinary(lhs, ts.SyntaxKind.CommaToken, astToTypescript(ast, maybeResolve)),
|
||||
astToTypescript(asts.pop() !, maybeResolve));
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user