Module: SeeingIsBelieving::RemoveInlineComments

Extended by:
RemoveInlineComments
Included in:
RemoveInlineComments
Defined in:
lib/seeing_is_believing/remove_inline_comments.rb

Defined Under Namespace

Modules: NonLeading

Class Method Summary collapse

Class Method Details

.call(code, options = {}, &selector) ⇒ Object

selector is a block that will receive the comment object if it returns true, the comment will be removed



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/seeing_is_believing/remove_inline_comments.rb', line 29

def self.call(code, options={}, &selector)
  selector            ||= Proc.new { true }
  additional_rewrites   = options.fetch :additional_rewrites, Proc.new {}
  buffer                = Parser::Source::Buffer.new "strip_comments"
  buffer.source         = code
  parser                = Parser::CurrentRuby.new
  rewriter              = Parser::Source::Rewriter.new(buffer)
  ast, comments         = parser.parse_with_comments(buffer)
  comments.select { |comment| comment.type == :inline }
          .select { |comment| selector.call comment }
          .each   { |comment| rewriter.remove comment.location.expression }
  additional_rewrites.call buffer, rewriter
  rewriter.process
rescue Parser::SyntaxError => e
  raise SyntaxError, e.message
end