diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-14 15:09:40 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-14 15:09:40 +0000 |
commit | fde3e0435c496af7dc37527f465573abd5657f5a (patch) | |
tree | 378395c009c4e9b40c3c509189531511494539e5 /rubocop | |
parent | 95671fac6e66cd23da4669706a619c1139eb1f14 (diff) | |
download | gitlab-ce-fde3e0435c496af7dc37527f465573abd5657f5a.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/gitlab/policy_rule_boolean.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/policy_rule_boolean.rb b/rubocop/cop/gitlab/policy_rule_boolean.rb new file mode 100644 index 00000000000..ca69eebab6e --- /dev/null +++ b/rubocop/cop/gitlab/policy_rule_boolean.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Gitlab + # This cop checks for usage of boolean operators in rule blocks, which + # does not work because conditions are objects, not booleans. + # + # @example + # + # # bad, `conducts_electricity` returns a Rule object, not a boolean! + # rule { conducts_electricity && batteries }.enable :light_bulb + # + # # good + # rule { conducts_electricity & batteries }.enable :light_bulb + # + # @example + # + # # bad, `conducts_electricity` returns a Rule object, so the ternary is always going to be true + # rule { conducts_electricity ? can?(:magnetize) : batteries }.enable :motor + # + # # good + # rule { conducts_electricity & can?(:magnetize) }.enable :motor + # rule { ~conducts_electricity & batteries }.enable :motor + class PolicyRuleBoolean < RuboCop::Cop::Cop + def_node_search :has_and_operator?, <<~PATTERN + (and ...) + PATTERN + + def_node_search :has_or_operator?, <<~PATTERN + (or ...) + PATTERN + + def_node_search :has_if?, <<~PATTERN + (if ...) + PATTERN + + def on_block(node) + return unless node.method_name == :rule + + if has_and_operator?(node) + add_offense(node, message: '&& is not allowed within a rule block. Did you mean to use `&`?') + end + + if has_or_operator?(node) + add_offense(node, message: '|| is not allowed within a rule block. Did you mean to use `|`?') + end + + if has_if?(node) + add_offense(node, message: 'if and ternary operators are not allowed within a rule block.') + end + end + end + end + end +end |