mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Deduplicate code in member classes (#56086)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56086 Changelog: [Internal] Deduplicates specialization_args, qualified names and function pointer check in the member classes. Reviewed By: cipolleschi Differential Revision: D96455789 fbshipit-source-id: 835b73406d4469808b3d7fec7601394f7e42313b
This commit is contained in:
1 parent
f0b51fc6bf
commit
d596a3d2f3
4 files changed
+31
-36
No files matched your search
@@ -32,11 +32,17 @@ class MemberKind(IntEnum):
|
||||
FRIEND = 6
|
||||
|
||||
|
||||
def is_function_pointer_argstring(argstring: str | None) -> bool:
|
||||
"""Check if an argstring indicates a function pointer type."""
|
||||
return argstring is not None and argstring.startswith(")(")
|
||||
|
||||
|
||||
class Member(ABC):
|
||||
def __init__(self, name: str, visibility: str) -> None:
|
||||
self.name: str = name
|
||||
self.visibility: str = visibility
|
||||
self.template_list: TemplateList | None = None
|
||||
self.specialization_args: list[str] | None = None
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
@@ -55,8 +61,19 @@ class Member(ABC):
|
||||
def close(self, scope: Scope):
|
||||
pass
|
||||
|
||||
def _get_qualified_name(self, qualification: str | None):
|
||||
return f"{qualification}::{self.name}" if qualification else self.name
|
||||
def _get_qualified_name(self, qualification: str | None) -> str:
|
||||
name = self.name
|
||||
if self.specialization_args is not None:
|
||||
name = f"{name}<{', '.join(self.specialization_args)}>"
|
||||
return f"{qualification}::{name}" if qualification else name
|
||||
|
||||
def _qualify_specialization_args(self, scope: Scope) -> None:
|
||||
if self.specialization_args is not None:
|
||||
from ..utils import qualify_type_str
|
||||
|
||||
self.specialization_args = [
|
||||
qualify_type_str(arg, scope) for arg in self.specialization_args
|
||||
]
|
||||
|
||||
def add_template(self, template: Template | [Template]) -> None:
|
||||
if template and self.template_list is None:
|
||||
|
||||
@@ -64,16 +64,7 @@ class FunctionMember(Member):
|
||||
def close(self, scope: Scope):
|
||||
self.type = qualify_type_str(self.type, scope)
|
||||
self.arguments = qualify_arguments(self.arguments, scope)
|
||||
if self.specialization_args is not None:
|
||||
self.specialization_args = [
|
||||
qualify_type_str(arg, scope) for arg in self.specialization_args
|
||||
]
|
||||
|
||||
def _get_qualified_name(self, qualification: str | None) -> str:
|
||||
name = self.name
|
||||
if self.specialization_args is not None:
|
||||
name = f"{name}<{', '.join(self.specialization_args)}>"
|
||||
return f"{qualification}::{name}" if qualification else name
|
||||
self._qualify_specialization_args(scope)
|
||||
|
||||
def to_string(
|
||||
self,
|
||||
|
||||
@@ -16,7 +16,7 @@ from ..utils import (
|
||||
qualify_arguments,
|
||||
qualify_parsed_type,
|
||||
)
|
||||
from .base import Member, MemberKind
|
||||
from .base import is_function_pointer_argstring, Member, MemberKind
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..scope import Scope
|
||||
@@ -48,14 +48,10 @@ class TypedefMember(Member):
|
||||
self._fp_arguments = qualify_arguments(self._fp_arguments, scope)
|
||||
self._parsed_type = qualify_parsed_type(self._parsed_type, scope)
|
||||
|
||||
def _is_function_pointer(self) -> bool:
|
||||
"""Check if this typedef is a function pointer type."""
|
||||
return self.argstring is not None and self.argstring.startswith(")(")
|
||||
|
||||
def get_value(self) -> str:
|
||||
if self.keyword == "using":
|
||||
return format_parsed_type(self._parsed_type)
|
||||
elif self._is_function_pointer():
|
||||
elif is_function_pointer_argstring(self.argstring):
|
||||
formatted_args = format_arguments(self._fp_arguments)
|
||||
qualified_type = format_parsed_type(self._parsed_type)
|
||||
if "(*" in qualified_type:
|
||||
@@ -84,7 +80,7 @@ class TypedefMember(Member):
|
||||
|
||||
if self.keyword == "using":
|
||||
result += f" {name} = {format_parsed_type(self._parsed_type)};"
|
||||
elif self._is_function_pointer():
|
||||
elif is_function_pointer_argstring(self.argstring):
|
||||
formatted_args = format_arguments(self._fp_arguments)
|
||||
qualified_type = format_parsed_type(self._parsed_type)
|
||||
# Function pointer typedef: "typedef return_type (*name)(args);"
|
||||
|
||||
@@ -15,10 +15,14 @@ from ..utils import (
|
||||
parse_type_with_argstrings,
|
||||
qualify_arguments,
|
||||
qualify_parsed_type,
|
||||
qualify_type_str,
|
||||
split_specialization,
|
||||
)
|
||||
from .base import Member, MemberKind, STORE_INITIALIZERS_IN_SNAPSHOT
|
||||
from .base import (
|
||||
is_function_pointer_argstring,
|
||||
Member,
|
||||
MemberKind,
|
||||
STORE_INITIALIZERS_IN_SNAPSHOT,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..scope import Scope
|
||||
@@ -65,20 +69,7 @@ class VariableMember(Member):
|
||||
def close(self, scope: Scope):
|
||||
self._fp_arguments = qualify_arguments(self._fp_arguments, scope)
|
||||
self._parsed_type = qualify_parsed_type(self._parsed_type, scope)
|
||||
if self.specialization_args is not None:
|
||||
self.specialization_args = [
|
||||
qualify_type_str(arg, scope) for arg in self.specialization_args
|
||||
]
|
||||
|
||||
def _is_function_pointer(self) -> bool:
|
||||
"""Check if this variable is a function pointer type."""
|
||||
return self.argstring is not None and self.argstring.startswith(")(")
|
||||
|
||||
def _get_qualified_name(self, qualification: str | None) -> str:
|
||||
name = self.name
|
||||
if self.specialization_args is not None:
|
||||
name = f"{name}<{', '.join(self.specialization_args)}>"
|
||||
return f"{qualification}::{name}" if qualification else name
|
||||
self._qualify_specialization_args(scope)
|
||||
|
||||
def to_string(
|
||||
self,
|
||||
@@ -108,7 +99,7 @@ class VariableMember(Member):
|
||||
if self.is_const and not self.is_constexpr:
|
||||
result += "const "
|
||||
|
||||
if self._is_function_pointer():
|
||||
if is_function_pointer_argstring(self.argstring):
|
||||
formatted_args = format_arguments(self._fp_arguments)
|
||||
qualified_type = format_parsed_type(self._parsed_type)
|
||||
# Function pointer types: argstring is ")(args...)"
|
||||
|
||||
Reference in new issue
Block a user