2018-10-24 19:02:25 -04:00
|
|
|
/**
|
|
|
|
* @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 {CompilerFacade, CoreEnvironment, ExportedCompilerFacade, R3ComponentMetadataFacade, R3DependencyMetadataFacade, R3DirectiveMetadataFacade, R3InjectableMetadataFacade, R3InjectorMetadataFacade, R3NgModuleMetadataFacade, R3PipeMetadataFacade, R3QueryMetadataFacade, StringMap, StringMapWithRename} from './compiler_facade_interface';
|
|
|
|
import {ConstantPool} from './constant_pool';
|
|
|
|
import {HostBinding, HostListener, Input, Output, Type} from './core';
|
|
|
|
import {compileInjectable} from './injectable_compiler_2';
|
2018-11-29 19:21:16 -05:00
|
|
|
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from './ml_parser/interpolation_config';
|
2019-02-08 17:10:19 -05:00
|
|
|
import {DeclareVarStmt, Expression, LiteralExpr, Statement, StmtModifier, WrappedNodeExpr} from './output/output_ast';
|
|
|
|
import {JitEvaluator} from './output/output_jit';
|
2019-01-24 20:25:46 -05:00
|
|
|
import {ParseError, ParseSourceSpan, r3JitTypeSourceSpan} from './parse_util';
|
2018-10-24 19:02:25 -04:00
|
|
|
import {R3DependencyMetadata, R3ResolvedDependencyType} from './render3/r3_factory';
|
2019-02-08 17:10:19 -05:00
|
|
|
import {R3JitReflector} from './render3/r3_jit';
|
2018-10-24 19:02:25 -04:00
|
|
|
import {R3InjectorMetadata, R3NgModuleMetadata, compileInjector, compileNgModule} from './render3/r3_module_compiler';
|
|
|
|
import {compilePipeFromMetadata} from './render3/r3_pipe_compiler';
|
|
|
|
import {R3Reference} from './render3/util';
|
|
|
|
import {R3DirectiveMetadata, R3QueryMetadata} from './render3/view/api';
|
2019-02-27 19:54:37 -05:00
|
|
|
import {ParsedHostBindings, compileComponentFromMetadata, compileDirectiveFromMetadata, parseHostBindings, verifyHostBindings} from './render3/view/compiler';
|
2018-10-24 19:02:25 -04:00
|
|
|
import {makeBindingParser, parseTemplate} from './render3/view/template';
|
2019-03-03 12:19:27 -05:00
|
|
|
import {ResourceLoader} from './resource_loader';
|
2018-11-22 09:38:28 -05:00
|
|
|
import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry';
|
2018-10-24 19:02:25 -04:00
|
|
|
|
|
|
|
export class CompilerFacadeImpl implements CompilerFacade {
|
|
|
|
R3ResolvedDependencyType = R3ResolvedDependencyType as any;
|
2019-03-03 12:19:27 -05:00
|
|
|
ResourceLoader = ResourceLoader;
|
2018-11-22 09:38:28 -05:00
|
|
|
private elementSchemaRegistry = new DomElementSchemaRegistry();
|
2019-02-08 17:10:20 -05:00
|
|
|
|
|
|
|
constructor(private jitEvaluator = new JitEvaluator()) {}
|
2018-10-24 19:02:25 -04:00
|
|
|
|
|
|
|
compilePipe(angularCoreEnv: CoreEnvironment, sourceMapUrl: string, facade: R3PipeMetadataFacade):
|
|
|
|
any {
|
|
|
|
const res = compilePipeFromMetadata({
|
|
|
|
name: facade.name,
|
|
|
|
type: new WrappedNodeExpr(facade.type),
|
2019-03-19 16:22:03 -04:00
|
|
|
typeArgumentCount: facade.typeArgumentCount,
|
2018-10-24 19:02:25 -04:00
|
|
|
deps: convertR3DependencyMetadataArray(facade.deps),
|
|
|
|
pipeName: facade.pipeName,
|
|
|
|
pure: facade.pure,
|
|
|
|
});
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, res.statements);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compileInjectable(
|
|
|
|
angularCoreEnv: CoreEnvironment, sourceMapUrl: string,
|
|
|
|
facade: R3InjectableMetadataFacade): any {
|
|
|
|
const {expression, statements} = compileInjectable({
|
|
|
|
name: facade.name,
|
|
|
|
type: new WrappedNodeExpr(facade.type),
|
2018-11-09 20:58:33 -05:00
|
|
|
typeArgumentCount: facade.typeArgumentCount,
|
2018-10-24 19:02:25 -04:00
|
|
|
providedIn: computeProvidedIn(facade.providedIn),
|
|
|
|
useClass: wrapExpression(facade, USE_CLASS),
|
|
|
|
useFactory: wrapExpression(facade, USE_FACTORY),
|
|
|
|
useValue: wrapExpression(facade, USE_VALUE),
|
|
|
|
useExisting: wrapExpression(facade, USE_EXISTING),
|
|
|
|
ctorDeps: convertR3DependencyMetadataArray(facade.ctorDeps),
|
|
|
|
userDeps: convertR3DependencyMetadataArray(facade.userDeps) || undefined,
|
|
|
|
});
|
|
|
|
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compileInjector(
|
|
|
|
angularCoreEnv: CoreEnvironment, sourceMapUrl: string,
|
|
|
|
facade: R3InjectorMetadataFacade): any {
|
|
|
|
const meta: R3InjectorMetadata = {
|
|
|
|
name: facade.name,
|
|
|
|
type: new WrappedNodeExpr(facade.type),
|
|
|
|
deps: convertR3DependencyMetadataArray(facade.deps),
|
|
|
|
providers: new WrappedNodeExpr(facade.providers),
|
2019-03-30 08:09:45 -04:00
|
|
|
imports: facade.imports.map(i => new WrappedNodeExpr(i)),
|
2018-10-24 19:02:25 -04:00
|
|
|
};
|
|
|
|
const res = compileInjector(meta);
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, res.statements);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compileNgModule(
|
|
|
|
angularCoreEnv: CoreEnvironment, sourceMapUrl: string,
|
|
|
|
facade: R3NgModuleMetadataFacade): any {
|
|
|
|
const meta: R3NgModuleMetadata = {
|
|
|
|
type: new WrappedNodeExpr(facade.type),
|
|
|
|
bootstrap: facade.bootstrap.map(wrapReference),
|
|
|
|
declarations: facade.declarations.map(wrapReference),
|
|
|
|
imports: facade.imports.map(wrapReference),
|
|
|
|
exports: facade.exports.map(wrapReference),
|
|
|
|
emitInline: true,
|
2019-03-08 20:57:34 -05:00
|
|
|
containsForwardDecls: false,
|
2019-02-11 18:03:04 -05:00
|
|
|
schemas: facade.schemas ? facade.schemas.map(wrapReference) : null,
|
2018-10-24 19:02:25 -04:00
|
|
|
};
|
|
|
|
const res = compileNgModule(meta);
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compileDirective(
|
|
|
|
angularCoreEnv: CoreEnvironment, sourceMapUrl: string,
|
|
|
|
facade: R3DirectiveMetadataFacade): any {
|
|
|
|
const constantPool = new ConstantPool();
|
|
|
|
const bindingParser = makeBindingParser();
|
|
|
|
|
|
|
|
const meta: R3DirectiveMetadata = convertDirectiveFacadeToMetadata(facade);
|
|
|
|
const res = compileDirectiveFromMetadata(meta, constantPool, bindingParser);
|
|
|
|
const preStatements = [...constantPool.statements, ...res.statements];
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, preStatements);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compileComponent(
|
|
|
|
angularCoreEnv: CoreEnvironment, sourceMapUrl: string,
|
|
|
|
facade: R3ComponentMetadataFacade): any {
|
|
|
|
// The ConstantPool is a requirement of the JIT'er.
|
|
|
|
const constantPool = new ConstantPool();
|
|
|
|
|
2018-11-29 19:21:16 -05:00
|
|
|
const interpolationConfig = facade.interpolation ?
|
|
|
|
InterpolationConfig.fromArray(facade.interpolation) :
|
|
|
|
DEFAULT_INTERPOLATION_CONFIG;
|
2018-10-24 19:02:25 -04:00
|
|
|
// Parse the template and check for errors.
|
2018-11-29 19:21:16 -05:00
|
|
|
const template = parseTemplate(
|
|
|
|
facade.template, sourceMapUrl,
|
2019-02-08 17:10:19 -05:00
|
|
|
{preserveWhitespaces: facade.preserveWhitespaces, interpolationConfig});
|
2018-10-24 19:02:25 -04:00
|
|
|
if (template.errors !== undefined) {
|
|
|
|
const errors = template.errors.map(err => err.toString()).join(', ');
|
|
|
|
throw new Error(`Errors during JIT compilation of template for ${facade.name}: ${errors}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the component metadata, including template, into an expression.
|
|
|
|
// TODO(alxhub): implement inputs, outputs, queries, etc.
|
|
|
|
const res = compileComponentFromMetadata(
|
|
|
|
{
|
|
|
|
...facade as R3ComponentMetadataFacadeNoPropAndWhitespace,
|
|
|
|
...convertDirectiveFacadeToMetadata(facade),
|
2018-11-22 09:38:28 -05:00
|
|
|
selector: facade.selector || this.elementSchemaRegistry.getDefaultComponentElementName(),
|
2018-10-24 19:02:25 -04:00
|
|
|
template,
|
|
|
|
wrapDirectivesAndPipesInClosure: false,
|
|
|
|
styles: facade.styles || [],
|
|
|
|
encapsulation: facade.encapsulation as any,
|
2018-11-29 19:21:16 -05:00
|
|
|
interpolation: interpolationConfig,
|
2018-12-20 03:49:24 -05:00
|
|
|
changeDetection: facade.changeDetection,
|
2018-10-24 19:02:25 -04:00
|
|
|
animations: facade.animations != null ? new WrappedNodeExpr(facade.animations) : null,
|
|
|
|
viewProviders: facade.viewProviders != null ? new WrappedNodeExpr(facade.viewProviders) :
|
|
|
|
null,
|
2018-11-16 12:57:23 -05:00
|
|
|
relativeContextFilePath: '',
|
|
|
|
i18nUseExternalIds: true,
|
2018-10-24 19:02:25 -04:00
|
|
|
},
|
2018-11-29 19:21:16 -05:00
|
|
|
constantPool, makeBindingParser(interpolationConfig));
|
2018-10-24 19:02:25 -04:00
|
|
|
const preStatements = [...constantPool.statements, ...res.statements];
|
2019-02-08 17:10:19 -05:00
|
|
|
return this.jitExpression(
|
2019-02-08 17:10:20 -05:00
|
|
|
res.expression, angularCoreEnv, `ng:///${facade.name}.js`, preStatements);
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
2019-01-24 20:25:46 -05:00
|
|
|
|
|
|
|
createParseSourceSpan(kind: string, typeName: string, sourceUrl: string): ParseSourceSpan {
|
|
|
|
return r3JitTypeSourceSpan(kind, typeName, sourceUrl);
|
|
|
|
}
|
2019-02-08 17:10:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JIT compiles an expression and returns the result of executing that expression.
|
|
|
|
*
|
|
|
|
* @param def the definition which will be compiled and executed to get the value to patch
|
|
|
|
* @param context an object map of @angular/core symbol names to symbols which will be available
|
|
|
|
* in the context of the compiled expression
|
|
|
|
* @param sourceUrl a URL to use for the source map of the compiled expression
|
|
|
|
* @param preStatements a collection of statements that should be evaluated before the expression.
|
|
|
|
*/
|
|
|
|
private jitExpression(
|
|
|
|
def: Expression, context: {[key: string]: any}, sourceUrl: string,
|
|
|
|
preStatements: Statement[]): any {
|
|
|
|
// The ConstantPool may contain Statements which declare variables used in the final expression.
|
|
|
|
// Therefore, its statements need to precede the actual JIT operation. The final statement is a
|
|
|
|
// declaration of $def which is set to the expression being compiled.
|
|
|
|
const statements: Statement[] = [
|
|
|
|
...preStatements,
|
|
|
|
new DeclareVarStmt('$def', def, undefined, [StmtModifier.Exported]),
|
|
|
|
];
|
|
|
|
|
|
|
|
const res = this.jitEvaluator.evaluateStatements(
|
|
|
|
sourceUrl, statements, new R3JitReflector(context), /* enableSourceMaps */ true);
|
|
|
|
return res['$def'];
|
|
|
|
}
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This seems to be needed to placate TS v3.0 only
|
|
|
|
type R3ComponentMetadataFacadeNoPropAndWhitespace = Pick<
|
|
|
|
R3ComponentMetadataFacade,
|
|
|
|
Exclude<Exclude<keyof R3ComponentMetadataFacade, 'preserveWhitespaces'>, 'propMetadata'>>;
|
|
|
|
|
|
|
|
const USE_CLASS = Object.keys({useClass: null})[0];
|
|
|
|
const USE_FACTORY = Object.keys({useFactory: null})[0];
|
|
|
|
const USE_VALUE = Object.keys({useValue: null})[0];
|
|
|
|
const USE_EXISTING = Object.keys({useExisting: null})[0];
|
|
|
|
|
|
|
|
const wrapReference = function(value: Type): R3Reference {
|
|
|
|
const wrapped = new WrappedNodeExpr(value);
|
|
|
|
return {value: wrapped, type: wrapped};
|
|
|
|
};
|
|
|
|
|
|
|
|
function convertToR3QueryMetadata(facade: R3QueryMetadataFacade): R3QueryMetadata {
|
|
|
|
return {
|
|
|
|
...facade,
|
|
|
|
predicate: Array.isArray(facade.predicate) ? facade.predicate :
|
|
|
|
new WrappedNodeExpr(facade.predicate),
|
|
|
|
read: facade.read ? new WrappedNodeExpr(facade.read) : null,
|
2019-02-18 20:33:59 -05:00
|
|
|
static: facade.static
|
2018-10-24 19:02:25 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertDirectiveFacadeToMetadata(facade: R3DirectiveMetadataFacade): R3DirectiveMetadata {
|
|
|
|
const inputsFromMetadata = parseInputOutputs(facade.inputs || []);
|
|
|
|
const outputsFromMetadata = parseInputOutputs(facade.outputs || []);
|
|
|
|
const propMetadata = facade.propMetadata;
|
|
|
|
const inputsFromType: StringMapWithRename = {};
|
|
|
|
const outputsFromType: StringMap = {};
|
|
|
|
for (const field in propMetadata) {
|
|
|
|
if (propMetadata.hasOwnProperty(field)) {
|
|
|
|
propMetadata[field].forEach(ann => {
|
|
|
|
if (isInput(ann)) {
|
|
|
|
inputsFromType[field] =
|
|
|
|
ann.bindingPropertyName ? [ann.bindingPropertyName, field] : field;
|
|
|
|
} else if (isOutput(ann)) {
|
|
|
|
outputsFromType[field] = ann.bindingPropertyName || field;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...facade as R3DirectiveMetadataFacadeNoPropAndWhitespace,
|
2019-01-24 20:25:46 -05:00
|
|
|
typeSourceSpan: facade.typeSourceSpan,
|
2018-10-24 19:02:25 -04:00
|
|
|
type: new WrappedNodeExpr(facade.type),
|
|
|
|
deps: convertR3DependencyMetadataArray(facade.deps),
|
2019-01-24 20:25:46 -05:00
|
|
|
host: extractHostBindings(facade.host, facade.propMetadata, facade.typeSourceSpan),
|
2018-10-24 19:02:25 -04:00
|
|
|
inputs: {...inputsFromMetadata, ...inputsFromType},
|
|
|
|
outputs: {...outputsFromMetadata, ...outputsFromType},
|
2018-11-12 21:02:47 -05:00
|
|
|
queries: facade.queries.map(convertToR3QueryMetadata),
|
2018-10-24 19:02:25 -04:00
|
|
|
providers: facade.providers != null ? new WrappedNodeExpr(facade.providers) : null,
|
2019-03-13 14:30:38 -04:00
|
|
|
viewQueries: facade.viewQueries.map(convertToR3QueryMetadata),
|
2018-10-24 19:02:25 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This seems to be needed to placate TS v3.0 only
|
|
|
|
type R3DirectiveMetadataFacadeNoPropAndWhitespace =
|
|
|
|
Pick<R3DirectiveMetadataFacade, Exclude<keyof R3DirectiveMetadataFacade, 'propMetadata'>>;
|
|
|
|
|
|
|
|
function wrapExpression(obj: any, property: string): WrappedNodeExpr<any>|undefined {
|
|
|
|
if (obj.hasOwnProperty(property)) {
|
|
|
|
return new WrappedNodeExpr(obj[property]);
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function computeProvidedIn(providedIn: Type | string | null | undefined): Expression {
|
|
|
|
if (providedIn == null || typeof providedIn === 'string') {
|
|
|
|
return new LiteralExpr(providedIn);
|
|
|
|
} else {
|
|
|
|
return new WrappedNodeExpr(providedIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertR3DependencyMetadata(facade: R3DependencyMetadataFacade): R3DependencyMetadata {
|
|
|
|
let tokenExpr;
|
|
|
|
if (facade.token === null) {
|
|
|
|
tokenExpr = new LiteralExpr(null);
|
|
|
|
} else if (facade.resolved === R3ResolvedDependencyType.Attribute) {
|
|
|
|
tokenExpr = new LiteralExpr(facade.token);
|
|
|
|
} else {
|
|
|
|
tokenExpr = new WrappedNodeExpr(facade.token);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
token: tokenExpr,
|
|
|
|
resolved: facade.resolved,
|
|
|
|
host: facade.host,
|
|
|
|
optional: facade.optional,
|
|
|
|
self: facade.self,
|
|
|
|
skipSelf: facade.skipSelf
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertR3DependencyMetadataArray(facades: R3DependencyMetadataFacade[] | null | undefined):
|
|
|
|
R3DependencyMetadata[]|null {
|
|
|
|
return facades == null ? null : facades.map(convertR3DependencyMetadata);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:25:46 -05:00
|
|
|
function extractHostBindings(
|
|
|
|
host: {[key: string]: string}, propMetadata: {[key: string]: any[]},
|
2019-02-27 19:54:37 -05:00
|
|
|
sourceSpan: ParseSourceSpan): ParsedHostBindings {
|
2018-10-24 19:02:25 -04:00
|
|
|
// First parse the declarations from the metadata.
|
2019-01-24 20:25:46 -05:00
|
|
|
const bindings = parseHostBindings(host || {});
|
|
|
|
|
|
|
|
// After that check host bindings for errors
|
|
|
|
const errors = verifyHostBindings(bindings, sourceSpan);
|
|
|
|
if (errors.length) {
|
|
|
|
throw new Error(errors.map((error: ParseError) => error.msg).join('\n'));
|
|
|
|
}
|
2018-10-24 19:02:25 -04:00
|
|
|
|
|
|
|
// Next, loop over the properties of the object, looking for @HostBinding and @HostListener.
|
|
|
|
for (const field in propMetadata) {
|
|
|
|
if (propMetadata.hasOwnProperty(field)) {
|
|
|
|
propMetadata[field].forEach(ann => {
|
|
|
|
if (isHostBinding(ann)) {
|
2019-01-24 20:25:46 -05:00
|
|
|
bindings.properties[ann.hostPropertyName || field] = field;
|
2018-10-24 19:02:25 -04:00
|
|
|
} else if (isHostListener(ann)) {
|
2019-01-24 20:25:46 -05:00
|
|
|
bindings.listeners[ann.eventName || field] = `${field}(${(ann.args || []).join(',')})`;
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:25:46 -05:00
|
|
|
return bindings;
|
2018-10-24 19:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function isHostBinding(value: any): value is HostBinding {
|
|
|
|
return value.ngMetadataName === 'HostBinding';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHostListener(value: any): value is HostListener {
|
|
|
|
return value.ngMetadataName === 'HostListener';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isInput(value: any): value is Input {
|
|
|
|
return value.ngMetadataName === 'Input';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isOutput(value: any): value is Output {
|
|
|
|
return value.ngMetadataName === 'Output';
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseInputOutputs(values: string[]): StringMap {
|
|
|
|
return values.reduce(
|
|
|
|
(map, value) => {
|
|
|
|
const [field, property] = value.split(',').map(piece => piece.trim());
|
|
|
|
map[field] = property || field;
|
|
|
|
return map;
|
|
|
|
},
|
|
|
|
{} as StringMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function publishFacade(global: any) {
|
|
|
|
const ng: ExportedCompilerFacade = global.ng || (global.ng = {});
|
|
|
|
ng.ɵcompilerFacade = new CompilerFacadeImpl();
|
2018-11-12 21:02:47 -05:00
|
|
|
}
|