Skip to content

Commit 8163e3e

Browse files
authored
Merge pull request rails#55450 from skipkayhil/hm-zzrsyxvtowtwrrym
[rail_inspector] Fix linting defaults post-release
2 parents 54c5884 + b2c6d62 commit 8163e3e

File tree

7 files changed

+228
-148
lines changed

7 files changed

+228
-148
lines changed

tools/rail_inspector/lib/rail_inspector/configuring.rb

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
require "pathname"
44
require_relative "./configuring/check/general_configuration"
55
require_relative "./configuring/check/framework_defaults"
6+
require_relative "./configuring/check/new_framework_defaults_file"
7+
require_relative "./configuring/document"
8+
require_relative "./visitor/framework_default"
69

710
module RailInspector
811
class Configuring
@@ -43,29 +46,6 @@ def method_missing(name, ...)
4346
end
4447
end
4548

46-
class Doc
47-
attr_accessor :general_config, :versioned_defaults
48-
49-
def initialize(content)
50-
@before, @versioned_defaults, @general_config, @after =
51-
content
52-
.split("\n")
53-
.slice_before do |line|
54-
[
55-
"### Versioned Default Values",
56-
"### Rails General Configuration",
57-
"### Configuring Assets"
58-
].include?(line)
59-
end
60-
.to_a
61-
end
62-
63-
def to_s
64-
(@before + @versioned_defaults + @general_config + @after).join("\n") +
65-
"\n"
66-
end
67-
end
68-
6949
attr_reader :errors, :files
7050

7151
def initialize(rails_path)
@@ -82,13 +62,23 @@ def initialize(rails_path)
8262
end
8363

8464
def check
85-
[Check::GeneralConfiguration, Check::FrameworkDefaults].each do |check|
86-
check.new(self).check
87-
end
65+
[
66+
Check::GeneralConfiguration.new(self),
67+
Check::FrameworkDefaults.new(
68+
self,
69+
framework_defaults_by_version,
70+
doc.versioned_defaults,
71+
),
72+
Check::NewFrameworkDefaultsFile.new(
73+
self,
74+
framework_defaults_by_version[rails_version].keys,
75+
files.new_framework_defaults.read
76+
),
77+
].each(&:check)
8878
end
8979

9080
def doc
91-
@doc ||= Configuring::Doc.new(files.doc_path.read)
81+
@doc ||= Configuring::Document.parse(files.doc_path.read)
9282
end
9383

9484
def rails_version
@@ -106,5 +96,12 @@ def error_message
10696
"Make sure new configurations are added to configuring.md#rails-general-configuration in alphabetical order.\n" +
10797
"Errors may be autocorrectable with the --autocorrect flag"
10898
end
99+
100+
private
101+
def framework_defaults_by_version
102+
@framework_defaults_by_version ||= Visitor::FrameworkDefault.new.tap { |visitor|
103+
visitor.visit(files.application_configuration.parse)
104+
}.config_map
105+
end
109106
end
110107
end

tools/rail_inspector/lib/rail_inspector/configuring/check/framework_defaults.rb

Lines changed: 39 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,67 @@
11
# frozen_string_literal: true
22

33
require "tempfile"
4-
require_relative "../../visitor/framework_default"
54

65
module RailInspector
76
class Configuring
87
module Check
98
class FrameworkDefaults
10-
class NewFrameworkDefaultsFile
11-
attr_reader :checker, :visitor
12-
13-
# Defaults are strings like:
14-
# self.yjit
15-
# action_controller.escape_json_responses
16-
def initialize(checker, defaults, file_content)
17-
@checker = checker
18-
@defaults = defaults
19-
@file_content = file_content
20-
end
21-
22-
def check
23-
@defaults.each do |config|
24-
if config.start_with? "self"
25-
next if @file_content.include? config.gsub(/^self/, "config")
26-
next if @file_content.include? config.gsub(/^self/, "configuration")
27-
end
28-
29-
next if @file_content.include? config
30-
31-
next if config == "self.yjit"
32-
33-
checker.errors << <<~MESSAGE
34-
#{checker.files.new_framework_defaults}: Missing new default
35-
#{config}
36-
37-
MESSAGE
38-
end
39-
end
40-
end
41-
429
attr_reader :checker
4310

44-
def initialize(checker)
11+
def initialize(checker, defaults_by_version, documented_defaults)
4512
@checker = checker
13+
@defaults_by_version = defaults_by_version
14+
@documented_defaults = documented_defaults
4615
end
4716

4817
def check
49-
header, *defaults_by_version = documented_defaults
50-
51-
NewFrameworkDefaultsFile.new(
52-
checker,
53-
visitor.config_map[checker.rails_version].keys,
54-
checker.files.new_framework_defaults.read
55-
).check
18+
expected_text = +""
5619

57-
checker.doc.versioned_defaults =
58-
header +
59-
defaults_by_version
60-
.map { |defaults| check_defaults(defaults) }
61-
.flatten
62-
end
20+
expected_text =
21+
@defaults_by_version.reverse_each.map { |version, defaults|
22+
expected_text = +"#### Default Values for Target Version #{version}\n"
6323

64-
private
65-
def app_config_tree
66-
checker.files.application_configuration.parse
67-
end
24+
expected_text << "\n" unless defaults.empty?
6825

69-
def check_defaults(defaults)
70-
header, configs = defaults[0], defaults[2, defaults.length - 3]
71-
configs ||= []
26+
defaults.map { |config, value|
27+
full_config =
28+
case config
29+
when /^[A-Z]/
30+
config
31+
when /^self/
32+
config.sub("self", "config")
33+
else
34+
"config.#{config}"
35+
end
7236

73-
version = header.match(/\d\.\d/)[0]
37+
"- [`#{full_config}`](##{full_config.tr("._", "-").downcase}): `#{value}`\n"
38+
}.sort.each { |t| expected_text << t }
7439

75-
generated_doc =
76-
visitor.config_map[version]
77-
.map do |config, value|
78-
full_config =
79-
case config
80-
when /^[A-Z]/
81-
config
82-
when /^self/
83-
config.sub("self", "config")
84-
else
85-
"config.#{config}"
86-
end
40+
expected_text
41+
}.join("\n")
8742

88-
"- [`#{full_config}`](##{full_config.tr("._", "-").downcase}): `#{value}`"
89-
end
90-
.sort
43+
config_diff =
44+
Tempfile.create("expected") do |doc|
45+
doc << expected_text
46+
doc.flush
9147

92-
config_diff =
93-
Tempfile.create("expected") do |doc|
94-
doc << generated_doc.join("\n")
95-
doc.flush
48+
Tempfile.create("actual") do |code|
49+
code << @documented_defaults
50+
code.flush
9651

97-
Tempfile.create("actual") do |code|
98-
code << configs.join("\n")
99-
code.flush
100-
101-
`git diff --color --no-index #{doc.path} #{code.path}`
102-
end
52+
`git diff --color --no-index #{doc.path} #{code.path}`
10353
end
54+
end
10455

105-
checker.errors << <<~MESSAGE unless config_diff.empty?
106-
#{checker.files.application_configuration}: Incorrect load_defaults docs
107-
--- Expected
108-
+++ Actual
109-
#{config_diff.split("\n")[5..].join("\n")}
110-
MESSAGE
111-
112-
[header, "", *generated_doc, ""]
113-
end
114-
115-
def documented_defaults
116-
checker
117-
.doc
118-
.versioned_defaults
119-
.slice_before { |line| line.start_with?("####") }
120-
.to_a
121-
end
56+
checker.errors << <<~MESSAGE unless config_diff.empty?
57+
#{checker.files.application_configuration}: Incorrect load_defaults docs
58+
--- Expected
59+
+++ Actual
60+
#{config_diff.split("\n")[5..].join("\n")}
61+
MESSAGE
12262

123-
def visitor
124-
@visitor ||=
125-
begin
126-
visitor = Visitor::FrameworkDefault.new
127-
visitor.visit(app_config_tree)
128-
visitor
129-
end
130-
end
63+
checker.doc.versioned_defaults = expected_text
64+
end
13165
end
13266
end
13367
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module RailInspector
4+
class Configuring
5+
module Check
6+
class NewFrameworkDefaultsFile
7+
attr_reader :checker, :visitor
8+
9+
# Defaults are strings like:
10+
# self.yjit
11+
# action_controller.escape_json_responses
12+
def initialize(checker, defaults, file_content)
13+
@checker = checker
14+
@defaults = defaults
15+
@file_content = file_content
16+
end
17+
18+
def check
19+
@defaults.each do |config|
20+
if config.start_with? "self"
21+
next if @file_content.include? config.gsub(/^self/, "config")
22+
next if @file_content.include? config.gsub(/^self/, "configuration")
23+
end
24+
25+
next if @file_content.include? config
26+
27+
next if config == "self.yjit"
28+
29+
checker.errors << <<~MESSAGE
30+
#{checker.files.new_framework_defaults}: Missing new default
31+
#{config}
32+
33+
MESSAGE
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module RailInspector
4+
class Configuring
5+
class Document
6+
class << self
7+
def parse(text)
8+
before, *versioned_defaults, general_config, after =
9+
text
10+
.split("\n")
11+
.slice_before do |line|
12+
[
13+
"#### Default Values for Target Version",
14+
"### Rails General Configuration",
15+
"### Configuring Assets"
16+
].any? { |s| line.start_with?(s) }
17+
end
18+
.to_a
19+
20+
new(before, versioned_defaults.flatten.join("\n"), general_config, after)
21+
end
22+
end
23+
24+
attr_accessor :general_config, :versioned_defaults
25+
26+
def initialize(before, versioned_defaults, general_config, after)
27+
@before, @versioned_defaults, @general_config, @after =
28+
before, versioned_defaults, general_config, after
29+
end
30+
31+
def to_s
32+
(@before + [@versioned_defaults] + @general_config + @after).join("\n") +
33+
"\n"
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)