Move inheritance logic to a separate class (#56074)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/56074

Changelog: [Internal]

Extracts the duplicated handling of inheritance and base classes to a separate, reusable class.

Reviewed By: cipolleschi

Differential Revision: D96287394

fbshipit-source-id: 6075b5dafb754167b6f78abbaee60c1243c99bb5
This commit is contained in:
Dawid Małecki authored and meta-codesync[bot] committed 2026-03-18 09:29:02 -07:00
1 parent 62ce98db99
commit 4391a5f4b0
4 files changed
+54 -91

No files matched your search

@@ -0,0 +1,38 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations
class Extendable:
class Base:
def __init__(
self, name: str, protection: str, virtual: bool, refid: str
) -> None:
self.name: str = name
self.protection: str = protection
self.virtual: bool = virtual
self.refid: str = refid
def __init__(self) -> None:
self.base_classes = []
def add_base(self, base: Base | list[Base]) -> None:
if isinstance(base, list):
for b in base:
self.base_classes.append(b)
else:
self.base_classes.append(base)
def get_inheritance_string(self) -> str:
bases = []
for base in self.base_classes:
base_text = [base.protection]
if base.virtual:
base_text.append("virtual")
base_text.append(base.name)
bases.append(" ".join(base_text))
return (" : " + ", ".join(bases)) if bases else ""
@@ -11,33 +11,16 @@ from natsort import natsorted
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
from .extendable import Extendable
if TYPE_CHECKING:
from .scope import Scope
class InterfaceScopeKind(ScopeKind):
class Base:
def __init__(
self, name: str, protection: str, virtual: bool, refid: str
) -> None:
self.name: str = name
self.protection: str = protection
self.virtual: bool = virtual
self.refid: str = refid
class InterfaceScopeKind(ScopeKind, Extendable):
def __init__(self) -> None:
super().__init__("interface")
self.base_classes: [InterfaceScopeKind.Base] = []
def add_base(
self, base: InterfaceScopeKind.Base | [InterfaceScopeKind.Base]
) -> None:
if isinstance(base, list):
for b in base:
self.base_classes.append(b)
else:
self.base_classes.append(base)
ScopeKind.__init__(self, "interface")
Extendable.__init__(self)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
@@ -47,16 +30,7 @@ class InterfaceScopeKind(ScopeKind):
def to_string(self, scope: Scope) -> str:
result = ""
bases = []
for base in self.base_classes:
base_text = [base.protection]
if base.virtual:
base_text.append("virtual")
base_text.append(base.name)
bases.append(" ".join(base_text))
inheritance_string = " : " + ", ".join(bases) if bases else ""
inheritance_string = self.get_inheritance_string()
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
@@ -11,31 +11,16 @@ from natsort import natsorted
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
from .extendable import Extendable
if TYPE_CHECKING:
from .scope import Scope
class ProtocolScopeKind(ScopeKind):
class Base:
def __init__(
self, name: str, protection: str, virtual: bool, refid: str
) -> None:
self.name: str = name
self.protection: str = protection
self.virtual: bool = virtual
self.refid: str = refid
class ProtocolScopeKind(ScopeKind, Extendable):
def __init__(self) -> None:
super().__init__("protocol")
self.base_classes: [ProtocolScopeKind.Base] = []
def add_base(self, base: ProtocolScopeKind.Base | [ProtocolScopeKind.Base]) -> None:
if isinstance(base, list):
for b in base:
self.base_classes.append(b)
else:
self.base_classes.append(base)
ScopeKind.__init__(self, "protocol")
Extendable.__init__(self)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
@@ -45,16 +30,7 @@ class ProtocolScopeKind(ScopeKind):
def to_string(self, scope: Scope) -> str:
result = ""
bases = []
for base in self.base_classes:
base_text = [base.protection]
if base.virtual:
base_text.append("virtual")
base_text.append(base.name)
bases.append(" ".join(base_text))
inheritance_string = " : " + ", ".join(bases) if bases else ""
inheritance_string = self.get_inheritance_string()
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
@@ -13,41 +13,24 @@ from natsort import natsorted
from ..template import Template, TemplateList
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
from .extendable import Extendable
if TYPE_CHECKING:
from .scope import Scope
class StructLikeScopeKind(ScopeKind):
class Base:
def __init__(
self, name: str, protection: str, virtual: bool, refid: str
) -> None:
self.name: str = name
self.protection: str = protection
self.virtual: bool = virtual
self.refid: str = refid
class StructLikeScopeKind(ScopeKind, Extendable):
class Type(Enum):
CLASS = "class"
STRUCT = "struct"
UNION = "union"
def __init__(self, type: Type) -> None:
super().__init__(type.value)
ScopeKind.__init__(self, type.value)
Extendable.__init__(self)
self.base_classes: [StructLikeScopeKind.Base] = []
self.template_list: TemplateList | None = None
def add_base(
self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base]
) -> None:
if isinstance(base, list):
for b in base:
self.base_classes.append(b)
else:
self.base_classes.append(base)
def add_template(self, template: Template | [Template]) -> None:
if template and self.template_list is None:
self.template_list = TemplateList()
@@ -66,18 +49,10 @@ class StructLikeScopeKind(ScopeKind):
def to_string(self, scope: Scope) -> str:
result = ""
bases = []
for base in self.base_classes:
base_text = [base.protection]
if base.virtual:
base_text.append("virtual")
base_text.append(base.name)
bases.append(" ".join(base_text))
inheritance_string = " : " + ", ".join(bases) if bases else ""
if self.template_list is not None:
result += "\n" + self.template_list.to_string() + "\n"
inheritance_string = self.get_inheritance_string()
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []