standard 0.0.13

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of standard might be problematic. Click here for more details.

@@ -0,0 +1,8 @@
1
+ inherit_from: ./ruby-1.9.yml
2
+
3
+ Style/Lambda:
4
+ Enabled: false
5
+
6
+ Style/HashSyntax:
7
+ EnforcedStyle: hash_rockets
8
+
@@ -0,0 +1,4 @@
1
+ inherit_from: ./ruby-2.2.yml
2
+
3
+ Style/Encoding:
4
+ Enabled: false
@@ -0,0 +1,8 @@
1
+ inherit_from: ./base.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.2
5
+
6
+ Layout:
7
+ IndentHeredoc:
8
+ Enabled: false
data/exe/standard ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
+
5
+ require "standard"
6
+
7
+ warn <<-WARNING.gsub(/^ {2}/, "")
8
+ WARNING: Run 'standardrb' instead of 'standard'. We are renaming the gem's binary
9
+ to avoid naming conflicts with the StandardJS 'standard' bin in the event both are
10
+ on a user's PATH.
11
+
12
+ The 'standard' bin will be removed in the next release of the standard gem
13
+ WARNING
14
+ exit Standard::Cli.new(ARGV).run
data/exe/standardrb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
4
+
5
+ require "standard"
6
+
7
+ exit Standard::Cli.new(ARGV).run
@@ -0,0 +1,37 @@
1
+ require_relative "config"
2
+
3
+ module Standard
4
+ class Cli
5
+ SUCCESS_STATUS_CODE = 0
6
+ FAILURE_STATUS_CODE = 1
7
+
8
+ def initialize(argv)
9
+ @argv = argv
10
+ @config = Config.new
11
+ end
12
+
13
+ def run
14
+ rubocop_config = @config.call(@argv)
15
+ runner = RuboCop::Runner.new(
16
+ rubocop_config.options,
17
+ rubocop_config.config_store
18
+ )
19
+
20
+ run_succeeded = runner.run(rubocop_config.paths)
21
+
22
+ if run_succeeded
23
+ SUCCESS_STATUS_CODE
24
+ else
25
+ (runner.warnings + runner.errors).each do |message|
26
+ warn message
27
+ end
28
+ puts <<-CALL_TO_ACTION.gsub(/^ {10}/, "")
29
+
30
+ Notice: Disagree with these rules? While StandardRB is pre-1.0.0, feel free to submit suggestions to:
31
+ https://github.com/testdouble/standard/issues/new
32
+ CALL_TO_ACTION
33
+ FAILURE_STATUS_CODE
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "loads_yaml_config"
2
+ require_relative "merges_settings"
3
+ require_relative "creates_config_store"
4
+
5
+ module Standard
6
+ class Config
7
+ RuboCopConfig = Struct.new(:paths, :options, :config_store)
8
+
9
+ def initialize
10
+ @loads_yaml_config = LoadsYamlConfig.new
11
+ @merges_settings = MergesSettings.new
12
+ @creates_config_store = CreatesConfigStore.new
13
+ end
14
+
15
+ def call(argv, search_path = Dir.pwd)
16
+ standard_config = @loads_yaml_config.call(search_path)
17
+ settings = @merges_settings.call(argv, standard_config)
18
+ RuboCopConfig.new(
19
+ settings.paths,
20
+ settings.options,
21
+ @creates_config_store.call(standard_config)
22
+ )
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,162 @@
1
+ module RuboCop::Cop
2
+ module Standard
3
+ class SemanticBlocks < RuboCop::Cop::Cop
4
+ include RuboCop::Cop::IgnoredMethods
5
+
6
+ def on_send(node)
7
+ return unless node.arguments?
8
+ return if node.parenthesized? || node.operator_method?
9
+
10
+ node.arguments.each do |arg|
11
+ get_blocks(arg) do |block|
12
+ # If there are no parentheses around the arguments, then braces
13
+ # and do-end have different meaning due to how they bind, so we
14
+ # allow either.
15
+ ignore_node(block)
16
+ end
17
+ end
18
+ end
19
+
20
+ def on_block(node)
21
+ return if ignored_node?(node) || proper_block_style?(node)
22
+
23
+ add_offense(node, location: :begin)
24
+ end
25
+
26
+ def autocorrect(node)
27
+ return if correction_would_break_code?(node)
28
+
29
+ if node.single_line?
30
+ replace_do_end_with_braces(node.loc)
31
+ elsif node.braces?
32
+ replace_braces_with_do_end(node.loc)
33
+ else
34
+ replace_do_end_with_braces(node.loc)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def message(node)
41
+ if node.single_line?
42
+ "Prefer `{...}` over `do...end` for single-line blocks."
43
+ elsif node.loc.begin.source == "{"
44
+ "Prefer `do...end` over `{...}` for procedural blocks."
45
+ else
46
+ "Prefer `{...}` over `do...end` for functional blocks."
47
+ end
48
+ end
49
+
50
+ def replace_braces_with_do_end(loc)
51
+ b = loc.begin
52
+ e = loc.end
53
+
54
+ lambda do |corrector|
55
+ corrector.insert_before(b, " ") unless whitespace_before?(b)
56
+ corrector.insert_before(e, " ") unless whitespace_before?(e)
57
+ corrector.insert_after(b, " ") unless whitespace_after?(b)
58
+ corrector.replace(b, "do")
59
+ corrector.replace(e, "end")
60
+ end
61
+ end
62
+
63
+ def replace_do_end_with_braces(loc)
64
+ b = loc.begin
65
+ e = loc.end
66
+
67
+ lambda do |corrector|
68
+ corrector.insert_after(b, " ") unless whitespace_after?(b, 2)
69
+
70
+ corrector.replace(b, "{")
71
+ corrector.replace(e, "}")
72
+ end
73
+ end
74
+
75
+ def whitespace_before?(range)
76
+ range.source_buffer.source[range.begin_pos - 1, 1] =~ /\s/
77
+ end
78
+
79
+ def whitespace_after?(range, length = 1)
80
+ range.source_buffer.source[range.begin_pos + length, 1] =~ /\s/
81
+ end
82
+
83
+ def get_blocks(node, &block)
84
+ case node.type
85
+ when :block
86
+ yield node
87
+ when :send
88
+ get_blocks(node.receiver, &block) if node.receiver
89
+ when :hash
90
+ # A hash which is passed as method argument may have no braces
91
+ # In that case, one of the K/V pairs could contain a block node
92
+ # which could change in meaning if do...end replaced {...}
93
+ return if node.braces?
94
+
95
+ node.each_child_node { |child| get_blocks(child, &block) }
96
+ when :pair
97
+ node.each_child_node { |child| get_blocks(child, &block) }
98
+ end
99
+ end
100
+
101
+ def proper_block_style?(node)
102
+ method_name = node.method_name
103
+
104
+ if ignored_method?(method_name)
105
+ true
106
+ elsif node.single_line?
107
+ node.braces?
108
+ elsif node.braces?
109
+ !procedural_method?(method_name) &&
110
+ (functional_method?(method_name) || functional_block?(node))
111
+ else
112
+ procedural_method?(method_name) || !return_value_used?(node)
113
+ end
114
+ end
115
+
116
+ def correction_would_break_code?(node)
117
+ return unless node.keywords?
118
+
119
+ node.send_node.arguments? && !node.send_node.parenthesized?
120
+ end
121
+
122
+ def functional_method?(method_name)
123
+ cop_config["FunctionalMethods"].map(&:to_sym).include?(method_name)
124
+ end
125
+
126
+ def functional_block?(node)
127
+ return_value_used?(node) || return_value_of_scope?(node)
128
+ end
129
+
130
+ def procedural_method?(method_name)
131
+ cop_config["ProceduralMethods"].map(&:to_sym).include?(method_name)
132
+ end
133
+
134
+ def return_value_used?(node)
135
+ return unless node.parent
136
+
137
+ # If there are parentheses around the block, check if that
138
+ # is being used.
139
+ if node.parent.begin_type?
140
+ return_value_used?(node.parent)
141
+ else
142
+ node.parent.assignment? || node.parent.send_type?
143
+ end
144
+ end
145
+
146
+ def return_value_of_scope?(node)
147
+ return unless node.parent
148
+
149
+ conditional?(node.parent) || array_or_range?(node.parent) ||
150
+ node.parent.children.last == node
151
+ end
152
+
153
+ def conditional?(node)
154
+ node.if_type? || node.or_type? || node.and_type?
155
+ end
156
+
157
+ def array_or_range?(node)
158
+ node.array_type? || node.irange_type? || node.erange_type?
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,58 @@
1
+ require "rubocop"
2
+ require "pathname"
3
+
4
+ module Standard
5
+ class CreatesConfigStore
6
+ def call(standard_config)
7
+ config_store = RuboCop::ConfigStore.new
8
+ config_store.options_config = rubocop_yaml_path(standard_config[:ruby_version])
9
+ options_config = config_store.instance_variable_get("@options_config")
10
+
11
+ options_config["AllCops"]["TargetRubyVersion"] = floatify_version(
12
+ max_rubocop_supported_version(standard_config[:ruby_version])
13
+ )
14
+
15
+ standard_config[:ignore].each do |(path, cops)|
16
+ cops.each do |cop|
17
+ options_config[cop] ||= {}
18
+ options_config[cop]["Exclude"] ||= []
19
+ options_config[cop]["Exclude"] |= [
20
+ Pathname.new(standard_config[:config_root]).join(path).to_s,
21
+ ]
22
+ end
23
+ end
24
+
25
+ config_store
26
+ end
27
+
28
+ private
29
+
30
+ def rubocop_yaml_path(desired_version)
31
+ file_name = if desired_version < Gem::Version.new("1.9")
32
+ "ruby-1.8.yml"
33
+ elsif desired_version < Gem::Version.new("2.0")
34
+ "ruby-1.9.yml"
35
+ elsif desired_version < Gem::Version.new("2.3")
36
+ "ruby-2.2.yml"
37
+ else
38
+ "base.yml"
39
+ end
40
+
41
+ Pathname.new(__dir__).join("../../config/#{file_name}")
42
+ end
43
+
44
+ def max_rubocop_supported_version(desired_version)
45
+ rubocop_supported_version = Gem::Version.new("2.2")
46
+ if desired_version < rubocop_supported_version
47
+ rubocop_supported_version
48
+ else
49
+ desired_version
50
+ end
51
+ end
52
+
53
+ def floatify_version(version)
54
+ major, minor = version.segments
55
+ "#{major}.#{minor}".to_f # lol
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,13 @@
1
+ require "pathname"
2
+
3
+ module Standard
4
+ class FileFinder
5
+ def call(name, search_path)
6
+ Pathname.new(search_path).expand_path.ascend do |path|
7
+ if (file = path + name).exist?
8
+ return file.to_s
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require "rubocop"
2
+
3
+ module Standard
4
+ class Formatter < RuboCop::Formatter::BaseFormatter
5
+ def file_finished(file, offenses)
6
+ uncorrected_offenses = offenses.reject(&:corrected?)
7
+ print_header_once unless uncorrected_offenses.empty?
8
+ working_directory = Pathname.new(Dir.pwd)
9
+
10
+ uncorrected_offenses.each do |o|
11
+ absolute_path = Pathname.new(file)
12
+ relative_path = absolute_path.relative_path_from(working_directory)
13
+ output.printf(" %s:%d:%d: %s\n", relative_path, o.line, o.real_column, o.message.tr("\n", " "))
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def print_header_once
20
+ return if @header_printed_already
21
+ command = if File.split($PROGRAM_NAME).last == "rake"
22
+ "rake standard:fix"
23
+ else
24
+ "standard --fix"
25
+ end
26
+
27
+ output.print <<-HEADER.gsub(/^ {8}/, "")
28
+ standard: Use Ruby Standard Style (https://github.com/testdouble/standard)
29
+ standard: Run `#{command}` to automatically fix some problems.
30
+ HEADER
31
+ @header_printed_already = true
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,53 @@
1
+ require "yaml"
2
+ require_relative "file_finder"
3
+ require "pathname"
4
+
5
+ module Standard
6
+ class LoadsYamlConfig
7
+ def call(search_path)
8
+ yaml_path = FileFinder.new.call(".standard.yml", search_path)
9
+ construct_config(yaml_path, load_standard_yaml(yaml_path))
10
+ end
11
+
12
+ private
13
+
14
+ def load_standard_yaml(yaml_path)
15
+ if yaml_path
16
+ YAML.load_file(yaml_path) || {}
17
+ else
18
+ {}
19
+ end
20
+ end
21
+
22
+ def construct_config(yaml_path, standard_yaml)
23
+ {
24
+ ruby_version: Gem::Version.new((standard_yaml["ruby_version"] || RUBY_VERSION)),
25
+ fix: !!standard_yaml["fix"],
26
+ format: standard_yaml["format"],
27
+ parallel: !!standard_yaml["parallel"],
28
+ ignore: expand_ignore_config(standard_yaml["ignore"]),
29
+ config_root: yaml_path ? Pathname.new(yaml_path).dirname.to_s : "",
30
+ }
31
+ end
32
+
33
+ def expand_ignore_config(ignore_config)
34
+ arrayify(ignore_config).map { |rule|
35
+ if rule.is_a?(String)
36
+ [rule, ["AllCops"]]
37
+ elsif rule.is_a?(Hash)
38
+ rule.entries.first
39
+ end
40
+ }
41
+ end
42
+
43
+ def arrayify(object)
44
+ if object.nil?
45
+ []
46
+ elsif object.respond_to?(:to_ary)
47
+ object.to_ary || [object]
48
+ else
49
+ [object]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ require "rubocop"
2
+
3
+ module Standard
4
+ class MergesSettings
5
+ Settings = Struct.new(:options, :paths)
6
+
7
+ def call(argv, standard_yaml)
8
+ standard_argv, rubocop_argv = separate_argv(argv)
9
+ standard_cli_flags = parse_standard_argv(standard_argv)
10
+ rubocop_cli_flags, lint_paths = RuboCop::Options.new.parse(rubocop_argv)
11
+
12
+ Settings.new(
13
+ merge(standard_yaml, standard_cli_flags, rubocop_cli_flags),
14
+ lint_paths
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ def separate_argv(argv)
21
+ argv.partition { |flag|
22
+ flag == "--fix"
23
+ }
24
+ end
25
+
26
+ def parse_standard_argv(argv)
27
+ argv.each_with_object({}) { |arg, cli_flags|
28
+ if arg == "--fix"
29
+ cli_flags[:auto_correct] = true
30
+ cli_flags[:safe_auto_correct] = true
31
+ end
32
+ }
33
+ end
34
+
35
+ def merge(standard_yaml, standard_cli_flags, rubocop_cli_flags)
36
+ {
37
+ auto_correct: standard_yaml[:fix],
38
+ safe_auto_correct: standard_yaml[:fix],
39
+ formatters: [[standard_yaml[:format] || "Standard::Formatter", nil]],
40
+ parallel: standard_yaml[:parallel],
41
+ }.merge(standard_cli_flags).merge(rubocop_cli_flags)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require "pathname"
2
+
3
+ module Standard
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :standard
6
+
7
+ rake_tasks do
8
+ load Pathname.new(__dir__).join("rake.rb")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ module Standard
2
+ module RakeSupport
3
+ # Allow command line flags set in STANDARDOPTS (like MiniTest's TESTOPTS)
4
+ def self.argvify
5
+ if ENV["STANDARDOPTS"]
6
+ ENV["STANDARDOPTS"].split(/\s+/)
7
+ else
8
+ []
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ desc "Lint with the Standard Ruby style guide"
15
+ task :standard do
16
+ require "standard"
17
+ exit_code = Standard::Cli.new(Standard::RakeSupport.argvify).run
18
+ fail unless exit_code == 0
19
+ end
20
+
21
+ desc "Lint and automatically fix with the Standard Ruby style guide"
22
+ task :"standard:fix" do
23
+ require "standard"
24
+ exit_code = Standard::Cli.new(Standard::RakeSupport.argvify + ["--fix"]).run
25
+ fail unless exit_code == 0
26
+ end
@@ -0,0 +1,7 @@
1
+ module RuboCop
2
+ class Cop::Lint::AssignmentInCondition
3
+ def message(_)
4
+ "Wrap assignment in parentheses if intentional"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Standard
2
+ VERSION = Gem::Version.new("0.0.13")
3
+ end
data/lib/standard.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "rubocop"
2
+
3
+ require "standard/rubocop/ext"
4
+
5
+ require "standard/version"
6
+ require "standard/cli"
7
+ require "standard/railtie" if defined?(Rails)
8
+
9
+ require "standard/formatter"
10
+ require "standard/cop/semantic_blocks"
11
+
12
+ module Standard
13
+ end
data/standard.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "standard/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "standard"
7
+ spec.version = Standard::VERSION
8
+ spec.authors = ["Justin Searls"]
9
+ spec.email = ["searls@gmail.com"]
10
+
11
+ spec.summary = "Ruby Style Guide, with linter & automatic code fixer"
12
+ spec.homepage = "https://github.com/testdouble/standard"
13
+
14
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rubocop", ">= 0.60"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.17"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "simplecov"
28
+ end