From 71d510a1bf01f049f9e9a62c7a89d73c8f2c7c26 Mon Sep 17 00:00:00 2001 From: codegen-bot Date: Sun, 16 Mar 2025 22:49:19 +0000 Subject: [PATCH 1/2] Add deAsyncify method to convert async TypeScript functions to synchronous --- .../core/detached_symbols/function_call.py | 29 +++++++++++++- .../typescript/expressions/function_type.py | 31 +++++++++++++- src/codegen/sdk/typescript/function.py | 40 ++++++++++++++++++- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/src/codegen/sdk/core/detached_symbols/function_call.py b/src/codegen/sdk/core/detached_symbols/function_call.py index 4507e65ff..738c0780e 100644 --- a/src/codegen/sdk/core/detached_symbols/function_call.py +++ b/src/codegen/sdk/core/detached_symbols/function_call.py @@ -221,6 +221,33 @@ def asyncify(self) -> None: self.insert_before("await (", newline=False) self.insert_after(")", newline=False) + @writer + def deAsyncify(self) -> None: + """Removes the 'await' keyword from an awaited function call. + + This method removes the 'await' syntax from a function call if it is awaited, converting it back to a regular function call. + + Args: + None + + Returns: + None + """ + if not self.is_awaited: + return + + # Find the await keyword and remove it along with any surrounding parentheses + parent = self.ts_node.parent + if parent and parent.type == "await_expression": + # Check if the await expression is wrapped in parentheses + if parent.parent and parent.parent.type == "parenthesized_expression": + # Remove the await keyword and both parentheses + self.remove_byte_range(parent.parent.start_byte, self.ts_node.start_byte) + self.remove_byte_range(self.ts_node.end_byte, parent.parent.end_byte) + else: + # Remove just the await keyword + self.remove_byte_range(parent.start_byte, self.ts_node.start_byte) + @property @reader def predecessor(self) -> FunctionCall[Parent] | None: @@ -718,4 +745,4 @@ def promise_chain(self) -> TSPromiseChain | None: """ if any(call.name == "then" for call in self.call_chain) is True: return TSPromiseChain(self.attribute_chain) - return None + return None \ No newline at end of file diff --git a/src/codegen/sdk/typescript/expressions/function_type.py b/src/codegen/sdk/typescript/expressions/function_type.py index 85807bade..75035d1ac 100644 --- a/src/codegen/sdk/typescript/expressions/function_type.py +++ b/src/codegen/sdk/typescript/expressions/function_type.py @@ -75,6 +75,35 @@ def asyncify(self) -> None: self.return_type.insert_before("Promise<", newline=False) self.return_type.insert_after(">", newline=False) + @writer + def deAsyncify(self) -> None: + """Modifies the function type to be synchronous by unwrapping its return type from a Promise. + + This method transforms an asynchronous function type into a synchronous one by modifying + its return type. It unwraps the existing return type from a Promise, effectively changing + 'Promise' to 'T'. + + Args: + self: The TSFunctionType instance to modify. + + Returns: + None + """ + if self.return_type and self.return_type.name == "Promise": + # Check if it's a generic Promise + if "<" in self.return_type.source and ">" in self.return_type.source: + # Extract the type inside Promise + inner_type = self.return_type.source[self.return_type.source.find("<") + 1:self.return_type.source.rfind(">")] + if inner_type.strip(): + # Replace Promise with T + self.return_type.edit(inner_type) + else: + # If Promise<> is empty or just whitespace, remove the return type + self.return_type.edit("") + else: + # If it's just Promise without generic type, remove it + self.return_type.edit("") + def _compute_dependencies(self, usage_type: UsageKind | None = None, dest: Importable | None = None): if self.return_type: self.return_type._compute_dependencies(UsageKind.GENERIC, dest) @@ -91,4 +120,4 @@ def descendant_symbols(self) -> list[Importable]: symbols = [] for param in self.parameters: symbols.extend(param.descendant_symbols) - return symbols + return symbols \ No newline at end of file diff --git a/src/codegen/sdk/typescript/function.py b/src/codegen/sdk/typescript/function.py index ee71ee9db..6bef3fed6 100644 --- a/src/codegen/sdk/typescript/function.py +++ b/src/codegen/sdk/typescript/function.py @@ -304,6 +304,44 @@ def asyncify(self) -> None: self.return_type.insert_before("Promise<", newline=False) self.return_type.insert_after(">", newline=False) + @writer + def deAsyncify(self) -> None: + """Modifies the function to be synchronous, if it is asynchronous. + + This method converts an asynchronous function to be synchronous by removing the 'async' keyword and unwrapping + the return type from a Promise if a return type exists. + + Returns: + None + + Note: + If the function is already synchronous, this method does nothing. + """ + if not self.is_async: + return + + # Remove the 'async' keyword + for child in self.ts_node.children: + if child.type == "async": + self.remove_byte_range(child.start_byte, child.end_byte) + break + + # Unwrap the return type from Promise if it exists + if self.return_type and self.return_type.name == "Promise": + # Check if it's a generic Promise + if "<" in self.return_type.source and ">" in self.return_type.source: + # Extract the type inside Promise + inner_type = self.return_type.source[self.return_type.source.find("<") + 1:self.return_type.source.rfind(">")] + if inner_type.strip(): + # Replace Promise with T + self.return_type.edit(inner_type) + else: + # If Promise<> is empty or just whitespace, remove the return type + self.return_type.edit("") + else: + # If it's just Promise without generic type, remove it + self.return_type.edit("") + @writer def arrow_to_named(self, name: str | None = None) -> None: """Converts an arrow function to a named function in TypeScript/JavaScript. @@ -450,4 +488,4 @@ def promise_chains(self) -> list[TSPromiseChain]: promise_chains.append(function_call.promise_chain) visited_base_functions.add(function_call.base) - return promise_chains + return promise_chains \ No newline at end of file From 5cfd55c84242b30c10b53824a00ef711d0335754 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Sun, 16 Mar 2025 22:50:03 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- src/codegen/sdk/core/detached_symbols/function_call.py | 4 ++-- src/codegen/sdk/typescript/expressions/function_type.py | 4 ++-- src/codegen/sdk/typescript/function.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/codegen/sdk/core/detached_symbols/function_call.py b/src/codegen/sdk/core/detached_symbols/function_call.py index 738c0780e..2c2d32af3 100644 --- a/src/codegen/sdk/core/detached_symbols/function_call.py +++ b/src/codegen/sdk/core/detached_symbols/function_call.py @@ -235,7 +235,7 @@ def deAsyncify(self) -> None: """ if not self.is_awaited: return - + # Find the await keyword and remove it along with any surrounding parentheses parent = self.ts_node.parent if parent and parent.type == "await_expression": @@ -745,4 +745,4 @@ def promise_chain(self) -> TSPromiseChain | None: """ if any(call.name == "then" for call in self.call_chain) is True: return TSPromiseChain(self.attribute_chain) - return None \ No newline at end of file + return None diff --git a/src/codegen/sdk/typescript/expressions/function_type.py b/src/codegen/sdk/typescript/expressions/function_type.py index 75035d1ac..61d038ef1 100644 --- a/src/codegen/sdk/typescript/expressions/function_type.py +++ b/src/codegen/sdk/typescript/expressions/function_type.py @@ -93,7 +93,7 @@ def deAsyncify(self) -> None: # Check if it's a generic Promise if "<" in self.return_type.source and ">" in self.return_type.source: # Extract the type inside Promise - inner_type = self.return_type.source[self.return_type.source.find("<") + 1:self.return_type.source.rfind(">")] + inner_type = self.return_type.source[self.return_type.source.find("<") + 1 : self.return_type.source.rfind(">")] if inner_type.strip(): # Replace Promise with T self.return_type.edit(inner_type) @@ -120,4 +120,4 @@ def descendant_symbols(self) -> list[Importable]: symbols = [] for param in self.parameters: symbols.extend(param.descendant_symbols) - return symbols \ No newline at end of file + return symbols diff --git a/src/codegen/sdk/typescript/function.py b/src/codegen/sdk/typescript/function.py index 6bef3fed6..1c4192e53 100644 --- a/src/codegen/sdk/typescript/function.py +++ b/src/codegen/sdk/typescript/function.py @@ -319,19 +319,19 @@ def deAsyncify(self) -> None: """ if not self.is_async: return - + # Remove the 'async' keyword for child in self.ts_node.children: if child.type == "async": self.remove_byte_range(child.start_byte, child.end_byte) break - + # Unwrap the return type from Promise if it exists if self.return_type and self.return_type.name == "Promise": # Check if it's a generic Promise if "<" in self.return_type.source and ">" in self.return_type.source: # Extract the type inside Promise - inner_type = self.return_type.source[self.return_type.source.find("<") + 1:self.return_type.source.rfind(">")] + inner_type = self.return_type.source[self.return_type.source.find("<") + 1 : self.return_type.source.rfind(">")] if inner_type.strip(): # Replace Promise with T self.return_type.edit(inner_type) @@ -488,4 +488,4 @@ def promise_chains(self) -> list[TSPromiseChain]: promise_chains.append(function_call.promise_chain) visited_base_functions.add(function_call.base) - return promise_chains \ No newline at end of file + return promise_chains