class PrelandsRails::CreateSimpleSource::ValidateZipContent::ValidateJs::Js

Принимает строку с JavaScript кодом разметкой и валидирует её.

Constants

FUNCS_RX
LOCATION_RX

Attributes

errors[R]

Public Class Methods

new(string) click to toggle source

@param [String] string Содержимое JavaScript файла.

# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_js/js.rb, line 14
def initialize(string)
  @errors = []
  @string = string.gsub(/(\r\n?|\n)/, '') # убираем переносы, мешающие регуляркам
end

Public Instance Methods

valid?() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_js/js.rb, line 19
def valid?
  @errors = [
    check_location_changes,
    check_framework_calls
  ].compact

  !@errors.any?
end

Private Instance Methods

check_framework_calls() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_js/js.rb, line 38
def check_framework_calls
  res = FUNCS_RX.map do |key, func|
    [ key, !!@string[func] ]
  end

  return if res.map { |kf| kf[1] }.all?

  res.to_h.map do |key, result|
    'Call of %s() not found' % key if !result
  end.compact.join('; ')
end
check_location_changes() click to toggle source
# File lib/prelands_rails/create_simple_source/validate_zip_content/validate_js/js.rb, line 30
def check_location_changes
  match_data    = @string.match LOCATION_RX
  is_loc_assign = ->(matched) { matched =~ /^('|")$/ || @string.match(/(;|\s*)#{matched}\s*=\s*('|")/) }
  return unless match_data && is_loc_assign[match_data[3]]

  'Location statement has found'
end