Class: LintFu::Scan

Inherits:
Object
  • Object
show all
Defined in:
lib/lint_fu/scan.rb

Constant Summary collapse

COMMENT =
/^\s*#/
VERBOSE_BLESSING_COMMENT =
/#\s*(lint|security)\s*[-:]\s*not\s*a?n?\s*([a-z0-9 ]*) ?(as|because|;)\s*(.*)/i
BLESSING_COMMENT =
/#\s*(lint|security)\s*[-:]\s*not\s*a?n?\s*([a-z0-9 ]*)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs_root) ⇒ Scan

Returns a new instance of Scan.



11
12
13
14
# File 'lib/lint_fu/scan.rb', line 11

def initialize(fs_root)
  @fs_root = fs_root
  @issues = Set.new
end

Instance Attribute Details

#fs_rootObject (readonly)

Returns the value of attribute fs_root.



9
10
11
# File 'lib/lint_fu/scan.rb', line 9

def fs_root
  @fs_root
end

#issuesObject (readonly)

Returns the value of attribute issues.



9
10
11
# File 'lib/lint_fu/scan.rb', line 9

def issues
  @issues
end

Instance Method Details

#blessed?(issue) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lint_fu/scan.rb', line 16

def blessed?(issue)
  comments = preceeding_comments(issue.sexp)
  return false unless comments

  match = nil
  comments.each do |line|
    match = VERBOSE_BLESSING_COMMENT.match(line)
    match = BLESSING_COMMENT.match(line) unless match
    break if match
  end

  return false unless match
  blessed_issue_class = match[2].downcase.split(/\s+/).join('_').camelize

  # Determine whether the blessed issue class appears anywhere in the class hierarchy of
  # issue_class.
  klass = issue.class
  while klass
    return true if klass.name.index(blessed_issue_class)
    klass = klass.superclass
  end

  return false
end

#preceeding_comments(sexp) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lint_fu/scan.rb', line 41

def preceeding_comments(sexp)
  @file_contents ||= {}
  @file_contents[sexp.file] ||= File.readlines(sexp.file)
  cont = @file_contents[sexp.file]

  comments = ''

  max_line = sexp.line - 1 - 1
  max_line = 0 if max_line < 0
  min_line = max_line

  while cont[min_line] =~ COMMENT && min_line >= 0
    min_line -= 1
  end

  if cont[max_line] =~ COMMENT
    min_line +=1 unless min_line == max_line
    return cont[min_line..max_line]
  else
    return nil
  end
end