|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "ripper" |
| 4 | + |
| 5 | +module SimpleCov |
| 6 | + # Parses `# simplecov:disable` / `# simplecov:enable` directive comments. |
| 7 | + # |
| 8 | + # Two forms are supported: |
| 9 | + # |
| 10 | + # Block form (the directive is the entire comment on its own line) opens a |
| 11 | + # region that runs until the matching `# simplecov:enable`: |
| 12 | + # |
| 13 | + # # simplecov:disable line |
| 14 | + # ... |
| 15 | + # # simplecov:enable line |
| 16 | + # |
| 17 | + # Inline form (the directive trails real code on the same line) only affects |
| 18 | + # that single line and does not need to be re-enabled: |
| 19 | + # |
| 20 | + # raise "absurd" # simplecov:disable |
| 21 | + # |
| 22 | + # Categories are `:line`, `:branch`, and `:method`. They may be combined |
| 23 | + # with commas. Omitting categories targets all three. |
| 24 | + # |
| 25 | + # Any text after the directive (and the optional category list) is treated |
| 26 | + # as a free-form reason and discarded: |
| 27 | + # |
| 28 | + # # simplecov:disable line not worth testing this glue |
| 29 | + # |
| 30 | + # As a consequence, an unrecognised category name silently falls into the |
| 31 | + # reason bucket. `# simplecov:disable cyclomatic` is parsed as the bare |
| 32 | + # form (disable everything) with reason "cyclomatic" — a deliberate |
| 33 | + # over-disable so the typo is visible in the report rather than silently |
| 34 | + # disabling nothing. |
| 35 | + # |
| 36 | + # Comment extraction goes through `Ripper.lex` so directive markers inside |
| 37 | + # string literals or heredocs are correctly ignored. |
| 38 | + class Directive |
| 39 | + CATEGORIES = %i[line branch method].freeze |
| 40 | + |
| 41 | + CATEGORY_PATTERN = "(?:#{CATEGORIES.join('|')})" |
| 42 | + CATEGORIES_PATTERN = "(?:#{CATEGORY_PATTERN}(?:\\s*,\\s*#{CATEGORY_PATTERN})*)" |
| 43 | + PATTERN = / |
| 44 | + \#\s*simplecov\s*:\s* |
| 45 | + (?<mode>disable|enable)\b |
| 46 | + (?:\s+(?<categories>#{CATEGORIES_PATTERN}))? |
| 47 | + .*? |
| 48 | + \s*\z |
| 49 | + /x.freeze |
| 50 | + |
| 51 | + attr_reader :line_number, :mode, :categories |
| 52 | + |
| 53 | + # Walk an array of source lines and return the disabled line ranges per |
| 54 | + # category as `{ line: [Range, ...], branch: [...], method: [...] }`. |
| 55 | + # An unclosed `disable` block extends to the end of the file. |
| 56 | + def self.disabled_ranges(src_lines) |
| 57 | + lines = src_lines.to_a |
| 58 | + ranges = CATEGORIES.to_h { |category| [category, []] } |
| 59 | + open_starts = {} |
| 60 | + |
| 61 | + directives_in(lines).each { |directive| directive.apply(ranges, open_starts) } |
| 62 | + open_starts.each { |category, start| ranges[category] << (start..lines.size) } |
| 63 | + |
| 64 | + ranges |
| 65 | + end |
| 66 | + |
| 67 | + # Extract every directive in the file, in source order. Comments inside |
| 68 | + # string literals or heredocs are skipped because Ripper.lex doesn't tag |
| 69 | + # them as :on_comment tokens. |
| 70 | + def self.directives_in(lines) |
| 71 | + return [] unless source_might_contain_directive?(lines) |
| 72 | + |
| 73 | + comments_in(lines).filter_map do |line_number, column, text| |
| 74 | + parse_comment(lines, line_number, column, text) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + # Cheap pre-check so we don't tokenize files that obviously can't contain |
| 79 | + # a directive. |
| 80 | + def self.source_might_contain_directive?(lines) |
| 81 | + lines.any? do |line| |
| 82 | + line.include?("simplecov") |
| 83 | + rescue ArgumentError, EncodingError |
| 84 | + false |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def self.parse_comment(lines, line_number, column, text) |
| 89 | + match = PATTERN.match(text) |
| 90 | + return nil unless match |
| 91 | + |
| 92 | + new( |
| 93 | + line_number: line_number, |
| 94 | + mode: match[:mode].to_sym, |
| 95 | + categories: parse_categories(match[:categories]), |
| 96 | + inline: inline?(lines, line_number, column + match.begin(0)) |
| 97 | + ) |
| 98 | + rescue ArgumentError, EncodingError |
| 99 | + # E.g., comment text contains an invalid byte sequence in UTF-8. |
| 100 | + nil |
| 101 | + end |
| 102 | + |
| 103 | + def self.parse_categories(text) |
| 104 | + return CATEGORIES.dup if text.nil? |
| 105 | + |
| 106 | + text.split(/\s*,\s*/).map(&:to_sym) |
| 107 | + end |
| 108 | + |
| 109 | + # Whether the directive sits after non-whitespace content on its line. |
| 110 | + # `column` is the byte column of the directive's `#` in the source line, |
| 111 | + # adjusted for any prefix that may precede it within the comment token |
| 112 | + # (e.g., `# prefix # simplecov:disable line`). |
| 113 | + def self.inline?(lines, line_number, column) |
| 114 | + line = lines[line_number - 1].to_s |
| 115 | + !line.byteslice(0, column).to_s.strip.empty? |
| 116 | + rescue ArgumentError, EncodingError |
| 117 | + false |
| 118 | + end |
| 119 | + |
| 120 | + def self.comments_in(lines) |
| 121 | + source = lines.map { |line| line.end_with?("\n") ? line : "#{line}\n" }.join |
| 122 | + Ripper.lex(source).filter_map do |(line_number, column), type, text| |
| 123 | + [line_number, column, text] if type == :on_comment |
| 124 | + end |
| 125 | + rescue ArgumentError, EncodingError |
| 126 | + [] |
| 127 | + end |
| 128 | + |
| 129 | + private_class_method :directives_in, :source_might_contain_directive?, |
| 130 | + :parse_comment, :parse_categories, :inline?, :comments_in |
| 131 | + |
| 132 | + def initialize(line_number:, mode:, categories:, inline:) |
| 133 | + @line_number = line_number |
| 134 | + @mode = mode |
| 135 | + @categories = categories |
| 136 | + @inline = inline |
| 137 | + end |
| 138 | + |
| 139 | + def disabled? |
| 140 | + mode == :disable |
| 141 | + end |
| 142 | + |
| 143 | + def enabled? |
| 144 | + mode == :enable |
| 145 | + end |
| 146 | + |
| 147 | + def inline? |
| 148 | + @inline |
| 149 | + end |
| 150 | + |
| 151 | + # Apply this directive's effect to the in-flight per-category state. |
| 152 | + # Inline directives mark just their line; block disables open a region; |
| 153 | + # block enables close one. Re-opening an already-open block is a no-op. |
| 154 | + def apply(ranges, open_starts) |
| 155 | + categories.each do |category| |
| 156 | + if inline? |
| 157 | + ranges[category] << (line_number..line_number) if disabled? |
| 158 | + elsif disabled? |
| 159 | + open_starts[category] ||= line_number |
| 160 | + elsif (start = open_starts.delete(category)) |
| 161 | + ranges[category] << (start..line_number) |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments