Module: SpacingChecker

Defined in:
lib/jscop/spacing_checker.rb

Class Method Summary collapse

Class Method Details

.check_spaces(file) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jscop/spacing_checker.rb', line 64

def self.check_spaces(file)
  seen_open = false
  counter = 0
  lines_with_spaces = []

  opening_tracker = 0
  closing_tracker = 0

  arr = file.lines
  err_type = 'SPACING_ERR'

  while counter < arr.length
    line = arr[counter]

    seen_open = true if open_curly(line.content)

    opening_tracker += 1 if line.content.match?(/{/)
    closing_tracker += 1 if line.content.match?(/}/) && !line.content.match?(/[\}][\s]*[\)]/)

    lines_with_spaces << line.number if line_beginining_spaces(line.content) unless seen_open
    opt = (opening_tracker == closing_tracker)
    seen_open = false if closed_curly(line.content) && opt

    lines_with_spaces << line.number if closing_curly_spacing(line.content) && opt
    lines_with_spaces << line.number if found_spaces(line.content) && !lines_with_spaces.nil?
    lines_with_spaces << line.number if spc_around_fn(line.content)

    counter += 1
  end

  lines_with_spaces.each { |line| raise_err(line, err_type, file.filename) if !lines_with_spaces.empty? }
end

.check_spaces_res(error_bin, path) ⇒ Object



4
5
6
7
8
9
# File 'lib/jscop/spacing_checker.rb', line 4

def self.check_spaces_res(error_bin, path)
  bad_spaced_lines = check_spaces(path)
  bad_spaced_lines.each { |line| error_bin << line if !bad_spaced_lines.empty? }

  error_bin
end

.closed_curly(cont) ⇒ Object



52
53
54
# File 'lib/jscop/spacing_checker.rb', line 52

def self.closed_curly(cont)
  cont.match?(/}/)
end

.closing_curly_spacing(cont) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/jscop/spacing_checker.rb', line 43

def self.closing_curly_spacing(cont)
  spaced_closing_curly = /[\s]+[\}][\s]*/
  c = spaced_closing_curly.match(cont)

  commented_line = cont.match?(%r{^\W+[\/\/]})

  c && !commented_line
end

.found_spaces(cont) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jscop/spacing_checker.rb', line 17

def self.found_spaces(cont)
  /(?<lhs>\w+[\W]*)\s{2,}(?<rhs>\w+[\W]*)/ =~ cont
  vsf = !Regexp.last_match.nil?

  /(?<lhs>\w+\W*)\s{2,}=\s*(?<rhs>\w+\W*)/ =~ cont
  beq = !Regexp.last_match.nil?

  /(?<lhs>\w+\W*)\s*=\s{2,}(?<rhs>\w+\W*)/ =~ cont
  aeq = !Regexp.last_match.nil?

  spaced_console = /(console.log)[\s+][\(][\w\W]+[\)]/
  spc = spaced_console.match?(cont)
  commented_line = cont.match?(%r{^\W+[\/\/]})

  !commented_line && (vsf || beq || aeq || spc)
end

.line_beginining_spaces(line) ⇒ Object



60
61
62
# File 'lib/jscop/spacing_checker.rb', line 60

def self.line_beginining_spaces(line)
  /^[\s+][\w\W]*/.match?(line)
end

.open_curly(cont) ⇒ Object



56
57
58
# File 'lib/jscop/spacing_checker.rb', line 56

def self.open_curly(cont)
  cont.match?(/{/)
end

.spc_around_fn(cont) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/jscop/spacing_checker.rb', line 34

def self.spc_around_fn(cont)
  around_funcs = /[\)][\s]{2,}[\{]/.match?(cont)
  around_classes = /[\w+\-*]*[\s]{2,}[\{]/.match?(cont)

  commented_line = cont.match?(%r{^\W+[\/\/]})

  !commented_line && (around_funcs || around_classes)
end