Class: Suspenders::Actions::StripCommentsAction::StripComments

Inherits:
Object
  • Object
show all
Defined in:
lib/suspenders/actions/strip_comments_action.rb

Overview

Strips full-line and inline comments from a buffer but does not remove whitespaces or newlines after the fact. Example input:

MyGem.application.configure do |config|
  # Full-line comment
  config.option1 = :value # Inline comment
end

The output is:

MyGem.application.configure do |config|

  config.option1 = :value
end

Class Method Summary collapse

Class Method Details

.call(source, parser) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/suspenders/actions/strip_comments_action.rb', line 53

def call(source, parser)
  buffer = Parser::Source::Buffer.new(nil, source: source)
  rewriter = Parser::Source::TreeRewriter.new(buffer)

  _, comments = parser.parse_with_comments(buffer)

  comments.each do |comment|
    strip_comment(comment, buffer, rewriter)
  end

  rewriter.process
end