Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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')) {
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');
(_b = this.referencesRegistry).add.apply(_b, tslib_1.__spread(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');
(_c = this.referencesRegistry).add.apply(_c, tslib_1.__spread(exports));
}
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;
}
var resolved = metadata_1.staticallyResolve(expr, reflector, checker);
if (typeof resolved !== 'string') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, "selector must be a string");
}
selector = resolved;
}
var host = extractHostBindings(directive, decoratedElements, reflector, checker, coreModule);
var providers = directive.has('providers') ? new compiler_1.WrappedNodeExpr(directive.get('providers')) : null;
// Determine if `ngOnChanges` is a lifecycle hook defined on the component.
var usesOnChanges = members.some(function (member) { return !member.isStatic && member.kind === host_1.ClassMemberKind.Method &&
member.name === 'ngOnChanges'; });
// Parse exportAs.
var exportAs = null;
if (directive.has('exportAs')) {
var expr = directive.get('exportAs');
var resolved = metadata_1.staticallyResolve(expr, reflector, checker);
if (typeof resolved !== 'string') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, "exportAs must be a string");
}
exportAs = resolved;
}
// Detect if the component inherits from another class
var usesInheritance = clazz.heritageClauses !== undefined &&
clazz.heritageClauses.some(function (hc) { return hc.token === ts.SyntaxKind.ExtendsKeyword; });
var metadata = {
name: clazz.name.text,
deps: util_1.getConstructorDependencies(clazz, reflector, isCore), host: host,
lifecycle: {
usesOnChanges: usesOnChanges,
},
inputs: tslib_1.__assign({}, inputsFromMeta, inputsFromFields),
outputs: tslib_1.__assign({}, outputsFromMeta, outputsFromFields), queries: queries, selector: selector,
viewQueries.push.apply(viewQueries, tslib_1.__spread(queriesFromDecorator.view));
}
var styles = null;
if (component.has('styles')) {
styles = directive_1.parseFieldArrayValue(component, 'styles', this.reflector, this.checker);
}
var styleUrls = this._extractStyleUrls(component);
if (styleUrls !== null) {
if (styles === null) {
styles = [];
}
styles.push.apply(styles, tslib_1.__spread(styleUrls.map(function (styleUrl) { return _this.resourceLoader.load(styleUrl, containingFile); })));
}
var encapsulation = 0;
if (component.has('encapsulation')) {
encapsulation = parseInt(metadata_1.staticallyResolve(component.get('encapsulation'), this.reflector, this.checker));
}
var animations = null;
if (component.has('animations')) {
animations = new compiler_1.WrappedNodeExpr(component.get('animations'));
}
return {
analysis: {
meta: tslib_1.__assign({}, metadata, { template: template,
viewQueries: viewQueries,
encapsulation: encapsulation,
interpolation: interpolation, styles: styles || [],
// These will be replaced during the compilation step, after all `NgModule`s have been
// analyzed and the full compilation scope for the component can be realized.
pipes: EMPTY_MAP, directives: EMPTY_ARRAY, wrapDirectivesAndPipesInClosure: false, //
animations: animations,
viewProviders: viewProviders, i18nUseExternalIds: this.i18nUseExternalIds, relativeContextFilePath: relativeContextFilePath }),
field.decorators.forEach(function (decorator) {
// The decorator either doesn't have an argument (@Input()) in which case the property
// name is used, or it has one argument (@Output('named')).
if (decorator.args == null || decorator.args.length === 0) {
results[fieldName] = fieldName;
}
else if (decorator.args.length === 1) {
var property = metadata_1.staticallyResolve(decorator.args[0], reflector, checker);
if (typeof property !== 'string') {
throw new Error("Decorator argument must resolve to a string");
}
results[fieldName] = mapValueResolver(property, fieldName);
}
else {
// Too many arguments.
throw new Error("Decorator must have 0 or 1 arguments, got " + decorator.args.length + " argument(s)");
}
});
return results;
}
// Extract the read and descendants options.
var read = null;
// The default value for descendants is true for every decorator except @ContentChildren.
var descendants = name !== 'ContentChildren';
if (args.length === 2) {
var optionsExpr = util_1.unwrapExpression(args[1]);
if (!ts.isObjectLiteralExpression(optionsExpr)) {
throw new Error("@" + name + " options must be an object literal");
}
var options = metadata_1.reflectObjectLiteral(optionsExpr);
if (options.has('read')) {
read = new compiler_1.WrappedNodeExpr(options.get('read'));
}
if (options.has('descendants')) {
var descendantsValue = metadata_1.staticallyResolve(options.get('descendants'), reflector, checker);
if (typeof descendantsValue !== 'boolean') {
throw new Error("@" + name + " options.descendants must be a boolean");
}
descendants = descendantsValue;
}
}
else if (args.length > 2) {
// Too many arguments.
throw new Error("@" + name + " has too many arguments");
}
return {
propertyName: propertyName, predicate: predicate, first: first, descendants: descendants, read: read,
};
}
exports.extractQueryMetadata = extractQueryMetadata;
this.literalCache.delete(decorator);
// @Component inherits @Directive, so begin by extracting the @Directive metadata and building
// on it.
var directiveResult = directive_1.extractDirectiveMetadata(node, decorator, this.checker, this.reflector, this.isCore, this.elementSchemaRegistry.getDefaultComponentElementName());
if (directiveResult === undefined) {
// `extractDirectiveMetadata` returns undefined when the @Directive has `jit: true`. In this
// case, compilation of the decorator is skipped. Returning an empty object signifies
// that no analysis was produced.
return {};
}
// Next, read the `@Component`-specific fields.
var decoratedElements = directiveResult.decoratedElements, component = directiveResult.decorator, metadata = directiveResult.metadata;
var templateStr = null;
if (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');
}
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');
}
decorators.forEach(function (decorator) {
var eventName = member.name;
var args = [];
if (decorator.args !== null && decorator.args.length > 0) {
if (decorator.args.length > 2) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.DECORATOR_ARITY_WRONG, decorator.args[2], "@HostListener() can have at most two arguments");
}
var resolved = metadata_1.staticallyResolve(decorator.args[0], reflector, checker);
if (typeof resolved !== 'string') {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, decorator.args[0], "@HostListener()'s event name argument must be a string");
}
eventName = resolved;
if (decorator.args.length === 2) {
var resolvedArgs = metadata_1.staticallyResolve(decorator.args[1], reflector, checker);
if (!isStringArrayOrDie(resolvedArgs, '@HostListener.args')) {
throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, decorator.args[1], "@HostListener second argument must be a string array");
}
args = resolvedArgs;
}
}
listeners[eventName] = member.name + "(" + args.join(',') + ")";
});
});