Class: JsDuck::Lint

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/lint.rb

Overview

Reports bugs and problems in documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relations) ⇒ Lint

Returns a new instance of Lint.



9
10
11
# File 'lib/jsduck/lint.rb', line 9

def initialize(relations)
  @relations = relations
end

Instance Attribute Details

#relationsObject

Returns the value of attribute relations.



7
8
9
# File 'lib/jsduck/lint.rb', line 7

def relations
  @relations
end

Instance Method Details

#each_member(&block) ⇒ Object

Loops through all members of all classes



98
99
100
# File 'lib/jsduck/lint.rb', line 98

def each_member(&block)
  @relations.each {|cls| cls.all_local_members.each(&block) }
end

#runObject

Runs the linter



14
15
16
17
18
19
20
# File 'lib/jsduck/lint.rb', line 14

def run
  warn_no_doc
  warn_unnamed
  warn_optional_params
  warn_duplicate_params
  warn_duplicate_members
end

#warn(type, msg, member) ⇒ Object

Prints warning + filename and linenumber from doc-context



103
104
105
106
# File 'lib/jsduck/lint.rb', line 103

def warn(type, msg, member)
  context = member[:files][0]
  Logger.instance.warn(type, msg, context[:filename], context[:linenr])
end

#warn_duplicate_membersObject

print warnings for duplicate member names



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jsduck/lint.rb', line 79

def warn_duplicate_members
  @relations.each do |cls|
    members = {:members => {}, :statics => {}}
    cls.all_local_members.each do |m|
      group = (m[:meta] && m[:meta][:static]) ? :statics : :members
      type = m[:tagname]
      name = m[:name]
      hash = members[group][type] || {}
      if hash[name]
        warn(:dup_member, "Duplicate #{type} name #{name}", hash[name])
        warn(:dup_member, "Duplicate #{type} name #{name}", m)
      end
      hash[name] = m
      members[group][type] = hash
    end
  end
end

#warn_duplicate_paramsObject

print warnings for duplicate parameter names



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jsduck/lint.rb', line 66

def warn_duplicate_params
  each_member do |member|
    params = {}
    (member[:params] || []).each do |p|
      if params[p[:name]]
        warn(:dup_param, "Duplicate parameter name #{p[:name]}", member)
      end
      params[p[:name]] = true
    end
  end
end

#warn_no_docObject

print warning for each class or public member with no name



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jsduck/lint.rb', line 37

def warn_no_doc
  @relations.each do |cls|
    if cls[:doc] == ""
      warn(:no_doc, "No documentation for #{cls[:name]}", cls)
    end
  end
  each_member do |member|
    if member[:doc] == "" && !member[:private]
      warn(:no_doc, "No documentation for #{member[:owner]}##{member[:name]}", member)
    end
  end
end

#warn_optional_paramsObject

print warning for each non-optional parameter that follows an optional parameter



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jsduck/lint.rb', line 51

def warn_optional_params
  each_member do |member|
    if member[:tagname] == :method
      optional_found = false
      member[:params].each do |p|
        if optional_found && !p[:optional]
          warn(:req_after_opt, "Optional param followed by regular param #{p[:name]}", member)
        end
        optional_found = optional_found || p[:optional]
      end
    end
  end
end

#warn_unnamedObject

print warning for each member or parameter with no name



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jsduck/lint.rb', line 23

def warn_unnamed
  each_member do |member|
    if !member[:name] || member[:name] == ""
      warn(:name_missing, "Unnamed #{member[:tagname]}", member)
    end
    (member[:params] || []).each do |p|
      if !p[:name] || p[:name] == ""
        warn(:name_missing, "Unnamed parameter", member)
      end
    end
  end
end