Class: RuboCop::MagicComment::SimpleComment

Inherits:
RuboCop::MagicComment show all
Defined in:
lib/rubocop/magic_comment.rb

Overview

Wrapper for regular magic comments not bound to an editor.

Simple comments can only specify one setting per comment.

Examples:

frozen string literal comments

comment1 = RuboCop::MagicComment.parse('# frozen_string_literal: true')
comment1.frozen_string_literal # => true
comment1.encoding              # => nil

encoding comments

comment2 = RuboCop::MagicComment.parse('# encoding: utf-8')
comment2.frozen_string_literal # => nil
comment2.encoding              # => 'utf-8'

Constant Summary

Constants inherited from RuboCop::MagicComment

KEYWORDS, TOKEN

Instance Method Summary collapse

Methods inherited from RuboCop::MagicComment

#any?, #encoding_specified?, #frozen_string_literal, #frozen_string_literal?, #frozen_string_literal_specified?, #initialize, parse, #shareable_constant_value, #shareable_constant_value_specified?, #valid?, #valid_literal_value?, #valid_shareable_constant_value?

Constructor Details

This class inherits a constructor from RuboCop::MagicComment

Instance Method Details

#encodingObject

Match `encoding` or `coding`


241
242
243
# File 'lib/rubocop/magic_comment.rb', line 241

def encoding
  extract(/\A\s*\#.*\b#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
end

#without(type) ⇒ Object

Rewrite the comment without a given token type


246
247
248
249
250
251
252
# File 'lib/rubocop/magic_comment.rb', line 246

def without(type)
  if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/)
    ''
  else
    @comment
  end
end