Sha256: 47da675ae8acfae4e94f8253934db60ce111fd904b07778d188752923bf352a2

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require_relative '../classes/error'

module SpacingChecker
  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

  private_class_method def self.raise_err(line, message, path)
    error = Error.new(line, message, path)
    spacing_err = error.print_err(line, message, path) if error
    spacing_err
  end

  def self.found_spaces(cont)
    spaces_before = /[\s]+(const|let|var|function|class|[\}$])[\s]+[\w]*/
    spaces_after = /(const|let|var|function|class)[\s]{2,}[\w]+[\s]*[\=]/
    spaced_console = /[\s]+(function|(console.log)[\(][\w]*[\)])[\s]*/
    closing_line = /[\s]+[\}][\s]*/

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

    a = spaces_before.match?(cont)
    b = spaces_after.match?(cont)
    c = spaced_console.match?(cont)

    !commented_line && (a || b || c || closing_line.match?(cont))
  end

  def self.check_spaces(file)
    lines_with_spaces = []
    check_line = lambda { |line|
      lines_with_spaces << line.number if found_spaces(line.content) && !lines_with_spaces.nil?
    }
    err_type = 'SPACING_ERR'
    file.lines.each(&check_line)

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jscop-0.1.4 lib/jscop/spacing_checker.rb