Class: Vinter::Linter
- Inherits:
-
Object
- Object
- Vinter::Linter
- Defined in:
- lib/vinter/linter.rb
Instance Method Summary collapse
-
#initialize(config_path: nil) ⇒ Linter
constructor
A new instance of Linter.
- #lint(content) ⇒ Object
- #register_default_rules ⇒ Object
- #register_rule(rule) ⇒ Object
- #traverse_ast(node) {|node| ... } ⇒ Object
Constructor Details
#initialize(config_path: nil) ⇒ Linter
Returns a new instance of Linter.
5 6 7 8 9 10 11 |
# File 'lib/vinter/linter.rb', line 5 def initialize(config_path: nil) @rules = [] @ignored_rules = [] @config_path = config_path || find_config_path load_config register_default_rules end |
Instance Method Details
#lint(content) ⇒ Object
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 |
# File 'lib/vinter/linter.rb', line 98 def lint(content) lexer = Lexer.new(content) tokens = lexer.tokenize parser = Parser.new(tokens, content) result = parser.parse issues = [] # Add parser errors result[:errors].each do |error| issues << { type: :error, message: error[:message], position: error[:position], line: error[:line] || 0, column: error[:column] || 0 } end # Add parser warnings result[:warnings].each do |warning| issues << { type: :warning, message: warning[:message], position: warning[:position], line: warning[:line] || 0, column: warning[:column] || 0 } end # Apply rules, ignoring those specified in config @rules.each do |rule| next if @ignored_rules.include?(rule.id) rule_issues = rule.apply(result[:ast]) issues.concat(rule_issues.map { |i| { type: :rule, rule: rule.id, message: i[:message], line: i[:line] || 0, column: i[:column] || 0 }}) end issues end |
#register_default_rules ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vinter/linter.rb', line 17 def register_default_rules # Rule: Vim9 script files should start with vim9script declaration register_rule(Rule.new("missing-vim9script-declaration", "Script does not start with vim9script declaration") do |ast| if ast[:type] == :program && (ast[:body].empty? || ast[:body][0][:type] != :vim9script_declaration) [{ message: "File should start with vim9script declaration", line: 1, column: 1 }] else [] end end) # Rule: Prefer def over function in Vim9 script register_rule(Rule.new("prefer-def-over-function", "Use def instead of function in Vim9 script") do |ast| issues = [] traverse_ast(ast) do |node| if node[:type] == :legacy_function issues << { message: "Use def instead of function for #{node[:name]}", line: node[:line] || 0, column: node[:column] || 0 } end end issues end) # Rule: Variables should have type annotations register_rule(Rule.new("missing-type-annotation", "Variable declaration is missing type annotation") do |ast| issues = [] traverse_ast(ast) do |node| if node[:type] == :variable_declaration && node[:var_type_annotation].nil? && node[:var_type] != 'const' issues << { message: "Variable #{node[:name]} should have a type annotation", line: node[:line] || 0, column: node[:column] || 0 } end end issues end) # Rule: Functions should have return type annotations register_rule(Rule.new("missing-return-type", "Function is missing return type annotation") do |ast| issues = [] traverse_ast(ast) do |node| if node[:type] == :def_function && node[:return_type].nil? issues << { message: "Function #{node[:name]} should have a return type annotation", line: node[:line] || 0, column: node[:column] || 0 } end end issues end) # Rule: Function parameters should have type annotations register_rule(Rule.new("missing-param-type", "Function parameter is missing type annotation") do |ast| issues = [] traverse_ast(ast) do |node| if node[:type] == :def_function node[:params].each do |param| if param[:type] == :parameter && param[:param_type].nil? issues << { message: "Parameter #{param[:name]} should have a type annotation", line: param[:line] || 0, column: param[:column] || 0 } end end end end issues end) end |
#register_rule(rule) ⇒ Object
13 14 15 |
# File 'lib/vinter/linter.rb', line 13 def register_rule(rule) @rules << rule end |
#traverse_ast(node) {|node| ... } ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/vinter/linter.rb', line 84 def traverse_ast(node, &block) return unless node.is_a?(Hash) yield node node.each do |key, value| if value.is_a?(Array) value.each { |item| traverse_ast(item, &block) if item.is_a?(Hash) } elsif value.is_a?(Hash) traverse_ast(value, &block) end end end |