Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #4247] Make all file inclusion lists configurable #5882

Merged
merged 1 commit into from May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* [#5821](https://github.com/bbatsov/rubocop/pull/5821): Support `AR::Migration#up_only` for `Rails/ReversibleMigration` cop. ([@koic][])
* [#5800](https://github.com/bbatsov/rubocop/issues/5800): Don't show a stracktrace for invalid command-line params. ([@shanecav84][])
* [#5845](https://github.com/bbatsov/rubocop/pull/5845): Add new `Lint/ErbNewArguments` cop. ([@koic][])
* [#4247](https://github.com/bbatsov/rubocop/issues/4247): Remove hard-coded file patterns and use only `Include`, `Exclude` and the new `RubyInterpreters` parameters for file selection. ([@jonas054][])

### Bug fixes

Expand Down
7 changes: 7 additions & 0 deletions config/default.yml
Expand Up @@ -7,8 +7,15 @@ inherit_from:

# Common configuration.
AllCops:
RubyInterpreters:
- ruby
- macruby
- rake
- jruby
- rbx
# Include common Ruby source files.
Include:
- '**/*.rb'
- '**/*.arb'
- '**/*.axlsx'
- '**/*.builder'
Expand Down
85 changes: 26 additions & 59 deletions lib/rubocop/target_finder.rb
Expand Up @@ -3,61 +3,6 @@
require 'set'

module RuboCop
RUBY_EXTENSIONS = %w[.rb
.arb
.axlsx
.builder
.fcgi
.gemfile
.gemspec
.god
.jb
.jbuilder
.mspec
.opal
.pluginspec
.podspec
.rabl
.rake
.rbuild
.rbw
.rbx
.ru
.ruby
.spec
.thor
.watchr].freeze

RUBY_INTERPRETERS = %w[ruby
macruby
rake
jruby
rbx].freeze

RUBY_FILENAMES = %w[.irbrc
.pryrc
Appraisals
Berksfile
Brewfile
Buildfile
Capfile
Cheffile
Dangerfile
Deliverfile
Fastfile
Gemfile
Guardfile
Jarfile
Mavenfile
Podfile
Puppetfile
Rakefile
Snapfile
Thorfile
Vagabondfile
Vagrantfile
buildfile].freeze

# This class finds target files to inspect by scanning the directory tree
# and picking ruby files.
class TargetFinder
Expand Down Expand Up @@ -169,22 +114,44 @@ def excluded_dirs(base_dir)
end

def ruby_extension?(file)
RUBY_EXTENSIONS.include?(File.extname(file))
ruby_extensions.include?(File.extname(file))
end

def ruby_extensions
ext_patterns = all_cops_include.select do |pattern|
pattern.start_with?('**/*.')
end
ext_patterns.map { |pattern| pattern.sub('**/*', '') }
end

def ruby_filename?(file)
RUBY_FILENAMES.include?(File.basename(file))
ruby_filenames.include?(File.basename(file))
end

def ruby_filenames
file_patterns = all_cops_include.reject do |pattern|
pattern.start_with?('**/*.')
end
file_patterns.map { |pattern| pattern.sub('**/', '') }
end

def all_cops_include
@config_store.for('.').for_all_cops['Include'].map(&:to_s)
end

def ruby_executable?(file)
return false unless File.extname(file).empty? && File.exist?(file)
first_line = File.open(file, &:readline)
!(first_line =~ /#!.*(#{RUBY_INTERPRETERS.join('|')})/).nil?
!(first_line =~ /#!.*(#{ruby_interpreters(file).join('|')})/).nil?
rescue EOFError, ArgumentError => e
warn "Unprocessable file #{file}: #{e.class}, #{e.message}" if debug?
false
end

def ruby_interpreters(file)
@config_store.for(file).for_all_cops['RubyInterpreters']
end

def stdin?
@options.key?(:stdin)
end
Expand All @@ -195,7 +162,7 @@ def ruby_file?(file)
end

def configured_include?(file)
@config_store.for(file).file_to_include?(file)
@config_store.for('.').file_to_include?(file)
end

def included_file?(file)
Expand Down
23 changes: 11 additions & 12 deletions manual/configuration.md
Expand Up @@ -191,23 +191,22 @@ directory, `config/default.yml` will be used.

### Including/Excluding files

RuboCop checks all files found by a recursive search starting from the
directory it is run in, or directories given as command line
arguments. However, it only recognizes files ending with `.rb` or
extensionless files with a `#!.*ruby` declaration as Ruby files.
Hidden directories (i.e., directories whose names start with a dot)
are not searched by default. If you'd like it to check files that are
not included by default, you'll need to pass them in on the command
line, or to add entries for them under `AllCops`/`Include`. Files and
directories can also be ignored through `AllCops`/`Exclude`.
RuboCop does a recursive file search starting from the directory it is
run in, or directories given as command line arguments. Files that
match any pattern listed under `AllCops`/`Include` and extensionless
files with a hash-bang (`#!`) declaration containing one of the known
ruby interpreters listed under `AllCops`/`RubyInterpreters` are
inspected, unless the file also matches a pattern in
`AllCops`/`Exclude`. Hidden directories (i.e., directories whose names
start with a dot) are not searched by default. If you'd like RuboCop
to check files that are not included by default, you'll need to pass
them in on the command line, or to add entries for them under
`AllCops`/`Include`.

Here is an example that might be used for a Rails project:

```yaml
AllCops:
Include:
- '**/Rakefile'
- '**/config.ru'
Exclude:
- 'db/**/*'
- 'config/**/*'
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -88,6 +88,8 @@
Exclude:
- Gemfile
Include:
- "**/*.rb"
- "**/*.rabl"
- "**/*.rabl2"
YAML
end
Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -970,6 +970,7 @@ def meow_at_4am?
AllCops:
Include:
- example
- "**/*.rb"
- "**/*.rake"
- !ruby/regexp /regexp$/
- .dot1/**/*
Expand Down