Class: Xing::Edicts::StructureChecker

Inherits:
Edict::Rule
  • Object
show all
Includes:
Find
Defined in:
lib/xing/edicts/structure-checker.rb

Defined Under Namespace

Classes: Context, Error, Problem

Instance Method Summary collapse

Instance Method Details

#actionObject



20
21
22
23
# File 'lib/xing/edicts/structure-checker.rb', line 20

def action
  analyze
  report
end

#analyzeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xing/edicts/structure-checker.rb', line 25

def analyze
  context = context_from_hash(context_hash || {:escapes => %w{common framework build}})
  return unless File.directory?(dir)
  find(dir) do |path|
    if File.directory?(path)
    else
      next if File.basename(path)[0] == ?.
      case path
      when /\.js\z/
        check_imports(path, context)
      end
    end
  end
end

#check_imports(path, context) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xing/edicts/structure-checker.rb', line 76

def check_imports(path, context)
  File.read(path).lines.grep(/\s*import/).each_with_index do |import_line, lineno|
    md = /.*from (?<quote>['"])(?<from>.*)\k<quote>/.match(import_line)
    if md.nil?
      problem "doesn't seem to have a 'from' clause...", import_line, lineno, path
    end

    if /\.\./ =~ md[:from]
      if /\A\.\./ !~ md[:from]
        problem "from includes .. not at pos 0", import_line, lineno, path
      end
      if /\w.*\.\./ =~ md[:from]
        problem "from includes .. after words", import_line, lineno, path
      end
      if !(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}", import_line, lineno, path
        end
      end
    end
  end
end

#context_from_hash(hash) ⇒ Object



40
41
42
# File 'lib/xing/edicts/structure-checker.rb', line 40

def context_from_hash(hash)
  Context.new(hash)
end

#problem(msg, line, lineno, file) ⇒ Object



72
73
74
# File 'lib/xing/edicts/structure-checker.rb', line 72

def problem(msg, line, lineno, file)
  @problems << Problem.new(msg, line.chomp, lineno + 1, file)
end

#reportObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xing/edicts/structure-checker.rb', line 44

def report
  unless @problems.empty?
    @problems.group_by do |problem|
      problem.file
    end.each do |file, problems|
      out_stream.puts "In #{file}"
      problems.each{|prob| out_stream.puts "  " + prob.to_s}
      out_stream.puts
    end
    raise Error, "Problems found in ECMAScript structure"
  end
end

#setupObject



16
17
18
# File 'lib/xing/edicts/structure-checker.rb', line 16

def setup
  @problems = []
end