Repository: seaofvoices/darklua (rust)

Issue:
rename_variables does not rename modules in typedefs
Minimal repro, expected this:

```luau
-- types
export type foo = { bar = "idk" }
return nil

-- other module
local types = require("./types")
type foo = types.foo
```

to be transformed as:

```luau
-- types
export type foo = { bar = "idk" }
return nil

-- other module
local a = require("./types")
type foo = a.foo
```

Actually transformed without renaming the typedef:


```luau
-- types
export type foo = { bar = "idk" }
return nil

-- other module
local a = require("./types")
type foo = types.foo
```

Came from https://github.com/prvdmwrong/prvdmwrong, run the build script (`lune run build-pkg`) and accept "Publish these packages to NPM and Wally?" when prompted (comment lines around 265 to not actually publish)

![Ảnh màn hình 2024-08-20 lúc 18 21 10](https://github.com/user-attachments/assets/2c84fe37-92b2-4091-a490-c4bca321905c)


Pull request description:
Fix `rename_variables` rule to rename type namespaces
Closes #206 


- [x] add entry to the changelog


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cfab396..2f39554 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## Unreleased
 
+* fix `rename_variables` rule to rename module names in types ([#233](https://github.com/seaofvoices/darklua/pull/233))
 * fix string encoding containing unicode characters taking more than 1 byte ([#232](https://github.com/seaofvoices/darklua/pull/232))
 * fix `append_text_comment` to not add an extra space to comments ([#231](https://github.com/seaofvoices/darklua/pull/231))
 * add rule to remove floor divisions (`remove_floor_division`) ([#230](https://github.com/seaofvoices/darklua/pull/230))
diff --git a/src/rules/rename_variables/rename_processor.rs b/src/rules/rename_variables/rename_processor.rs
index 930a7a7..e3dfef3 100644
--- a/src/rules/rename_variables/rename_processor.rs
+++ b/src/rules/rename_variables/rename_processor.rs
@@ -1,4 +1,4 @@
-use crate::nodes::{Expression, Identifier, LocalFunctionStatement};
+use crate::nodes::{Expression, Identifier, LocalFunctionStatement, TypeField};
 use crate::process::utils::{identifier_permutator, CharPermutator};
 use crate::process::{utils::KEYWORDS, NodeProcessor, Scope};
 
@@ -164,6 +164,14 @@ impl NodeProcessor for RenameProcessor {
             variable.set_name(obfuscated_name);
         }
     }
+
+    fn process_type_field(&mut self, type_field: &mut TypeField) {
+        if let Some(obfuscated_name) =
+            self.get_obfuscated_name(type_field.get_namespace().get_name())
+        {
+            type_field.mutate_namespace().set_name(obfuscated_name);
+        }
+    }
 }
 
 #[cfg(test)]


Test patch:
diff --git a/tests/rule_tests/rename_variables.rs b/tests/rule_tests/rename_variables.rs
index a2e9335..dad8437 100644
--- a/tests/rule_tests/rename_variables.rs
+++ b/tests/rule_tests/rename_variables.rs
@@ -44,6 +44,9 @@ test_rule!(
     function_expression_parameters_reference("return function(foo, bar) return foo + bar end")
         => "return function(a, b) return a + b end",
     recycle_previous_identifiers("do local foo end local foo") => "do local a end local a",
+    reexported_type_field("local types = require('./types') export type Oof = types.Oof") => "local a = require('./types') export type Oof = a.Oof",
+    type_variable_type_field("local React = require('@pkg/@jsdotlua/react') type Props = { children: React.ReactNode }")
+        => "local a = require('@pkg/@jsdotlua/react') type Props = { children: a.ReactNode }",
 );
 
 test_rule!(
@@ -88,6 +91,9 @@ test_rule!(
     function_expression_parameters_reference("return function(foo, bar) return foo + bar end")
         => "return function(a, b) return a + b end",
     recycle_previous_identifiers("do local foo end local foo") => "do local a end local a",
+    reexported_type_field("local types = require('./types') export type Oof = types.Oof") => "local a = require('./types') export type Oof = a.Oof",
+    type_variable_type_field("local React = require('@pkg/@jsdotlua/react') type Props = { children: React.ReactNode }")
+        => "local a = require('@pkg/@jsdotlua/react') type Props = { children: a.ReactNode }",
 );
 
 test_rule_without_effects!(