Module: SimpleCov::Directive::Erb

Defined in:
lib/simplecov/directive/erb.rb

Overview

The Ruby an ERB template holds, at the template's own line numbers, so directive comments in a template are found the way they are in a .rb file. Lexing the template itself as Ruby is not an option: %> opens a percent literal that swallows everything up to the next >, so comments after the first tag are never tokenized.

Text outside the tags is blanked and the tag delimiters become spaces, so every token keeps its line and column. A comment tag becomes a Ruby comment, which makes <%# simplecov:disable %> the template's own form of the directive.

Constant Summary collapse

SEGMENT =
/
  (?<escaped><%%)
  | <%(?<kind>\#|==?|-)?(?<body>.*?)(?<close>-?%>)
  | (?<text>[^<]+|<)
/mx

Class Method Summary collapse

Class Method Details

.ruby_lines(lines) ⇒ Object



22
23
24
25
26
27
# File 'lib/simplecov/directive/erb.rb', line 22

def self.ruby_lines(lines)
  source = lines.map { |line| line.end_with?("\n") ? line : "#{line}\n" }.join
  source.gsub(SEGMENT) { convert(Regexp.last_match) }.lines
rescue ArgumentError, EncodingError
  lines
end