rubomatic-html 1.1.0.pre.rc.4 → 1.1.0.pre.rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.adoc +10 -1
  3. data/config/default.yml +28 -0
  4. data/docs/cops/style/README.adoc +9 -0
  5. data/docs/cops/style/no_fields_for/README.adoc +28 -0
  6. data/docs/cops/style/no_form_for/README.adoc +28 -0
  7. data/docs/cops/style/no_form_tag/README.adoc +28 -0
  8. data/docs/cops/style/no_on_before_unload/README.adoc +29 -0
  9. data/docs/cops/style/no_on_click/README.adoc +29 -0
  10. data/docs/cops/style/no_on_drag/README.adoc +29 -0
  11. data/docs/cops/style/no_on_load/README.adoc +29 -0
  12. data/docs/cops/style/no_on_unload/README.adoc +29 -0
  13. data/docs/cops/style/no_on_wheel/README.adoc +29 -0
  14. data/exe/rubomatic-html +1 -1
  15. data/lib/rubomatic-html/cop/base.rb +58 -0
  16. data/lib/rubomatic-html/cop/cops.rb +19 -0
  17. data/lib/rubomatic-html/cop/layout/base.rb +17 -0
  18. data/lib/rubomatic-html/cop/layout/line_length.rb +28 -0
  19. data/lib/rubomatic-html/cop/layout/multiple_line_breaks.rb +42 -0
  20. data/lib/rubomatic-html/cop/layout/trailing_whitespace.rb +28 -0
  21. data/lib/rubomatic-html/cop/style/base.rb +17 -0
  22. data/lib/rubomatic-html/cop/style/no_fields_for.rb +28 -0
  23. data/lib/rubomatic-html/cop/style/no_form_for.rb +28 -0
  24. data/lib/rubomatic-html/cop/style/no_form_tag.rb +28 -0
  25. data/lib/rubomatic-html/cop/style/no_on_attribute.rb +31 -0
  26. data/lib/rubomatic-html/cop/style/no_on_before_unload.rb +27 -0
  27. data/lib/rubomatic-html/cop/style/no_on_click.rb +27 -0
  28. data/lib/rubomatic-html/cop/style/no_on_drag.rb +27 -0
  29. data/lib/rubomatic-html/cop/style/no_on_load.rb +27 -0
  30. data/lib/rubomatic-html/cop/style/no_on_unload.rb +27 -0
  31. data/lib/rubomatic-html/cop/style/no_on_wheel.rb +27 -0
  32. data/lib/rubomatic-html/cop/style/partial_instance_variable.rb +44 -0
  33. data/lib/rubomatic-html/generator/cop_readme_injector.rb +48 -0
  34. data/lib/rubomatic-html/generator/dept_readme_injector.rb +111 -0
  35. data/lib/rubomatic-html/generator.rb +330 -0
  36. data/lib/rubomatic-html/inject.rb +19 -0
  37. data/lib/rubomatic-html/runner.rb +129 -0
  38. data/lib/rubomatic-html/version.rb +5 -0
  39. data/lib/rubomatic-html.rb +11 -1
  40. metadata +47 -18
  41. data/lib/rubomatic/html/cop/base.rb +0 -42
  42. data/lib/rubomatic/html/cop/cops.rb +0 -9
  43. data/lib/rubomatic/html/cop/layout/base.rb +0 -19
  44. data/lib/rubomatic/html/cop/layout/line_length.rb +0 -26
  45. data/lib/rubomatic/html/cop/layout/multiple_line_breaks.rb +0 -40
  46. data/lib/rubomatic/html/cop/layout/trailing_whitespace.rb +0 -26
  47. data/lib/rubomatic/html/cop/style/base.rb +0 -19
  48. data/lib/rubomatic/html/cop/style/partial_instance_variable.rb +0 -28
  49. data/lib/rubomatic/html/version.rb +0 -7
  50. data/lib/rubomatic/html.rb +0 -115
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubomaticHtml
4
+ module Inject
5
+ # This was a generated method from https://github.com/rubocop/rubocop-extension-generator
6
+ #
7
+ def self.defaults!
8
+ path = CONFIG_DEFAULT.to_s
9
+ hash = ConfigLoader.__send__(:load_yaml_configuration, path)
10
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
11
+
12
+ puts("configuration from #{path}") if ConfigLoader.debug?
13
+
14
+ config = ConfigLoader.merge_with_default(config, path)
15
+
16
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+ require 'yaml'
5
+
6
+ module RubomaticHtml
7
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
8
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
9
+ CONFIG = ::YAML.safe_load(CONFIG_DEFAULT.read).freeze
10
+
11
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
12
+
13
+ class Config < RuboCop::Config
14
+ end
15
+
16
+ class ConfigLoader < RuboCop::ConfigLoader
17
+ end
18
+
19
+ class Error < StandardError
20
+ end
21
+
22
+ class Runner
23
+ # @return [Array<String>]
24
+ attr_accessor :files_to_lint
25
+ # @return [Hash]
26
+ attr_accessor :config
27
+
28
+ # @param linted_files [Array<String>]
29
+ #
30
+ def initialize(linted_files)
31
+ files_to_lint = Array(linted_files)
32
+
33
+ if files_to_lint.empty?
34
+ files_to_lint = Dir[File.join('app', 'views', '**', '*')]
35
+ end
36
+
37
+ @files_to_lint = files_to_lint
38
+
39
+ custom_config = ::YAML.safe_load(Pathname.new('.rubomatic-html.yml').read).freeze
40
+ config = {}
41
+ base_transformations = { 'Enabled' => :enabled, 'Exclude' => :exclude }
42
+
43
+ CONFIG.each do |cop_name, cop_config|
44
+ the_cop = all_cops.find { |cop| cop.name == cop_name }
45
+ transformations = base_transformations.merge(the_cop&.allowed_config_transform || {})
46
+ config[cop_name] = {}
47
+
48
+ transformations.each do |allowed_node, ruby_node|
49
+ config[cop_name][ruby_node] = cop_config[allowed_node] if cop_config.has_key?(allowed_node)
50
+
51
+ next unless custom_config.fetch(cop_name, {}).has_key?(allowed_node)
52
+
53
+ config[cop_name][ruby_node] = custom_config.dig(cop_name, allowed_node)
54
+ end
55
+
56
+ config[cop_name][:exclude] = Array(config[cop_name][:exclude])
57
+ end
58
+
59
+ @config = config
60
+ end
61
+
62
+ # Runs all cops against all files
63
+ #
64
+ # @return [void]
65
+ #
66
+ def run
67
+ all_config = config.fetch('AllCops')
68
+
69
+ files_to_lint.each do |file|
70
+ next if all_config.fetch(:exclude).any? { |ignored| file.end_with?(ignored) }
71
+
72
+ ext = File.extname(file)
73
+
74
+ next if ext.match?(/haml/i)
75
+
76
+ check_it = ext.match?(/html/i)
77
+ check_it ||= ext.match?(/erb\z/i)
78
+
79
+ next unless check_it
80
+
81
+ run_file(file)
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ # List of all cops available
88
+ #
89
+ # @return [Array<*>]
90
+ #
91
+ def all_cops
92
+ @all_cops ||= RubomaticHtml::Cop.constants.flat_map do |mod_name|
93
+ RubomaticHtml::Cop.const_get(mod_name).constants.filter_map do |klass_name|
94
+ klass = RubomaticHtml::Cop.const_get(mod_name).const_get(klass_name)
95
+
96
+ next if klass.abstract_cop?
97
+
98
+ klass
99
+ end
100
+ end
101
+ end
102
+
103
+ # Runs all cops against a given file
104
+ #
105
+ # @param file [String]
106
+ #
107
+ # @return [void]
108
+ #
109
+ def run_file(file)
110
+ cops = all_cops.filter_map do |cop|
111
+ cop_config = config.fetch(cop.name, {})
112
+
113
+ next unless cop_config.dig(:enabled)
114
+
115
+ cop.new(file, cop_config)
116
+ end
117
+
118
+ return if cops.empty?
119
+
120
+ File.open(file).each_line(chomp: true).with_index(1) do |line, index|
121
+ cops.each do |cop|
122
+ next if config.dig(cop.class.name).fetch(:exclude, []).any? { |ignored| file.end_with?(ignored) }
123
+
124
+ cop.run_for_line(line, index)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubomaticHtml
4
+ VERSION = '1.1.0-rc.5'
5
+ end
@@ -1,3 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'rubomatic/html'
3
+ require 'pathname'
4
+
5
+ require_relative 'rubomatic-html/generator'
6
+ require_relative 'rubomatic-html/runner'
7
+ require_relative 'rubomatic-html/version'
8
+
9
+ require_relative 'rubomatic-html/inject'
10
+
11
+ RubomaticHtml::Inject.defaults!
12
+
13
+ require_relative 'rubomatic-html/cop/cops'
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubomatic-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.pre.rc.4
4
+ version: 1.1.0.pre.rc.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brands Insurance
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-05 00:00:00.000000000 Z
11
+ date: 2023-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rubomatic
14
+ name: rubocop-rubomatic
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.2.0
20
- type: :development
19
+ version: 1.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: 2.2.0
32
+ version: '2.0'
27
33
  description:
28
34
  email:
29
35
  - documents@brandsinsurance.com
@@ -41,19 +47,42 @@ files:
41
47
  - docs/cops/layout/multiple_line_breaks/README.adoc
42
48
  - docs/cops/layout/trailing_whitespace/README.adoc
43
49
  - docs/cops/style/README.adoc
50
+ - docs/cops/style/no_fields_for/README.adoc
51
+ - docs/cops/style/no_form_for/README.adoc
52
+ - docs/cops/style/no_form_tag/README.adoc
53
+ - docs/cops/style/no_on_before_unload/README.adoc
54
+ - docs/cops/style/no_on_click/README.adoc
55
+ - docs/cops/style/no_on_drag/README.adoc
56
+ - docs/cops/style/no_on_load/README.adoc
57
+ - docs/cops/style/no_on_unload/README.adoc
58
+ - docs/cops/style/no_on_wheel/README.adoc
44
59
  - docs/cops/style/partial_instance_variable/README.adoc
45
60
  - exe/rubomatic-html
46
61
  - lib/rubomatic-html.rb
47
- - lib/rubomatic/html.rb
48
- - lib/rubomatic/html/cop/base.rb
49
- - lib/rubomatic/html/cop/cops.rb
50
- - lib/rubomatic/html/cop/layout/base.rb
51
- - lib/rubomatic/html/cop/layout/line_length.rb
52
- - lib/rubomatic/html/cop/layout/multiple_line_breaks.rb
53
- - lib/rubomatic/html/cop/layout/trailing_whitespace.rb
54
- - lib/rubomatic/html/cop/style/base.rb
55
- - lib/rubomatic/html/cop/style/partial_instance_variable.rb
56
- - lib/rubomatic/html/version.rb
62
+ - lib/rubomatic-html/cop/base.rb
63
+ - lib/rubomatic-html/cop/cops.rb
64
+ - lib/rubomatic-html/cop/layout/base.rb
65
+ - lib/rubomatic-html/cop/layout/line_length.rb
66
+ - lib/rubomatic-html/cop/layout/multiple_line_breaks.rb
67
+ - lib/rubomatic-html/cop/layout/trailing_whitespace.rb
68
+ - lib/rubomatic-html/cop/style/base.rb
69
+ - lib/rubomatic-html/cop/style/no_fields_for.rb
70
+ - lib/rubomatic-html/cop/style/no_form_for.rb
71
+ - lib/rubomatic-html/cop/style/no_form_tag.rb
72
+ - lib/rubomatic-html/cop/style/no_on_attribute.rb
73
+ - lib/rubomatic-html/cop/style/no_on_before_unload.rb
74
+ - lib/rubomatic-html/cop/style/no_on_click.rb
75
+ - lib/rubomatic-html/cop/style/no_on_drag.rb
76
+ - lib/rubomatic-html/cop/style/no_on_load.rb
77
+ - lib/rubomatic-html/cop/style/no_on_unload.rb
78
+ - lib/rubomatic-html/cop/style/no_on_wheel.rb
79
+ - lib/rubomatic-html/cop/style/partial_instance_variable.rb
80
+ - lib/rubomatic-html/generator.rb
81
+ - lib/rubomatic-html/generator/cop_readme_injector.rb
82
+ - lib/rubomatic-html/generator/dept_readme_injector.rb
83
+ - lib/rubomatic-html/inject.rb
84
+ - lib/rubomatic-html/runner.rb
85
+ - lib/rubomatic-html/version.rb
57
86
  homepage: https://github.com/BrandsInsurance/expert-chainsaw/
58
87
  licenses:
59
88
  - MIT
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- class Base
7
- # @return [String]
8
- attr_accessor :file
9
-
10
- # Name for cop
11
- #
12
- # @return [String]
13
- #
14
- def self.name
15
- 'Base'
16
- end
17
-
18
- # :nodoc:
19
- def initialize(file)
20
- @file = file
21
- end
22
-
23
- private
24
-
25
- # Outputs filename:line_number locations of HTML files that trigger the cop
26
- #
27
- # @param _line [String] the line in the html
28
- # @param _index [Integer] the 1-index of the line
29
- #
30
- # @return [void]
31
- #
32
- def run_for_line(_line, _index)
33
- error_message = <<~TEXT
34
- Warning: Method `run_for_line` needs overridden! Some cops may not display failing messages.
35
- TEXT
36
-
37
- puts("\e[33m#{error_message}\e[0m")
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
- require_relative 'layout/base'
5
- require_relative 'layout/line_length'
6
- require_relative 'layout/multiple_line_breaks'
7
- require_relative 'layout/trailing_whitespace'
8
- require_relative 'style/base'
9
- require_relative 'style/partial_instance_variable'
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Layout
7
- class Base < Rubomatic::Html::Cop::Base
8
- # Department for cop
9
- #
10
- # @return [String]
11
- #
12
- def self.department
13
- 'Layout'
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Layout
7
- class LineLength < Rubomatic::Html::Cop::Layout::Base
8
- # Name for the cop
9
- #
10
- # @return [String]
11
- #
12
- def self.name
13
- [department, 'LineLength'].join('/')
14
- end
15
-
16
- # @see super
17
- def run_for_line(line, index)
18
- return if line.size <= 120
19
-
20
- puts("#{file}:#{index}: is over 120 characters")
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Layout
7
- class MultipleLineBreaks < Rubomatic::Html::Cop::Layout::Base
8
- # @return [Boolean] tracks multiple consecutive line breaks
9
- attr_accessor :prev_break
10
-
11
- # Name for the cop
12
- #
13
- # @return [String]
14
- #
15
- def self.name
16
- [department, 'MultipleLineBreaks'].join('/')
17
- end
18
-
19
- # :nodoc:
20
- def initialize(file)
21
- super
22
-
23
- @prev_break = false
24
- end
25
-
26
- # @see super
27
- def run_for_line(line, index)
28
- if prev_break && line.empty?
29
- puts("#{file}:#{index}: has multiple line breaks")
30
- elsif line.empty?
31
- @prev_break = true
32
- else
33
- @prev_break = false
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Layout
7
- class TrailingWhitespace < Rubomatic::Html::Cop::Layout::Base
8
- # Name for the cop
9
- #
10
- # @return [String]
11
- #
12
- def self.name
13
- [department, 'TrailingWhitespace'].join('/')
14
- end
15
-
16
- # @see super
17
- def run_for_line(line, index)
18
- return unless line.match?(/\s\z/i)
19
-
20
- puts("#{file}:#{index}: has trailing whitespace")
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Style
7
- class Base < Rubomatic::Html::Cop::Base
8
- # Department for cop
9
- #
10
- # @return [String]
11
- #
12
- def self.department
13
- 'Style'
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- module Cop
6
- module Style
7
- class PartialInstanceVariable < Rubomatic::Html::Cop::Style::Base
8
- # Name for the cop
9
- #
10
- # @return [String]
11
- #
12
- def self.name
13
- [department, 'PartialInstanceVariable'].join('/')
14
- end
15
-
16
- # @see super
17
- def run_for_line(line, index)
18
- return unless File.basename(file).match?(/^_/i)
19
-
20
- return unless line.match?(/@/i)
21
-
22
- puts("#{file}:#{index}: might use an instance variable")
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rubomatic
4
- module Html
5
- VERSION = '1.1.0-rc.4'
6
- end
7
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
- require_relative 'html/cop/cops'
5
- require_relative 'html/version'
6
- require 'yaml'
7
-
8
- module Rubomatic
9
- module Html
10
- class Runner
11
- PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
12
- CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
13
- CONFIG = ::YAML.safe_load(CONFIG_DEFAULT.read).freeze
14
-
15
- # @return [Array<String>]
16
- attr_accessor :files_to_lint
17
- # @return [Hash]
18
- attr_accessor :config
19
-
20
- # @param linted_files [Array<String>]
21
- #
22
- def initialize(linted_files)
23
- files_to_lint = Array(linted_files)
24
-
25
- if files_to_lint.empty?
26
- files_to_lint = Dir[File.join('app', 'views', '**', '*')]
27
- end
28
-
29
- @files_to_lint = files_to_lint
30
-
31
- custom_config = ::YAML.safe_load(Pathname.new('.rubomatic-html.yml').read).freeze
32
-
33
- config = {}
34
-
35
- CONFIG.each do |cop, cop_config|
36
- config[cop] = {}
37
- config[cop][:enabled] = cop_config['Enabled']
38
- config[cop][:exclude] = Array(cop_config['Exclude'])
39
-
40
- if custom_config.fetch(cop, {}).has_key?('Enabled')
41
- config[cop][:enabled] = custom_config[cop]['Enabled']
42
- end
43
-
44
- if custom_config.fetch(cop, {}).has_key?('Exclude')
45
- config[cop][:exclude] = Array(custom_config[cop]['Exclude'])
46
- end
47
- end
48
-
49
- @config = config
50
- end
51
-
52
- # Runs all cops against all files
53
- #
54
- # @return [void]
55
- #
56
- def run
57
- all_config = config.fetch('AllCops')
58
-
59
- files_to_lint.each do |file|
60
- next if all_config.fetch(:exclude).any? { |ignored| file.end_with?(ignored) }
61
-
62
- ext = File.extname(file)
63
-
64
- next if ext.match?(/haml/i)
65
-
66
- check_it = ext.match?(/html/i)
67
- check_it ||= ext.match?(/erb\z/i)
68
-
69
- next unless check_it
70
-
71
- run_file(file)
72
- end
73
- end
74
-
75
- private
76
-
77
- # List of all cops available
78
- #
79
- # @return [Array<*>]
80
- #
81
- def all_cops
82
- @all_cops ||= [
83
- Rubomatic::Html::Cop::Layout::LineLength,
84
- Rubomatic::Html::Cop::Layout::MultipleLineBreaks,
85
- Rubomatic::Html::Cop::Layout::TrailingWhitespace,
86
- Rubomatic::Html::Cop::Style::PartialInstanceVariable
87
- ]
88
- end
89
-
90
- # Runs all cops against a given file
91
- #
92
- # @param file [String]
93
- #
94
- # @return [void]
95
- #
96
- def run_file(file)
97
- cops = all_cops.filter_map do |cop|
98
- next unless config.dig(cop.name, :enabled)
99
-
100
- cop.new(file)
101
- end
102
-
103
- return if cops.empty?
104
-
105
- File.open(file).each_line(chomp: true).with_index(1) do |line, index|
106
- cops.each do |cop|
107
- next if config.dig(cop.class.name).fetch(:exclude, []).any? { |ignored| file.end_with?(ignored) }
108
-
109
- cop.run_for_line(line, index)
110
- end
111
- end
112
- end
113
- end
114
- end
115
- end