• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript config.getConfig函数代码示例发布时间:2022-05-28
下一篇:
TypeScript ast-utils.addImportToModule函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap