Sha256: 75aa1341e3f59aa03fb98a9ae3048e5b0bc817974ef883186be165ae7b0a8261
Contents?: true
Size: 1.41 KB
Versions: 13
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true module RuboCop module Cop # Common functionality for cops checking for missing space after # punctuation. module SpaceAfterPunctuation MSG = 'Space missing after %s.'.freeze def investigate(processed_source) each_missing_space(processed_source.tokens) do |token| add_offense(token, token.pos, format(MSG, kind(token))) end end def each_missing_space(tokens) tokens.each_cons(2) do |t1, t2| next unless kind(t1) next unless space_missing?(t1, t2) next unless space_required_before?(t2) yield t1 end end def space_missing?(t1, t2) t1.pos.line == t2.pos.line && t2.pos.column == t1.pos.column + offset end def space_required_before?(token) !(allowed_type?(token) || (token.type == :tRCURLY && space_forbidden_before_rcurly?)) end def allowed_type?(token) [:tRPAREN, :tRBRACK, :tPIPE].include?(token.type) end def space_forbidden_before_rcurly? style = space_style_before_rcurly style == 'no_space' end # The normal offset, i.e., the distance from the punctuation # token where a space should be, is 1. def offset 1 end def autocorrect(token) ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') } end end end end
Version data entries
13 entries across 13 versions & 2 rubygems