Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function baseDirectiveFields(meta, constantPool, bindingParser) {
var definitionMap = new util_3.DefinitionMap();
// e.g. `type: MyDirective`
definitionMap.set('type', meta.type);
// e.g. `selectors: [['', 'someDir', '']]`
definitionMap.set('selectors', createDirectiveSelector(meta.selector));
// e.g. `factory: () => new MyApp(directiveInject(ElementRef))`
var result = r3_factory_1.compileFactoryFunction({
name: meta.name,
type: meta.type,
deps: meta.deps,
injectFn: r3_identifiers_1.Identifiers.directiveInject,
});
definitionMap.set('factory', result.factory);
definitionMap.set('contentQueries', createContentQueriesFunction(meta, constantPool));
definitionMap.set('contentQueriesRefresh', createContentQueriesRefreshFunction(meta));
// Initialize hostVarsCount to number of bound host properties (interpolations illegal),
// except 'style' and 'class' properties, since they should *not* allocate host var slots
var hostVarsCount = Object.keys(meta.host.properties)
.filter(function (name) {
var prefix = getStylingPrefix(name);
return prefix !== 'style' && prefix !== 'class';
})
.length;
var elVarExp = o.variable('elIndex');
var contextVarExp = o.variable(util_3.CONTEXT_NAME);
var styleBuilder = new styling_builder_1.StylingBuilder(elVarExp, contextVarExp);
var allOtherAttributes = {};
var t = o.variable('t');
var statements = [];
// The type to instantiate via constructor invocation. If there is no delegated factory, meaning
// this type is always created by constructor invocation, then this is the type-to-create
// parameter provided by the user (t) if specified, or the current type if not. If there is a
// delegated factory (which is used to create the current type) then this is only the type-to-
// create parameter (t).
var typeForCtor = !isDelegatedMetadata(meta) ? new o.BinaryOperatorExpr(o.BinaryOperator.Or, t, meta.type) : t;
var ctorExpr = null;
if (meta.deps !== null) {
// There is a constructor (either explicitly or implicitly defined).
ctorExpr = new o.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.injectFn));
}
else {
var baseFactory = o.variable("\u0275" + meta.name + "_BaseFactory");
var getInheritedFactory = o.importExpr(r3_identifiers_1.Identifiers.getInheritedFactory);
var baseFactoryStmt = baseFactory.set(getInheritedFactory.callFn([meta.type])).toDeclStmt(o.INFERRED_TYPE, [
o.StmtModifier.Exported, o.StmtModifier.Final
]);
statements.push(baseFactoryStmt);
// There is no constructor, use the base class' factory to construct typeForCtor.
ctorExpr = baseFactory.callFn([typeForCtor]);
}
var ctorExprFinal = ctorExpr;
var body = [];
var retExpr = null;
function makeConditionalFactory(nonCtorExpr) {
var r = o.variable('r');
body.push(r.set(o.NULL_EXPR).toDeclStmt());
body.push(o.ifStmt(t, [r.set(ctorExprFinal).toStmt()], [r.set(nonCtorExpr).toStmt()]));
return r;
}
function createViewQueriesFunction(meta, constantPool) {
var createStatements = [];
var updateStatements = [];
var tempAllocator = util_3.temporaryAllocator(updateStatements, util_3.TEMPORARY_NAME);
for (var i = 0; i < meta.viewQueries.length; i++) {
var query = meta.viewQueries[i];
// creation, e.g. r3.Q(0, somePredicate, true);
var queryDefinition = createQueryDefinition(query, constantPool, i);
createStatements.push(queryDefinition.toStmt());
// update, e.g. (r3.qR(tmp = r3.ɵload(0)) && (ctx.someDir = tmp));
var temporary = tempAllocator();
var getQueryList = o.importExpr(r3_identifiers_1.Identifiers.load).callFn([o.literal(i)]);
var refresh = o.importExpr(r3_identifiers_1.Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
var updateDirective = o.variable(util_3.CONTEXT_NAME)
.prop(query.propertyName)
.set(query.first ? temporary.prop('first') : temporary);
updateStatements.push(refresh.and(updateDirective).toStmt());
}
var viewQueryFnName = meta.name ? meta.name + "_Query" : null;
return o.fn([new o.FnParam(util_3.RENDER_FLAGS, o.NUMBER_TYPE), new o.FnParam(util_3.CONTEXT_NAME, null)], [
template_1.renderFlagCheckIfStmt(1 /* Create */, createStatements),
template_1.renderFlagCheckIfStmt(2 /* Update */, updateStatements)
], o.INFERRED_TYPE, null, viewQueryFnName);
}
// Return a host binding function or null if one is not necessary.
// what all the bindings are and then generate the statements required to register
// those bindings to the element via `elementStyling`.
var elementStylingInstruction = styleBuilder.buildElementStylingInstruction(null, constantPool);
if (elementStylingInstruction) {
createStatements.push(createStylingStmt(elementStylingInstruction, bindingContext, bindingFn));
}
// finally each binding that was registered in the statement above will need to be added to
// the update block of a component/directive templateFn/hostBindingsFn so that the bindings
// are evaluated and updated for the element.
styleBuilder.buildUpdateLevelInstructions(valueConverter).forEach(function (instruction) {
updateStatements.push(createStylingStmt(instruction, bindingContext, bindingFn));
});
}
}
if (totalHostVarsCount) {
createStatements.unshift(o.importExpr(r3_identifiers_1.Identifiers.allocHostVars).callFn([o.literal(totalHostVarsCount)]).toStmt());
}
if (createStatements.length > 0 || updateStatements.length > 0) {
var hostBindingsFnName = meta.name ? meta.name + "_HostBindings" : null;
var statements = [];
if (createStatements.length > 0) {
statements.push(template_1.renderFlagCheckIfStmt(1 /* Create */, createStatements));
}
if (updateStatements.length > 0) {
statements.push(template_1.renderFlagCheckIfStmt(2 /* Update */, updateStatements));
}
return o.fn([
new o.FnParam(util_3.RENDER_FLAGS, o.NUMBER_TYPE), new o.FnParam(util_3.CONTEXT_NAME, null),
new o.FnParam(elVarExp.name, o.NUMBER_TYPE)
], statements, o.INFERRED_TYPE, null, hostBindingsFnName);
}
return null;
function createQueryDefinition(query, constantPool, idx) {
var predicate = util_3.getQueryPredicate(query, constantPool);
// e.g. r3.query(null, somePredicate, false) or r3.query(0, ['div'], false)
var parameters = [
o.literal(idx, o.INFERRED_TYPE),
predicate,
o.literal(query.descendants),
];
if (query.read) {
parameters.push(query.read);
}
return o.importExpr(r3_identifiers_1.Identifiers.query).callFn(parameters);
}
// Turn a directive selector into an R3-compatible selector for directive def
function addFeatures(definitionMap, meta) {
// e.g. `features: [NgOnChangesFeature]`
var features = [];
var providers = meta.providers;
var viewProviders = meta.viewProviders;
if (providers || viewProviders) {
var args = [providers || new o.LiteralArrayExpr([])];
if (viewProviders) {
args.push(viewProviders);
}
features.push(o.importExpr(r3_identifiers_1.Identifiers.ProvidersFeature).callFn(args));
}
if (meta.usesInheritance) {
features.push(o.importExpr(r3_identifiers_1.Identifiers.InheritDefinitionFeature));
}
if (meta.lifecycle.usesOnChanges) {
features.push(o.importExpr(r3_identifiers_1.Identifiers.NgOnChangesFeature));
}
if (features.length) {
definitionMap.set('features', o.literalArr(features));
}
}
/**
function compilePipeFromMetadata(metadata) {
var definitionMapValues = [];
// e.g. `name: 'myPipe'`
definitionMapValues.push({ key: 'name', value: o.literal(metadata.pipeName), quoted: false });
// e.g. `type: MyPipe`
definitionMapValues.push({ key: 'type', value: metadata.type, quoted: false });
var templateFactory = r3_factory_1.compileFactoryFunction({
name: metadata.name,
type: metadata.type,
deps: metadata.deps,
injectFn: r3_identifiers_1.Identifiers.directiveInject,
});
definitionMapValues.push({ key: 'factory', value: templateFactory.factory, quoted: false });
// e.g. `pure: true`
definitionMapValues.push({ key: 'pure', value: o.literal(metadata.pure), quoted: false });
var expression = o.importExpr(r3_identifiers_1.Identifiers.definePipe).callFn([o.literalMap(definitionMapValues)]);
var type = new o.ExpressionType(o.importExpr(r3_identifiers_1.Identifiers.PipeDefWithMeta, [
new o.ExpressionType(metadata.type),
new o.ExpressionType(new o.LiteralExpr(metadata.pipeName)),
]));
return { expression: expression, type: type, statements: templateFactory.statements };
}
exports.compilePipeFromMetadata = compilePipeFromMetadata;
function createHostBindingsFunction(meta, bindingParser) {
var statements = [];
var temporary = util_2.temporaryAllocator(statements, util_2.TEMPORARY_NAME);
var hostBindingSourceSpan = meta.typeSourceSpan;
// Calculate the queries
for (var index = 0; index < meta.queries.length; index++) {
var query = meta.queries[index];
// e.g. r3.qR(tmp = r3.ld(dirIndex)[1]) && (r3.ld(dirIndex)[0].someDir = tmp);
var getDirectiveMemory = o.importExpr(r3_identifiers_1.Identifiers.load).callFn([o.variable('dirIndex')]);
// The query list is at the query index + 1 because the directive itself is in slot 0.
var getQueryList = getDirectiveMemory.key(o.literal(index + 1));
var assignToTemporary = temporary().set(getQueryList);
var callQueryRefresh = o.importExpr(r3_identifiers_1.Identifiers.queryRefresh).callFn([assignToTemporary]);
var updateDirective = getDirectiveMemory.key(o.literal(0, o.INFERRED_TYPE))
.prop(query.propertyName)
.set(query.first ? temporary().prop('first') : temporary());
var andExpression = callQueryRefresh.and(updateDirective);
statements.push(andExpression.toStmt());
}
var directiveSummary = metadataAsSummary(meta);
// Calculate the host property bindings
var bindings = bindingParser.createBoundHostProperties(directiveSummary, hostBindingSourceSpan);
var bindingContext = o.importExpr(r3_identifiers_1.Identifiers.load).callFn([o.variable('dirIndex')]);
if (bindings) {
try {
for (var bindings_1 = tslib_1.__values(bindings), bindings_1_1 = bindings_1.next(); !bindings_1_1.done; bindings_1_1 = bindings_1.next()) {
var binding = bindings_1_1.value;
var bindingExpr = expression_converter_1.convertPropertyBinding(null, bindingContext, binding.expression, 'b', expression_converter_1.BindingForm.TrySimple, function () { return util_1.error('Unexpected interpolation'); });
statements.push.apply(statements, tslib_1.__spread(bindingExpr.stmts));
case 7:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation3).callFn(args);
case 9:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation4).callFn(args);
case 11:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation5).callFn(args);
case 13:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation6).callFn(args);
case 15:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation7).callFn(args);
case 17:
return o.importExpr(r3_identifiers_1.Identifiers.interpolation8).callFn(args);
}
(args.length >= 19 && args.length % 2 == 1) ||
util_1.error("Invalid interpolation argument length " + args.length);
return o.importExpr(r3_identifiers_1.Identifiers.interpolationV).callFn([o.literalArr(args)]);
}
/**
function compileModuleFactory(outputCtx, module, backPatchReferenceOf, resolver) {
var ngModuleFactoryVar = compile_metadata_1.identifierName(module.type) + "NgFactory";
var parentInjector = 'parentInjector';
var createFunction = o.fn([new o.FnParam(parentInjector, o.DYNAMIC_TYPE)], [new o.IfStmt(o.THIS_EXPR.prop(r3_identifiers_1.Identifiers.PATCH_DEPS).notIdentical(o.literal(true, o.INFERRED_TYPE)), [
o.THIS_EXPR.prop(r3_identifiers_1.Identifiers.PATCH_DEPS).set(o.literal(true, o.INFERRED_TYPE)).toStmt(),
backPatchReferenceOf(module.type).callFn([]).toStmt()
])], o.INFERRED_TYPE, null, ngModuleFactoryVar + "_Create");
var moduleFactoryLiteral = o.literalMap([
{ key: 'moduleType', value: outputCtx.importExpr(module.type.reference), quoted: false },
{ key: 'create', value: createFunction, quoted: false }
]);
outputCtx.statements.push(o.variable(ngModuleFactoryVar).set(moduleFactoryLiteral).toDeclStmt(o.DYNAMIC_TYPE, [
o.StmtModifier.Exported, o.StmtModifier.Final
]));
}
exports.compileModuleFactory = compileModuleFactory;