Class: GraphQL::Modernize::SuppressionIndex
- Inherits:
-
Object
- Object
- GraphQL::Modernize::SuppressionIndex
- Defined in:
- lib/graphql/modernize/suppression_index.rb
Defined Under Namespace
Classes: Directive
Constant Summary collapse
- PATTERN =
/graphql-modernize:(?<action>disable-file|disable|enable)\b(?<body>.*)/
Instance Method Summary collapse
-
#initialize(source_file, require_reason: false) ⇒ SuppressionIndex
constructor
A new instance of SuppressionIndex.
- #suppressed?(rule_id, location) ⇒ Boolean
- #unused_offenses ⇒ Object
Constructor Details
#initialize(source_file, require_reason: false) ⇒ SuppressionIndex
Returns a new instance of SuppressionIndex.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/graphql/modernize/suppression_index.rb', line 9 def initialize(source_file, require_reason: false) @source_file = source_file first_statement = source_file.ast.statements.body.first @directives = source_file.comments.filter_map do |comment| match = comment.location.slice.match(PATTERN) next unless match reason = match[:body].split("--", 2)[1] next if require_reason && match[:action] != "enable" && reason.to_s.strip.empty? rules_text = match[:body].split("--", 2).first.strip ids = rules_text.split(/[,\s]+/) valid_rules = rules_text.empty? || ids.all? { |id| id.match?(/\AGQLM\d{3}\z/) } line_start = source_file.line_start(comment.location.start_offset) prefix = source_file.source.byteslice(line_start, comment.location.start_offset - line_start) inline = !prefix.strip.empty? valid_file_position = match[:action] != "disable-file" || (!inline && (!first_statement || comment.location.start_offset < first_statement.location.start_offset)) Directive.new(action: match[:action].tr("-", "_").to_sym, line: comment.location.start_line, rules: rules_text.empty? ? nil : ids.freeze, location: comment.location, inline: inline, valid: valid_rules && valid_file_position, used: false) end end |
Instance Method Details
#suppressed?(rule_id, location) ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/graphql/modernize/suppression_index.rb', line 33 def suppressed?(rule_id, location) directives = matching_directives(rule_id, location.start_line) file_directive = directives.find { |directive| directive.action == :disable_file } if file_directive file_directive.used = true return true end inline = directives.find do |directive| directive.inline && directive.action == :disable && directive.line == location.start_line end if inline inline.used = true return true end active_directive = nil directives.reject(&:inline).each do |directive| active_directive = directive.action == :disable ? directive : nil end if active_directive active_directive.used = true closing = @directives.find do |directive| directive.valid && !directive.inline && directive.action == :enable && directive.line > active_directive.line && matches_rule?(directive, rule_id) end closing.used = true if closing end !active_directive.nil? end |
#unused_offenses ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/graphql/modernize/suppression_index.rb', line 65 def unused_offenses @directives.reject(&:used).map do |directive| Offense.new(rule_id: "GQLM000", location: directive.location, message: "Unused suppression directive", safety: :manual, path: @source_file.path, correctable: false) end end |