rubocop_lineup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5c3f8e0a125f78fd6cb186b10cb6c341b5e6667
4
+ data.tar.gz: b0a2df3b60e4393b3a07408683b7dfb37fb8d1d2
5
+ SHA512:
6
+ metadata.gz: 8328d4ccc0dc825014ec067ed80d8009ac252aeb7be7954f6668b26b92962d444eacf7d41ee1915bda777498a85a8b6f6372a0dc32c2ebd3ca009f9d8727463f
7
+ data.tar.gz: be1e7a4b240ccecd5b25f631f7f1e6a3ae399573185a0e3e2a69ed6c5929220934240247227d9b11ae4fd50bbd34b9dcc3e8e37491d747ae9913326efcb2811b
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /bin/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ AllCops:
2
+ TargetRubyVersion: "2.1"
3
+ Exclude:
4
+ - "bin/**"
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Layout/SpaceInsideHashLiteralBraces:
11
+ Enabled: false
12
+
13
+ Metrics/LineLength:
14
+ Enabled: true
15
+ Max: 120
16
+
17
+ Style/PercentLiteralDelimiters:
18
+ PreferredDelimiters:
19
+ default: '()'
20
+ '%w': '()'
21
+
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - "spec/**"
25
+
26
+ Style/Documentation:
27
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.4
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.4
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in rubocop_lineup.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Mystery Science
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # RubocopLineup
2
+
3
+ TODO: Delete this and the text above, and describe your gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubocop_lineup'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rubocop_lineup
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop_lineup.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:spec, "rubocop:auto_correct"]
9
+
10
+ require "rubocop/rake_task"
11
+
12
+ RuboCop::RakeTask.new
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocop_lineup"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubocopLineup
4
+ # This class depends on git diffs generated the with -U0 option.
5
+ class DiffLiner
6
+ def self.diff_uncommitted(dir = Dir.pwd)
7
+ new(diff_from(dir, "HEAD"))
8
+ end
9
+
10
+ def self.diff_branch(parent_branch, dir = Dir.pwd)
11
+ new(diff_from(dir, "#{parent_branch}..."))
12
+ end
13
+
14
+ def self.diff_from(dir, obj)
15
+ # The -U0 option is essential to how this gem works, since it eliminates
16
+ # any unchanged lines in the output, allowing for simple math based on
17
+ # the line number header in the git output.
18
+
19
+ Git.open(dir).diff(obj, "-U0")
20
+ end
21
+
22
+ # Expects a Git::Diff instance, which handles parsing diff output into files.
23
+ def initialize(diff)
24
+ @data = process(diff)
25
+ end
26
+
27
+ def files
28
+ @data.keys
29
+ end
30
+
31
+ def changed_line_numbers
32
+ @data
33
+ end
34
+
35
+ private
36
+
37
+ def process(diff)
38
+ Hash[diff.map { |diff_file| process_diff_file(diff_file) }]
39
+ end
40
+
41
+ def process_diff_file(diff_file)
42
+ [diff_file.path,
43
+ calc_line_numbers(diff_file.patch.scan(/@@(.*)@@/).flatten)]
44
+ end
45
+
46
+ def calc_line_numbers(diff_line_summaries)
47
+ diff_line_summaries.map do |line_summary|
48
+ LineNumberCalculator.git_line_summary_to_numbers(line_summary.strip)
49
+ end.flatten
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubocopLineup
4
+ # This class depends on git diffs generated the with -U0 option.
5
+ class LineNumberCalculator
6
+ # presumes the text has been parsed already to just the -/+ bits
7
+ # e.g. "-1 +1"
8
+ def self.git_line_summary_to_numbers(text)
9
+ # TODO: Ruby Golf this - a regex with proper grouping could probably be better.
10
+ _changed, added = text.split(/ /).reject(&:empty?)
11
+ start, count = added.sub(/^-/, "").split(/,/).map(&:to_i)
12
+ count ||= 1
13
+ (start..(start + count - 1)).to_a
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module DuckPunch
6
+ module CommentConfig
7
+ def cop_enabled_at_line?(cop, line_number)
8
+ source_file = processed_source.path.sub("#{Dir.pwd}/", "")
9
+ files_hash = RubocopLineup.line_em_up(Dir.pwd)
10
+ return false unless files_hash.key?(source_file)
11
+ changed_line_numbers = files_hash[source_file]
12
+ changed_line_numbers.include?(line_number) ? super : false
13
+ end
14
+ end
15
+ end
16
+
17
+ RuboCop::CommentConfig.prepend(DuckPunch::CommentConfig)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubocopLineup
4
+ VERSION = "0.1.0".freeze
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop_lineup/version"
4
+ require "rubocop_lineup/line_number_calculator"
5
+ require "rubocop_lineup/diff_liner"
6
+ require "rubocop_lineup/monkey_patch_rubocop"
7
+
8
+ module RubocopLineup
9
+ # This defaults the parent branch to 'master'. This is a reasonable
10
+ # default, but not always accurate. Unfortunately, due to the nature
11
+ # of git, there is no 100% deterministic way to know the name of the
12
+ # parent branch. There are ways to calculate the parent branch name
13
+ # that cover common cases, but that's more complicated and may be added
14
+ # in a future version.
15
+ def self.line_em_up(directory, parent_branch = "master")
16
+ @line_em_up ||= begin
17
+ Dir.chdir(directory) do
18
+ uncommitted_changes = DiffLiner.diff_uncommitted.changed_line_numbers
19
+ committed_changes_on_branch = DiffLiner.diff_branch(parent_branch).changed_line_numbers
20
+
21
+ # When a file has committed changes AND uncommitted_changes,
22
+ # we will only include the lines from the uncommitted changes
23
+ # because the lines in the previous committed changes may not
24
+ # be accurate anymore. See test suite for a sample case.
25
+
26
+ committed_changes_on_branch.merge(uncommitted_changes)
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.reset
32
+ @line_em_up = nil
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "rubocop_lineup/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rubocop_lineup"
9
+ spec.version = RubocopLineup::VERSION
10
+ spec.authors = ["chrismo"]
11
+ spec.email = ["chrismo@clabs.org"]
12
+
13
+ spec.summary = "Rubocop plugin to restrict cops to only changed lines."
14
+ spec.homepage = "https://github.com/mysterysci/rubocop_lineup"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = "~> 2.1"
25
+
26
+ spec.add_dependency "git", "~> 1.3"
27
+ spec.add_dependency "rubocop", "~> 0.55"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop_lineup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - chrismo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.55'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.55'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - chrismo@clabs.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/rubocop_lineup.rb
102
+ - lib/rubocop_lineup/diff_liner.rb
103
+ - lib/rubocop_lineup/line_number_calculator.rb
104
+ - lib/rubocop_lineup/monkey_patch_rubocop.rb
105
+ - lib/rubocop_lineup/version.rb
106
+ - rubocop_lineup.gemspec
107
+ homepage: https://github.com/mysterysci/rubocop_lineup
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '2.1'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.14.1
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Rubocop plugin to restrict cops to only changed lines.
131
+ test_files: []