/** * For each property in the object literal, if it's templateUrl or styleUrls, replace it * with content. * @param node the arguments to @Component() or args property of decorators: [{type:Component}] * @param loader provides access to the loadResource method of the host * @returns updated arguments */ function updateComponentProperties(args, loader) { if (args.length !== 1) { // User should have gotten a type-check error because @Component takes one argument return args; } var componentArg = args[0]; if (!ts.isObjectLiteralExpression(componentArg)) { // User should have gotten a type-check error because @Component takes an object literal // argument return args; } var newProperties = []; var newStyleExprs = []; componentArg.properties.forEach(function (prop) { if (!ts.isPropertyAssignment(prop) || ts.isComputedPropertyName(prop.name)) { newProperties.push(prop); return; } switch (prop.name.text) { case 'styles': if (!ts.isArrayLiteralExpression(prop.initializer)) { throw new Error('styles takes an array argument'); } newStyleExprs.push.apply(newStyleExprs, tslib_1.__spread(prop.initializer.elements)); break; case 'styleUrls': if (!ts.isArrayLiteralExpression(prop.initializer)) { throw new Error('styleUrls takes an array argument'); } newStyleExprs.push.apply(newStyleExprs, tslib_1.__spread(prop.initializer.elements.map(function (expr) { if (!ts.isStringLiteral(expr) && !ts.isNoSubstitutionTemplateLiteral(expr)) { throw new Error('Can only accept string literal arguments to styleUrls. ' + PRECONDITIONS_TEXT); } var styles = loader.get(expr.text); return ts.createLiteral(styles); }))); break; case 'templateUrl': if (!ts.isStringLiteral(prop.initializer) && !ts.isNoSubstitutionTemplateLiteral(prop.initializer)) { throw new Error('Can only accept a string literal argument to templateUrl. ' + PRECONDITIONS_TEXT); } var template = loader.get(prop.initializer.text); newProperties.push(ts.updatePropertyAssignment(prop, ts.createIdentifier('template'), ts.createLiteral(template))); break; default: newProperties.push(prop); } }); // Add the non-inline styles if (newStyleExprs.length > 0) { var newStyles = ts.createPropertyAssignment(ts.createIdentifier('styles'), ts.createArrayLiteral(newStyleExprs)); newProperties.push(newStyles); } return ts.createNodeArray([ts.updateObjectLiteral(componentArg, newProperties)]); }
var newAnnotations = node.initializer.elements.map(function (annotation) { // No-op if there's a non-object-literal mixed in the decorators values if (!ts.isObjectLiteralExpression(annotation)) return annotation; var decoratorType = annotation.properties.find(function (p) { return isIdentifierNamed(p, 'type'); }); // No-op if there's no 'type' property, or if it's not initialized to the Component symbol if (!decoratorType || !ts.isPropertyAssignment(decoratorType) || !ts.isIdentifier(decoratorType.initializer) || !isComponentSymbol(decoratorType.initializer, typeChecker)) { return annotation; } var newAnnotation = annotation.properties.map(function (prop) { // No-op if this isn't the 'args' property or if it's not initialized to an array if (!isIdentifierNamed(prop, 'args') || !ts.isPropertyAssignment(prop) || !ts.isArrayLiteralExpression(prop.initializer)) return prop; var newDecoratorArgs = ts.updatePropertyAssignment(prop, prop.name, ts.createArrayLiteral(updateComponentProperties(prop.initializer.elements, loader))); return newDecoratorArgs; }); return ts.updateObjectLiteral(annotation, newAnnotation); });