当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript ast-utils.getDecoratorMetadata函数代码示例

本文整理汇总了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}`);
  }
}
开发者ID:kevinheader,项目名称:nebular,代码行数:25,代码来源:routing.ts

示例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;
  }

}
开发者ID:SrgGs,项目名称:ng-zorro-antd,代码行数:56,代码来源:register-locale.ts

示例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,
  );
}
开发者ID:kevinheader,项目名称:nebular,代码行数:18,代码来源:component.ts

示例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);
}
开发者ID:kevinheader,项目名称:nebular,代码行数:23,代码来源:add-to-modules.ts


注:本文中的@schematics/angular/utility/ast-utils.getDecoratorMetadata函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。