Split scope definitions into smaller files (#56073)

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

Changelog: [Internal]

Splits `scope.py` into multiple smaller files, each containing a separate defitnition.

Reviewed By: cipolleschi

Differential Revision: D96284004

fbshipit-source-id: f99275cc9b5f8844f9a14f9d6207ae988aa9ccd7
This commit is contained in:
Dawid Małecki authored and meta-codesync[bot] committed 2026-03-18 09:29:02 -07:00
1 parent 756203cb27
commit 62ce98db99
10 files changed
+446 -296

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
# 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 .base_scope_kind import ScopeKind, ScopeKindT
from .category_scope_kind import CategoryScopeKind
from .enum_scope_kind import EnumScopeKind
from .interface_scope_kind import InterfaceScopeKind
from .namespace_scope_kind import NamespaceScopeKind
from .protocol_scope_kind import ProtocolScopeKind
from .scope import Scope
from .struct_like_scope_kind import StructLikeScopeKind
from .temporary_scope_kind import TemporaryScopeKind
__all__ = [
"CategoryScopeKind",
"EnumScopeKind",
"InterfaceScopeKind",
"NamespaceScopeKind",
"ProtocolScopeKind",
"Scope",
"ScopeKind",
"ScopeKindT",
"StructLikeScopeKind",
"TemporaryScopeKind",
]
@@ -0,0 +1,36 @@
# 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
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, TypeVar
from natsort import natsort_keygen
if TYPE_CHECKING:
from .scope import Scope
# Pre-create natsort key function for efficiency
_natsort_key = natsort_keygen()
class ScopeKind(ABC):
def __init__(self, name) -> None:
self.name: str = name
@abstractmethod
def to_string(self, scope: Scope) -> str:
pass
def close(self, scope: Scope) -> None:
"""Called when the scope is closed. Override to perform cleanup."""
pass
def print_scope(self, scope: Scope) -> None:
print(self.to_string(scope))
ScopeKindT = TypeVar("ScopeKindT", bound=ScopeKind)
@@ -0,0 +1,37 @@
# 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
from typing import TYPE_CHECKING
from natsort import natsorted
from .base_scope_kind import ScopeKind
if TYPE_CHECKING:
from .scope import Scope
class CategoryScopeKind(ScopeKind):
def __init__(self, class_name: str, category_name: str) -> None:
super().__init__("category")
self.class_name: str = class_name
self.category_name: str = category_name
def to_string(self, scope: Scope) -> str:
result = f"{self.name} {self.class_name}({self.category_name}) {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
@@ -0,0 +1,42 @@
# 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
from typing import TYPE_CHECKING
from natsort import natsorted
from .base_scope_kind import ScopeKind
if TYPE_CHECKING:
from .scope import Scope
class EnumScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("enum")
self.type: str | None = None
def to_string(self, scope: Scope) -> str:
result = ""
inheritance_string = f" : {self.type}" if self.type else ""
result += (
"\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
)
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2) + ",")
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
@@ -0,0 +1,72 @@
# 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
from typing import TYPE_CHECKING
from natsort import natsorted
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
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
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)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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 ""
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
@@ -0,0 +1,40 @@
# 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
from typing import TYPE_CHECKING
from natsort import natsorted
from ..member import MemberKind
from .base_scope_kind import ScopeKind
if TYPE_CHECKING:
from .scope import Scope
class NamespaceScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("namespace")
def to_string(self, scope: Scope) -> str:
qualification = scope.get_qualified_name()
# Group members by kind
groups: dict[MemberKind, list[str]] = {kind: [] for kind in MemberKind}
for member in scope.get_members():
kind = member.member_kind
stringified = member.to_string(0, qualification, hide_visibility=True)
groups[kind].append(stringified)
# Sort within each group and combine in kind order
result = []
for kind in MemberKind:
sorted_group = natsorted(groups[kind])
result.extend(sorted_group)
return "\n".join(result)
@@ -0,0 +1,70 @@
# 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
from typing import TYPE_CHECKING
from natsort import natsorted
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
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
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)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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 ""
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
@@ -5,304 +5,16 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from enum import Enum
from typing import Generic, TypeVar
from typing import Generic
from natsort import natsort_keygen, natsorted
from natsort import natsorted
from .member import FriendMember, Member, MemberKind, TypedefMember
from .template import Template, TemplateList
from .utils import parse_qualified_path, qualify_template_args_only, qualify_type_str
# Pre-create natsort key function for efficiency
_natsort_key = natsort_keygen()
class ScopeKind(ABC):
def __init__(self, name) -> None:
self.name: str = name
@abstractmethod
def to_string(self, scope: Scope) -> str:
pass
def close(self, scope: Scope) -> None:
"""Called when the scope is closed. Override to perform cleanup."""
pass
def print_scope(self, scope: Scope) -> None:
print(self.to_string(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 Type(Enum):
CLASS = "class"
STRUCT = "struct"
UNION = "union"
def __init__(self, type: Type) -> None:
super().__init__(type.value)
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()
if isinstance(template, list):
for t in template:
self.template_list.add(t)
else:
self.template_list.add(template)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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"
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
class NamespaceScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("namespace")
def to_string(self, scope: Scope) -> str:
qualification = scope.get_qualified_name()
# Group members by kind
groups: dict[MemberKind, list[str]] = {kind: [] for kind in MemberKind}
for member in scope.get_members():
kind = member.member_kind
stringified = member.to_string(0, qualification, hide_visibility=True)
groups[kind].append(stringified)
# Sort within each group and combine in kind order
result = []
for kind in MemberKind:
sorted_group = natsorted(groups[kind])
result.extend(sorted_group)
return "\n".join(result)
class EnumScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("enum")
self.type: str | None = None
def to_string(self, scope: Scope) -> str:
result = ""
inheritance_string = f" : {self.type}" if self.type else ""
result += (
"\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
)
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2) + ",")
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
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
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)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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 ""
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
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
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)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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 ""
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
class CategoryScopeKind(ScopeKind):
def __init__(self, class_name: str, category_name: str) -> None:
super().__init__("category")
self.class_name: str = class_name
self.category_name: str = category_name
def to_string(self, scope: Scope) -> str:
result = f"{self.name} {self.class_name}({self.category_name}) {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
class TemporaryScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("temporary")
def to_string(self, scope: Scope) -> str:
raise RuntimeError("Temporary scope should not be printed")
ScopeKindT = TypeVar("ScopeKindT", bound=ScopeKind)
from ..member import FriendMember, Member, TypedefMember
from ..utils import parse_qualified_path, qualify_template_args_only
from .base_scope_kind import _natsort_key, ScopeKindT
from .enum_scope_kind import EnumScopeKind
from .namespace_scope_kind import NamespaceScopeKind
from .struct_like_scope_kind import StructLikeScopeKind
class Scope(Generic[ScopeKindT]):
@@ -0,0 +1,93 @@
# 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
from enum import Enum
from typing import TYPE_CHECKING
from natsort import natsorted
from ..template import Template, TemplateList
from ..utils import qualify_type_str
from .base_scope_kind import ScopeKind
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 Type(Enum):
CLASS = "class"
STRUCT = "struct"
UNION = "union"
def __init__(self, type: Type) -> None:
super().__init__(type.value)
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()
if isinstance(template, list):
for t in template:
self.template_list.add(t)
else:
self.template_list.add(template)
def close(self, scope: Scope) -> None:
"""Qualify base class names and their template arguments."""
for base in self.base_classes:
base.name = qualify_type_str(base.name, scope)
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"
result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
stringified_members = []
for member in scope.get_members():
stringified_members.append(member.to_string(2))
stringified_members = natsorted(stringified_members)
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
stringified_members
)
result += "\n}"
return result
@@ -0,0 +1,21 @@
# 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
from typing import TYPE_CHECKING
from .base_scope_kind import ScopeKind
if TYPE_CHECKING:
from .scope import Scope
class TemporaryScopeKind(ScopeKind):
def __init__(self) -> None:
super().__init__("temporary")
def to_string(self, scope: Scope) -> str:
raise RuntimeError("Temporary scope should not be printed")