ts-morph#PropertyDeclaration TypeScript Examples

The following examples show how to use ts-morph#PropertyDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: class.ts    From cli with MIT License 6 votes vote down vote up
// 获取类的属性声明,可根据属性名查找
export function getExistClassPropDeclarations(
  source: SourceFile,
  className: string,
  propName?: string
): PropertyDeclaration | PropertyDeclaration[] {
  const classDeclarations = source
    .getFirstChildByKind(SyntaxKind.SyntaxList)
    .getChildrenOfKind(SyntaxKind.ClassDeclaration);

  const targetClass = classDeclarations.filter(
    classDecs =>
      classDecs.getFirstChildByKind(SyntaxKind.Identifier).getText() ===
      className
  );

  if (!targetClass.length) {
    return;
  }

  const targetClassItem = targetClass[0];

  const props = targetClassItem.getProperties();

  if (propName) {
    return props.filter(
      m => m.getFirstChildByKind(SyntaxKind.Identifier).getText() === propName
    )[0];
  } else {
    return props;
  }
}
Example #2
Source File: helpers.ts    From prisma-nestjs-graphql with MIT License 6 votes vote down vote up
export function getFieldOptions(
  sourceFile: SourceFile,
  property: string | PropertyDeclaration,
) {
  let propertyDeclaration: PropertyDeclaration | undefined;
  if (typeof property === 'string') {
    propertyDeclaration = sourceFile.getClass(() => true)?.getProperty(property);
  }
  const result = propertyDeclaration?.getStructure()?.decorators?.[0]?.arguments?.[1];
  return result as string;
  // return new Function(`return ${text}`)();
}
Example #3
Source File: generate.spec.ts    From prisma-nestjs-graphql with MIT License 5 votes vote down vote up
describe('emit single and decorators', () => {
  before(async () => {
    ({ project, sourceFiles } = await testGenerate({
      schema: `
                model User {
                  id    Int    @id
                  /// @Validator.MinLength(3)
                  name String
                  /// @PropertyType({ name: 'G.Email', from: 'graphql-type-email', input: true })
                  email String?
                }
                `,
      options: [
        `emitSingle = true`,
        `outputFilePattern = "{name}.{type}.ts"`,
        `fields_Validator_from = "class-validator"`,
        `fields_Validator_input = true`,
      ],
    }));
    setSourceFile('index.ts');
  });

  it('should contain custom decorator import', () => {
    const importDeclaration = importDeclarations.find(
      x => x.moduleSpecifier === 'graphql-type-email',
    );
    expect(importDeclaration).toEqual(
      expect.objectContaining({
        namespaceImport: 'G',
      }),
    );
  });

  it('validator namespace for name should be imported', () => {
    expect(importDeclarations).toContainEqual(
      expect.objectContaining({
        namespaceImport: 'Validator',
      }),
    );
  });

  describe('user create input name', () => {
    let property: PropertyDeclaration;
    before(() => {
      property = sourceFile.getClass('UserCreateInput')?.getProperty('name')!;
    });

    it('decorator validator', () => {
      const d = property.getDecorator(d => d.getFullText().includes('MinLength'));
      expect(trim(d?.getFullText())).toEqual('@Validator.MinLength(3)');
    });
  });
});