src/cleanup_rules/ruby/rules.toml (1,103 lines of code) (raw):

# 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) operand: (parenthesized_statements (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) operand: (parenthesized_statements (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 : # after_commit :do_something, on: :create, if: Proc.new { true } # After : # after_commit :do_something, on: :create # [[rules]] name = "remove_Proc_if_block_true" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call receiver: (constant) @receiver block: (block body: (block_body (true) ) ) ) )@proc_pair )@arguments )@call (#eq? @operator "if") (#eq? @receiver "Proc") ) """ replace = "" replace_node = "proc_pair" # Before : # after_commit :do_something, on: :create, if: proc { true } # After : # after_commit :do_something, on: :create # [[rules]] name = "remove_proc_if_block_true" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call method: (identifier) @receiver block: (block body: (block_body (true) ) ) ) )@proc_pair ) )@call (#eq? @operator "if") (#eq? @receiver "proc") ) """ replace = "" replace_node = "proc_pair" # Before : # after_commit :do_something, on: :create, if: Proc.new { false } # After : # # [[rules]] name = "remove_Proc_if_block_false" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call receiver: (constant) @receiver block: (block body: (block_body (false) ) ) ) )@proc_pair )@arguments )@call (#eq? @operator "if") (#eq? @receiver "Proc") ) """ replace = "" replace_node = "call" # Before : # after_commit :do_something, on: :create, if: proc { false } # After : # # [[rules]] name = "remove_proc_if_block_false" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call method: (identifier) @receiver block: (block body: (block_body (false) ) ) ) )@proc_pair ) )@call (#eq? @operator "if") (#eq? @receiver "proc") ) """ replace = "" replace_node = "call" # Before : # after_commit :do_something, on: :create, unless: Proc.new { true } # After : # # [[rules]] name = "remove_Proc_unless_block_true" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call receiver: (constant) @receiver block: (block body: (block_body (true) ) ) ) )@proc_pair )@arguments )@call (#eq? @operator "unless") (#eq? @receiver "Proc") ) """ replace = "" replace_node = "call" # Before : # after_commit :do_something, on: :create, unless: proc { true } # After : # # [[rules]] name = "remove_proc_unless_block_true" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call method: (identifier) @receiver block: (block body: (block_body (true) ) ) ) )@proc_pair ) )@call (#eq? @operator "unless") (#eq? @receiver "proc") ) """ replace = "" replace_node = "call" # Before : # after_commit :do_something, on: :create, unless: Proc.new { false } # After : # after_commit :do_something, on: :create # [[rules]] name = "remove_Proc_unless_block_false" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call receiver: (constant) @receiver block: (block body: (block_body (false) ) ) ) )@proc_pair )@arguments )@call (#eq? @operator "unless") (#eq? @receiver "Proc") ) """ replace = "" replace_node = "proc_pair" # Before : # after_commit :do_something, on: :create, unless: proc { false } # After : # after_commit :do_something, on: :create # [[rules]] name = "remove_proc_unless_block_false" groups = ["block_removal"] query = """ ( (call arguments: (argument_list (pair key: (hash_key_symbol) @operator value: (call method: (identifier) @receiver block: (block body: (block_body (false) ) ) ) )@proc_pair ) )@call (#eq? @operator "unless") (#eq? @receiver "proc") ) """ replace = "" replace_node = "proc_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) (instance_variable)] @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) (instance_variable)] @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