Skip to content

Commit

Permalink
Cleanup global types script (#289)
Browse files Browse the repository at this point in the history
* Cleanup global types script

* Include enums with no members

* Forward declare deprecated classes
  • Loading branch information
JohnnyMorganz authored Feb 9, 2023
1 parent e006ec2 commit 6a812d4
Show file tree
Hide file tree
Showing 2 changed files with 734 additions and 1,291 deletions.
58 changes: 24 additions & 34 deletions scripts/dumpRobloxTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@
"GlobalSettings", # redefined explicitly
]

# These classes are deferred to the very end of the dump, so that they have access to all the types
DEFERRED_CLASSES: List[str] = [
"ServiceProvider",
# The following must be deferred as they rely on ServiceProvider
"DataModel",
"GenericSettings",
"AnalysticsSettings",
"UserSettings",
# Plugin is deferred after its items are declared
"Plugin",
]

# Methods / Properties ignored in classes. Commonly used to add corrections
IGNORED_MEMBERS = {
"Instance": [
Expand Down Expand Up @@ -432,7 +420,6 @@
type QFont = string
type FloatCurveKey = any
type RotationCurveKey = any
type Instance = any
declare class Enum
function GetEnumItems(self): { any }
Expand Down Expand Up @@ -759,7 +746,7 @@ def resolveParameterList(params: List[ApiParameter]):
return ", ".join(map(resolveParameter, params))


def resolveReturnType(member: Union[ApiFunction, ApiCallback]):
def resolveReturnType(member: Union[ApiFunction, ApiCallback]) -> str:
return (
"(" + ", ".join(map(resolveType, member["TupleReturns"])) + ")"
if "TupleReturns" in member
Expand All @@ -769,7 +756,11 @@ def resolveReturnType(member: Union[ApiFunction, ApiCallback]):

def filterMember(klassName: str, member: ApiMember):
if not INCLUDE_DEPRECATED_METHODS and (
("Tags" in member and "Deprecated" in member["Tags"])
(
"Tags" in member
and member["Tags"] is not None
and "Deprecated" in member["Tags"]
)
or ("Deprecated" in member and member["Deprecated"])
):
return False
Expand All @@ -789,16 +780,21 @@ def filterMember(klassName: str, member: ApiMember):
return True


def declareClass(klass: ApiClass):
if klass["Name"] in IGNORED_INSTANCES:
return ""

if (
def shouldExcludeAsDeprecated(klass: ApiClass):
return (
not INCLUDE_DEPRECATED_METHODS
and "Tags" in klass
and klass["Tags"] is not None
and "Deprecated" in klass["Tags"]
and not klass["Name"] in OVERRIDE_DEPRECATED_REMOVAL
):
)


def declareClass(klass: ApiClass) -> str:
if klass["Name"] in IGNORED_INSTANCES:
return ""

if shouldExcludeAsDeprecated(klass):
return ""

out = "declare class " + klass["Name"]
Expand Down Expand Up @@ -840,15 +836,15 @@ def declareService(service: str):
)

out += "".join(sorted(memberDefinitions))

out += "end"

return out


def printEnums(dump: ApiDump):
enums: defaultdict[str, List[str]] = defaultdict(list)
enums: dict[str, List[str]] = {}
for enum in dump["Enums"]:
enums[enum["Name"]] = []
for item in enum["Items"]:
enums[enum["Name"]].append(item["Name"])

Expand All @@ -875,24 +871,18 @@ def printEnums(dump: ApiDump):


def printClasses(dump: ApiDump):
# Forward declare all the types
# Forward declare "deprecated" classes in case they are still used
for klass in dump["Classes"]:
if klass["Name"] in IGNORED_INSTANCES:
continue
if klass["Name"] != "Instance":
if shouldExcludeAsDeprecated(klass):
print(f"type {klass['Name']} = any")

for klass in dump["Classes"]:
if klass["Name"] in DEFERRED_CLASSES or klass["Name"] in IGNORED_INSTANCES:
if klass["Name"] in IGNORED_INSTANCES:
continue

print(declareClass(klass))
print()

for klassName in DEFERRED_CLASSES:
print(declareClass(CLASSES[klassName]))
print()


def printDataTypes(types: List[DataType]):
for klass in types:
Expand Down Expand Up @@ -1064,7 +1054,7 @@ def loadClassesIntoStructures(dump: ApiDump):
continue

isCreatable = True
if "Tags" in klass:
if "Tags" in klass and klass["Tags"] is not None:
if (
"Deprecated" in klass
and not INCLUDE_DEPRECATED_METHODS
Expand All @@ -1087,7 +1077,7 @@ def topologicalSortDataTypes(dataTypes: List[DataType]) -> List[DataType]:

dataTypeNames = {klass["Name"] for klass in dataTypes}

def resolveClass(type: Union[ApiValueType, CorrectionsValueType]):
def resolveClass(type: Union[ApiValueType, CorrectionsValueType]) -> Optional[str]:
name = (
type["Generic"]
if "Generic" in type
Expand Down
Loading

0 comments on commit 6a812d4

Please sign in to comment.