Class: PlatformosCheck::SpaceInsideBraces

Inherits:
LiquidCheck show all
Defined in:
lib/platformos_check/checks/space_inside_braces.rb

Overview

Ensure … % & … } have consistent spaces.

Defined Under Namespace

Classes: BlockMarkup

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #platformos_app

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Instance Method Details

#add_offense_for_match(message, match, node, source_offset) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 140

def add_offense_for_match(message, match, node, source_offset, &)
  add_offense(
    message,
    node:,
    markup: match[:offense],
    node_markup_offset: source_offset + match.begin(:offense),
    &
  )
end

#add_space_missing_after_offense(match, node, source_offset) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 76

def add_space_missing_after_offense(match, node, source_offset)
  add_offense_for_match(
    "Space missing after '#{match[:token]}'",
    match,
    node,
    source_offset
  ) do |corrector|
    corrector.insert_after(
      node,
      ' ',
      (node.start_index + source_offset + match.begin(:token))...
      (node.start_index + source_offset + match.end(:token))
    )
  end
end

#add_space_missing_before_offense(match, node, source_offset) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 108

def add_space_missing_before_offense(match, node, source_offset)
  add_offense_for_match(
    "Space missing before '#{match[:token]}'",
    match,
    node,
    source_offset
  ) do |corrector|
    corrector.insert_before(
      node,
      ' ',
      (node.start_index + source_offset + match.begin(:token))...
      (node.start_index + source_offset + match.end(:token))
    )
  end
end

#add_too_many_spaces_after_offense(match, node, source_offset) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 92

def add_too_many_spaces_after_offense(match, node, source_offset)
  add_offense_for_match(
    "Too many spaces after '#{match[:token]}'",
    match,
    node,
    source_offset
  ) do |corrector|
    corrector.replace(
      node,
      ' ',
      (node.start_index + source_offset + match.begin(:offense))...
      (node.start_index + source_offset + match.end(:offense))
    )
  end
end

#add_too_many_spaces_before_offense(match, node, source_offset) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 124

def add_too_many_spaces_before_offense(match, node, source_offset)
  add_offense_for_match(
    "Too many spaces before '#{match[:token]}'",
    match,
    node,
    source_offset
  ) do |corrector|
    corrector.replace(
      node,
      ' ',
      (node.start_index + source_offset + match.begin(:offense))...
      (node.start_index + source_offset + match.end(:offense))
    )
  end
end

#on_node(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 10

def on_node(node)
  return unless node.markup
  return if node.literal?
  return if node.assigned_or_echoed_variable?

  outside_of_strings(node.markup) do |chunk, chunk_start|
    chunk.scan(/(?<token>[,:|]|==|<>|<=|>=|<|>|!=)(?<offense>  +)/) do |_match|
      add_too_many_spaces_after_offense(Regexp.last_match, node, chunk_start)
    end
    chunk.scan(/(?<offense>(?<token>[,:|]|==|<>|<=|>=|<\b|>\b|!=)(\S|\z))/) do |_match|
      add_space_missing_after_offense(Regexp.last_match, node, chunk_start)
    end
    chunk.scan(/(?<offense>\s{2,})(?<token>\||==|<>|<=|>=|<|>|!=)+/) do |_match|
      add_too_many_spaces_before_offense(Regexp.last_match, node, chunk_start) unless Regexp.last_match(:offense).include?("\n")
    end
    chunk.scan(/(\A|\S)(?<offense>(?<token>\||==|<>|<=|>=|<|\b>|!=))/) do |_match|
      add_space_missing_before_offense(Regexp.last_match, node, chunk_start)
    end
  end
end

#on_tag(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 33

def on_tag(node)
  return if node.inside_liquid_tag?

  # Both the start and end tags
  blocks = [
    BlockMarkup.new(node.block_start_markup, node.block_start_start_index - node.start_index),
    BlockMarkup.new(node.block_end_markup, node.block_end_start_index - node.start_index)
  ]

  blocks.each do |block|
    # Looking at spaces after the start token
    add_space_missing_after_offense(Regexp.last_match, node, block.node_markup_offset) if block.markup =~ /^(?<token>{%-?)(?<offense>[^ \n\t-])/

    add_too_many_spaces_after_offense(Regexp.last_match, node, block.node_markup_offset) if block.markup =~ /^(?<token>{%-?)(?<offense> {2,})\S/

    # Looking at spaces before the end token
    add_space_missing_before_offense(Regexp.last_match, node, block.node_markup_offset) if block.markup =~ /(?<offense>[^ \n\t-])(?<token>-?%})$/

    add_too_many_spaces_before_offense(Regexp.last_match, node, block.node_markup_offset) if block.markup =~ /\S(?<offense> {2,})(?<token>-?%})$/

    next
  end
end

#on_variable(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/platformos_check/checks/space_inside_braces.rb', line 57

def on_variable(node)
  return if node.markup.empty?
  return if node.assigned_or_echoed_variable?

  block_start_offset = node.block_start_start_index - node.start_index

  # Looking at spaces after the start token
  add_space_missing_after_offense(Regexp.last_match, node, block_start_offset) if node.block_start_markup =~ /^(?<token>{{-?)(?<offense>[^ \n\t-])/

  add_too_many_spaces_after_offense(Regexp.last_match, node, block_start_offset) if node.block_start_markup =~ /^(?<token>{{-?)(?<offense> {2,})\S/

  # Looking at spaces before the end token
  add_space_missing_before_offense(Regexp.last_match, node, block_start_offset) if node.block_start_markup =~ /(?<offense>[^ \n\t-])(?<token>-?}})$/

  return unless node.block_start_markup =~ /\S(?<offense> {2,})(?<token>-?}})$/

  add_too_many_spaces_before_offense(Regexp.last_match, node, block_start_offset)
end