From 205a9e0501863989bc31036b1507c067974aa806 Mon Sep 17 00:00:00 2001 From: Bennet Sunder Date: Fri, 3 May 2024 09:23:48 +0530 Subject: [PATCH] add support for ruby --- Cargo.toml | 1 + site/docs/reference/languages.md | 1 + src/cleanup_rules/ruby/edges.toml | 50 + src/cleanup_rules/ruby/rules.toml | 893 ++++++++++++++++++ src/cleanup_rules/ruby/scope_config.toml | 36 + src/models/default_configs.rs | 1 + src/models/language.rs | 20 +- src/models/piranha_arguments.rs | 5 +- src/tests/mod.rs | 2 +- src/tests/test_piranha_ruby.rs | 30 + .../configurations/rules.toml | 21 + .../expected/delete_lines_after_return.rb | 4 + .../input/delete_lines_after_return.rb | 6 + .../configurations/rules.toml | 30 + .../expected/test_empty_if_unless_rules.rb | 23 + .../input/test_empty_if_unless_rules.rb | 39 + .../configurations/rules.toml | 146 +++ .../expected/test_if_rules_inline.rb | 13 + .../expected/test_if_rules_with_else_block.rb | 23 + .../test_if_rules_without_else_block.rb | 13 + .../input/test_if_rules_inline.rb | 15 + .../input/test_if_rules_with_else_block.rb | 41 + .../input/test_if_rules_without_else_block.rb | 23 + .../configurations/rules.toml | 45 + .../expected/test_if_rules_ternary.rb | 15 + .../input/test_if_rules_ternary.rb | 15 + .../configurations/rules.toml | 110 +++ .../expected/test_unless_rules_inline.rb | 13 + .../test_unless_rules_with_else_block.rb | 15 + .../test_unless_rules_without_else_block.rb | 13 + .../input/test_unless_rules_inline.rb | 15 + .../test_unless_rules_with_else_block.rb | 31 + .../test_unless_rules_without_else_block.rb | 23 + .../configurations/rules.toml | 206 ++++ .../expected/boolean_simplify.rb | 90 ++ .../input/boolean_simplify.rb | 111 +++ .../configurations/rules.toml | 109 +++ ...mplify_if_lambda_conditional_statements.rb | 8 + ...mplify_if_lambda_conditional_statements.rb | 13 + .../configurations/edges.toml | 4 + .../configurations/rules.toml | 55 ++ ...lify_local_variable_assigned_flag_check.rb | 14 + ...lify_local_variable_assigned_flag_check.rb | 18 + .../configurations/rules.toml | 28 + .../simplify_rspec_block_expressions.rb | 3 + .../input/simplify_rspec_block_expressions.rb | 29 + .../configurations/rules.toml | 109 +++ ...fy_unless_lambda_conditional_statements.rb | 8 + ...fy_unless_lambda_conditional_statements.rb | 13 + 49 files changed, 2545 insertions(+), 4 deletions(-) create mode 100644 src/cleanup_rules/ruby/edges.toml create mode 100644 src/cleanup_rules/ruby/rules.toml create mode 100644 src/cleanup_rules/ruby/scope_config.toml create mode 100644 src/tests/test_piranha_ruby.rs create mode 100644 test-resources/rb/delete_lines_after_return/configurations/rules.toml create mode 100644 test-resources/rb/delete_lines_after_return/expected/delete_lines_after_return.rb create mode 100644 test-resources/rb/delete_lines_after_return/input/delete_lines_after_return.rb create mode 100644 test-resources/rb/replace_empty_if_or_unless_statement/configurations/rules.toml create mode 100644 test-resources/rb/replace_empty_if_or_unless_statement/expected/test_empty_if_unless_rules.rb create mode 100644 test-resources/rb/replace_empty_if_or_unless_statement/input/test_empty_if_unless_rules.rb create mode 100644 test-resources/rb/replace_if_statement/configurations/rules.toml create mode 100644 test-resources/rb/replace_if_statement/expected/test_if_rules_inline.rb create mode 100644 test-resources/rb/replace_if_statement/expected/test_if_rules_with_else_block.rb create mode 100644 test-resources/rb/replace_if_statement/expected/test_if_rules_without_else_block.rb create mode 100644 test-resources/rb/replace_if_statement/input/test_if_rules_inline.rb create mode 100644 test-resources/rb/replace_if_statement/input/test_if_rules_with_else_block.rb create mode 100644 test-resources/rb/replace_if_statement/input/test_if_rules_without_else_block.rb create mode 100644 test-resources/rb/replace_ternary_operator/configurations/rules.toml create mode 100644 test-resources/rb/replace_ternary_operator/expected/test_if_rules_ternary.rb create mode 100644 test-resources/rb/replace_ternary_operator/input/test_if_rules_ternary.rb create mode 100644 test-resources/rb/replace_unless_statement/configurations/rules.toml create mode 100644 test-resources/rb/replace_unless_statement/expected/test_unless_rules_inline.rb create mode 100644 test-resources/rb/replace_unless_statement/expected/test_unless_rules_with_else_block.rb create mode 100644 test-resources/rb/replace_unless_statement/expected/test_unless_rules_without_else_block.rb create mode 100644 test-resources/rb/replace_unless_statement/input/test_unless_rules_inline.rb create mode 100644 test-resources/rb/replace_unless_statement/input/test_unless_rules_with_else_block.rb create mode 100644 test-resources/rb/replace_unless_statement/input/test_unless_rules_without_else_block.rb create mode 100644 test-resources/rb/simplify_boolean_expressions/configurations/rules.toml create mode 100644 test-resources/rb/simplify_boolean_expressions/expected/boolean_simplify.rb create mode 100644 test-resources/rb/simplify_boolean_expressions/input/boolean_simplify.rb create mode 100644 test-resources/rb/simplify_if_lambda_conditional_statements/configurations/rules.toml create mode 100644 test-resources/rb/simplify_if_lambda_conditional_statements/expected/simplify_if_lambda_conditional_statements.rb create mode 100644 test-resources/rb/simplify_if_lambda_conditional_statements/input/simplify_if_lambda_conditional_statements.rb create mode 100644 test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/edges.toml create mode 100644 test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/rules.toml create mode 100644 test-resources/rb/simplify_local_variable_assigned_flag_check/expected/simplify_local_variable_assigned_flag_check.rb create mode 100644 test-resources/rb/simplify_local_variable_assigned_flag_check/input/simplify_local_variable_assigned_flag_check.rb create mode 100644 test-resources/rb/simplify_rspec_block_expressions/configurations/rules.toml create mode 100644 test-resources/rb/simplify_rspec_block_expressions/expected/simplify_rspec_block_expressions.rb create mode 100644 test-resources/rb/simplify_rspec_block_expressions/input/simplify_rspec_block_expressions.rb create mode 100644 test-resources/rb/simplify_unless_lambda_conditional_statements/configurations/rules.toml create mode 100644 test-resources/rb/simplify_unless_lambda_conditional_statements/expected/simplify_unless_lambda_conditional_statements.rb create mode 100644 test-resources/rb/simplify_unless_lambda_conditional_statements/input/simplify_unless_lambda_conditional_statements.rb diff --git a/Cargo.toml b/Cargo.toml index ce5b30cff..9bcd00cc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ tree-sitter-java = "0.20.2" # TODO: Update after: https://github.com/alex-pinkus/tree-sitter-swift/issues/278 resolves tree-sitter-swift = { git = "https://github.com/satyam1749/tree-sitter-swift.git", rev = "08a28993599f1968bc81631a89690503e1db7704" } tree-sitter-python = "0.20.2" +tree-sitter-ruby = "0.20.1" tree-sitter-typescript = "0.20.1" # TODO: Update after https://github.com/tree-sitter/tree-sitter-go/pull/103 lands tree-sitter-go = { git = "https://github.com/uber/tree-sitter-go.git", rev = "f8cffd0af7baaf7bf6062e403efe7c0d06319c41" } diff --git a/site/docs/reference/languages.md b/site/docs/reference/languages.md index a2712fba1..f48d5339e 100644 --- a/site/docs/reference/languages.md +++ b/site/docs/reference/languages.md @@ -18,3 +18,4 @@ title: Languages supported | Scala | :heavy_check_mark: | :calendar: | :calendar: | | C# | :calendar: | :calendar: | :calendar: | | JavaScript | :calendar: | :calendar: | :calendar: | +| Ruby | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | diff --git a/src/cleanup_rules/ruby/edges.toml b/src/cleanup_rules/ruby/edges.toml new file mode 100644 index 000000000..f625eeaef --- /dev/null +++ b/src/cleanup_rules/ruby/edges.toml @@ -0,0 +1,50 @@ +[[edges]] +scope = "Parent" +from = "replace_expression_with_boolean_literal" +to = ["boolean_literal_cleanup"] + + +[[edges]] +scope = "Parent" +from = "boolean_literal_cleanup" +to = ["boolean_expression_simplify", "statement_cleanup", "block_removal"] + +[[edges]] +scope = "Parent" +from = "boolean_expression_simplify" +to = ["boolean_literal_cleanup"] + +[[edges]] +scope = "Parent" +from = "statement_cleanup" +to = ["if_cleanup"] + +[[edges]] +scope = "Parent" +from = "if_cleanup" +to = ["delete_all_statements_after_return"] + +[[edges]] +scope = "Parent" +from = "delete_all_statements_after_return" +to = ["delete_all_statements_after_return"] + +[[edges]] +scope = "Parent" +from = "statement_cleanup" +to = ["delete_variable_declaration"] + +[[edges]] +scope = "Method" +from = "delete_variable_declaration" +to = ["replace_identifier_with_value"] + +[[edges]] +scope = "Parent" +from = "replace_identifier_with_value" +to = ["boolean_literal_cleanup"] + +[[edges]] +scope = "Parent" +from = "delete_variable_declaration" +to = ["identify_empty_methods"] diff --git a/src/cleanup_rules/ruby/rules.toml b/src/cleanup_rules/ruby/rules.toml new file mode 100644 index 000000000..65c6913fe --- /dev/null +++ b/src/cleanup_rules/ruby/rules.toml @@ -0,0 +1,893 @@ +# Before : +# if true +# do_something +# else +# do_something_else +# end +# After : +# do_something +[[rules]] +name = "replace_if_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (if + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence : ((then) @consequence) + )@if_statement +) +""" +replace = "@consequence" +replace_node = "if_statement" + + +# Before : +# if method_call +# [:meth_fields] +# elsif true +# [:flag_fields] +# elsif another_method_call +# [:another_meth_fields] +# else +# [:default_fields] +# end +# After : +# if method_call +# [:meth_fields] +# else +# [:flag_fields] +# end +[[rules]] +name = "replace_elsif_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +(elsif + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: ((then) @consequence) +) @alternative +""" +replace = "else @consequence" +replace_node = "alternative" + +# Before : +# if false +# do_something +# else +# do_something_else +# end +# After : +# do_something_else +[[rules]] +name = "replace_if_false" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (if + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) + alternative: (else ((_)? @alternative)) + )@if_statement +) +""" +replace = "@alternative" +replace_node = "if_statement" + +# Before : +# if false +# do_something +# end +# After : +# +[[rules]] +name = "replace_if_statement_false_without_else" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (if + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) + . + )@if_statement +) +""" +replace = "" +replace_node = "if_statement" + +# Before : +# fields_array << :field_name if false +# After : +# +[[rules]] +name = "replace_if_inline_false" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (if_modifier + body : ((_) @body) + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + )@if_modifier +) +""" +replace = "" +replace_node = "if_modifier" + +# Before : +# fields_array << :field_name if true +# After : +# fields_array << :field_name +# +[[rules]] +name = "replace_if_inline_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +(if_modifier + body: (_) @body + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] +) @if_modifier +""" +replace = "@body" +replace_node = "if_modifier" + +# Before : +# unless false +# do_something +# else +# do_something_else +# end +# After : +# do_something +[[rules]] +name = "replace_unless_false" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +(unless + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) +)@unless +""" +replace = "@consequence" +replace_node = "unless" + +# Before : +# fields_array << :field_name unless true +# After : +# +[[rules]] +name = "replace_unless_inline_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (unless_modifier + body : ((_) @body) + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + )@unless_modifier +) +""" +replace = "" +replace_node = "unless_modifier" + +# Before : +# fields_array << :field_name unless false +# After : +# fields_array << :field_name +[[rules]] +name = "replace_unless_inline_false" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (unless_modifier + body : ((_) @body) + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + )@unless_modifier +) +""" +replace = "@body" +replace_node = "unless_modifier" + +# Before : +# unless true +# do_something +# else +# do_something_else +# end +# After : +# do_something_else +[[rules]] +name = "replace_unless_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +(unless + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: (_) @consequence + alternative: (else ((_)? @alternative)) +)@unless +""" + +replace = "@alternative" +replace_node = "unless" +# Before : +# unless true +# do_something +# end +# After : +# +[[rules]] +name = "simplify_unless_statement_true_without_else" +groups = ["if_cleanup"] +query = """ +(unless + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: (_) @consequence + . +)@unless +""" +replace = "" +replace_node = "unless" + +# Before : +# true ? flag_flow : non_flag_flow +# After : +# flag_flow +# +[[rules]] +name = "replace_ternary_operator_true" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (conditional + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence : (_)* @consequence + alternative: (_)* @alternative + ) +@conditional) +""" +replace = "@consequence" +replace_node = "conditional" + +# Before : +# false ? flag_flow : non_flag_flow +# After : +# non_flag_flow +# +[[rules]] +name = "replace_ternary_operator_false" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +( + (conditional + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence : (_)* @consequence + alternative: (_)* @alternative + ) +@conditional) +""" +replace = "@alternative" +replace_node = "conditional" + +# Before : +# "something #{flag_check?}" +# After : +# "something true" +# +[[rules]] +name = "replace_interpolation" +groups = ["if_cleanup"] +is_seed_rule = false +query = """ +(interpolation [(true) (false) (identifier)] @inter_body) @inter +""" +replace = "@inter_body" +replace_node = "inter" + + +# Before : +# !false +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_not_false" +query = """ +( + (unary + operand: (false)) +@unary_expression) +""" +replace = "true" +replace_node = "unary_expression" + +# Before : +# !true +# After : +# false +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_not_true" +query = """ +( + (unary + operand: (true)) +@unary_expression) +""" +replace = "false" +replace_node = "unary_expression" + +# Before: +# (true) +# After +# true + + +# Before : +# abc() && true +# After : +# abc() +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_something_and_true" +query = """ +( + (binary + left : (_) @lhs + operator: ["&&" "and"] + right: (true) + )@binary +) +""" +replace = "@lhs" +replace_node = "binary" + +# Before : +# true && abc() +# After : +# abc() +# + +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_true_and_something" +query = """ +( + (binary + left: (true) + operator: ["&&" "and"] + right : (_) @rhs) +) @binary +""" +replace = "@rhs" +replace_node = "binary" + + +# Before : +# abc || true +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_something_or_true" +query = """ +( + (binary + left : (_) @lhs + operator: ["||" "or"] + right: (true) + ) +@binary)""" +replace = "true" +replace_node = "binary" + +# Before : +# true || abc() +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_true_or_something" +query = """ +( + (binary + left : (true) + operator: ["||" "or"] + right: (_) @rhs + ) +@binary) +""" +replace = "true" +replace_node = "binary" + +# Before : +# abc() && false +# After : +# false +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_something_and_false" +query = """ +( + (binary + left : (_) @lhs + operator: ["&&" "and"] + right: (false) + )@binary +) +""" +replace = "false" +replace_node = "binary" + +# Before : +# false && abc() +# After : +# false +# + +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_false_and_something" +query = """ +( + (binary + left : (false) + operator: ["&&" "and"] + right: (_) @rhs + )@binary +) +""" +replace = "false" +replace_node = "binary" + + +# Before : +# abc || false +# After : +# abc +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_something_or_false" +query = """ +( + (binary + left : (_) @lhs + operator: ["||" "or"] + right: (false) + ) +@binary)""" +replace = "@lhs" +replace_node = "binary" + +# Before : +# false || abc() +# After : +# abc() +# +[[rules]] +groups = ["boolean_expression_simplify"] +is_seed_rule = false +name = "replace_false_or_something" +query = """ +( + (binary + left : (false) + operator: ["||" "or"] + right: (_) @rhs + ) +@binary) +""" +replace = "@rhs" +replace_node = "binary" + +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { true } +# After : +# before_action :map_user_roles, only: [:create, :update] +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (true) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :authenticate_user!, if: lambda { true } +# After : +# before_action :authenticate_user! +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (true) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (false) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :authenticate_user!, if: lambda { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (false) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :map_user_roles, only: [:create, :update], unless: -> { true } +# After : +# before_action :map_user_roles, only: [:create, :update] +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_unless_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (true) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "unless") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :authenticate_user!, unless: lambda { true } +# After : +# before_action :authenticate_user! +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_unless_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (true) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "unless") +(#eq? @identifier "lambda") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :map_user_roles, only: [:create, :update], unless: -> { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_unless_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (false) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "unless") +)@call +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :authenticate_user!, unless: lambda { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_unless_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (false) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "unless") +(#eq? @identifier "lambda") +)@call +""" +replace = "" +replace_node = "pair" + +# Before : +# before(:all) { } +# After : +# +# +[[rules]] +name = "remove_empty_rspec_blocks" +groups = ["block_removal"] +query = """ +( +(call + method: (identifier) @method + (argument_list (simple_symbol) @args) + [ + (block + !body + ) + (do_block + !body + ) + ] +)@call +(#match? @method "(before|after)") +(#match? @args "(:suite|:all|:context|:each|:example)") +) +""" +replace = "" +replace_node = "call" + + +# Before : +# def fourth_method +# return true +# call_this_method +# end +# After : +# def fourth_method +# return true +# end +# +[[rules]] +name = "delete_all_statements_after_return" +query = """ +( + (body_statement + ((_)* @pre) + [ + ( + (return) + ?(comment) + ) @r + ( + (return) + ) @r + ] + ((_)+ @post) + ) @body +) +""" +replace = "" +replace_node = "post" +is_seed_rule = false + + +[[rules]] +name = "delete_variable_declaration" +query = """ +( + (assignment + left: (identifier) @variable_name + right: [(true) (false)] @init + )@variable_declaration +) +""" +replace = "" +replace_node = "variable_declaration" +is_seed_rule = false +# Check if there is no assignment where the variable @variable_name is +# assigned to a value other than @init, within the method body +# Please note that the tree-sitter queries in the filter uses holes (i.e. `@variable_name` and `@init`). +# These holes will be filled contextually based on the code snippet matched to `rule.query +[[rules.filters]] +enclosing_node = "(method) @md" +not_contains = [""" +( + (assignment + left: (_) @a.lhs + right: (_) @a.rhs + )@assignment + (#eq? @a.lhs "@variable_name") + (#not-eq? @a.rhs "@init") +) +"""] + +# Replace identifier with value if : +# (i) There is no local variable declaration in the enclosing method with the name as the identifier +[[rules]] +name = "replace_identifier_with_value" +query = """ +( +(identifier) @identifier +(#eq? @identifier "@variable_name") +) +""" +replace = "@init" +replace_node = "identifier" +holes = ["variable_name", "init"] +is_seed_rule = false +[[rules.filters]] +# There should exist no local variable declaration named `@identifer` +enclosing_node = "(method) @md" +not_contains = [""" +( + (assignment + left: (_) @vdcl.lhs + right: (_) @vdcl.init + )@field_declaration + (#eq? @vdcl.lhs "@identifier") +) +"""] + +# Dummy rule that acts as a junction for all statement based cleanups +[[rules]] +name = "boolean_literal_cleanup" +is_seed_rule = false + +[[rules]] +name = "statement_cleanup" +is_seed_rule = false diff --git a/src/cleanup_rules/ruby/scope_config.toml b/src/cleanup_rules/ruby/scope_config.toml new file mode 100644 index 000000000..7b4661bd7 --- /dev/null +++ b/src/cleanup_rules/ruby/scope_config.toml @@ -0,0 +1,36 @@ +[[scopes]] +name = "Method" +[[scopes.rules]] +enclosing_node = """ +( + [ + (method + name: (identifier) @n + ) + (method + name: (identifier) @n + parameters: (method_parameters) @fp + ) + ] +@xdn) +""" +scope = """ +( + [ + ( + (method + name: (identifier) @z + ) + (#eq? @z "@n") + ) + ( + (method + name: (identifier) @z + parameters: (method_parameters) @tp + ) + (#eq? @z "@n") + (#eq? @tp "@fp") + ) + ] +)@qdn +""" diff --git a/src/models/default_configs.rs b/src/models/default_configs.rs index c4532d3d7..a269c7787 100644 --- a/src/models/default_configs.rs +++ b/src/models/default_configs.rs @@ -32,6 +32,7 @@ pub const THRIFT: &str = "thrift"; pub const STRINGS: &str = "strings"; pub const TS_SCHEME: &str = "scm"; // We support scheme files that contain tree-sitter query pub const SCALA: &str = "scala"; +pub const RUBY: &str = "rb"; pub const REGEX_QUERY_PREFIX: &str = "rgx "; pub const CONCRETE_SYNTAX_QUERY_PREFIX: &str = "cs "; diff --git a/src/models/language.rs b/src/models/language.rs index e10c8c621..fc5664538 100644 --- a/src/models/language.rs +++ b/src/models/language.rs @@ -21,7 +21,7 @@ use crate::utilities::parse_toml; use super::{ default_configs::{ - default_language, GO, JAVA, JAVA_CS, KOTLIN, PYTHON, SCALA, STRINGS, SWIFT, THRIFT, TSX, + default_language, GO, JAVA, JAVA_CS, KOTLIN, PYTHON, RUBY, SCALA, STRINGS, SWIFT, THRIFT, TSX, TS_SCHEME, TYPESCRIPT, }, outgoing_edges::Edges, @@ -68,6 +68,7 @@ pub enum SupportedLanguage { Strings, TsScheme, Scala, + Ruby, } impl PiranhaLanguage { @@ -272,6 +273,23 @@ impl std::str::FromStr for PiranhaLanguage { .to_vec(), comment_nodes: vec![], }), + RUBY => { + let ruby_rules: Rules = parse_toml(include_str!("../cleanup_rules/ruby/rules.toml")); + let ruby_edges: Edges = parse_toml(include_str!("../cleanup_rules/ruby/edges.toml")); + Ok(PiranhaLanguage { + extension: language.to_string(), + supported_language: SupportedLanguage::Ruby, + language: tree_sitter_ruby::language(), + scopes: parse_toml::(include_str!( + "../cleanup_rules/ruby/scope_config.toml" + )) + .scopes() + .to_vec(), + comment_nodes: vec![], + rules: Some(ruby_rules), + edges: Some(ruby_edges), + }) + } _ => Err("Language not supported"), } } diff --git a/src/models/piranha_arguments.rs b/src/models/piranha_arguments.rs index 9144047b3..aaf429160 100644 --- a/src/models/piranha_arguments.rs +++ b/src/models/piranha_arguments.rs @@ -18,7 +18,8 @@ use super::{ default_dry_run, default_exclude, default_global_tag_prefix, default_graph_validation, default_include, default_number_of_ancestors_in_parent_scope, default_path_to_configurations, default_path_to_output_summaries, default_paths_to_codebase, default_piranha_language, - default_rule_graph, default_substitutions, GO, JAVA, KOTLIN, PYTHON, SWIFT, TSX, TYPESCRIPT, + default_rule_graph, default_substitutions, GO, JAVA, KOTLIN, PYTHON, RUBY, SWIFT, TSX, + TYPESCRIPT, }, language::PiranhaLanguage, rule_graph::{read_user_config_files, RuleGraph, RuleGraphBuilder}, @@ -92,7 +93,7 @@ pub struct PiranhaArguments { /// The target language #[get = "pub"] #[builder(default = "default_piranha_language()")] - #[clap(short = 'l', value_parser = clap::builder::PossibleValuesParser::new([JAVA, SWIFT, PYTHON, KOTLIN, GO, TSX, TYPESCRIPT]) + #[clap(short = 'l', value_parser = clap::builder::PossibleValuesParser::new([JAVA, SWIFT, PYTHON, KOTLIN, GO, TSX, TYPESCRIPT, RUBY]) .map(|s| s.parse::().unwrap()))] language: PiranhaLanguage, diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 38dd84368..f5db3b601 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -37,6 +37,7 @@ mod test_piranha_scala; mod test_piranha_thrift; +mod test_piranha_ruby; mod test_piranha_scm; mod test_piranha_strings; @@ -222,7 +223,6 @@ macro_rules! create_rewrite_tests { super::initialize(); let _path= std::path::PathBuf::from("test-resources").join($language).join($path_to_test); let temp_dir= super::copy_folder_to_temp_dir(&_path.join("input")); - let piranha_arguments = $crate::models::piranha_arguments::PiranhaArgumentsBuilder::default() .paths_to_codebase(vec![temp_dir.path().to_str().unwrap().to_string()]) .path_to_configurations(_path.join("configurations").to_str().unwrap().to_string()) diff --git a/src/tests/test_piranha_ruby.rs b/src/tests/test_piranha_ruby.rs new file mode 100644 index 000000000..95b04b516 --- /dev/null +++ b/src/tests/test_piranha_ruby.rs @@ -0,0 +1,30 @@ +/* + Copyright (c) 2023 Uber Technologies, Inc. + +

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + except in compliance with the License. You may obtain a copy of the License at +

http://www.apache.org/licenses/LICENSE-2.0 + +

Unless required by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + express or implied. See the License for the specific language governing permissions and + limitations under the License. +*/ + +use crate::models::default_configs::RUBY; + +use super::create_rewrite_tests; + +create_rewrite_tests! { + RUBY, + test_replace_empty_if_unless_statement: "replace_empty_if_or_unless_statement", 1; + test_replace_ternary_operator: "replace_ternary_operator", 1; + test_replace_if_statement: "replace_if_statement", 3; + test_replace_unless_statement: "replace_unless_statement", 3; + test_boolean_cleanup: "simplify_boolean_expressions", 1; + test_simplify_rspec_block_expressions: "simplify_rspec_block_expressions", 1; + test_simplify_if_lambda_conditional_statements: "simplify_if_lambda_conditional_statements", 1; + test_simplify_unless_lambda_conditional_statements: "simplify_unless_lambda_conditional_statements", 1; + test_delete_lines_after_return: "delete_lines_after_return", 1; + test_simplify_local_variable_assigned_flag_check: "simplify_local_variable_assigned_flag_check", 1; +} diff --git a/test-resources/rb/delete_lines_after_return/configurations/rules.toml b/test-resources/rb/delete_lines_after_return/configurations/rules.toml new file mode 100644 index 000000000..0ff543f51 --- /dev/null +++ b/test-resources/rb/delete_lines_after_return/configurations/rules.toml @@ -0,0 +1,21 @@ +[[rules]] +name = "delete_all_statements_after_return" +query = """ +( + (body_statement + ((_)* @pre) + [ + ( + (return) + ?(comment) + ) @r + ( + (return) + ) @r + ] + ((_)+ @post) + ) @body +) +""" +replace = "" +replace_node = "post" diff --git a/test-resources/rb/delete_lines_after_return/expected/delete_lines_after_return.rb b/test-resources/rb/delete_lines_after_return/expected/delete_lines_after_return.rb new file mode 100644 index 000000000..db700d035 --- /dev/null +++ b/test-resources/rb/delete_lines_after_return/expected/delete_lines_after_return.rb @@ -0,0 +1,4 @@ +def remove_multiple_lines_after_return + before_return + return super +end diff --git a/test-resources/rb/delete_lines_after_return/input/delete_lines_after_return.rb b/test-resources/rb/delete_lines_after_return/input/delete_lines_after_return.rb new file mode 100644 index 000000000..39ce04a06 --- /dev/null +++ b/test-resources/rb/delete_lines_after_return/input/delete_lines_after_return.rb @@ -0,0 +1,6 @@ +def remove_multiple_lines_after_return + before_return + return super + after_return + another_method_call +end diff --git a/test-resources/rb/replace_empty_if_or_unless_statement/configurations/rules.toml b/test-resources/rb/replace_empty_if_or_unless_statement/configurations/rules.toml new file mode 100644 index 000000000..1c72418c1 --- /dev/null +++ b/test-resources/rb/replace_empty_if_or_unless_statement/configurations/rules.toml @@ -0,0 +1,30 @@ +# Before : +# if true +# end +# After : +# +[[rules]] +name = "replace_empty_if_or_unless" +groups = ["if_cleanup"] +query = """ +[ + (if + condition: [(true) (false)] + . + )@conditional + (if + condition: (parenthesized_statements [(true) (false)]) + . + )@conditional + (unless + condition: [(true) (false)] + . + )@conditional + (unless + condition: (parenthesized_statements [(true) (false)]) + . + )@conditional +] +""" +replace = "" +replace_node = "conditional" diff --git a/test-resources/rb/replace_empty_if_or_unless_statement/expected/test_empty_if_unless_rules.rb b/test-resources/rb/replace_empty_if_or_unless_statement/expected/test_empty_if_unless_rules.rb new file mode 100644 index 000000000..84f4ef168 --- /dev/null +++ b/test-resources/rb/replace_empty_if_or_unless_statement/expected/test_empty_if_unless_rules.rb @@ -0,0 +1,23 @@ +def test_empty_unless_statement_true +end + +def test_empty_unless_statement_false +end + +def test_empty_unless_statement_parenthesized_true +end + +def test_empty_unless_statement_parenthesized_false +end + +def test_empty_if_statement_true +end + +def test_empty_if_statement_false +end + +def test_empty_if_statement_parenthesized_true +end + +def test_empty_if_statement_parenthesized_false +end diff --git a/test-resources/rb/replace_empty_if_or_unless_statement/input/test_empty_if_unless_rules.rb b/test-resources/rb/replace_empty_if_or_unless_statement/input/test_empty_if_unless_rules.rb new file mode 100644 index 000000000..422bb9f46 --- /dev/null +++ b/test-resources/rb/replace_empty_if_or_unless_statement/input/test_empty_if_unless_rules.rb @@ -0,0 +1,39 @@ +def test_empty_unless_statement_true + unless true + end +end + +def test_empty_unless_statement_false + unless false + end +end + +def test_empty_unless_statement_parenthesized_true + unless(true) + end +end + +def test_empty_unless_statement_parenthesized_false + unless(false) + end +end + +def test_empty_if_statement_true + if true + end +end + +def test_empty_if_statement_false + if false + end +end + +def test_empty_if_statement_parenthesized_true + if(true) + end +end + +def test_empty_if_statement_parenthesized_false + if(false) + end +end diff --git a/test-resources/rb/replace_if_statement/configurations/rules.toml b/test-resources/rb/replace_if_statement/configurations/rules.toml new file mode 100644 index 000000000..2983a1a15 --- /dev/null +++ b/test-resources/rb/replace_if_statement/configurations/rules.toml @@ -0,0 +1,146 @@ +# Before : +# if true +# do_something +# else +# do_something_else +# end +# After : +# do_something +[[rules]] +name = "replace_if_true" +groups = ["if_cleanup"] +query = """ +( + (if + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence : ((then) @consequence) + )@if_statement +) +""" +replace = "@consequence" +replace_node = "if_statement" + +# Before : +# if method_call +# [:meth_fields] +# elsif true +# [:flag_fields] +# elsif another_method_call +# [:another_meth_fields] +# else +# [:default_fields] +# end +# After : +# if method_call +# [:meth_fields] +# else +# [:flag_fields] +# end +[[rules]] +name = "replace_elsif_true" +groups = ["if_cleanup"] +query = """ +(elsif + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: ((then) @consequence) +) @alternative +""" +replace = "else @consequence" +replace_node = "alternative" + +# Before : +# if false +# do_something +# else +# do_something_else +# end +# After : +# do_something_else +[[rules]] +name = "replace_if_false" +groups = ["if_cleanup"] +query = """ +( + (if + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) + alternative: (else ((_)? @alternative)) + )@if_statement +) +""" +replace = "@alternative" +replace_node = "if_statement" + +# Before : +# if false +# do_something +# end +# After : +# +[[rules]] +name = "replace_if_statement_false_without_else" +groups = ["if_cleanup"] +query = """ +( + (if + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) + . + )@if_statement +) +""" +replace = "" +replace_node = "if_statement" + +# Before : +# fields_array << :field_name if false +# After : +# +[[rules]] +name = "replace_if_inline_false" +groups = ["if_cleanup"] +query = """ +( + (if_modifier + body : ((_) @body) + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + )@if_modifier +) +""" +replace = "" +replace_node = "if_modifier" + +# Before : +# fields_array << :field_name if true +# After : +# fields_array << :field_name +# +[[rules]] +name = "replace_if_inline_true" +groups = ["if_cleanup"] +query = """ +(if_modifier + body: (_) @body + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] +) @if_modifier +""" +replace = "@body" +replace_node = "if_modifier" diff --git a/test-resources/rb/replace_if_statement/expected/test_if_rules_inline.rb b/test-resources/rb/replace_if_statement/expected/test_if_rules_inline.rb new file mode 100644 index 000000000..cb973cf3d --- /dev/null +++ b/test-resources/rb/replace_if_statement/expected/test_if_rules_inline.rb @@ -0,0 +1,13 @@ +def test_if_statement_inline_true + do_something +end + +def test_if_statement_inline_false +end + +def test_if_statement_inline_true_parenthesized + do_something +end + +def test_if_statement_inline_false_parenthesized +end diff --git a/test-resources/rb/replace_if_statement/expected/test_if_rules_with_else_block.rb b/test-resources/rb/replace_if_statement/expected/test_if_rules_with_else_block.rb new file mode 100644 index 000000000..cb6ad7d47 --- /dev/null +++ b/test-resources/rb/replace_if_statement/expected/test_if_rules_with_else_block.rb @@ -0,0 +1,23 @@ +def test_if_statement_true_with_else + do_something +end + +def test_if_statement_true_parenthesized_with_else + do_something +end + +def test_if_statement_false_with_else + do_something_else +end + +def test_if_statement_false_parenthesized_with_else + do_something_else +end + +def test_if_statement_with_elsif + if check_if_true? + do_something + else + do_something_else + end +end diff --git a/test-resources/rb/replace_if_statement/expected/test_if_rules_without_else_block.rb b/test-resources/rb/replace_if_statement/expected/test_if_rules_without_else_block.rb new file mode 100644 index 000000000..1573ac897 --- /dev/null +++ b/test-resources/rb/replace_if_statement/expected/test_if_rules_without_else_block.rb @@ -0,0 +1,13 @@ +def test_if_statement_true_without_else + do_something +end + +def test_if_statement_true_parenthesized_without_else + do_something +end + +def test_if_statement_false_without_else +end + +def test_if_statement_false_parenthesized_without_else +end diff --git a/test-resources/rb/replace_if_statement/input/test_if_rules_inline.rb b/test-resources/rb/replace_if_statement/input/test_if_rules_inline.rb new file mode 100644 index 000000000..1e127969a --- /dev/null +++ b/test-resources/rb/replace_if_statement/input/test_if_rules_inline.rb @@ -0,0 +1,15 @@ +def test_if_statement_inline_true + do_something if true +end + +def test_if_statement_inline_false + do_something if false +end + +def test_if_statement_inline_true_parenthesized + do_something if (true) +end + +def test_if_statement_inline_false_parenthesized + do_something if (false) +end diff --git a/test-resources/rb/replace_if_statement/input/test_if_rules_with_else_block.rb b/test-resources/rb/replace_if_statement/input/test_if_rules_with_else_block.rb new file mode 100644 index 000000000..32a1fee40 --- /dev/null +++ b/test-resources/rb/replace_if_statement/input/test_if_rules_with_else_block.rb @@ -0,0 +1,41 @@ +def test_if_statement_true_with_else + if true + do_something + else + do_something_else + end +end + +def test_if_statement_true_parenthesized_with_else + if(true) + do_something + else + do_something_else + end +end + +def test_if_statement_false_with_else + if false + do_something + else + do_something_else + end +end + +def test_if_statement_false_parenthesized_with_else + if(false) + do_something + else + do_something_else + end +end + +def test_if_statement_with_elsif + if check_if_true? + do_something + elsif true + do_something_else + else + do_something_else_again + end +end diff --git a/test-resources/rb/replace_if_statement/input/test_if_rules_without_else_block.rb b/test-resources/rb/replace_if_statement/input/test_if_rules_without_else_block.rb new file mode 100644 index 000000000..6de642435 --- /dev/null +++ b/test-resources/rb/replace_if_statement/input/test_if_rules_without_else_block.rb @@ -0,0 +1,23 @@ +def test_if_statement_true_without_else + if true + do_something + end +end + +def test_if_statement_true_parenthesized_without_else + if(true) + do_something + end +end + +def test_if_statement_false_without_else + if false + do_something + end +end + +def test_if_statement_false_parenthesized_without_else + if(false) + do_something_else + end +end diff --git a/test-resources/rb/replace_ternary_operator/configurations/rules.toml b/test-resources/rb/replace_ternary_operator/configurations/rules.toml new file mode 100644 index 000000000..051cb3c1f --- /dev/null +++ b/test-resources/rb/replace_ternary_operator/configurations/rules.toml @@ -0,0 +1,45 @@ +# Before : +# true ? flag_flow : non_flag_flow +# After : +# flag_flow +# +[[rules]] +name = "replace_ternary_operator_true" +groups = ["if_cleanup"] +query = """ +( + (conditional + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence : (_)* @consequence + alternative: (_)* @alternative + ) +@conditional) +""" +replace = "@consequence" +replace_node = "conditional" + +# Before : +# false ? flag_flow : non_flag_flow +# After : +# non_flag_flow +# +[[rules]] +name = "replace_ternary_operator_false" +groups = ["if_cleanup"] +query = """ +( + (conditional + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence : (_)* @consequence + alternative: (_)* @alternative + ) +@conditional) +""" +replace = "@alternative" +replace_node = "conditional" diff --git a/test-resources/rb/replace_ternary_operator/expected/test_if_rules_ternary.rb b/test-resources/rb/replace_ternary_operator/expected/test_if_rules_ternary.rb new file mode 100644 index 000000000..73a27d066 --- /dev/null +++ b/test-resources/rb/replace_ternary_operator/expected/test_if_rules_ternary.rb @@ -0,0 +1,15 @@ +def test_if_ternary + do_something +end + +def test_if_ternary_negeated + do_something_else +end + +def test_if_ternary_parenthesized + do_something +end + +def test_if_ternary_negeated_parenthesized + do_something_else +end diff --git a/test-resources/rb/replace_ternary_operator/input/test_if_rules_ternary.rb b/test-resources/rb/replace_ternary_operator/input/test_if_rules_ternary.rb new file mode 100644 index 000000000..9faa5886e --- /dev/null +++ b/test-resources/rb/replace_ternary_operator/input/test_if_rules_ternary.rb @@ -0,0 +1,15 @@ +def test_if_ternary + true ? do_something : do_something_else +end + +def test_if_ternary_negeated + false ? do_something : do_something_else +end + +def test_if_ternary_parenthesized + (true) ? do_something : do_something_else +end + +def test_if_ternary_negeated_parenthesized + (false) ? do_something : do_something_else +end diff --git a/test-resources/rb/replace_unless_statement/configurations/rules.toml b/test-resources/rb/replace_unless_statement/configurations/rules.toml new file mode 100644 index 000000000..b3b90adad --- /dev/null +++ b/test-resources/rb/replace_unless_statement/configurations/rules.toml @@ -0,0 +1,110 @@ +# Before : +# unless false +# do_something +# else +# do_something_else +# end +# After : +# do_something +[[rules]] +name = "replace_unless_false" +groups = ["if_cleanup"] +query = """ +(unless + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + consequence: ((then) @consequence) +)@unless +""" +replace = "@consequence" +replace_node = "unless" + +# Before : +# fields_array << :field_name unless true +# After : +# +[[rules]] +name = "replace_unless_inline_true" +groups = ["if_cleanup"] +query = """ +( + (unless_modifier + body : ((_) @body) + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + )@unless_modifier +) +""" +replace = "" +replace_node = "unless_modifier" + +# Before : +# fields_array << :field_name unless false +# After : +# fields_array << :field_name +[[rules]] +name = "replace_unless_inline_false" +groups = ["if_cleanup"] +query = """ +( + (unless_modifier + body : ((_) @body) + [ + condition: (false) + condition: (parenthesized_statements (false)) + ] + )@unless_modifier +) +""" +replace = "@body" +replace_node = "unless_modifier" + +# Before : +# unless true +# do_something +# else +# do_something_else +# end +# After : +# do_something_else +[[rules]] +name = "replace_unless_true" +groups = ["if_cleanup"] +query = """ +(unless + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: (_) @consequence + alternative: (else ((_)? @alternative)) +)@unless +""" + +replace = "@alternative" +replace_node = "unless" +# Before : +# unless true +# do_something +# end +# After : +# +[[rules]] +name = "simplify_unless_statement_true_without_else" +groups = ["if_cleanup"] +query = """ +(unless + [ + condition: (true) + condition: (parenthesized_statements (true)) + ] + consequence: (_) @consequence + . +)@unless +""" +replace = "" +replace_node = "unless" diff --git a/test-resources/rb/replace_unless_statement/expected/test_unless_rules_inline.rb b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_inline.rb new file mode 100644 index 000000000..7bd00641a --- /dev/null +++ b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_inline.rb @@ -0,0 +1,13 @@ +def test_unless_statement_inline_true +end + +def test_unless_statement_inline_false + do_something +end + +def test_unless_statement_inline_true_parenthesized +end + +def test_unless_statement_inline_false_parenthesized + do_something +end diff --git a/test-resources/rb/replace_unless_statement/expected/test_unless_rules_with_else_block.rb b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_with_else_block.rb new file mode 100644 index 000000000..8404ed0e1 --- /dev/null +++ b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_with_else_block.rb @@ -0,0 +1,15 @@ +def test_unless_statement_true_with_else + do_something_else +end + +def test_unless_statement_true_parenthesized_with_else + do_something_else +end + +def test_unless_statement_false_with_else + do_something +end + +def test_unless_statement_false_parenthesized_with_else + do_something +end diff --git a/test-resources/rb/replace_unless_statement/expected/test_unless_rules_without_else_block.rb b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_without_else_block.rb new file mode 100644 index 000000000..8a7c10c31 --- /dev/null +++ b/test-resources/rb/replace_unless_statement/expected/test_unless_rules_without_else_block.rb @@ -0,0 +1,13 @@ +def test_unless_statement_true_without_else +end + +def test_unless_statement_true_parenthesized_without_else +end + +def test_unless_statement_false_without_else + do_something +end + +def test_unless_statement_false_parenthesized_without_else + do_something_else +end diff --git a/test-resources/rb/replace_unless_statement/input/test_unless_rules_inline.rb b/test-resources/rb/replace_unless_statement/input/test_unless_rules_inline.rb new file mode 100644 index 000000000..5137692f0 --- /dev/null +++ b/test-resources/rb/replace_unless_statement/input/test_unless_rules_inline.rb @@ -0,0 +1,15 @@ +def test_unless_statement_inline_true + do_something unless true +end + +def test_unless_statement_inline_false + do_something unless false +end + +def test_unless_statement_inline_true_parenthesized + do_something unless (true) +end + +def test_unless_statement_inline_false_parenthesized + do_something unless (false) +end diff --git a/test-resources/rb/replace_unless_statement/input/test_unless_rules_with_else_block.rb b/test-resources/rb/replace_unless_statement/input/test_unless_rules_with_else_block.rb new file mode 100644 index 000000000..94fdb02a8 --- /dev/null +++ b/test-resources/rb/replace_unless_statement/input/test_unless_rules_with_else_block.rb @@ -0,0 +1,31 @@ +def test_unless_statement_true_with_else + unless true + do_something + else + do_something_else + end +end + +def test_unless_statement_true_parenthesized_with_else + unless(true) + do_something + else + do_something_else + end +end + +def test_unless_statement_false_with_else + unless false + do_something + else + do_something_else + end +end + +def test_unless_statement_false_parenthesized_with_else + unless(false) + do_something + else + do_something_else + end +end diff --git a/test-resources/rb/replace_unless_statement/input/test_unless_rules_without_else_block.rb b/test-resources/rb/replace_unless_statement/input/test_unless_rules_without_else_block.rb new file mode 100644 index 000000000..7a75225a9 --- /dev/null +++ b/test-resources/rb/replace_unless_statement/input/test_unless_rules_without_else_block.rb @@ -0,0 +1,23 @@ +def test_unless_statement_true_without_else + unless true + do_something + end +end + +def test_unless_statement_true_parenthesized_without_else + unless(true) + do_something + end +end + +def test_unless_statement_false_without_else + unless false + do_something + end +end + +def test_unless_statement_false_parenthesized_without_else + unless(false) + do_something_else + end +end diff --git a/test-resources/rb/simplify_boolean_expressions/configurations/rules.toml b/test-resources/rb/simplify_boolean_expressions/configurations/rules.toml new file mode 100644 index 000000000..fcd27ab57 --- /dev/null +++ b/test-resources/rb/simplify_boolean_expressions/configurations/rules.toml @@ -0,0 +1,206 @@ +# Before : +# !false +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_not_false" +query = """ +( + (unary + operand: (false)) +@unary_expression) +""" +replace = "true" +replace_node = "unary_expression" + +# Before : +# !true +# After : +# false +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_not_true" +query = """ +( + (unary + operand: (true)) +@unary_expression) +""" +replace = "false" +replace_node = "unary_expression" + +# Before: +# (true) +# After +# true + +[[rules]] +name = "simplify_parenthesized_expression" +query = "(parenthesized_statements ([(true) (false) (identifier)] @expression)) @p_expr" +replace = "@expression" +replace_node = "p_expr" +groups = ["boolean_expression_simplify"] + +# Before : +# abc() && true +# After : +# abc() +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_something_and_true" +query = """ +( + (binary + left : (_) @lhs + operator: ["&&" "and"] + right: (true) + )@binary +) +""" +replace = "@lhs" +replace_node = "binary" + +# Before : +# true && abc() +# After : +# abc() +# + +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_true_and_something" +query = """ +( + (binary + left: (true) + operator: ["&&" "and"] + right : (_) @rhs) +) @binary +""" +replace = "@rhs" +replace_node = "binary" + + +# Before : +# abc || true +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_something_or_true" +query = """ +( + (binary + left : (_) @lhs + operator: ["||" "or"] + right: (true) + ) +@binary)""" +replace = "true" +replace_node = "binary" + +# Before : +# true || abc() +# After : +# true +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_true_or_something" +query = """ +( + (binary + left : (true) + operator: ["||" "or"] + right: (_) @rhs + ) +@binary) +""" +replace = "true" +replace_node = "binary" + +# Before : +# abc() && false +# After : +# false +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_something_and_false" +query = """ +( + (binary + left : (_) @lhs + operator: ["&&" "and"] + right: (false) + )@binary +) +""" +replace = "false" +replace_node = "binary" + +# Before : +# false && abc() +# After : +# false +# + +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_false_and_something" +query = """ +( + (binary + left : (false) + operator: ["&&" "and"] + right: (_) @rhs + )@binary +) +""" +replace = "false" +replace_node = "binary" + + +# Before : +# abc || false +# After : +# abc +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_something_or_false" +query = """ +( + (binary + left : (_) @lhs + operator: ["||" "or"] + right: (false) + ) +@binary)""" +replace = "@lhs" +replace_node = "binary" + +# Before : +# false || abc() +# After : +# abc() +# +[[rules]] +groups = ["boolean_expression_simplify"] +name = "simplify_false_or_something" +query = """ +( + (binary + left : (false) + operator: ["||" "or"] + right: (_) @rhs + ) +@binary) +""" +replace = "@rhs" +replace_node = "binary" diff --git a/test-resources/rb/simplify_boolean_expressions/expected/boolean_simplify.rb b/test-resources/rb/simplify_boolean_expressions/expected/boolean_simplify.rb new file mode 100644 index 000000000..46429daba --- /dev/null +++ b/test-resources/rb/simplify_boolean_expressions/expected/boolean_simplify.rb @@ -0,0 +1,90 @@ +def test_boolean_not_true + +end + +def test_boolean_not_false + do_something +end + +def test_parenthesized_expression_true + do_something +end + +def test_parenthesized_expression_false +end + +def test_boolean_something_and_true + if abc() + do_something + end +end + +def test_boolean_something_and_true_1 + if abc() && xyz() + do_something + end +end + +def test_boolean_true_and_something + if abc() + do_something + end +end + +def test_boolean_true_and_something_1 + if abc() && xyz() + do_something + end +end + +def test_boolean_something_and_false +end + +def test_boolean_something_and_false_1 +end + +def test_boolean_false_and_something +end + +def test_boolean_false_and_something_1 +end + +def test_boolean_something_or_true + do_something +end + +def test_boolean_something_or_true_1 + do_something +end + +def test_boolean_true_or_something + do_something +end + +def test_boolean_true_or_something_1 + do_something +end + +def test_boolean_something_or_false + if abc() + do_something + end +end + +def test_boolean_something_or_false_1 + if abc() || xyz() + do_something + end +end + +def test_boolean_false_or_something + if abc() + do_something + end +end + +def test_boolean_false_or_something_1 + if abc() || xyz() + do_something + end +end diff --git a/test-resources/rb/simplify_boolean_expressions/input/boolean_simplify.rb b/test-resources/rb/simplify_boolean_expressions/input/boolean_simplify.rb new file mode 100644 index 000000000..b6b13e063 --- /dev/null +++ b/test-resources/rb/simplify_boolean_expressions/input/boolean_simplify.rb @@ -0,0 +1,111 @@ +def test_boolean_not_true + do_something if !true +end + +def test_boolean_not_false + do_something if !false +end + +def test_parenthesized_expression_true + do_something if (true) +end + +def test_parenthesized_expression_false + do_something if (false) +end + +def test_boolean_something_and_true + if abc() && true + do_something + end +end + +def test_boolean_something_and_true_1 + if abc() && xyz() && true + do_something + end +end + +def test_boolean_true_and_something + if true && abc() + do_something + end +end + +def test_boolean_true_and_something_1 + if true && abc() && xyz() + do_something + end +end + +def test_boolean_something_and_false + if abc() && false + do_something + end +end + +def test_boolean_something_and_false_1 + if abc() && xyz() && false + do_something + end +end + +def test_boolean_false_and_something + if false && abc() + do_something + end +end + +def test_boolean_false_and_something_1 + if false && abc() && xyz() + do_something + end +end + +def test_boolean_something_or_true + if abc() || true + do_something + end +end + +def test_boolean_something_or_true_1 + if abc() || xyz() || true + do_something + end +end + +def test_boolean_true_or_something + if true || abc() + do_something + end +end + +def test_boolean_true_or_something_1 + if true || abc() || xyz() + do_something + end +end + +def test_boolean_something_or_false + if abc() || false + do_something + end +end + +def test_boolean_something_or_false_1 + if abc() || xyz() || false + do_something + end +end + +def test_boolean_false_or_something + if false || abc() + do_something + end +end + +def test_boolean_false_or_something_1 + if false || abc() || xyz() + do_something + end +end diff --git a/test-resources/rb/simplify_if_lambda_conditional_statements/configurations/rules.toml b/test-resources/rb/simplify_if_lambda_conditional_statements/configurations/rules.toml new file mode 100644 index 000000000..4aadb557e --- /dev/null +++ b/test-resources/rb/simplify_if_lambda_conditional_statements/configurations/rules.toml @@ -0,0 +1,109 @@ +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { true } +# After : +# before_action :map_user_roles, only: [:create, :update] +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (true) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :authenticate_user!, if: lambda { true } +# After : +# before_action :authenticate_user! +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (true) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (false) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :authenticate_user!, if: lambda { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (false) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +)@call +""" +replace = "" +replace_node = "call" diff --git a/test-resources/rb/simplify_if_lambda_conditional_statements/expected/simplify_if_lambda_conditional_statements.rb b/test-resources/rb/simplify_if_lambda_conditional_statements/expected/simplify_if_lambda_conditional_statements.rb new file mode 100644 index 000000000..0274e983d --- /dev/null +++ b/test-resources/rb/simplify_if_lambda_conditional_statements/expected/simplify_if_lambda_conditional_statements.rb @@ -0,0 +1,8 @@ +class CleanedUpController < ApplicationController + before_action :keep_if_action, only: [:create, :update] + before_action :set_variable + before_action :authenticate_user! + before_action :authenticate_user_with_params! + + before_action :do_not_change, only: [:create, :update], if: -> { check_something? } + end diff --git a/test-resources/rb/simplify_if_lambda_conditional_statements/input/simplify_if_lambda_conditional_statements.rb b/test-resources/rb/simplify_if_lambda_conditional_statements/input/simplify_if_lambda_conditional_statements.rb new file mode 100644 index 000000000..486bb4f90 --- /dev/null +++ b/test-resources/rb/simplify_if_lambda_conditional_statements/input/simplify_if_lambda_conditional_statements.rb @@ -0,0 +1,13 @@ +class CleanedUpController < ApplicationController + before_action :keep_if_action, only: [:create, :update], if: -> { true } + before_action :set_variable, if: ->(c) { true } + before_action :authenticate_user!, if: lambda { true } + before_action :authenticate_user_with_params!, if: lambda { |c| true } + + before_action :keep_if_action, only: [:create, :update], if: -> { false } + before_action :set_variable, if: ->(c) { false } + before_action :authenticate_user!, if: lambda { false } + before_action :authenticate_user_with_params!, if: lambda { |c| false } + + before_action :do_not_change, only: [:create, :update], if: -> { check_something? } +end diff --git a/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/edges.toml b/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/edges.toml new file mode 100644 index 000000000..05f3757dc --- /dev/null +++ b/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/edges.toml @@ -0,0 +1,4 @@ +[[edges]] +scope = "Method" +from = "delete_variable_declaration" +to = ["replace_identifier_with_value"] diff --git a/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/rules.toml b/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/rules.toml new file mode 100644 index 000000000..09c5b2995 --- /dev/null +++ b/test-resources/rb/simplify_local_variable_assigned_flag_check/configurations/rules.toml @@ -0,0 +1,55 @@ +[[rules]] +name = "delete_variable_declaration" +query = """ +( + (assignment + left: (identifier) @variable_name + right: [(true) (false)] @init + )@variable_declaration +) +""" +replace = "" +replace_node = "variable_declaration" +# Check if there is no assignment where the variable @variable_name is +# assigned to a value other than @init, within the method body +# Please note that the tree-sitter queries in the filter uses holes (i.e. `@variable_name` and `@init`). +# These holes will be filled contextually based on the code snippet matched to `rule.query +[[rules.filters]] +enclosing_node = "(method) @md" +not_contains = [""" +( + (assignment + left: (_) @a.lhs + right: (_) @a.rhs + )@assignment + (#eq? @a.lhs "@variable_name") + (#not-eq? @a.rhs "@init") +) +"""] + +# Replace identifier with value if : +# (i) There is no local variable declaration in the enclosing method with the name as the identifier +[[rules]] +name = "replace_identifier_with_value" +query = """ +( +(identifier) @identifier +(#eq? @identifier "@variable_name") +) +""" +replace = "@init" +is_seed_rule = false +replace_node = "identifier" +holes = ["variable_name", "init"] +[[rules.filters]] +# There should exist no local variable declaration named `@identifer` +enclosing_node = "(method) @md" +not_contains = [""" +( + (assignment + left: (_) @vdcl.lhs + right: (_) @vdcl.init + )@field_declaration + (#eq? @vdcl.lhs "@identifier") +) +"""] diff --git a/test-resources/rb/simplify_local_variable_assigned_flag_check/expected/simplify_local_variable_assigned_flag_check.rb b/test-resources/rb/simplify_local_variable_assigned_flag_check/expected/simplify_local_variable_assigned_flag_check.rb new file mode 100644 index 000000000..7b4663886 --- /dev/null +++ b/test-resources/rb/simplify_local_variable_assigned_flag_check/expected/simplify_local_variable_assigned_flag_check.rb @@ -0,0 +1,14 @@ +def test_cleanup_when_assigned_to_local_variable + + do_flag_enabled_stuff +end + +def test_cleanup_when_assigned_to_local_variable_and_redeclared + is_flag_enabled = true + is_flag_enabled = check_another_condition? + if(is_flag_enabled) + do_flag_enabled_stuff + else + do_non_flag_stuff + end +end diff --git a/test-resources/rb/simplify_local_variable_assigned_flag_check/input/simplify_local_variable_assigned_flag_check.rb b/test-resources/rb/simplify_local_variable_assigned_flag_check/input/simplify_local_variable_assigned_flag_check.rb new file mode 100644 index 000000000..60247b6a9 --- /dev/null +++ b/test-resources/rb/simplify_local_variable_assigned_flag_check/input/simplify_local_variable_assigned_flag_check.rb @@ -0,0 +1,18 @@ +def test_cleanup_when_assigned_to_local_variable + is_flag_enabled = true + if(is_flag_enabled) + do_flag_enabled_stuff + else + do_non_flag_stuff + end +end + +def test_cleanup_when_assigned_to_local_variable_and_redeclared + is_flag_enabled = true + is_flag_enabled = check_another_condition? + if(is_flag_enabled) + do_flag_enabled_stuff + else + do_non_flag_stuff + end +end diff --git a/test-resources/rb/simplify_rspec_block_expressions/configurations/rules.toml b/test-resources/rb/simplify_rspec_block_expressions/configurations/rules.toml new file mode 100644 index 000000000..16eaf6c6c --- /dev/null +++ b/test-resources/rb/simplify_rspec_block_expressions/configurations/rules.toml @@ -0,0 +1,28 @@ +# Before : +# before(:all) { } +# After : +# +# +[[rules]] +name = "remove_empty_rspec_blocks" +groups = ["block_removal"] +query = """ +( +(call + method: (identifier) @method + (argument_list (simple_symbol) @args) + [ + (block + !body + ) + (do_block + !body + ) + ] +)@call +(#match? @method "(before|after)") +(#match? @args "(:suite|:all|:context|:each|:example)") +) +""" +replace = "" +replace_node = "call" diff --git a/test-resources/rb/simplify_rspec_block_expressions/expected/simplify_rspec_block_expressions.rb b/test-resources/rb/simplify_rspec_block_expressions/expected/simplify_rspec_block_expressions.rb new file mode 100644 index 000000000..7f4a3dda2 --- /dev/null +++ b/test-resources/rb/simplify_rspec_block_expressions/expected/simplify_rspec_block_expressions.rb @@ -0,0 +1,3 @@ +after(:all) do + do_something +end diff --git a/test-resources/rb/simplify_rspec_block_expressions/input/simplify_rspec_block_expressions.rb b/test-resources/rb/simplify_rspec_block_expressions/input/simplify_rspec_block_expressions.rb new file mode 100644 index 000000000..137d25b7d --- /dev/null +++ b/test-resources/rb/simplify_rspec_block_expressions/input/simplify_rspec_block_expressions.rb @@ -0,0 +1,29 @@ +before(:all) do +end + +after(:all) { } + +before(:each) do +end + +after(:each) { } + +before(:suite) do +end + +after(:suite) { } + +before(:context) do +end + +after(:context) { } + +before(:example) do +end + +after(:example) { } + + +after(:all) do + do_something +end diff --git a/test-resources/rb/simplify_unless_lambda_conditional_statements/configurations/rules.toml b/test-resources/rb/simplify_unless_lambda_conditional_statements/configurations/rules.toml new file mode 100644 index 000000000..4aadb557e --- /dev/null +++ b/test-resources/rb/simplify_unless_lambda_conditional_statements/configurations/rules.toml @@ -0,0 +1,109 @@ +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { true } +# After : +# before_action :map_user_roles, only: [:create, :update] +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (true) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :authenticate_user!, if: lambda { true } +# After : +# before_action :authenticate_user! +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_true" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (true) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +) +""" +replace = "" +replace_node = "pair" + +# Before : +# before_action :map_user_roles, only: [:create, :update], if: -> { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_stabby_lambda_syntax_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (lambda + body: (block + body: (block_body + (false) + )@block_body + ) @block + ))@pair) @arguments +(#eq? @hash_key_symbol "if") +)@call +""" +replace = "" +replace_node = "call" + +# Before : +# before_action :authenticate_user!, if: lambda { false } +# After : +# +[[rules]] +name = "remove_lambda_block_with_lambda_keyword_if_block_false" +groups = ["block_removal"] +query = """ +(call + arguments: (argument_list + (pair + key: (hash_key_symbol) @hash_key_symbol + value: (call + method: (identifier) @identifier + block: (block + body: (block_body + (false) + ) + ) + ) + )@pair + ) @arguments +(#eq? @hash_key_symbol "if") +(#eq? @identifier "lambda") +)@call +""" +replace = "" +replace_node = "call" diff --git a/test-resources/rb/simplify_unless_lambda_conditional_statements/expected/simplify_unless_lambda_conditional_statements.rb b/test-resources/rb/simplify_unless_lambda_conditional_statements/expected/simplify_unless_lambda_conditional_statements.rb new file mode 100644 index 000000000..8cf76a292 --- /dev/null +++ b/test-resources/rb/simplify_unless_lambda_conditional_statements/expected/simplify_unless_lambda_conditional_statements.rb @@ -0,0 +1,8 @@ +class CleanedUpController < ApplicationController + before_action :unless_staby_syntax_keep_unless_action, only: [:create, :update] + before_action :unless_staby_syntax_set_variable + before_action :unless_staby_syntax_authenticate_user! + before_action :unless_staby_syntax_authenticate_user_with_params! + + before_action :do_not_change, only: [:create, :update], unless: -> { check_something? } +end diff --git a/test-resources/rb/simplify_unless_lambda_conditional_statements/input/simplify_unless_lambda_conditional_statements.rb b/test-resources/rb/simplify_unless_lambda_conditional_statements/input/simplify_unless_lambda_conditional_statements.rb new file mode 100644 index 000000000..3365c56e1 --- /dev/null +++ b/test-resources/rb/simplify_unless_lambda_conditional_statements/input/simplify_unless_lambda_conditional_statements.rb @@ -0,0 +1,13 @@ +class CleanedUpController < ApplicationController + before_action :unless_lambda_syntax_keep_unless_action, only: [:create, :update], unless: -> { true } + before_action :unless_lambda_syntax_set_variable, unless: ->(c) { true } + before_action :unless_lambda_syntax_authenticate_user!, unless: lambda { true } + before_action :unless_lambda_syntax_authenticate_user_with_params!, unless: lambda { |c| true } + + before_action :unless_staby_syntax_keep_unless_action, only: [:create, :update], unless: -> { false } + before_action :unless_staby_syntax_set_variable, unless: ->(c) { false } + before_action :unless_staby_syntax_authenticate_user!, unless: lambda { false } + before_action :unless_staby_syntax_authenticate_user_with_params!, unless: lambda { |c| false } + + before_action :do_not_change, only: [:create, :update], unless: -> { check_something? } +end