standard 0.0.16

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,26 @@
1
+ require_relative "loads_yaml_config"
2
+ require_relative "merges_settings"
3
+ require_relative "creates_config_store"
4
+
5
+ module Standard
6
+ Config = Struct.new(:runner, :paths, :rubocop_options, :rubocop_config_store)
7
+
8
+ class BuildsConfig
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(argv, search_path)
17
+ settings = @merges_settings.call(argv, standard_config)
18
+ Config.new(
19
+ settings.runner,
20
+ settings.paths,
21
+ settings.options,
22
+ @creates_config_store.call(standard_config)
23
+ )
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require_relative "builds_config"
2
+ require_relative "loads_runner"
3
+
4
+ module Standard
5
+ class Cli
6
+ SUCCESS_STATUS_CODE = 0
7
+ FAILURE_STATUS_CODE = 1
8
+
9
+ def initialize(argv)
10
+ @argv = argv
11
+ @builds_config = BuildsConfig.new
12
+ @loads_runner = LoadsRunner.new
13
+ end
14
+
15
+ def run
16
+ config = @builds_config.call(@argv)
17
+
18
+ success = @loads_runner.call(config.runner).call(config)
19
+
20
+ success ? SUCCESS_STATUS_CODE : FAILURE_STATUS_CODE
21
+ end
22
+ end
23
+ 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
+ "standardrb --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,9 @@
1
+ module Standard
2
+ class LoadsRunner
3
+ # Warning: clever metaprogramming. 99% of the time this is Runners::Rubocop
4
+ def call(command)
5
+ require_relative "runners/#{command}"
6
+ ::Standard::Runners.const_get(command.to_s.capitalize).new
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,59 @@
1
+ require "yaml"
2
+ require "pathname"
3
+ require_relative "file_finder"
4
+ require_relative "parses_cli_option"
5
+
6
+ module Standard
7
+ class LoadsYamlConfig
8
+ def initialize
9
+ @parses_cli_option = ParsesCliOption.new
10
+ end
11
+
12
+ def call(argv, search_path)
13
+ yaml_path = @parses_cli_option.call(argv, "--config") ||
14
+ FileFinder.new.call(".standard.yml", search_path)
15
+ construct_config(yaml_path, load_standard_yaml(yaml_path))
16
+ end
17
+
18
+ private
19
+
20
+ def load_standard_yaml(yaml_path)
21
+ if yaml_path
22
+ YAML.load_file(yaml_path) || {}
23
+ else
24
+ {}
25
+ end
26
+ end
27
+
28
+ def construct_config(yaml_path, standard_yaml)
29
+ {
30
+ ruby_version: Gem::Version.new((standard_yaml["ruby_version"] || RUBY_VERSION)),
31
+ fix: !!standard_yaml["fix"],
32
+ format: standard_yaml["format"],
33
+ parallel: !!standard_yaml["parallel"],
34
+ ignore: expand_ignore_config(standard_yaml["ignore"]),
35
+ config_root: yaml_path ? Pathname.new(yaml_path).dirname.to_s : "",
36
+ }
37
+ end
38
+
39
+ def expand_ignore_config(ignore_config)
40
+ arrayify(ignore_config).map { |rule|
41
+ if rule.is_a?(String)
42
+ [rule, ["AllCops"]]
43
+ elsif rule.is_a?(Hash)
44
+ rule.entries.first
45
+ end
46
+ }
47
+ end
48
+
49
+ def arrayify(object)
50
+ if object.nil?
51
+ []
52
+ elsif object.respond_to?(:to_ary)
53
+ object.to_ary || [object]
54
+ else
55
+ [object]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,61 @@
1
+ require "rubocop"
2
+
3
+ module Standard
4
+ class MergesSettings
5
+ Settings = Struct.new(:runner, :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
+ determine_command(standard_argv),
14
+ merge(standard_yaml, standard_cli_flags, without_banned(rubocop_cli_flags)),
15
+ lint_paths
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ def separate_argv(argv)
22
+ argv.partition { |flag|
23
+ ["--fix", "--version", "-v", "--help", "-h"].include?(flag)
24
+ }
25
+ end
26
+
27
+ def parse_standard_argv(argv)
28
+ argv.each_with_object({}) { |arg, cli_flags|
29
+ if arg == "--fix"
30
+ cli_flags[:auto_correct] = true
31
+ cli_flags[:safe_auto_correct] = true
32
+ end
33
+ }
34
+ end
35
+
36
+ def determine_command(argv)
37
+ if (argv & ["--help", "-h"]).any?
38
+ :help
39
+ elsif (argv & ["--version", "-v"]).any?
40
+ :version
41
+ else
42
+ :rubocop
43
+ end
44
+ end
45
+
46
+ def merge(standard_yaml, standard_cli_flags, rubocop_cli_flags)
47
+ {
48
+ auto_correct: standard_yaml[:fix],
49
+ safe_auto_correct: standard_yaml[:fix],
50
+ formatters: [[standard_yaml[:format] || "Standard::Formatter", nil]],
51
+ parallel: standard_yaml[:parallel],
52
+ }.merge(standard_cli_flags).merge(rubocop_cli_flags)
53
+ end
54
+
55
+ def without_banned(rubocop_cli_flags)
56
+ rubocop_cli_flags.tap do |flags|
57
+ flags.delete(:config)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,21 @@
1
+ require "pathname"
2
+
3
+ module Standard
4
+ class ParsesCliOption
5
+ def call(argv, option_name)
6
+ return unless (config_file = argv_value_for(argv, option_name))
7
+
8
+ resolved_config = Pathname.new(config_file)
9
+ if resolved_config.exist?
10
+ resolved_config.expand_path
11
+ else
12
+ raise "Configuration file \"#{resolved_config.expand_path}\" not found."
13
+ end
14
+ end
15
+
16
+ def argv_value_for(argv, option_name)
17
+ return unless (index = argv.index(option_name))
18
+ argv[index + 1]
19
+ end
20
+ end
21
+ 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,43 @@
1
+ require_relative "../file_finder"
2
+
3
+ module Standard
4
+ module Runners
5
+ class Help
6
+ def call(config)
7
+ puts <<-MESSAGE.gsub(/^ {10}/, "")
8
+ Usage: standardrb [--fix] [-vh] [--format <name>] [--] [FILE]...
9
+
10
+ Options:
11
+
12
+ --fix Automatically fix failures where possible
13
+ --format <name> Format output with any RuboCop formatter (e.g. "json")
14
+ -v, --version Print the version of Standard
15
+ -h, --help Print this message
16
+ FILE Files to lint [default: ./]
17
+
18
+ Standard also forwards most CLI arguments to RuboCop. To see them, run:
19
+
20
+ $ rubocop --help
21
+
22
+ While Standard only offers a few configuration options, most can be set in
23
+ a `.standard.yml` file. For full documentation, please visit:
24
+
25
+ https://github.com/testdouble/standard
26
+
27
+ Having trouble? Here's some diagnostic information:
28
+
29
+ Ruby version: #{RUBY_VERSION}
30
+ Current directory: #{Dir.pwd}
31
+ RuboCop version: #{RuboCop::Version.version}
32
+ Standard version: #{Standard::VERSION}
33
+ Standard config file: #{FileFinder.new.call(".standard.yml", Dir.pwd) || "[No file found]"}
34
+
35
+ Please report any problems (and include the above information) at the URL below:
36
+
37
+ https://github.com/testdouble/standard/issues/new
38
+
39
+ MESSAGE
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ require "rubocop"
2
+
3
+ module Standard
4
+ module Runners
5
+ class Rubocop
6
+ def call(config)
7
+ rubocop_runner = RuboCop::Runner.new(
8
+ config.rubocop_options,
9
+ config.rubocop_config_store
10
+ )
11
+
12
+ rubocop_runner.run(config.paths).tap do |success|
13
+ unless success
14
+ print_errors_and_warnings(rubocop_runner)
15
+ print_call_for_feedback
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def print_errors_and_warnings(rubocop_runner)
23
+ (rubocop_runner.warnings + rubocop_runner.errors).each do |message|
24
+ warn message
25
+ end
26
+ end
27
+
28
+ def print_call_for_feedback
29
+ puts <<-CALL_TO_ACTION.gsub(/^ {10}/, "")
30
+
31
+ Notice: Disagree with these rules? While StandardRB is pre-1.0.0, feel free to submit suggestions to:
32
+ https://github.com/testdouble/standard/issues/new
33
+ CALL_TO_ACTION
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module Standard
2
+ module Runners
3
+ class Version
4
+ def call(config)
5
+ puts Standard::VERSION
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Standard
2
+ VERSION = Gem::Version.new("0.0.16")
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