How to use the @schematics/angular/utility/ast-utils.getDecoratorMetadata function in @schematics/angular

To help you get started, we’ve selected a few @schematics/angular examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NG-ZORRO / ng-zorro-antd / schematics / ng-add / setup-project / register-locale.ts View on Github external
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;
github NativeScript / nativescript-schematics / src / ast-utils.ts View on Github external
export function addSymbolToDecoratorMetadata(
  source: ts.SourceFile,
  componentPath: string,
  metadataField: string,
  symbolName: string,
  decoratorName: string,
  decoratorPackage: string,
): Change[] {
  const nodes = getDecoratorMetadata(source, decoratorName, decoratorPackage);
  let node: any = nodes[0];  // tslint:disable-line:no-any

  // Find the decorator declaration.
  if (!node) {
    return [];
  }

  // Get all the children property assignment of object literals.
  const matchingProperties: ts.ObjectLiteralElement[] =
    (node as ts.ObjectLiteralExpression).properties
    .filter(prop => prop.kind == ts.SyntaxKind.PropertyAssignment)
    // Filter out every fields that's not 'metadataField'. Also handles string literals
    // (but not expressions).
    .filter((prop: ts.PropertyAssignment) => {
      const name = prop.name;
      switch (name.kind) {
github angular / universal / modules / common / schematics / migrations / update-9 / index.ts View on Github external
s.moduleSpecifier &&
          ts.isStringLiteral(s.moduleSpecifier) &&
          s.moduleSpecifier.text === moduleMapLoaderPackageName
        ))
        .forEach(node => {
          const index = node.getFullStart();
          const length = node.getFullWidth();
          recorder.remove(index, length);
        });


      // Create a TS printer to get the text
      const printer = ts.createPrinter();

      // Remove 'ModuleMapLoaderModule' from 'NgModule' imports
      getDecoratorMetadata(appServerSourceFile, 'NgModule', '@angular/core')
        .forEach((metadata: ts.ObjectLiteralExpression) => {
          const matchingProperties = getMetadataField(metadata, 'imports');

          if (!matchingProperties) {
            return;
          }

          const assignment = matchingProperties[0] as ts.PropertyAssignment;
          if (!ts.isArrayLiteralExpression(assignment.initializer)) {
            return;
          }

          const arrayLiteral = assignment.initializer;
          const newImports = arrayLiteral.elements
            .filter(n => !(ts.isIdentifier(n) && n.text === 'ModuleMapLoaderModule'));
github angular / components / src / material / schematics / ng-update / upgrade-rules / hammer-gestures-v9 / hammer-gestures-rule.ts View on Github external
private _setupNewGestureConfigInRootModule(project: WorkspaceProject, gestureConfigPath: string) {
    const mainFilePath = join(this.basePath, getProjectMainFile(project));
    const rootModuleSymbol = this._getRootModuleSymbol(mainFilePath);

    if (rootModuleSymbol === null) {
      this.failures.push({
        filePath: mainFilePath,
        message: `Could not setup Hammer gestures in module. Please ` +
            `manually ensure that the Hammer gesture config is set up.`,
      });
      return;
    }

    const sourceFile = rootModuleSymbol.valueDeclaration.getSourceFile();
    const relativePath = relative(this.basePath, sourceFile.fileName);
    const metadata = getDecoratorMetadata(sourceFile, 'NgModule', '@angular/core') as
        ts.ObjectLiteralExpression[];

    // If no "NgModule" definition is found inside the source file, we just do nothing.
    if (!metadata.length) {
      return;
    }

    const recorder = this.getUpdateRecorder(sourceFile.fileName);
    const providersField = getMetadataField(metadata[0], 'providers')[0];
    const providerIdentifiers =
        providersField ? findMatchingChildNodes(providersField, ts.isIdentifier) : null;
    const gestureConfigExpr = this._importManager.addImportToSourceFile(
        sourceFile, GESTURE_CONFIG_CLASS_NAME,
        getModuleSpecifier(gestureConfigPath, sourceFile.fileName), false,
        this._getGestureConfigIdentifiersOfFile(sourceFile));
    const hammerConfigTokenExpr = this._importManager.addImportToSourceFile(
github akveo / nebular / schematics / playground-module / add-to-modules.ts View on Github external
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.`);
  }
github akveo / nebular / schematics / utils / routing.ts View on Github external
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;
    }
github ng-alain / delon / packages / schematics / utils / ast.ts View on Github external
export function updateComponentMetadata(
  host: Tree,
  src: string,
  callback: (node: ts.Node) => Change[],
  propertyName?: string,
) {
  const source = getSourceFile(host, src);

  const nodes = getDecoratorMetadata(source, 'Component', '@angular/core');
  if (nodes.length === 0) return;

  const directiveMetadata = nodes[0] as ts.ObjectLiteralExpression;

  let changes: Change[] = [];
  if (propertyName) {
    const property = directiveMetadata.properties.find(p => p.name!.getText() === propertyName);
    if (property) changes = callback(property as ts.Node);
  } else {
    changes = callback(directiveMetadata);
  }

  if (changes && changes.length > 0) {
    commitChanges(host, src, changes);
  }
}