本文整理匯總了TypeScript中@schematics/angular/utility/ast-utils.getDecoratorMetadata函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript getDecoratorMetadata函數的具體用法?TypeScript getDecoratorMetadata怎麽用?TypeScript getDecoratorMetadata使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了getDecoratorMetadata函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: findRoutesArray
export function findRoutesArray(tree: Tree, modulePath: Path): ts.ArrayLiteralExpression {
const source = getSourceFile(tree, modulePath);
const decoratorNode = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] as ts.ObjectLiteralExpression;
if (decoratorNode == null) {
throw new SchematicsException(`Error in ${modulePath}. Can't find NgModule decorator.`);
}
try {
const imports = getImports(decoratorNode);
const routerModuleCall = getRouterModuleCall(imports);
const routesArgument = routerModuleCall.arguments[0];
if (routesArgument.kind === ts.SyntaxKind.ArrayLiteralExpression) {
return routesArgument as ts.ArrayLiteralExpression;
}
if (routesArgument.kind === ts.SyntaxKind.Identifier) {
const declaration = getRoutesVariableDeclaration(source, (routesArgument as ts.Identifier));
return declaration.initializer as ts.ArrayLiteralExpression;
}
throw new SchematicsException(`Expecting RouterModule.forChild parameter to be an array or variable identifier.`);
} catch (e) {
throw new SchematicsException(`Error in ${modulePath}. ${e.message}`);
}
}
示例2: insertI18nTokenProvide
function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string, locale: string): Change[] {
const metadataField = 'providers';
const nodes = getDecoratorMetadata(moduleSource, 'NgModule', '@angular/core');
const addProvide = addSymbolToNgModuleMetadata(moduleSource, modulePath, 'providers',
`{ provide: NZ_I18N, useValue: ${locale} }`, null);
let node: any = nodes[ 0 ]; // tslint:disable-line:no-any
if (!node) {
return [];
}
const matchingProperties: ts.ObjectLiteralElement[] =
(node as ts.ObjectLiteralExpression).properties
.filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
.filter((prop: ts.PropertyAssignment) => {
const name = prop.name;
switch (name.kind) {
case ts.SyntaxKind.Identifier:
return (name as ts.Identifier).getText(moduleSource) === metadataField;
case ts.SyntaxKind.StringLiteral:
return (name as ts.StringLiteral).text === metadataField;
}
return false;
});
if (!matchingProperties) {
return [];
}
if (matchingProperties.length) {
const assignment = matchingProperties[ 0 ] as ts.PropertyAssignment;
if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return [];
}
const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
if (arrLiteral.elements.length === 0) {
return addProvide;
} else {
node = arrLiteral.elements.filter(e => e.getText && e.getText().includes('NZ_I18N'));
if (node.length === 0) {
return addProvide;
} else {
console.log();
console.log(chalk.yellow(`Could not provide the locale token to your app.module file (${chalk.blue(modulePath)}).` +
`because there is already a locale token in provides.`));
console.log(chalk.yellow(`Please manually add the following code to your provides:`));
console.log(chalk.cyan(`{ provide: NZ_I18N, useValue: ${locale} }`));
return [];
}
}
} else {
return addProvide;
}
}
示例3: getComponentTemplateDescriptor
export function getComponentTemplateDescriptor(host: Tree, componentPath: string): TemplateDescriptor {
const compSource: ts.SourceFile = getSourceFile(host, componentPath);
const compMetadata: ts.Node = getDecoratorMetadata(compSource, 'Component', '@angular/core')[0];
const templateProp = getMetadataProperty(compMetadata, 'template');
const templateUrlProp = getMetadataProperty(compMetadata, 'templateUrl');
const template: string = getComponentTemplate(host, componentPath, {
templateProp,
templateUrlProp,
});
return new TemplateDescriptor(
templateProp,
templateUrlProp,
componentPath,
template,
);
}
示例4: multilineDeclarationsArray
function multilineDeclarationsArray(tree: Tree, modulePath: Path): void {
const source = getSourceFile(tree, modulePath);
const decoratorNode = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] as ts.ObjectLiteralExpression;
if (!decoratorNode) {
throw new SchematicsException(`Can't find NgModule decorator in ${modulePath}`);
}
const declarationsNode = decoratorNode.properties
.filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
.find((prop: ts.PropertyAssignment) => prop.name.getText() === 'declarations') as ts.PropertyAssignment;
if (!declarationsNode) {
return;
}
if (declarationsNode.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
throw new SchematicsException(`Error in ${modulePath}. Expecting declarations to be an array.`);
}
const declarationsArray = declarationsNode.initializer as ts.ArrayLiteralExpression;
const replaces = multilineArrayLiteral(source.getFullText(), declarationsArray);
applyReplaceChange(tree, modulePath, ...replaces);
}