Class: Templatecop::Offense

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/templatecop/offense.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, offset:, rubocop_offense:, source:) ⇒ Offense

Returns a new instance of Offense.

Parameters:

  • offset (Integer)
  • rubocop_offense (RuboCop::Cop::Offense)
  • source (String)

    Template code.



40
41
42
43
44
45
# File 'lib/templatecop/offense.rb', line 40

def initialize(file_path:, offset:, rubocop_offense:, source:)
  @file_path = file_path
  @offset = offset
  @rubocop_offense = rubocop_offense
  @source = source
end

Instance Attribute Details

#file_pathString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/templatecop/offense.rb', line 13

def file_path
  @file_path
end

#offsetInteger (readonly)

Returns:

  • (Integer)


16
17
18
# File 'lib/templatecop/offense.rb', line 16

def offset
  @offset
end

Instance Method Details

#locationParser::Source::Range

Returns:

  • (Parser::Source::Range)


48
49
50
51
52
53
54
# File 'lib/templatecop/offense.rb', line 48

def location
  @location ||= ::Parser::Source::Range.new(
    buffer,
    @rubocop_offense.location.begin_pos + @offset,
    @rubocop_offense.location.end_pos + @offset
  )
end

#marshal_dumpHash

Note:

For Parallel.

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/templatecop/offense.rb', line 58

def marshal_dump
  {
    begin_pos: @rubocop_offense.location.begin_pos,
    cop_name: @rubocop_offense.cop_name,
    end_pos: @rubocop_offense.location.end_pos,
    file_path: @file_path,
    message: @rubocop_offense.message.dup.force_encoding(::Encoding::UTF_8).scrub,
    offset: @offset,
    severity: @rubocop_offense.severity.to_s,
    source: @source,
    status: @rubocop_offense.status
  }
end

#marshal_load(hash) ⇒ Object

Note:

For Parallel.

Parameters:

  • hash (Hash)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/templatecop/offense.rb', line 74

def marshal_load(hash)
  @file_path = hash[:file_path]
  @offset = hash[:offset]
  @rubocop_offense = ::RuboCop::Cop::Offense.new(
    hash[:severity],
    ::Parser::Source::Range.new(
      ::Parser::Source::Buffer.new(
        @file_path,
        source: @source
      ),
      hash[:begin_pos],
      hash[:end_pos]
    ),
    hash[:message],
    hash[:cop_name],
    hash[:status].to_sym
  )
  @source = hash[:source]
end