solargraph-cookstyle 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c63732878d45fdf9e57c1970e3f91e0a50bd8162badbd035d36e7ce8abe5af6d
4
+ data.tar.gz: 2b93c1397ddd504c23938deef49c9e8d510d3acf41c8179844129d0d4aca39a6
5
+ SHA512:
6
+ metadata.gz: 55039ae33d8c285ab67ae9188a4ac3e7df0354de43a26d7118397187c4dd2f9f16c9c40620d3959bf17cd5b5de792da7e4f727ecc38442908b991b57138485d0
7
+ data.tar.gz: 83f89784883e084525b5a07036e27baea1d57454c023b2d9be0e20918a242a143ae649522a2de18ded95f74a66d4494d9fc9d677ec1dd5b0ce0cfe8cc04ea812
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solargraph'
4
+ require 'cookstyle'
5
+ require 'stringio'
6
+
7
+ require_relative 'solargraph_cookstyle_helpers'
8
+
9
+ # Cookstyle plugin for solargraph
10
+ module Solargraph
11
+ module Diagnostics
12
+ # Cookstyle class
13
+ class Cookstyle < Base
14
+ include CookstyleHelpers
15
+
16
+ # Conversion of RuboCop severity names to LSP constants
17
+ SEVERITIES = {
18
+ 'refactor' => Severities::HINT,
19
+ 'convention' => Severities::INFORMATION,
20
+ 'warning' => Severities::WARNING,
21
+ 'error' => Severities::ERROR,
22
+ 'fatal' => Severities::ERROR
23
+ }.freeze
24
+
25
+ def diagnose(source, _api_map)
26
+ options, paths = generate_options(source.filename, source.code)
27
+ store = RuboCop::ConfigStore.new
28
+ runner = RuboCop::Runner.new(options, store)
29
+ result = redirect_stdout { runner.run(paths) }
30
+ make_array JSON.parse(result)
31
+ rescue RuboCop::ValidationError, RuboCop::ConfigNotFoundError => e
32
+ raise Solargraph::DiagnosticsError,
33
+ "Error in RuboCop configuration: #{e.message}"
34
+ rescue JSON::ParserError
35
+ raise Solargraph::DiagnosticsError, 'RuboCop returned invalid data'
36
+ end
37
+
38
+ private
39
+
40
+ def make_array(resp)
41
+ diagnostics = []
42
+ resp['files'].each do |file|
43
+ file['offenses'].each do |off|
44
+ diagnostics.push offense_to_diagnostic(off)
45
+ end
46
+ end
47
+ diagnostics
48
+ end
49
+
50
+ def offense_to_diagnostic(off)
51
+ {
52
+ range: offense_range(off).to_hash,
53
+ severity: SEVERITIES[off['severity']],
54
+ source: off['cop_name'],
55
+ message: off['message'].gsub(/^#{off['cop_name']}\:/, '')
56
+ }
57
+ end
58
+
59
+ def offense_range(off)
60
+ Range.new(offense_start_position(off),
61
+ offense_ending_position(off))
62
+ end
63
+
64
+ def offense_start_position(off)
65
+ Position.new(
66
+ off['location']['start_line'] - 1,
67
+ off['location']['start_column'] - 1
68
+ )
69
+ end
70
+
71
+ def offense_ending_position(off)
72
+ if off['location']['start_line'] != off['location']['last_line']
73
+ Position.new(off['location']['start_line'], 0)
74
+ else
75
+ Position.new(
76
+ off['location']['start_line'] - 1, off['location']['last_column']
77
+ )
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ Solargraph::Diagnostics.register('cookstyle',
85
+ Solargraph::Diagnostics::Cookstyle)
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # cookstyle helpers
4
+ module Solargraph
5
+ module Diagnostics
6
+ # Cookstyle helpers
7
+ module CookstyleHelpers
8
+ module_function
9
+
10
+ def generate_options(filename, code)
11
+ args = ['-f', 'j']
12
+ rubocop_file = find_rubocop_file(filename)
13
+ args.push('-c', fix_drive_letter(rubocop_file)) unless rubocop_file.nil?
14
+ args.push filename
15
+ base_options = RuboCop::Options.new
16
+ options, paths = base_options.parse(args)
17
+ options[:stdin] = code
18
+ [options, paths]
19
+ end
20
+
21
+ def find_rubocop_file(filename)
22
+ return nil unless File.exist?(filename)
23
+
24
+ filename = File.realpath(filename)
25
+ dir = File.dirname(filename)
26
+ until File.dirname(dir) == dir
27
+ here = File.join(dir, '.rubocop.yml')
28
+ return here if File.exist?(here)
29
+
30
+ dir = File.dirname(dir)
31
+ end
32
+ nil
33
+ end
34
+
35
+ def fix_drive_letter(path)
36
+ return path unless path.match(/^[a-z]:/)
37
+
38
+ path[0].upcase + path[1..-1]
39
+ end
40
+
41
+ def redirect_stdout
42
+ redir = StringIO.new
43
+ $stdout = redir
44
+ yield if block_given?
45
+ $stdout = STDOUT
46
+ redir.string
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solargraph-cookstyle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chad Denyar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/solargraph_cookstyle.rb
20
+ - lib/solargraph_cookstyle_helpers.rb
21
+ homepage: https://github.com/cdenyar/solargraph-cookstyle
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Cookstyle reporter for solargraph
44
+ test_files: []