rubocop-itamae 0.1.0.alpha1

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
+ SHA256:
3
+ metadata.gz: 43b0711e43b2fe39e6b0b5115686d402e602801b3df7be1cdf397d4007ba4ad8
4
+ data.tar.gz: de4843a38e16ab19b2f4867d866a0a97ac1e23ace11742cada1d55b287bf7e4b
5
+ SHA512:
6
+ metadata.gz: a684ebe47004196a3e0e203478207171f20deb418e7d0ca3afdbd503c77fa3a0bccd8bcb265cff9109efb4dc2d944aae2e30cd9d92f83e2b8c348b18092595eb
7
+ data.tar.gz: fe327d5b6f73761648e04f54510aef76332887f3eda1470880e473f614e49af26483f857244cf37834952433b3f891ad9e2c3224eae08c73a69d780a6f070c04
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ # rubocop requires ruby 2.1+ since v0.51.0
3
+ # https://github.com/rubocop-hq/rubocop/commit/753227914c1ad311e6116311fc1ef76d9b0904dc
4
+ TargetRubyVersion: 2.1
5
+
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - "spec/**/*.rb"
9
+
10
+ Metrics/LineLength:
11
+ Exclude:
12
+ - "spec/spec_helper.rb"
13
+
14
+ Style/HashSyntax:
15
+ Exclude:
16
+ - "Rakefile"
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## master
2
+ [full changelog](http://github.com/sue445/rubocop-itamae/compare/v0.1.0...master)
3
+
4
+ ## v0.1.0
5
+ * first release
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-itamae.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 sue445
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,39 @@
1
+ # RuboCop::Itamae
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubocop/itamae`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rubocop-itamae'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubocop-itamae
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop-itamae.
36
+
37
+ ## License
38
+
39
+ 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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ Dir['tasks/**/*.rake'].each { |t| load t }
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubocop/itamae'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,9 @@
1
+ Itamae/CdInExecute:
2
+ Description: Check that `cd` in `execute`.
3
+ Enabled: true
4
+ StyleGuide: http://www.rubydoc.info/gems/rubocop-itamae/RuboCop/Cop/Itamae/CdInExecute
5
+
6
+ Itamae/CommandEqualsToName:
7
+ Description: Check that `command` doesn't equals to name of `execute`.
8
+ Enabled: true
9
+ StyleGuide: http://www.rubydoc.info/gems/rubocop-itamae/RuboCop/Cop/Itamae/CommandEqualsToName
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Itamae
6
+ # Check that `cd` in `execute`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # execute 'cd /tmp && rm -rf /tmp/*'
11
+ #
12
+ # # good
13
+ # execute 'rm -rf /tmp/*' do
14
+ # cwd '/tmp'
15
+ # end
16
+ class CdInExecute < Cop
17
+ MSG = "Insert `cwd '%<dir>s'` and remove this.".freeze
18
+
19
+ def_node_search :find_execute_with_block, <<-PATTERN
20
+ (block
21
+ (send nil? :execute
22
+ (str $_)
23
+ )
24
+ args
25
+ $...
26
+ )
27
+ PATTERN
28
+
29
+ def_node_search :find_execute_without_block, <<-PATTERN
30
+ (send nil? :execute
31
+ (str $_)
32
+ )
33
+ PATTERN
34
+
35
+ def_node_search :find_command, <<-PATTERN
36
+ (send nil? :command
37
+ (str $_)
38
+ )
39
+ PATTERN
40
+
41
+ def on_block(node)
42
+ find_execute_with_block(node) do |name, param_nodes|
43
+ add_offence_for_execute_block_name(node, name)
44
+
45
+ param_nodes.compact.each do |param_node|
46
+ find_command(param_node) do |command|
47
+ add_offense_for_command_param(param_node, command)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def on_send(node)
54
+ return if node.parent && node.parent.block_type?
55
+
56
+ find_execute_without_block(node) do |name|
57
+ add_offense_for_execute_name(node, name)
58
+ end
59
+ end
60
+
61
+ # rubocop:disable Metrics/LineLength
62
+ # def autocorrect(node)
63
+ # if node.block_type?
64
+ # lambda do |corrector|
65
+ # find_execute_with_block(node) do |name, param_nodes|
66
+ # if offence_command?(name)
67
+ # corrector.remove(cd_location(node.child_nodes.first.child_nodes.first, name))
68
+ #
69
+ # # TODO: insert cwd
70
+ # next
71
+ # end
72
+ # end
73
+ # end
74
+ # elsif node.begin_type?
75
+ # lambda do |corrector|
76
+ # find_command(node) do |command|
77
+ # next unless offence_command?(command)
78
+ #
79
+ # command_node = node.child_nodes.first.child_nodes.first
80
+ # corrector.remove(cd_location(command_node, command))
81
+ #
82
+ # # TODO: insert cwd
83
+ # end
84
+ # end
85
+ # elsif node.send_type?
86
+ # lambda do |corrector|
87
+ # find_execute_without_block(node) do |name|
88
+ # next unless offence_command?(name)
89
+ #
90
+ # corrector.remove(cd_location(node.child_nodes.first, name))
91
+ #
92
+ # # TODO: insert cwd
93
+ # end
94
+ #
95
+ # find_command(node) do |command|
96
+ # next unless offence_command?(command)
97
+ #
98
+ # command_node = node.child_nodes.first
99
+ # corrector.remove(cd_location(command_node, command))
100
+ #
101
+ # # TODO: insert cwd
102
+ # end
103
+ # end
104
+ # end
105
+ # end
106
+ # rubocop:enable Metrics/LineLength
107
+
108
+ private
109
+
110
+ def add_offense_for_execute_name(node, name)
111
+ dir = cd_dir_in_command(name)
112
+ return unless dir
113
+
114
+ loc = cd_location(node.child_nodes.first, name)
115
+ add_offense(node, message: format(MSG, dir: dir), location: loc)
116
+ end
117
+
118
+ def add_offence_for_execute_block_name(node, name)
119
+ dir = cd_dir_in_command(name)
120
+ return unless dir
121
+
122
+ loc = cd_location(node.child_nodes.first.child_nodes.first, name)
123
+ add_offense(node, message: format(MSG, dir: dir), location: loc)
124
+ end
125
+
126
+ def add_offense_for_command_param(param_node, command)
127
+ dir = cd_dir_in_command(command)
128
+ return unless dir
129
+
130
+ command_node =
131
+ if param_node.child_nodes.first.child_nodes.empty?
132
+ param_node.child_nodes.first
133
+ else
134
+ param_node.child_nodes.first.child_nodes.first
135
+ end
136
+
137
+ loc = cd_location(command_node, command)
138
+ add_offense(param_node, message: format(MSG, dir: dir), location: loc)
139
+ end
140
+
141
+ def cd_dir_in_command(command)
142
+ dir, = parse_command(command)
143
+ dir
144
+ end
145
+
146
+ def parse_command(command)
147
+ command =~ /^\s*cd\s+(.+?)&&(.+)$/
148
+ if Regexp.last_match(1) && Regexp.last_match(2)
149
+ [Regexp.last_match(1).strip, Regexp.last_match(2).strip]
150
+ else
151
+ [nil, nil]
152
+ end
153
+ end
154
+
155
+ def cd_location(node, command)
156
+ _, second_command = parse_command(command)
157
+ second_command_pos = command.index(second_command)
158
+ begin_pos = node.loc.begin.end_pos
159
+ end_pos = begin_pos + second_command_pos
160
+ Parser::Source::Range.new(node.loc.expression.source_buffer,
161
+ begin_pos, end_pos)
162
+ end
163
+
164
+ # def indent_num(node)
165
+ # node.loc.expression.source_buffer.source =~ /^(\s+)/
166
+ #
167
+ # return Regexp.last_match(1).length if Regexp.last_match(1)
168
+ # 0
169
+ # end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Itamae
6
+ # Check that `command` doesn't equals to name of `execute`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # execute 'rm -rf /tmp/*' do
11
+ # command 'rm -rf /tmp/*'
12
+ # end
13
+ #
14
+ # # good
15
+ # execute 'rm -rf /tmp/*'
16
+ #
17
+ # execute 'Remove temporary files' do
18
+ # command 'rm -rf /tmp/*'
19
+ # end
20
+ class CommandEqualsToName < Cop
21
+ include RangeHelp
22
+
23
+ MSG = 'Prefer to omit `command` if `command` equals to ' \
24
+ 'name of `execute`'.freeze
25
+
26
+ def_node_search :find_execute, <<-PATTERN
27
+ (block
28
+ (send nil? :execute
29
+ (str $_)
30
+ )
31
+ args
32
+ $...
33
+ )
34
+ PATTERN
35
+
36
+ def_node_search :find_command, <<-PATTERN
37
+ (send nil? :command
38
+ (str $_)
39
+ )
40
+ PATTERN
41
+
42
+ def on_block(node)
43
+ find_execute(node) do |name, param_nodes|
44
+ param_nodes.compact.each do |param_node|
45
+ find_command(param_node) do |command|
46
+ next unless name == command
47
+
48
+ add_offense(param_node, location: :expression)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def autocorrect(node)
55
+ lambda do |corrector|
56
+ if node.begin_type?
57
+ node.each_child_node do |param_node|
58
+ remove_command_param(corrector, node.parent, param_node)
59
+ end
60
+
61
+ elsif node.send_type?
62
+ remove_command_param(corrector, node.parent, node)
63
+ end
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def remove_command_param(corrector, parent_node, param_node)
70
+ find_execute(parent_node) do |name|
71
+ find_command(param_node) do |command|
72
+ next unless name == command
73
+
74
+ corrector.remove(node_range(param_node))
75
+ end
76
+ end
77
+ end
78
+
79
+ def node_range(node)
80
+ range_by_whole_lines(node.source_range, include_final_newline: true)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'itamae/cd_in_execute'
4
+ require_relative 'itamae/command_equals_to_name'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Itamae
5
+ VERSION = '0.1.0.alpha1'.freeze
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop/itamae/version'
4
+
5
+ module RuboCop
6
+ module Itamae # rubocop:disable Style/Documentation
7
+ # Your code goes here...
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/itamae'
6
+ require_relative 'rubocop/itamae/version'
7
+
8
+ require_relative 'rubocop/cop/itamae_cops'
@@ -0,0 +1,31 @@
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/itamae/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rubocop-itamae'
9
+ spec.version = RuboCop::Itamae::VERSION
10
+ spec.authors = ['sue445']
11
+ spec.email = ['sue445@sue445.net']
12
+
13
+ spec.summary = 'Code style checking for itamae recipes'
14
+ spec.description = 'Code style checking for itamae recipes'
15
+ spec.homepage = 'https://github.com/sue445/rubocop-itamae'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'rubocop'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'rake', '>= 11.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ desc 'Generate a new cop template'
6
+ task :new_cop, [:cop] do |_task, args|
7
+ cop_name = args.fetch(:cop) do
8
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
9
+ exit!
10
+ end
11
+
12
+ github_user = `git config github.user`.chop
13
+ github_user = 'your_id' if github_user.empty?
14
+
15
+ generator = RuboCop::Cop::Generator.new(cop_name, github_user)
16
+
17
+ generator.write_source
18
+ generator.write_spec
19
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/itamae_cops.rb')
20
+
21
+ begin
22
+ generator.inject_config(config_file_path: 'config/default.yml')
23
+ rescue TypeError # rubocop:disable Lint/HandleExceptions
24
+ # nop
25
+ end
26
+
27
+ # puts generator.todo
28
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-itamae
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - sue445
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '11.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '11.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: Code style checking for itamae recipes
84
+ email:
85
+ - sue445@sue445.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - config/default.yml
102
+ - lib/rubocop-itamae.rb
103
+ - lib/rubocop/cop/itamae/cd_in_execute.rb
104
+ - lib/rubocop/cop/itamae/command_equals_to_name.rb
105
+ - lib/rubocop/cop/itamae_cops.rb
106
+ - lib/rubocop/itamae.rb
107
+ - lib/rubocop/itamae/version.rb
108
+ - rubocop-itamae.gemspec
109
+ - tasks/new_cop.rake
110
+ homepage: https://github.com/sue445/rubocop-itamae
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">"
126
+ - !ruby/object:Gem::Version
127
+ version: 1.3.1
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Code style checking for itamae recipes
134
+ test_files: []