ts-morph#DecoratorStructure TypeScript Examples

The following examples show how to use ts-morph#DecoratorStructure. 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: configuration.ts    From cli with MIT License 6 votes vote down vote up
// 确保类拥有属性
export function ensureLifeCycleClassProperty(
  source: SourceFile,
  className: string,
  propKey: string,
  decorators: string[] = [],
  propType = 'unknown',
  apply = true
) {
  const existClassProps = getExistClassProps(source, className);

  if (existClassProps.includes(propKey)) {
    return;
  }

  const targetClass = getClassDecByName(source, 'ContainerLifeCycle');

  const applyDecorators: Array<DecoratorStructure> = decorators.map(
    decorator => ({
      name: decorator,
      kind: StructureKind.Decorator,
      arguments: [],
    })
  );

  targetClass.addProperty({
    name: propKey,
    decorators: applyDecorators,
    type: propType,
  });

  // FIXME: 生成到所有方法声明前

  apply && source.saveSync();
}