Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
NgModuleDecoratorHandler.prototype.analyze = function (node, decorator) {
var _this = this;
var _a, _b, _c, _d;
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');
(_a = this.referencesRegistry).add.apply(_a, tslib_1.__spread(declarations));
}
var imports = [];
if (ngModule.has('imports')) {
function extractQueryMetadata(exprNode, name, args, propertyName, reflector, checker) {
if (args.length === 0) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, exprNode, "@" + name + " must have arguments");
}
var first = name === 'ViewChild' || name === 'ContentChild';
var node = util_1.unwrapForwardRef(args[0], reflector);
var arg = metadata_1.staticallyResolve(node, reflector, checker);
// Extract the predicate
var predicate = null;
if (arg instanceof metadata_1.Reference) {
predicate = new compiler_1.WrappedNodeExpr(node);
}
else if (typeof arg === 'string') {
predicate = [arg];
}
else if (isStringArrayOrDie(arg, '@' + name)) {
predicate = arg;
}
else {
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');
}
templateStr = this.resourceLoader.load(templateUrl, containingFile);
}
else if (component.has('template')) {
var templateExpr = component.get('template');
var resolvedTemplate = metadata_1.staticallyResolve(templateExpr, this.reflector, this.checker);
if (typeof resolvedTemplate !== 'string') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, templateExpr, 'template must be a string');
}
templateStr = resolvedTemplate;
}
else {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.COMPONENT_MISSING_TEMPLATE, decorator.node, 'component is missing a template');
}
var preserveWhitespaces = this.defaultPreserveWhitespaces;
if (component.has('preserveWhitespaces')) {
var expr = component.get('preserveWhitespaces');
var value = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
if (typeof value !== 'boolean') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, 'preserveWhitespaces must be a boolean');
}
preserveWhitespaces = value;
}
var viewProviders = component.has('viewProviders') ?
new compiler_1.WrappedNodeExpr(component.get('viewProviders')) :
null;
// Go through the root directories for this project, and select the one with the smallest
// relative path representation.
var filePath = node.getSourceFile().fileName;
var relativeContextFilePath = this.rootDirs.reduce(function (previous, rootDir) {
var candidate = path.posix.relative(rootDir, filePath);
if (previous === undefined || candidate.length < previous.length) {
return candidate;
}
else {
return previous;
}
}, undefined);
var interpolation = compiler_1.DEFAULT_INTERPOLATION_CONFIG;
if (component.has('interpolation')) {
var expr = component.get('interpolation');
var value = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
if (!Array.isArray(value) || value.length !== 2 ||
!value.every(function (element) { return typeof element === 'string'; })) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, 'interpolation must be an array with 2 elements of string type');
}
interpolation = compiler_1.InterpolationConfig.fromArray(value);
}
var template = compiler_1.parseTemplate(templateStr, node.getSourceFile().fileName + "#" + node.name.text + "/template.html", { preserveWhitespaces: preserveWhitespaces, interpolationConfig: interpolation });
if (template.errors !== undefined) {
throw new Error("Errors parsing template: " + template.errors.map(function (e) { return e.toString(); }).join(', '));
}
// If the component has a selector, it should be registered with the `SelectorScopeRegistry` so
// when this component appears in an `@NgModule` scope, its selector can be determined.
if (metadata.selector !== null) {
var ref = new metadata_1.ResolvedReference(node, node.name);
this.scopeRegistry.registerDirective(node, tslib_1.__assign({ ref: ref, name: node.name.text, directive: ref, selector: metadata.selector, exportAs: metadata.exportAs, inputs: metadata.inputs, outputs: metadata.outputs, queries: metadata.queries.map(function (query) { return query.propertyName; }), isComponent: true }, util_1.extractDirectiveGuards(node, this.reflector)));
}
// Construct the list of view queries.
var coreModule = this.isCore ? undefined : '@angular/core';
var viewChildFromFields = directive_1.queriesFromFields(metadata_1.filterToMembersWithDecorator(decoratedElements, 'ViewChild', coreModule), this.reflector, this.checker);
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);
var typeArgumentCount = reflector.getGenericArityOfClass(clazz) || 0;
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,
typeArgumentCount: typeArgumentCount,
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.");
ComponentDecoratorHandler.prototype._extractStyleUrls = function (component) {
if (!component.has('styleUrls')) {
return null;
}
var styleUrlsExpr = component.get('styleUrls');
var styleUrls = metadata_1.staticallyResolve(styleUrlsExpr, this.reflector, this.checker);
if (!Array.isArray(styleUrls) || !styleUrls.every(function (url) { return typeof url === 'string'; })) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, styleUrlsExpr, 'styleUrls must be an array of strings');
}
return styleUrls;
};
return ComponentDecoratorHandler;
ComponentDecoratorHandler.prototype._resolveLiteral = function (decorator) {
if (this.literalCache.has(decorator)) {
return this.literalCache.get(decorator);
}
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 @Component decorator");
}
var meta = util_1.unwrapExpression(decorator.args[0]);
if (!ts.isObjectLiteralExpression(meta)) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARG_NOT_LITERAL, meta, "Decorator argument must be literal.");
}
this.literalCache.set(decorator, meta);
return meta;
};
ComponentDecoratorHandler.prototype._extractStyleUrls = function (component) {
function extractHostBindings(metadata, members, reflector, checker, coreModule) {
var hostMetadata = {};
if (metadata.has('host')) {
var expr = metadata.get('host');
var hostMetaMap = metadata_1.staticallyResolve(expr, reflector, checker);
if (!(hostMetaMap instanceof Map)) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARG_NOT_LITERAL, expr, "Decorator host metadata must be an object");
}
hostMetaMap.forEach(function (value, key) {
if (typeof value !== 'string' || typeof key !== 'string') {
throw new Error("Decorator host metadata must be a string -> string object, got " + value);
}
hostMetadata[key] = value;
});
}
var _a = compiler_1.parseHostBindings(hostMetadata), attributes = _a.attributes, listeners = _a.listeners, properties = _a.properties;
metadata_1.filterToMembersWithDecorator(members, 'HostBinding', coreModule)
.forEach(function (_a) {
var member = _a.member, decorators = _a.decorators;
decorators.forEach(function (decorator) {
var hostPropertyName = member.name;
if (decorator.args !== null && decorator.args.length > 0) {
if (decorator.args.length !== 1) {
}
if (decorator.args.length !== 1) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, '@Pipe must have exactly one argument');
}
var meta = util_1.unwrapExpression(decorator.args[0]);
if (!ts.isObjectLiteralExpression(meta)) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARG_NOT_LITERAL, meta, '@Pipe must have a literal argument');
}
var pipe = metadata_1.reflectObjectLiteral(meta);
if (!pipe.has('name')) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.PIPE_MISSING_NAME, meta, "@Pipe decorator is missing name field");
}
var pipeNameExpr = pipe.get('name');
var pipeName = metadata_1.staticallyResolve(pipeNameExpr, this.reflector, this.checker);
if (typeof pipeName !== 'string') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, pipeNameExpr, "@Pipe.name must be a string");
}
this.scopeRegistry.registerPipe(clazz, pipeName);
var pure = true;
if (pipe.has('pure')) {
var expr = pipe.get('pure');
var pureValue = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
if (typeof pureValue !== 'boolean') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, "@Pipe.pure must be a boolean");
}
pure = pureValue;
}
return {
analysis: {
meta: {
name: name,
type: type,