Class: Xing::Utils::ImportChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/utils/import_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, context) ⇒ ImportChecker

Returns a new instance of ImportChecker.



4
5
6
7
# File 'lib/xing/utils/import_checker.rb', line 4

def initialize(path, context)
  @path = path
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/xing/utils/import_checker.rb', line 9

def context
  @context
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/xing/utils/import_checker.rb', line 9

def path
  @path
end

Instance Method Details

#check(&error_block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xing/utils/import_checker.rb', line 67

def check(&error_block)
  begin
    initialize_check(error_block)
    while @lineno < @lines.length
      read_next
      if is_import_line
        skip_to_end_of_import
        match_line
        check_structure if !check_empty_match
      end
      @lineno += 1
    end
  rescue
  end
end

#check_empty_matchObject



36
37
38
39
40
41
42
43
# File 'lib/xing/utils/import_checker.rb', line 36

def check_empty_match
  if @md.nil?
    problem "doesn't seem to have a 'from' clause..."
    true
  else
    false
  end
end

#check_structureObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xing/utils/import_checker.rb', line 45

def check_structure
  if /\.\./ =~ @md[:from]
    problem "from includes .. not at pos 0" if /\A\.\./ !~ @md[:from]
    problem "from includes .. after words" if /\w.*\.\./ =~ @md[:from]
    unless (violation = %r{(?<dir>\w+)/\w}.match @md[:from]).nil?
      unless %r{\.\./(#{context.escape_clause_list.join("|")})} =~ @md[:from]
        problem "Imports Rule: 'from' includes ../ and then #{violation[:dir].inspect} not in #{context.escape_clause_list.inspect}"
      end
    end
  end
end

#initialize_check(error_block) ⇒ Object



61
62
63
64
65
# File 'lib/xing/utils/import_checker.rb', line 61

def initialize_check(error_block)
  @error_block = error_block
  @lines = File.read(path).lines
  @lineno = 0
end

#is_import_lineObject



15
16
17
# File 'lib/xing/utils/import_checker.rb', line 15

def is_import_line
  /^\s*import/.match(@import_line)
end

#match_lineObject



32
33
34
# File 'lib/xing/utils/import_checker.rb', line 32

def match_line
  @md = /.*from (?<quote>['"])(?<from>.*)\k<quote>/.match(@import_line)
end

#problem(message) ⇒ Object



57
58
59
# File 'lib/xing/utils/import_checker.rb', line 57

def problem(message)
  @error_block.call(message, @import_line, @lineno)
end

#read_nextObject



11
12
13
# File 'lib/xing/utils/import_checker.rb', line 11

def read_next
  @import_line = @lines[@lineno]
end

#skip_to_end_of_importObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xing/utils/import_checker.rb', line 19

def skip_to_end_of_import
  # for a multi-line import, we need to skip past multiple lines of import
  begin_check = /.*(?<begin>\{).*/.match(@import_line)
  end_check = /.*(?<end>\}).*/.match(@import_line)
  if begin_check and !end_check
    until end_check
      @lineno += 1
      read_next
      end_check = /.*(?<end>\}).*/.match(@import_line)
    end
  end
end