|
| 1 | +module PuppetLanguageServer |
| 2 | + module Puppetfile |
| 3 | + module ValidationProvider |
| 4 | + def self.max_line_length |
| 5 | + # TODO: ... need to figure out the actual line length |
| 6 | + 1000 |
| 7 | + end |
| 8 | + |
| 9 | + def self.validate(content, _workspace, _max_problems = 100) |
| 10 | + result = [] |
| 11 | + # TODO: Need to implement max_problems |
| 12 | + _problems = 0 |
| 13 | + |
| 14 | + # Attempt to parse the file |
| 15 | + puppetfile = nil |
| 16 | + begin |
| 17 | + puppetfile = PuppetLanguageServer::Puppetfile::R10K::Puppetfile.new |
| 18 | + puppetfile.load!(content) |
| 19 | + rescue StandardError, SyntaxError, LoadError => detail |
| 20 | + # Find the originating error from within the puppetfile |
| 21 | + loc = detail.backtrace_locations |
| 22 | + .select { |item| item.absolute_path == PuppetLanguageServer::Puppetfile::R10K::PUPPETFILE_MONIKER } |
| 23 | + .first |
| 24 | + start_line_number = loc.nil? ? 0 : loc.lineno - 1 # Line numbers from ruby are base 1 |
| 25 | + end_line_number = loc.nil? ? content.lines.count - 1 : loc.lineno - 1 # Line numbers from ruby are base 1 |
| 26 | + # Note - Ruby doesn't give a character position so just highlight the entire line |
| 27 | + result << LanguageServer::Diagnostic.create('severity' => LanguageServer::DIAGNOSTICSEVERITY_ERROR, |
| 28 | + 'fromline' => start_line_number, |
| 29 | + 'toline' => end_line_number, |
| 30 | + 'fromchar' => 0, |
| 31 | + 'tochar' => max_line_length, |
| 32 | + 'source' => 'Puppet', |
| 33 | + 'message' => detail.to_s) |
| 34 | + |
| 35 | + puppetfile = nil |
| 36 | + end |
| 37 | + return result if puppetfile.nil? |
| 38 | + |
| 39 | + # Check for invalid module definitions |
| 40 | + puppetfile.modules.each do |mod| |
| 41 | + next unless mod.properties[:type] == :invalid |
| 42 | + # Note - Ruby doesn't give a character position so just highlight the entire line |
| 43 | + result << LanguageServer::Diagnostic.create('severity' => LanguageServer::DIAGNOSTICSEVERITY_ERROR, |
| 44 | + 'fromline' => mod.puppetfile_line_number, |
| 45 | + 'toline' => mod.puppetfile_line_number, |
| 46 | + 'fromchar' => 0, |
| 47 | + 'tochar' => max_line_length, |
| 48 | + 'source' => 'Puppet', |
| 49 | + 'message' => mod.properties[:error_message]) |
| 50 | + end |
| 51 | + |
| 52 | + # Check for duplicate module definitions |
| 53 | + dupes = puppetfile.modules |
| 54 | + .group_by { |mod| mod.name } |
| 55 | + .select { |_, v| v.size > 1 } |
| 56 | + .map(&:first) |
| 57 | + dupes.each do |dupe_module_name| |
| 58 | + puppetfile.modules.select { |mod| mod.name == dupe_module_name }.each do |puppet_module| |
| 59 | + # Note - Ruby doesn't give a character position so just highlight the entire line |
| 60 | + result << LanguageServer::Diagnostic.create('severity' => LanguageServer::DIAGNOSTICSEVERITY_ERROR, |
| 61 | + 'fromline' => puppet_module.puppetfile_line_number, |
| 62 | + 'toline' => puppet_module.puppetfile_line_number, |
| 63 | + 'fromchar' => 0, |
| 64 | + 'tochar' => max_line_length, |
| 65 | + 'source' => 'Puppet', |
| 66 | + 'message' => "Duplicate module definition for '#{puppet_module.name}'") |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + result |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments