Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rename_variables rule to rename type namespaces #233

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion src/rules/rename_variables/rename_processor.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions tests/rule_tests/rename_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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!(
Expand Down
Loading