Example #1
0
 ComponentDecoratorHandler.prototype.preanalyze = function (node, decorator) {
     var meta = this._resolveLiteral(decorator);
     var component = metadata_1.reflectObjectLiteral(meta);
     if (this.resourceLoader.preload !== undefined && component.has('templateUrl')) {
         var templateUrl = metadata_1.staticallyResolve(component.get('templateUrl'), this.reflector, this.checker);
         if (typeof templateUrl !== 'string') {
             throw new Error("templateUrl should be a string");
         }
         var url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
         return this.resourceLoader.preload(url);
     }
     return undefined;
 };
 ComponentDecoratorHandler.prototype.preanalyze = function (node, decorator) {
     var meta = this._resolveLiteral(decorator);
     var component = metadata_1.reflectObjectLiteral(meta);
     if (this.resourceLoader.preload !== undefined && component.has('templateUrl')) {
         var templateUrlExpr = component.get('templateUrl');
         var templateUrl = metadata_1.staticallyResolve(templateUrlExpr, this.reflector, this.checker);
         if (typeof templateUrl !== 'string') {
             throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, templateUrlExpr, 'templateUrl must be a string');
         }
         var url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
         return this.resourceLoader.preload(url);
     }
     return undefined;
 };
Example #3
0
 function reflectArrayElement(element) {
     return ts.isObjectLiteralExpression(element) ? metadata_1.reflectObjectLiteral(element) : null;
 }
Example #4
0
 /**
  * Read metadata from the `@Injectable` decorator and produce the `IvyInjectableMetadata`, the input
  * metadata needed to run `compileIvyInjectable`.
  */
 function extractInjectableMetadata(clazz, decorator, reflector, isCore) {
     if (clazz.name === undefined) {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ON_ANONYMOUS_CLASS, decorator.node, "@Injectable on anonymous class");
     }
     var name = clazz.name.text;
     var type = new compiler_1.WrappedNodeExpr(clazz.name);
     var ctorDeps = util_1.getConstructorDependencies(clazz, reflector, isCore);
     if (decorator.args === null) {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_NOT_CALLED, decorator.node, '@Injectable must be called');
     }
     if (decorator.args.length === 0) {
         return {
             name: name,
             type: type,
             providedIn: new compiler_1.LiteralExpr(null), ctorDeps: ctorDeps,
         };
     }
     else if (decorator.args.length === 1) {
         var metaNode = decorator.args[0];
         // Firstly make sure the decorator argument is an inline literal - if not, it's illegal to
         // transport references from one location to another. This is the problem that lowering
         // used to solve - if this restriction proves too undesirable we can re-implement lowering.
         if (!ts.isObjectLiteralExpression(metaNode)) {
             throw new Error("In Ivy, decorator metadata must be inline.");
         }
         // Resolve the fields of the literal into a map of field name to expression.
         var meta = metadata_1.reflectObjectLiteral(metaNode);
         var providedIn = new compiler_1.LiteralExpr(null);
         if (meta.has('providedIn')) {
             providedIn = new compiler_1.WrappedNodeExpr(meta.get('providedIn'));
         }
         var userDeps = undefined;
         if ((meta.has('useClass') || meta.has('useFactory')) && meta.has('deps')) {
             var depsExpr = meta.get('deps');
             if (!ts.isArrayLiteralExpression(depsExpr)) {
                 throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_NOT_LITERAL, depsExpr, "In Ivy, deps metadata must be an inline array.");
             }
             if (depsExpr.elements.length > 0) {
                 throw new Error("deps not yet supported");
             }
             userDeps = depsExpr.elements.map(function (dep) { return getDep(dep, reflector); });
         }
         if (meta.has('useValue')) {
             return {
                 name: name,
                 type: type,
                 ctorDeps: ctorDeps,
                 providedIn: providedIn,
                 useValue: new compiler_1.WrappedNodeExpr(meta.get('useValue'))
             };
         }
         else if (meta.has('useExisting')) {
             return {
                 name: name,
                 type: type,
                 ctorDeps: ctorDeps,
                 providedIn: providedIn,
                 useExisting: new compiler_1.WrappedNodeExpr(meta.get('useExisting'))
             };
         }
         else if (meta.has('useClass')) {
             return {
                 name: name,
                 type: type,
                 ctorDeps: ctorDeps,
                 providedIn: providedIn,
                 useClass: new compiler_1.WrappedNodeExpr(meta.get('useClass')), userDeps: userDeps
             };
         }
         else if (meta.has('useFactory')) {
             // useFactory is special - the 'deps' property must be analyzed.
             var factory = new compiler_1.WrappedNodeExpr(meta.get('useFactory'));
             return { name: name, type: type, providedIn: providedIn, useFactory: factory, ctorDeps: ctorDeps, userDeps: userDeps };
         }
         else {
             return { name: name, type: type, providedIn: providedIn, ctorDeps: ctorDeps };
         }
     }
     else {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, decorator.args[2], 'Too many arguments to @Injectable');
     }
 }
 NgModuleDecoratorHandler.prototype.analyze = function (node, decorator) {
     var _this = this;
     if (decorator.args === null || decorator.args.length > 1) {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, "Incorrect number of arguments to @NgModule decorator");
     }
     // @NgModule can be invoked without arguments. In case it is, pretend as if a blank object
     // literal was specified. This simplifies the code below.
     var meta = decorator.args.length === 1 ? util_1.unwrapExpression(decorator.args[0]) :
         ts.createObjectLiteral([]);
     if (!ts.isObjectLiteralExpression(meta)) {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARG_NOT_LITERAL, meta, '@NgModule argument must be an object literal');
     }
     var ngModule = metadata_1.reflectObjectLiteral(meta);
     if (ngModule.has('jit')) {
         // The only allowed value is true, so there's no need to expand further.
         return {};
     }
     // Extract the module declarations, imports, and exports.
     var declarations = [];
     if (ngModule.has('declarations')) {
         var expr = ngModule.get('declarations');
         var declarationMeta = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
         declarations = this.resolveTypeList(expr, declarationMeta, 'declarations');
     }
     var imports = [];
     if (ngModule.has('imports')) {
         var expr = ngModule.get('imports');
         var importsMeta = metadata_1.staticallyResolve(expr, this.reflector, this.checker, function (ref) { return _this._extractModuleFromModuleWithProvidersFn(ref.node); });
         imports = this.resolveTypeList(expr, importsMeta, 'imports');
     }
     var exports = [];
     if (ngModule.has('exports')) {
         var expr = ngModule.get('exports');
         var exportsMeta = metadata_1.staticallyResolve(expr, this.reflector, this.checker, function (ref) { return _this._extractModuleFromModuleWithProvidersFn(ref.node); });
         exports = this.resolveTypeList(expr, exportsMeta, 'exports');
     }
     var bootstrap = [];
     if (ngModule.has('bootstrap')) {
         var expr = ngModule.get('bootstrap');
         var bootstrapMeta = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
         bootstrap = this.resolveTypeList(expr, bootstrapMeta, 'bootstrap');
     }
     // Register this module's information with the SelectorScopeRegistry. This ensures that during
     // the compile() phase, the module's metadata is available for selector scope computation.
     this.scopeRegistry.registerModule(node, { declarations: declarations, imports: imports, exports: exports });
     var context = node.getSourceFile();
     var ngModuleDef = {
         type: new compiler_1.WrappedNodeExpr(node.name),
         bootstrap: bootstrap.map(function (bootstrap) { return util_1.toR3Reference(bootstrap, context); }),
         declarations: declarations.map(function (decl) { return util_1.toR3Reference(decl, context); }),
         exports: exports.map(function (exp) { return util_1.toR3Reference(exp, context); }),
         imports: imports.map(function (imp) { return util_1.toR3Reference(imp, context); }),
         emitInline: false,
     };
     var providers = ngModule.has('providers') ?
         new compiler_1.WrappedNodeExpr(ngModule.get('providers')) :
         new compiler_1.LiteralArrayExpr([]);
     var injectorImports = [];
     if (ngModule.has('imports')) {
         injectorImports.push(new compiler_1.WrappedNodeExpr(ngModule.get('imports')));
     }
     if (ngModule.has('exports')) {
         injectorImports.push(new compiler_1.WrappedNodeExpr(ngModule.get('exports')));
     }
     var ngInjectorDef = {
         name: node.name.text,
         type: new compiler_1.WrappedNodeExpr(node.name),
         deps: util_1.getConstructorDependencies(node, this.reflector, this.isCore), providers: providers,
         imports: new compiler_1.LiteralArrayExpr(injectorImports),
     };
     return {
         analysis: {
             ngModuleDef: ngModuleDef, ngInjectorDef: ngInjectorDef,
         },
         factorySymbolName: node.name !== undefined ? node.name.text : undefined,
     };
 };