mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Move scope string generation logic to the base class (#56085)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56085 Changelog: [Internal] Moves the logic responsible for generating a string view of the scope's body to the base class to reduce duplication. Similarly, moves the logic responsible for qualifying template arguments to the `Extensible` class. Reviewed By: cipolleschi Differential Revision: D96455746 fbshipit-source-id: 1d800c7992080e02831f61fee4aaa9cb31f8fc9a
This commit is contained in:
1 parent
3a14967f81
commit
20cf9bca83
7 files changed
+36
-99
No files matched your search
@@ -8,7 +8,7 @@ from __future__ import annotations
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, TypeVar
|
||||
|
||||
from natsort import natsort_keygen
|
||||
from natsort import natsort_keygen, natsorted
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .scope import Scope
|
||||
@@ -29,6 +29,18 @@ class ScopeKind(ABC):
|
||||
"""Called when the scope is closed. Override to perform cleanup."""
|
||||
pass
|
||||
|
||||
def _format_scope_body(self, scope: Scope, member_suffix: str = "") -> str:
|
||||
"""Format the members list inside a scope's braces."""
|
||||
stringified_members = [
|
||||
member.to_string(2) + member_suffix for member in scope.get_members()
|
||||
]
|
||||
stringified_members = natsorted(stringified_members)
|
||||
result = "{"
|
||||
if stringified_members:
|
||||
result += "\n" + "\n".join(stringified_members)
|
||||
result += "\n}"
|
||||
return result
|
||||
|
||||
def print_scope(self, scope: Scope) -> None:
|
||||
print(self.to_string(scope))
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from natsort import natsorted
|
||||
|
||||
from .base_scope_kind import ScopeKind
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -22,16 +20,5 @@ class CategoryScopeKind(ScopeKind):
|
||||
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
|
||||
header = f"{self.name} {self.class_name}({self.category_name}) "
|
||||
return header + self._format_scope_body(scope)
|
||||
@@ -7,8 +7,6 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from natsort import natsorted
|
||||
|
||||
from .base_scope_kind import ScopeKind
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -21,22 +19,6 @@ class EnumScopeKind(ScopeKind):
|
||||
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
|
||||
header = f"\n{self.name} {scope.get_qualified_name()}{inheritance_string} "
|
||||
return header + self._format_scope_body(scope, member_suffix=",")
|
||||
@@ -26,6 +26,13 @@ class Extendable:
|
||||
else:
|
||||
self.base_classes.append(base)
|
||||
|
||||
def qualify_base_classes(self, scope) -> None:
|
||||
"""Qualify base class names and their template arguments."""
|
||||
from ..utils import qualify_type_str
|
||||
|
||||
for base in self.base_classes:
|
||||
base.name = qualify_type_str(base.name, scope)
|
||||
|
||||
def get_inheritance_string(self) -> str:
|
||||
bases = []
|
||||
for base in self.base_classes:
|
||||
|
||||
@@ -7,9 +7,6 @@ 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
|
||||
from .extendable import Extendable
|
||||
|
||||
@@ -23,24 +20,9 @@ class InterfaceScopeKind(ScopeKind, Extendable):
|
||||
Extendable.__init__(self)
|
||||
|
||||
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)
|
||||
self.qualify_base_classes(scope)
|
||||
|
||||
def to_string(self, scope: Scope) -> str:
|
||||
result = ""
|
||||
|
||||
inheritance_string = self.get_inheritance_string()
|
||||
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
|
||||
inheritance = self.get_inheritance_string()
|
||||
header = f"{self.name} {scope.get_qualified_name()}{inheritance} "
|
||||
return header + self._format_scope_body(scope)
|
||||
@@ -7,9 +7,6 @@ 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
|
||||
from .extendable import Extendable
|
||||
|
||||
@@ -23,24 +20,9 @@ class ProtocolScopeKind(ScopeKind, Extendable):
|
||||
Extendable.__init__(self)
|
||||
|
||||
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)
|
||||
self.qualify_base_classes(scope)
|
||||
|
||||
def to_string(self, scope: Scope) -> str:
|
||||
result = ""
|
||||
|
||||
inheritance_string = self.get_inheritance_string()
|
||||
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
|
||||
inheritance = self.get_inheritance_string()
|
||||
header = f"{self.name} {scope.get_qualified_name()}{inheritance} "
|
||||
return header + self._format_scope_body(scope)
|
||||
@@ -8,10 +8,7 @@ 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
|
||||
from .extendable import Extendable
|
||||
|
||||
@@ -45,9 +42,7 @@ class StructLikeScopeKind(ScopeKind, Extendable):
|
||||
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)
|
||||
self.qualify_base_classes(scope)
|
||||
|
||||
def to_string(self, scope: Scope) -> str:
|
||||
result = ""
|
||||
@@ -55,17 +50,7 @@ class StructLikeScopeKind(ScopeKind, Extendable):
|
||||
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 = []
|
||||
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}"
|
||||
|
||||
inheritance = self.get_inheritance_string()
|
||||
result += f"{self.name} {scope.get_qualified_name()}{inheritance} "
|
||||
result += self._format_scope_body(scope)
|
||||
return result
|
||||
Reference in new issue
Block a user