Exemplo n.º 1
0
     .forEach((node) => {
     const key = _getContentOfKeyLiteral(node.name);
     if (key == 'templateUrl') {
         const resourcePath = _getResourceRequest(node.initializer, sourceFile);
         const requireCall = _createRequireCall(resourcePath);
         const propAssign = ts.createPropertyAssignment('template', requireCall);
         replacements.push({
             resourcePaths: [resourcePath],
             replaceNodeOperation: new interfaces_1.ReplaceNodeOperation(sourceFile, node, propAssign),
         });
     }
     else if (key == 'styleUrls') {
         const arr = ast_helpers_1.collectDeepNodes(node, ts.SyntaxKind.ArrayLiteralExpression);
         if (!arr || arr.length == 0 || arr[0].elements.length == 0) {
             return;
         }
         const stylePaths = arr[0].elements.map((element) => {
             return _getResourceRequest(element, sourceFile);
         });
         const requireArray = ts.createArrayLiteral(stylePaths.map((path) => _createRequireCall(path)));
         const propAssign = ts.createPropertyAssignment('styles', requireArray);
         replacements.push({
             resourcePaths: stylePaths,
             replaceNodeOperation: new interfaces_1.ReplaceNodeOperation(sourceFile, node, propAssign),
         });
     }
 });
Exemplo n.º 2
0
 /**
  * Convert a reflected decorator to metadata.
  */
 function decoratorToMetadata(decorator) {
     // Decorators have a type.
     var properties = [
         ts.createPropertyAssignment('type', ts.updateIdentifier(decorator.identifier)),
     ];
     // Sometimes they have arguments.
     if (decorator.args !== null && decorator.args.length > 0) {
         var args = decorator.args.map(function (arg) { return ts.getMutableClone(arg); });
         properties.push(ts.createPropertyAssignment('args', ts.createArrayLiteral(args)));
     }
     return ts.createObjectLiteral(properties, true);
 }
Exemplo n.º 3
0
 /**
  * Given a class declaration, generate a call to `setClassMetadata` with the Angular metadata
  * present on the class or its member fields.
  *
  * If no such metadata is present, this function returns `null`. Otherwise, the call is returned
  * as a `Statement` for inclusion along with the class.
  */
 function generateSetClassMetadataCall(clazz, reflection, defaultImportRecorder, isCore) {
     if (!reflection.isClass(clazz)) {
         return null;
     }
     var id = ts.updateIdentifier(clazz.name);
     // Reflect over the class decorators. If none are present, or those that are aren't from
     // Angular, then return null. Otherwise, turn them into metadata.
     var classDecorators = reflection.getDecoratorsOfDeclaration(clazz);
     if (classDecorators === null) {
         return null;
     }
     var ngClassDecorators = classDecorators.filter(function (dec) { return isAngularDecorator(dec, isCore); }).map(decoratorToMetadata);
     if (ngClassDecorators.length === 0) {
         return null;
     }
     var metaDecorators = ts.createArrayLiteral(ngClassDecorators);
     // Convert the constructor parameters to metadata, passing null if none are present.
     var metaCtorParameters = new compiler_1.LiteralExpr(null);
     var classCtorParameters = reflection.getConstructorParameters(clazz);
     if (classCtorParameters !== null) {
         var ctorParameters = classCtorParameters.map(function (param) { return ctorParameterToMetadata(param, defaultImportRecorder, isCore); });
         metaCtorParameters = new compiler_1.FunctionExpr([], [
             new compiler_1.ReturnStatement(new compiler_1.LiteralArrayExpr(ctorParameters)),
         ]);
     }
     // Do the same for property decorators.
     var metaPropDecorators = ts.createNull();
     var decoratedMembers = reflection.getMembersOfClass(clazz)
         .filter(function (member) { return !member.isStatic && member.decorators !== null; })
         .map(function (member) { return classMemberToMetadata(member.name, member.decorators, isCore); });
     if (decoratedMembers.length > 0) {
         metaPropDecorators = ts.createObjectLiteral(decoratedMembers);
     }
     // Generate a pure call to setClassMetadata with the class identifier and its metadata.
     var setClassMetadata = new compiler_1.ExternalExpr(compiler_1.Identifiers.setClassMetadata);
     var fnCall = new compiler_1.InvokeFunctionExpr(
     /* fn */ setClassMetadata, 
     /* args */
     [
         new compiler_1.WrappedNodeExpr(id),
         new compiler_1.WrappedNodeExpr(metaDecorators),
         metaCtorParameters,
         new compiler_1.WrappedNodeExpr(metaPropDecorators),
     ], 
     /* type */ undefined, 
     /* sourceSpan */ undefined, 
     /* pure */ true);
     return fnCall.toStmt();
 }
Exemplo n.º 4
0
 /**
  * Convert a reflected constructor parameter to metadata.
  */
 function ctorParameterToMetadata(param, defaultImportRecorder, isCore) {
     // Parameters sometimes have a type that can be referenced. If so, then use it, otherwise
     // its type is undefined.
     var type = param.typeValueReference !== null ?
         util_1.valueReferenceToExpression(param.typeValueReference, defaultImportRecorder) :
         new compiler_1.LiteralExpr(undefined);
     var mapEntries = [
         { key: 'type', value: type, quoted: false },
     ];
     // If the parameter has decorators, include the ones from Angular.
     if (param.decorators !== null) {
         var ngDecorators = param.decorators.filter(function (dec) { return isAngularDecorator(dec, isCore); }).map(decoratorToMetadata);
         var value = new compiler_1.WrappedNodeExpr(ts.createArrayLiteral(ngDecorators));
         mapEntries.push({ key: 'decorators', value: value, quoted: false });
     }
     return compiler_1.literalMap(mapEntries);
 }
 _NodeEmitterVisitor.prototype.visitLiteralArrayExpr = function (expr) {
     var _this = this;
     return this.record(expr, ts.createArrayLiteral(expr.entries.map(function (entry) { return entry.visitExpression(_this, null); })));
 };
Exemplo n.º 6
0
 /**
  * Convert an `AST` to TypeScript code directly, without going through an intermediate `Expression`
  * AST.
  */
 function astToTypescript(ast, maybeResolve, config) {
     var resolved = maybeResolve(ast);
     if (resolved !== null) {
         return resolved;
     }
     // Branch based on the type of expression being processed.
     if (ast instanceof compiler_1.ASTWithSource) {
         // Fall through to the underlying AST.
         return astToTypescript(ast.ast, maybeResolve, config);
     }
     else if (ast instanceof compiler_1.PropertyRead) {
         // This is a normal property read - convert the receiver to an expression and emit the correct
         // TypeScript expression to read the property.
         var receiver = astToTypescript(ast.receiver, maybeResolve, config);
         return ts.createPropertyAccess(receiver, ast.name);
     }
     else if (ast instanceof compiler_1.Interpolation) {
         return astArrayToExpression(ast.expressions, maybeResolve, config);
     }
     else if (ast instanceof compiler_1.Binary) {
         var lhs = astToTypescript(ast.left, maybeResolve, config);
         var rhs = astToTypescript(ast.right, maybeResolve, config);
         var op = BINARY_OPS.get(ast.operation);
         if (op === undefined) {
             throw new Error("Unsupported Binary.operation: " + ast.operation);
         }
         return ts.createBinary(lhs, op, rhs);
     }
     else if (ast instanceof compiler_1.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 compiler_1.MethodCall) {
         var receiver = astToTypescript(ast.receiver, maybeResolve, config);
         var method = ts.createPropertyAccess(receiver, ast.name);
         var args = ast.args.map(function (expr) { return astToTypescript(expr, maybeResolve, config); });
         return ts.createCall(method, undefined, args);
     }
     else if (ast instanceof compiler_1.Conditional) {
         var condExpr = astToTypescript(ast.condition, maybeResolve, config);
         var trueExpr = astToTypescript(ast.trueExp, maybeResolve, config);
         var falseExpr = astToTypescript(ast.falseExp, maybeResolve, config);
         return ts.createParen(ts.createConditional(condExpr, trueExpr, falseExpr));
     }
     else if (ast instanceof compiler_1.LiteralArray) {
         var elements = ast.expressions.map(function (expr) { return astToTypescript(expr, maybeResolve, config); });
         return ts.createArrayLiteral(elements);
     }
     else if (ast instanceof compiler_1.LiteralMap) {
         var properties = ast.keys.map(function (_a, idx) {
             var key = _a.key;
             var value = astToTypescript(ast.values[idx], maybeResolve, config);
             return ts.createPropertyAssignment(ts.createStringLiteral(key), value);
         });
         return ts.createObjectLiteral(properties, true);
     }
     else if (ast instanceof compiler_1.KeyedRead) {
         var receiver = astToTypescript(ast.obj, maybeResolve, config);
         var key = astToTypescript(ast.key, maybeResolve, config);
         return ts.createElementAccess(receiver, key);
     }
     else if (ast instanceof compiler_1.NonNullAssert) {
         var expr = astToTypescript(ast.expression, maybeResolve, config);
         return ts.createNonNullExpression(expr);
     }
     else if (ast instanceof compiler_1.PrefixNot) {
         return ts.createLogicalNot(astToTypescript(ast.expression, maybeResolve, config));
     }
     else if (ast instanceof compiler_1.SafePropertyRead) {
         // A safe property expression a?.b takes the form `(a != null ? a!.b : whenNull)`, where
         // whenNull is either of type 'any' or or 'undefined' depending on strictness. The non-null
         // assertion is necessary because in practice 'a' may be a method call expression, which won't
         // have a narrowed type when repeated in the ternary true branch.
         var receiver = astToTypescript(ast.receiver, maybeResolve, config);
         var expr = ts.createPropertyAccess(ts.createNonNullExpression(receiver), ast.name);
         var whenNull = config.strictSafeNavigationTypes ? UNDEFINED : NULL_AS_ANY;
         return safeTernary(receiver, expr, whenNull);
     }
     else if (ast instanceof compiler_1.SafeMethodCall) {
         var receiver = astToTypescript(ast.receiver, maybeResolve, config);
         // See the comment in SafePropertyRead above for an explanation of the need for the non-null
         // assertion here.
         var method = ts.createPropertyAccess(ts.createNonNullExpression(receiver), ast.name);
         var args = ast.args.map(function (expr) { return astToTypescript(expr, maybeResolve, config); });
         var expr = ts.createCall(method, undefined, args);
         var whenNull = config.strictSafeNavigationTypes ? UNDEFINED : NULL_AS_ANY;
         return safeTernary(receiver, expr, whenNull);
     }
     else {
         throw new Error("Unknown node type: " + Object.getPrototypeOf(ast).constructor);
     }
 }
Exemplo n.º 7
0
 /**
  * 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)]);
 }
Exemplo n.º 8
0
 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;
 });
Exemplo n.º 9
0
 /**
  * Convert a reflected class member to metadata.
  */
 function classMemberToMetadata(name, decorators, isCore) {
     var ngDecorators = decorators.filter(function (dec) { return isAngularDecorator(dec, isCore); }).map(decoratorToMetadata);
     var decoratorMeta = ts.createArrayLiteral(ngDecorators);
     return ts.createPropertyAssignment(name, decoratorMeta);
 }