Module: MilkTea::Linter::LinterDocTags
- Included in:
- MilkTea::Linter
- Defined in:
- lib/milk_tea/tooling/linter/doc_tags.rb
Instance Method Summary collapse
- #collect_doc_comment_block_for_line(line) ⇒ Object
- #doc_tag_column(line) ⇒ Object
- #each_doc_comment_definition(source_file) ⇒ Object
- #emit_doc_tag_warnings(source_file) ⇒ Object
- #parse_doc_tag_block(lines) ⇒ Object
- #validate_doc_tags_for_declaration(declaration, tags) ⇒ Object
Instance Method Details
#collect_doc_comment_block_for_line(line) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 45 def collect_doc_comment_block_for_line(line) index = line.to_i - 2 return nil if index.negative? docs = [] while index >= 0 stripped = @source_lines[index].to_s.strip break if stripped.empty? break unless stripped.start_with?('##') docs << { line: index + 1, text: stripped.sub(/\A##\s?/, ''), } index -= 1 end return nil if docs.empty? docs.reverse end |
#doc_tag_column(line) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 201 def doc_tag_column(line) text = @source_lines[line.to_i - 1].to_s at_index = text.index('@') return 1 if at_index.nil? at_index + 1 end |
#each_doc_comment_definition(source_file) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 27 def each_doc_comment_definition(source_file) declarations = source_file.declarations.flat_map do |declaration| case declaration when AST::ExtendingBlock declaration.methods else declaration end end declarations.each do |declaration| docs = collect_doc_comment_block_for_line(declaration.line) next if docs.nil? || docs.empty? yield declaration, docs end end |
#emit_doc_tag_warnings(source_file) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 6 def emit_doc_tag_warnings(source_file) return if @source_lines.empty? each_doc_comment_definition(source_file) do |declaration, doc_lines| parsed = parse_doc_tag_block(doc_lines) parsed[:errors].each do |error| @warnings << Warning.new( path: @path, line: error[:line], column: error[:column], length: error[:length], code: "doc-tag", message: error[:message], severity: :hint, ) end (declaration, parsed[:tags]) end end |
#parse_doc_tag_block(lines) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 66 def parse_doc_tag_block(lines) = { params: [], returns: nil, throws: [], see: [], } errors = [] lines.each do |entry| text = entry[:text].to_s match = text.match(/\A\s*@([A-Za-z_][A-Za-z0-9_-]*)(?:\s+(.*))?\z/) next unless match tag = match[1].to_s.downcase payload = match[2].to_s.strip column = doc_tag_column(entry[:line]) case tag when 'param' if payload.empty? errors << { line: entry[:line], column: column, length: 6, message: 'doc tag @param requires a parameter name', } next end name_match = payload.match(/\A([A-Za-z_][A-Za-z0-9_]*)(?:\s+(.*))?\z/) unless name_match errors << { line: entry[:line], column: column, length: 6, message: 'doc tag @param has an invalid parameter name', } next end [:params] << { name: name_match[1], text: name_match[2].to_s.strip, line: entry[:line], column: column, } when 'return', 'returns' [:returns] = { text: payload, line: entry[:line], column: column, } when 'throws', 'throw' [:throws] << { text: payload, line: entry[:line], column: column, } when 'see' [:see] << { text: payload, line: entry[:line], column: column, } else errors << { line: entry[:line], column: column, length: tag.length + 1, message: "unknown doc tag @#{tag}", } end end { tags: , errors: errors, } end |
#validate_doc_tags_for_declaration(declaration, tags) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/milk_tea/tooling/linter/doc_tags.rb', line 146 def (declaration, ) callable = declaration.respond_to?(:params) && declaration.respond_to?(:return_type) = Array([:params]) return_tag = [:returns] unless callable ([*, return_tag].compact).each do |tag_entry| @warnings << Warning.new( path: @path, line: tag_entry[:line], column: tag_entry[:column], length: 1, code: 'doc-tag', message: 'callable doc tags are only valid on function and method declarations', severity: :hint, ) end return end params = Array(declaration.params) param_names = params.map(&:name) param_name_set = param_names.to_set .each do |tag_entry| next if param_name_set.include?(tag_entry[:name]) @warnings << Warning.new( path: @path, line: tag_entry[:line], column: tag_entry[:column], length: tag_entry[:name].length, code: 'doc-tag', message: "doc tag @param '#{tag_entry[:name]}' does not match any parameter in '#{declaration.name}'", severity: :hint, symbol_name: tag_entry[:name], ) end return unless return_tag return_type_text = declaration.return_type.to_s return unless return_type_text == 'void' @warnings << Warning.new( path: @path, line: return_tag[:line], column: return_tag[:column], length: 7, code: 'doc-tag', message: "doc tag @return is stale for '#{declaration.name}' because it returns void", severity: :hint, ) end |