Sha256: 266fd4241792d6706c656490cf708a0d1cf3971290573ed3ec96293aae9529a7
Contents?: true
Size: 1.31 KB
Versions: 5
Compression:
Stored size: 1.31 KB
Contents
# encoding: utf-8 module Rubocop module Cop class Indentation < Cop ERROR_MESSAGE = 'Indent when as deep as case.' def inspect(file, source, tokens, sexp) case_tokens = find_keywords(tokens, 'case') when_tokens = find_keywords(tokens, 'when') each_when(sexp) do |case_ix| when_pos = when_tokens.shift.pos if when_pos.column != case_tokens[case_ix].pos.column add_offence(:convention, when_pos.lineno, ERROR_MESSAGE) end end end def find_keywords(tokens, keyword) indexes = tokens.each_index.find_all do |ix| keyword?(tokens, ix, keyword) end tokens.values_at(*indexes) end def keyword?(tokens, ix, keyword) [tokens[ix].type, tokens[ix].text] == [:on_kw, keyword] && tokens[ix - 1].type != :on_symbeg end # Does a depth first search for :when, yielding the index of the # corresponding :case for each one. def each_when(sexp, case_ix = -1, &block) if sexp[0] == :case @total_case_ix = (@total_case_ix || -1) + 1 each_when(sexp[2], @total_case_ix, &block) else yield case_ix if sexp[0] == :when sexp.grep(Array).each { |s| each_when(s, case_ix, &block) } end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems