createViewClass function

ClassStmt createViewClass(
  1. CompileView view,
  2. ExpressionParser parser
)

Generates a class AST for a view.

Implementation

o.ClassStmt createViewClass(CompileView view, ExpressionParser parser) {
  final viewConstructor = _createViewConstructor(view);
  final viewMethods = [
    o.ClassMethod(
      'build',
      [],
      _generateBuildMethod(view, parser),
      null,
      null,
      [o.importExpr(Identifiers.dartCoreOverride)],
    ),
    view.writeInjectorGetMethod(),
    if (view.component.isChangeDetectionLink)
      o.ClassMethod(
        'detectChangesInCheckAlwaysViews',
        [],
        view.writeCheckAlwaysChangeDetectionStatements(),
        null,
        null,
        [o.importExpr(Identifiers.dartCoreOverride)],
      ),
    o.ClassMethod(
      'detectChangesInternal',
      [],
      view.writeChangeDetectionStatements(),
      null,
      null,
      [o.importExpr(Identifiers.dartCoreOverride)],
    ),
    if (view.viewType == ViewType.embedded)
      o.ClassMethod(
        'dirtyParentQueriesInternal',
        [],
        view.dirtyParentQueriesMethod.finish(),
        null,
        null,
        [o.importExpr(Identifiers.dartCoreOverride)],
      ),
    o.ClassMethod(
      'destroyInternal',
      [],
      _generateDestroyMethod(view),
      null,
      null,
      [o.importExpr(Identifiers.dartCoreOverride)],
    ),
    ...view.methods,
  ];
  if (view.viewType == ViewType.component &&
      view.detectHostChangesMethod != null) {
    final methodStatements = view.detectHostChangesMethod!.finish();
    viewMethods.add(
      o.ClassMethod(
        'detectHostChanges',
        [o.FnParam(DetectChangesVars.firstCheck.name!, o.BOOL_TYPE)],
        [
          ...maybeCachedCtxDeclarationStatement(statements: methodStatements),
          ...methodStatements,
        ],
      ),
    );
  }
  for (final method in viewMethods) {
    NodeReferenceStorageVisitor.visitScopedStatements(
      method.body.whereType<o.Statement>().toList(),
    );
  }
  for (final getter in view.getters) {
    NodeReferenceStorageVisitor.visitScopedStatements(getter.body);
  }
  final viewClass = o.ClassStmt(
    view.className,
    _createParentClassExpr(view),
    view.storage.fields,
    view.getters,
    viewConstructor,
    viewMethods.where((method) => method.body.isNotEmpty).toList(),
    typeParameters: view.component.originType!.typeParameters,
  );
  initStyleEncapsulation(view, viewClass);
  return viewClass;
}