standard 1.25.5 → 1.26.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dd3dd8a74fc215df752f020ed7b5e29f831592bc15cccc3b6c8d8a256bfbee1
4
- data.tar.gz: c914ab5a31d00ca0dd0ff6f483f2364b1a885d97988c3df38d169ef3c1edf5f8
3
+ metadata.gz: 98dc49ce80e703d0a3fb0d1d70bf4bac16c28e9dbf16988c09ebada4cb8bcd9f
4
+ data.tar.gz: 1e771a7f5eb05183f7700c590bb119eaa5cb104566b656b1945a2198099dd2f2
5
5
  SHA512:
6
- metadata.gz: 60960dec029698d932669cc588558bce61cf0f58fec3eea09659d04a0c4d3b5708d7b6e055b652eb0cd46ea5c5c253685107df110212a48fc48ae13d78cc030e
7
- data.tar.gz: d2d277d165b146c517837384d9519f044c3661f0499198189404ca9e9630065e774e7a3760f57f7f25e0648836558104c64e286f35163bc8e0d2460da3c20c9e
6
+ metadata.gz: 2ed743c84d1ca8fc6ed19678292463cdb98c1e82d6f698c3fce646a2bcc022dd133e0d1040216c68f5c70d6884248b1fae9a04ccace31497403ba200a0b512b5
7
+ data.tar.gz: fbef7298ac3da5a0842e087225ed5abd61ec75f8eb6a51629c6213fa196b477cef5b7a7b3b5be161434e405fb6068b9d2f5557f645f0ea145cb9ab39f444fdf0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.26.0
4
+
5
+ * Introduce `--fix-unsafely` and `rake standard:fix_unsafely` for running
6
+ unsafe corrections. Improve output about fix suggestions, as well
7
+ [#545](https://github.com/testdouble/standard/pull/545)
8
+
3
9
  ## 1.25.5
4
10
 
5
11
  * Temporarily disable registration of `executeCommand` to prevent conflict with
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- standard (1.25.5)
4
+ standard (1.26.0)
5
5
  language_server-protocol (~> 3.17.0.2)
6
6
  rubocop (~> 1.48.1)
7
7
  rubocop-performance (~> 1.16.0)
data/README.md CHANGED
@@ -76,8 +76,8 @@ files in the current working directory:
76
76
  ```bash
77
77
  $ bundle exec standardrb
78
78
  standard: Use Ruby Standard Style (https://github.com/testdouble/standard)
79
- standard: Run `standardrb --fix` to automatically fix some problems.
80
- /Users/code/cli.rb:31:23: Style/Semicolon: Do not use semicolons to terminate expressions.
79
+ standard: Run `standardrb --fix` to potentially fix one problem.
80
+ /Users/jill/code/cli.rb:31:23: Style/Semicolon: Do not use semicolons to terminate expressions.
81
81
  ```
82
82
 
83
83
  You can optionally pass in a directory (or directories) using a glob pattern.
@@ -121,6 +121,7 @@ Here are the tasks bundled with Standard:
121
121
  ```
122
122
  $ rake standard # equivalent to running `standardrb`
123
123
  $ rake standard:fix # equivalent to running `standardrb --fix`
124
+ $ rake standard:fix_unsafely # equivalent to running `standardrb --fix-unsafely`
124
125
  ```
125
126
 
126
127
  You may also pass command line options to Standard's Rake tasks by embedding
@@ -268,6 +269,11 @@ Yes! You can use `standardrb --fix` to fix most issues automatically.
268
269
  `standardrb --fix` is built into `standardrb` for maximum convenience. Most
269
270
  problems are fixable, but some errors must be fixed manually.
270
271
 
272
+ If you're feeling sinister, you can also use `standardrb --fix-unsafely`, which
273
+ will also perform fixes, even if they run the risk of altering program behavior.
274
+ If you read your git diffs closely and have good tests, this is often okay, but
275
+ YMMV… it's called unsafe for a reason!
276
+
271
277
  ## Can I override the `fix: true` config setting?
272
278
 
273
279
  Also yes! You can use `standardrb --no-fix`. Not `fix`ing is the default
data/config/base.yml CHANGED
@@ -78,7 +78,6 @@ Layout/AssignmentIndentation:
78
78
  Layout/BeginEndAlignment:
79
79
  Enabled: true
80
80
  EnforcedStyleAlignWith: start_of_line
81
- AutoCorrect: true
82
81
  Severity: warning
83
82
 
84
83
  Layout/BlockAlignment:
@@ -110,7 +109,6 @@ Layout/ConditionPosition:
110
109
  Layout/DefEndAlignment:
111
110
  Enabled: true
112
111
  EnforcedStyleAlignWith: start_of_line
113
- AutoCorrect: true
114
112
  Severity: warning
115
113
 
116
114
  Layout/DotPosition:
@@ -174,7 +172,6 @@ Layout/EmptyLinesAroundModuleBody:
174
172
 
175
173
  Layout/EndAlignment:
176
174
  Enabled: true
177
- AutoCorrect: true
178
175
  EnforcedStyleAlignWith: variable
179
176
  Severity: warning
180
177
 
@@ -447,6 +444,8 @@ Lint/AmbiguousRegexpLiteral:
447
444
  Lint/AssignmentInCondition:
448
445
  Enabled: true
449
446
  AllowSafeAssignment: true
447
+ # Intentionally disable autocorrect to force us to intentionally decide
448
+ # whether assignment is intended as opposed to comparison
450
449
  AutoCorrect: false
451
450
 
452
451
  Lint/BigDecimalNew:
@@ -529,7 +528,6 @@ Lint/EmptyConditionalBody:
529
528
 
530
529
  Lint/EmptyEnsure:
531
530
  Enabled: true
532
- AutoCorrect: true
533
531
 
534
532
  Lint/EmptyExpression:
535
533
  Enabled: true
@@ -695,7 +693,6 @@ Lint/RedundantWithObject:
695
693
 
696
694
  Lint/RefinementImportMethods:
697
695
  Enabled: true
698
- SafeAutoCorrect: false
699
696
 
700
697
  Lint/RegexpAsCondition:
701
698
  Enabled: true
@@ -1003,7 +1000,6 @@ Performance/FlatMap:
1003
1000
 
1004
1001
  Performance/InefficientHashSearch:
1005
1002
  Enabled: true
1006
- Safe: false
1007
1003
 
1008
1004
  Performance/IoReadlines:
1009
1005
  Enabled: false
@@ -1019,7 +1015,6 @@ Performance/OpenStruct:
1019
1015
 
1020
1016
  Performance/RangeInclude:
1021
1017
  Enabled: true
1022
- Safe: false
1023
1018
 
1024
1019
  Performance/RedundantBlockCall:
1025
1020
  Enabled: false
@@ -1096,23 +1091,18 @@ Security/Eval:
1096
1091
 
1097
1092
  Security/IoMethods:
1098
1093
  Enabled: false
1099
- Safe: false
1100
1094
 
1101
1095
  Security/JSONLoad:
1102
1096
  Enabled: true
1103
- AutoCorrect: false
1104
- SafeAutoCorrect: false
1105
1097
 
1106
1098
  Security/MarshalLoad:
1107
1099
  Enabled: false
1108
1100
 
1109
1101
  Security/Open:
1110
1102
  Enabled: true
1111
- Safe: false
1112
1103
 
1113
1104
  Security/YAMLLoad:
1114
1105
  Enabled: true
1115
- SafeAutoCorrect: false
1116
1106
 
1117
1107
  Standard/BlockSingleLineBraces:
1118
1108
  Enabled: true
@@ -1305,7 +1295,6 @@ Style/Encoding:
1305
1295
 
1306
1296
  Style/EndBlock:
1307
1297
  Enabled: true
1308
- AutoCorrect: true
1309
1298
 
1310
1299
  Style/EndlessMethod:
1311
1300
  Enabled: false
@@ -1443,7 +1432,6 @@ Style/LambdaCall:
1443
1432
 
1444
1433
  Style/LineEndConcatenation:
1445
1434
  Enabled: true
1446
- SafeAutoCorrect: false
1447
1435
 
1448
1436
  Style/MagicCommentFormat:
1449
1437
  Enabled: false
@@ -7,15 +7,15 @@ module Standard
7
7
 
8
8
  class BuildsConfig
9
9
  def initialize
10
- @parses_cli_option = ParsesCliOption.new
10
+ @resolves_yaml_option = ResolvesYamlOption.new
11
11
  @loads_yaml_config = LoadsYamlConfig.new
12
12
  @merges_settings = MergesSettings.new
13
13
  @creates_config_store = CreatesConfigStore.new
14
14
  end
15
15
 
16
16
  def call(argv, search_path = Dir.pwd)
17
- standard_yaml_path = determine_yaml_file(argv, search_path, "--config", ".standard.yml")
18
- todo_yaml_path = determine_yaml_file(argv, search_path, "--todo", ".standard_todo.yml")
17
+ standard_yaml_path = @resolves_yaml_option.call(argv, search_path, "--config", ".standard.yml")
18
+ todo_yaml_path = @resolves_yaml_option.call(argv, search_path, "--todo", ".standard_todo.yml")
19
19
  standard_config = @loads_yaml_config.call(standard_yaml_path, todo_yaml_path)
20
20
 
21
21
  settings = @merges_settings.call(argv, standard_config)
@@ -26,11 +26,5 @@ module Standard
26
26
  @creates_config_store.call(standard_config)
27
27
  )
28
28
  end
29
-
30
- private
31
-
32
- def determine_yaml_file(argv, search_path, option_name, default_file)
33
- @parses_cli_option.call(argv, option_name) || FileFinder.new.call(default_file, search_path)
34
- end
35
29
  end
36
30
  end
@@ -1,47 +1,41 @@
1
1
  require "yaml"
2
2
  require "rubocop"
3
- require_relative "detects_fixability"
4
3
 
5
4
  module Standard
6
5
  class Formatter < RuboCop::Formatter::BaseFormatter
7
- STANDARD_GREETING = <<-MSG.gsub(/^ {6}/, "")
6
+ STANDARD_GREETING = <<~MSG
8
7
  standard: Use Ruby Standard Style (https://github.com/testdouble/standard)
9
8
  MSG
10
9
 
11
- def self.fixable_error_message(command)
12
- <<-MSG.gsub(/^ {8}/, "")
13
- standard: Run `#{command}` to automatically fix some problems.
14
- MSG
15
- end
16
-
17
- def initialize(*args)
18
- super
19
- @detects_fixability = DetectsFixability.new
20
- @header_printed_already = false
21
- @fix_suggestion_printed_already = false
22
- @offenses_encountered = false
23
- end
24
-
25
10
  def started(_target_files)
11
+ @header_printed_already = false
12
+ @total_correction_count = 0
13
+ @total_correctable_count = 0
14
+ @total_uncorrected_count = 0
26
15
  print_todo_warning
27
16
  end
28
17
 
29
18
  def file_finished(file, offenses)
30
- uncorrected_offenses = offenses.reject(&:corrected?)
31
-
32
- return unless uncorrected_offenses.any?
33
-
34
- @offenses_encountered = true
19
+ track_stats(offenses)
35
20
 
36
- print_header_once
37
- print_fix_suggestion_once(uncorrected_offenses)
21
+ if (uncorrected_offenses = offenses.reject(&:corrected?)).any?
22
+ print_header_once
38
23
 
39
- uncorrected_offenses.each do |o|
40
- output.printf(" %s:%d:%d: %s\n", path_to(file), o.line, o.real_column, o.message.tr("\n", " "))
24
+ uncorrected_offenses.each do |o|
25
+ output.printf(" %s:%d:%d: %s\n", path_to(file), o.line, o.real_column, o.message.tr("\n", " "))
26
+ end
41
27
  end
42
28
  end
43
29
 
30
+ def track_stats(offenses)
31
+ corrected = offenses.count(&:corrected?)
32
+ @total_correction_count += corrected
33
+ @total_correctable_count += offenses.count(&:correctable?) - corrected
34
+ @total_uncorrected_count += offenses.count - corrected
35
+ end
36
+
44
37
  def finished(_inspected_files)
38
+ print_fix_suggestion
45
39
  print_todo_congratulations
46
40
  end
47
41
 
@@ -55,16 +49,17 @@ module Standard
55
49
  @header_printed_already = true
56
50
  end
57
51
 
58
- def print_fix_suggestion_once(offenses)
59
- if !@fix_suggestion_printed_already && should_suggest_fix?(offenses)
60
- command = if File.split($PROGRAM_NAME).last == "rake"
61
- "rake standard:fix"
52
+ def print_fix_suggestion
53
+ if (fix_mode = potential_fix_mode)
54
+ run_mode = determine_run_mode
55
+
56
+ command = if run_mode == :rake
57
+ "rake standard:#{fix_mode}"
62
58
  else
63
- "standardrb --fix"
59
+ "standardrb --#{fix_mode.to_s.tr("_", "-")}"
64
60
  end
65
61
 
66
- output.print self.class.fixable_error_message(command)
67
- @fix_suggestion_printed_already = true
62
+ output.print fixable_error_message(command)
68
63
  end
69
64
  end
70
65
 
@@ -75,7 +70,7 @@ module Standard
75
70
  todo_ignore_files = options[:todo_ignore_files]
76
71
  return unless todo_ignore_files&.any?
77
72
 
78
- output.print <<-HEADER.gsub(/^ {8}/, "")
73
+ output.print <<~HEADER
79
74
  WARNING: this project is being migrated to standard gradually via `#{todo_file}` and is ignoring these files:
80
75
  HEADER
81
76
 
@@ -85,29 +80,55 @@ module Standard
85
80
  end
86
81
 
87
82
  def print_todo_congratulations
88
- return if @offenses_encountered
89
-
90
- todo_file = options[:todo_file]
91
- return unless todo_file
92
-
93
- todo_ignore_files = options[:todo_ignore_files]
94
- return if todo_ignore_files&.any?
95
-
96
- output.print <<-HEADER.gsub(/^ {8}/, "")
97
- Congratulations, you've successfully migrated this project to Standard! Delete `#{todo_file}` in celebration.
98
- HEADER
83
+ if @total_uncorrected_count == 0 &&
84
+ options[:todo_file] &&
85
+ options[:todo_ignore_files]&.none?
86
+ output.print <<~HEADER
87
+ Congratulations, you've successfully migrated this project to Standard! Delete `#{options[:todo_file]}` in celebration.
88
+ HEADER
89
+ end
99
90
  end
100
91
 
101
92
  def path_to(file)
102
93
  Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd))
103
94
  end
104
95
 
105
- def auto_correct_option_provided?
106
- options[:autocorrect]
96
+ def potential_fix_mode
97
+ return nil unless @total_correctable_count > 0
98
+
99
+ if !options[:autocorrect]
100
+ :fix
101
+ elsif options[:autocorrect] && options[:safe_autocorrect]
102
+ :fix_unsafely
103
+ end
104
+ end
105
+
106
+ def determine_run_mode
107
+ if File.split($PROGRAM_NAME).last == "rake"
108
+ :rake
109
+ else
110
+ :cli
111
+ end
107
112
  end
108
113
 
109
- def should_suggest_fix?(offenses)
110
- !auto_correct_option_provided? && @detects_fixability.call(offenses)
114
+ def fixable_error_message(command)
115
+ sales_pitch = if !options[:autocorrect]
116
+ if @total_correctable_count > 1
117
+ "fix up to #{@total_correctable_count} problems"
118
+ else
119
+ "potentially fix one problem"
120
+ end
121
+ elsif options[:autocorrect] && options[:safe_autocorrect]
122
+ if @total_correctable_count > 1
123
+ "DANGEROUSLY fix #{@total_correctable_count} problems"
124
+ else
125
+ "DANGEROUSLY fix one problem"
126
+ end
127
+ end
128
+
129
+ <<~MSG
130
+ standard: Run `#{command}` to #{sales_pitch}.
131
+ MSG
111
132
  end
112
133
  end
113
134
  end
@@ -1,7 +1,7 @@
1
1
  require "yaml"
2
2
  require "pathname"
3
3
  require_relative "file_finder"
4
- require_relative "parses_cli_option"
4
+ require_relative "resolves_yaml_option"
5
5
 
6
6
  module Standard
7
7
  class LoadsYamlConfig
@@ -49,9 +49,9 @@ module Standard
49
49
  }
50
50
  def fork_config(path, text, format:)
51
51
  options = if format
52
- {stdin: text, autocorrect: true, formatters: [], safe_autocorrect: true}
52
+ {stdin: text, autocorrect: true, safe_autocorrect: true, formatters: []}
53
53
  else
54
- {stdin: text, autocorrect: false, formatters: [["json"]], format: "json"}
54
+ {stdin: text, autocorrect: false, safe_autocorrect: false, formatters: [["json"]], format: "json"}
55
55
  end
56
56
  Standard::Config.new(@config.runner, [path], BASE_OPTIONS.merge(options), @config.rubocop_config_store)
57
57
  end
@@ -20,7 +20,7 @@ module Standard
20
20
 
21
21
  def separate_argv(argv)
22
22
  argv.partition do |flag|
23
- ["--generate-todo", "--fix", "--no-fix", "--version", "-v", "--verbose-version", "-V", "--help", "-h", "--lsp"].include?(flag)
23
+ ["--generate-todo", "--fix", "--fix-unsafely", "--no-fix", "--version", "-v", "--verbose-version", "-V", "--help", "-h", "--lsp"].include?(flag)
24
24
  end
25
25
  end
26
26
 
@@ -28,8 +28,13 @@ module Standard
28
28
  argv.each_with_object({}) do |arg, cli_flags|
29
29
  if arg == "--fix"
30
30
  cli_flags[:autocorrect] = true
31
+ cli_flags[:safe_autocorrect] = true
32
+ elsif arg == "--fix-unsafely"
33
+ cli_flags[:autocorrect] = true
34
+ cli_flags[:safe_autocorrect] = false
31
35
  elsif arg == "--no-fix"
32
36
  cli_flags[:autocorrect] = false
37
+ cli_flags[:safe_autocorrect] = false
33
38
  end
34
39
  end
35
40
  end
@@ -53,6 +58,7 @@ module Standard
53
58
  def merge(standard_yaml, standard_cli_flags, rubocop_cli_flags)
54
59
  {
55
60
  autocorrect: standard_yaml[:fix],
61
+ safe_autocorrect: true,
56
62
  formatters: [[standard_yaml[:format] || "Standard::Formatter", nil]],
57
63
  parallel: standard_yaml[:parallel],
58
64
  todo_file: standard_yaml[:todo_file],
data/lib/standard/rake.rb CHANGED
@@ -18,9 +18,16 @@ task :standard do
18
18
  fail unless exit_code == 0
19
19
  end
20
20
 
21
- desc "Lint and automatically fix with the Standard Ruby style guide"
21
+ desc "Lint and automatically make safe fixes with the Standard Ruby style guide"
22
22
  task :"standard:fix" do
23
23
  require "standard"
24
24
  exit_code = Standard::Cli.new(Standard::RakeSupport.argvify + ["--fix"]).run
25
25
  fail unless exit_code == 0
26
26
  end
27
+
28
+ desc "Lint and automatically make fixes (even unsafe ones!) with the Standard Ruby style guide"
29
+ task :"standard:fix_unsafely" do
30
+ require "standard"
31
+ exit_code = Standard::Cli.new(Standard::RakeSupport.argvify + ["--fix-unsafely"]).run
32
+ fail unless exit_code == 0
33
+ end
@@ -1,8 +1,14 @@
1
1
  require "pathname"
2
2
 
3
3
  module Standard
4
- class ParsesCliOption
5
- def call(argv, option_name)
4
+ class ResolvesYamlOption
5
+ def call(argv, search_path, option_name, default_file)
6
+ search_argv(argv, option_name) || FileFinder.new.call(default_file, search_path)
7
+ end
8
+
9
+ private
10
+
11
+ def search_argv(argv, option_name)
6
12
  return unless (config_file = argv_value_for(argv, option_name))
7
13
 
8
14
  resolved_config = Pathname.new(config_file)
@@ -9,7 +9,8 @@ module Standard
9
9
 
10
10
  Options:
11
11
 
12
- --fix Automatically fix failures where possible
12
+ --fix Apply automatic fixes that we're confident won't break your code
13
+ --fix-unsafely Apply even more fixes, including some that may change code behavior
13
14
  --no-fix Do not automatically fix failures
14
15
  --format <name> Format output with any RuboCop formatter (e.g. "json")
15
16
  --generate-todo Create a .standard_todo.yml that lists all the files that contain errors
@@ -4,7 +4,7 @@ module Standard
4
4
  module Runners
5
5
  class VerboseVersion
6
6
  def call(config)
7
- puts <<-MSG.gsub(/^ {10}/, "")
7
+ puts <<~MSG
8
8
  Standard version: #{Standard::VERSION}
9
9
  RuboCop version: #{RuboCop::Version.version(debug: true)}
10
10
  MSG
@@ -1,3 +1,3 @@
1
1
  module Standard
2
- VERSION = Gem::Version.new("1.25.5")
2
+ VERSION = Gem::Version.new("1.26.0")
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.25.5
4
+ version: 1.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-01 00:00:00.000000000 Z
11
+ date: 2023-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -97,7 +97,6 @@ files:
97
97
  - lib/standard/creates_config_store/configures_ignored_paths.rb
98
98
  - lib/standard/creates_config_store/merges_user_config_extensions.rb
99
99
  - lib/standard/creates_config_store/sets_target_ruby_version.rb
100
- - lib/standard/detects_fixability.rb
101
100
  - lib/standard/file_finder.rb
102
101
  - lib/standard/formatter.rb
103
102
  - lib/standard/loads_runner.rb
@@ -108,9 +107,9 @@ files:
108
107
  - lib/standard/lsp/server.rb
109
108
  - lib/standard/lsp/standardizer.rb
110
109
  - lib/standard/merges_settings.rb
111
- - lib/standard/parses_cli_option.rb
112
110
  - lib/standard/railtie.rb
113
111
  - lib/standard/rake.rb
112
+ - lib/standard/resolves_yaml_option.rb
114
113
  - lib/standard/rubocop/ext.rb
115
114
  - lib/standard/runners/genignore.rb
116
115
  - lib/standard/runners/help.rb
@@ -1,16 +0,0 @@
1
- module Standard
2
- class DetectsFixability
3
- def call(offenses)
4
- offenses.any? { |offense|
5
- cop = cop_instance(offense.cop_name)
6
- cop.correctable? && cop.safe_autocorrect?
7
- }
8
- end
9
-
10
- private
11
-
12
- def cop_instance(cop_name)
13
- RuboCop::Cop.const_get(cop_name.gsub("/", "::")).new
14
- end
15
- end
16
- end