erb_lint 0.0.30 → 0.0.35

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b136922bb523d860a4167f2ee6a896c332bf083bbf88fcb30ea983cd28ad1ed5
4
- data.tar.gz: 145bd0a1217c31f9e4ccd7221548fef987910c4f026c221fb89fa65b8834d7d7
3
+ metadata.gz: 1bec7519ea346d89eb70e485c59015ea8faae7f4c85f4e3b450a4f8853eaa9b5
4
+ data.tar.gz: dba705708221bc3b5a71c57d6d9cbf1db2f7c8afbfaa7ed45fc63e71433383b5
5
5
  SHA512:
6
- metadata.gz: f3bba6099a1aaf0402d046d0f6acc0a7e17af7d0ca1a05883ae9b6016cf79d141f54976974e5a7035b60cb257f123434c49447c870fdda0a699d1ae62de158cb
7
- data.tar.gz: fdab81ad466f3407ca177f60fe727ed7c760c82448d5ba009722fc04175c89f28dc9b43875a90c4c75df600eef0607c7aa7fbdca1da85d1f262cf970cec9cab0
6
+ metadata.gz: 770cd1f544b749a7210949b1221ec4401349cb30bf9a171c1b7677c3d051f9887e264d62e1c8b86c5b09c593bc96b24b867a35b2bf11f4dfe2d8a17f54332d5c
7
+ data.tar.gz: ae1f2c592fba2fce5a70eb0620024c49cdca3173388791036bb9e2ed56b3b0cbdf3e6361c0b68c94fc222640aba5422c6fd5c15d8691fa019d0a9cfc038e96a3
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rubocop'
4
+
3
5
  require 'erb_lint/corrector'
4
6
  require 'erb_lint/file_loader'
5
7
  require 'erb_lint/linter_config'
@@ -33,15 +33,16 @@ module ERBLint
33
33
  end
34
34
 
35
35
  def run(args = ARGV)
36
- load_options(args)
37
- @files = args.dup
36
+ dupped_args = args.dup
37
+ load_options(dupped_args)
38
+ @files = dupped_args
38
39
 
39
40
  load_config
40
41
 
41
42
  if !@files.empty? && lint_files.empty?
42
- success!("no files found...\n")
43
+ failure!("no files found...\n")
43
44
  elsif lint_files.empty?
44
- success!("no files given...\n#{option_parser}")
45
+ failure!("no files found or given, specify files or config...\n#{option_parser}")
45
46
  end
46
47
 
47
48
  ensure_files_exist(lint_files)
@@ -54,9 +55,12 @@ module ERBLint
54
55
  "#{enabled_linter_classes.size} #{'autocorrectable ' if autocorrect?}linters..."
55
56
  puts
56
57
 
58
+ runner = ERBLint::Runner.new(file_loader, @config)
59
+
57
60
  lint_files.each do |filename|
61
+ runner.clear_offenses
58
62
  begin
59
- run_with_corrections(filename)
63
+ run_with_corrections(runner, filename)
60
64
  rescue => e
61
65
  @stats.exceptions += 1
62
66
  puts "Exception occured when processing: #{relative_filename(filename)}"
@@ -101,11 +105,9 @@ module ERBLint
101
105
  @options[:autocorrect]
102
106
  end
103
107
 
104
- def run_with_corrections(filename)
108
+ def run_with_corrections(runner, filename)
105
109
  file_content = File.read(filename, encoding: Encoding::UTF_8)
106
110
 
107
- runner = ERBLint::Runner.new(file_loader, @config)
108
-
109
111
  7.times do
110
112
  processed_source = ERBLint::ProcessedSource.new(filename, file_content)
111
113
  runner.run(processed_source)
@@ -148,14 +150,15 @@ module ERBLint
148
150
  def load_config
149
151
  if File.exist?(config_filename)
150
152
  config = RunnerConfig.new(file_loader.yaml(config_filename), file_loader)
151
- @config = RunnerConfig.default.merge(config)
153
+ @config = RunnerConfig.default_for(config)
152
154
  else
153
155
  warn(Rainbow("#{config_filename} not found: using default config").yellow)
154
156
  @config = RunnerConfig.default
155
157
  end
156
- @config.merge!(runner_config_override)
157
158
  rescue Psych::SyntaxError => e
158
159
  failure!("error parsing config: #{e.message}")
160
+ ensure
161
+ @config.merge!(runner_config_override)
159
162
  end
160
163
 
161
164
  def file_loader
@@ -167,17 +170,22 @@ module ERBLint
167
170
  end
168
171
 
169
172
  def lint_files
170
- if @options[:lint_all]
171
- pattern = File.expand_path(DEFAULT_LINT_ALL_GLOB, Dir.pwd)
172
- Dir[pattern].select { |filename| !excluded?(filename) }
173
- else
174
- @files
175
- .map { |f| Dir.exist?(f) ? Dir[File.join(f, DEFAULT_LINT_ALL_GLOB)] : f }
176
- .map { |f| f.include?('*') ? Dir[f] : f }
177
- .flatten
178
- .map { |f| File.expand_path(f, Dir.pwd) }
179
- .select { |filename| !excluded?(filename) }
180
- end
173
+ @lint_files ||=
174
+ if @options[:lint_all]
175
+ pattern = File.expand_path(glob, Dir.pwd)
176
+ Dir[pattern].select { |filename| !excluded?(filename) }
177
+ else
178
+ @files
179
+ .map { |f| Dir.exist?(f) ? Dir[File.join(f, glob)] : f }
180
+ .map { |f| f.include?('*') ? Dir[f] : f }
181
+ .flatten
182
+ .map { |f| File.expand_path(f, Dir.pwd) }
183
+ .select { |filename| !excluded?(filename) }
184
+ end
185
+ end
186
+
187
+ def glob
188
+ @config.to_hash["glob"] || DEFAULT_LINT_ALL_GLOB
181
189
  end
182
190
 
183
191
  def excluded?(filename)
@@ -250,7 +258,7 @@ module ERBLint
250
258
  end
251
259
  end
252
260
 
253
- opts.on("--lint-all", "Lint all files matching #{DEFAULT_LINT_ALL_GLOB}") do |config|
261
+ opts.on("--lint-all", "Lint all files matching configured glob [default: #{DEFAULT_LINT_ALL_GLOB}]") do |config|
254
262
  @options[:lint_all] = config
255
263
  end
256
264
 
@@ -268,7 +276,7 @@ module ERBLint
268
276
  @options[:enabled_linters] = linters
269
277
  end
270
278
 
271
- opts.on("--autocorrect", "Correct offenses that can be corrected automatically (default: false)") do |config|
279
+ opts.on("-a", "--autocorrect", "Correct offenses automatically if possible (default: false)") do |config|
272
280
  @options[:autocorrect] = config
273
281
  end
274
282
 
@@ -17,11 +17,22 @@ module ERBLint
17
17
  end
18
18
 
19
19
  def corrector
20
- RuboCop::Cop::Corrector.new(@processed_source.source_buffer, corrections)
20
+ BASE.new(@processed_source.source_buffer, corrections)
21
21
  end
22
22
 
23
- def diagnostics
24
- corrector.diagnostics
23
+ if ::RuboCop::Version::STRING.to_f >= 0.87
24
+ require 'rubocop/cop/legacy/corrector'
25
+ BASE = ::RuboCop::Cop::Legacy::Corrector
26
+
27
+ def diagnostics
28
+ []
29
+ end
30
+ else
31
+ BASE = ::RuboCop::Cop::Corrector
32
+
33
+ def diagnostics
34
+ corrector.diagnostics
35
+ end
25
36
  end
26
37
  end
27
38
  end
@@ -9,8 +9,14 @@ module ERBLint
9
9
  @base_path = base_path
10
10
  end
11
11
 
12
- def yaml(filename)
13
- YAML.safe_load(read_content(filename), [Regexp, Symbol], [], false, filename) || {}
12
+ if RUBY_VERSION >= "2.6"
13
+ def yaml(filename)
14
+ YAML.safe_load(read_content(filename), permitted_classes: [Regexp, Symbol], filename: filename) || {}
15
+ end
16
+ else
17
+ def yaml(filename)
18
+ YAML.safe_load(read_content(filename), [Regexp, Symbol], [], false, filename) || {}
19
+ end
14
20
  end
15
21
 
16
22
  private
@@ -63,7 +63,7 @@ module ERBLint
63
63
  string = offense.source_range.source
64
64
  return unless (klass = load_corrector)
65
65
  return unless string.strip.length > 1
66
- node = RuboCop::AST::StrNode.new(:str, [string])
66
+ node = ::RuboCop::AST::StrNode.new(:str, [string])
67
67
  corrector = klass.new(node, processed_source.filename, corrector_i18n_load_path, offense.source_range)
68
68
  corrector.autocorrect(tag_start: '<%= ', tag_end: ' %>')
69
69
  rescue MissingCorrector, MissingI18nLoadPath
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'better_html'
3
4
  require 'better_html/ast/node'
4
5
  require 'better_html/test_helper/ruby_node'
5
6
  require 'erb_lint/utils/block_map'
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'better_html'
4
- require 'rubocop'
5
4
  require 'tempfile'
6
5
  require 'erb_lint/utils/offset_corrector'
7
6
 
@@ -26,7 +25,7 @@ module ERBLint
26
25
  super
27
26
  @only_cops = @config.only
28
27
  custom_config = config_from_hash(@config.rubocop_config)
29
- @rubocop_config = RuboCop::ConfigLoader.merge_with_default(custom_config, '')
28
+ @rubocop_config = ::RuboCop::ConfigLoader.merge_with_default(custom_config, '')
30
29
  end
31
30
 
32
31
  def run(processed_source)
@@ -35,17 +34,29 @@ module ERBLint
35
34
  end
36
35
  end
37
36
 
38
- def autocorrect(processed_source, offense)
39
- return unless offense.context
40
-
41
- lambda do |corrector|
42
- passthrough = Utils::OffsetCorrector.new(
43
- processed_source,
44
- corrector,
45
- offense.context[:offset],
46
- offense.context[:bound_range],
47
- )
48
- offense.context[:rubocop_correction].call(passthrough)
37
+ if ::RuboCop::Version::STRING.to_f >= 0.87
38
+ def autocorrect(_processed_source, offense)
39
+ return unless offense.context
40
+ rubocop_correction = offense.context[:rubocop_correction]
41
+ return unless rubocop_correction
42
+
43
+ lambda do |corrector|
44
+ corrector.import!(rubocop_correction, offset: offense.context[:offset])
45
+ end
46
+ end
47
+ else
48
+ def autocorrect(processed_source, offense)
49
+ return unless offense.context
50
+
51
+ lambda do |corrector|
52
+ passthrough = Utils::OffsetCorrector.new(
53
+ processed_source,
54
+ corrector,
55
+ offense.context[:offset],
56
+ offense.context[:bound_range],
57
+ )
58
+ offense.context[:rubocop_correction].call(passthrough)
59
+ end
49
60
  end
50
61
  end
51
62
 
@@ -62,22 +73,23 @@ module ERBLint
62
73
  original_source = code_node.loc.source
63
74
  trimmed_source = original_source.sub(BLOCK_EXPR, '').sub(SUFFIX_EXPR, '')
64
75
  alignment_column = code_node.loc.column
76
+ offset = code_node.loc.begin_pos - alignment_column
65
77
  aligned_source = "#{' ' * alignment_column}#{trimmed_source}"
66
78
 
67
79
  source = rubocop_processed_source(aligned_source, processed_source.filename)
68
80
  return unless source.valid_syntax?
69
81
 
70
- team = build_team
71
- team.inspect_file(source)
72
- team.cops.each do |cop|
73
- correction_offset = 0
74
- cop.offenses.reject(&:disabled?).each do |rubocop_offense|
75
- if rubocop_offense.corrected?
76
- correction = cop.corrections[correction_offset]
77
- correction_offset += 1
78
- end
82
+ activate_team(processed_source, source, offset, code_node, build_team)
83
+ end
84
+
85
+ if ::RuboCop::Version::STRING.to_f >= 0.87
86
+ def activate_team(processed_source, source, offset, code_node, team)
87
+ report = team.investigate(source)
88
+ report.offenses.each do |rubocop_offense|
89
+ next if rubocop_offense.disabled?
90
+
91
+ correction = rubocop_offense.corrector if rubocop_offense.corrected?
79
92
 
80
- offset = code_node.loc.begin_pos - alignment_column
81
93
  offense_range = processed_source
82
94
  .to_source_range(rubocop_offense.location)
83
95
  .offset(offset)
@@ -85,6 +97,25 @@ module ERBLint
85
97
  add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range)
86
98
  end
87
99
  end
100
+ else
101
+ def activate_team(processed_source, source, offset, code_node, team)
102
+ team.inspect_file(source)
103
+ team.cops.each do |cop|
104
+ correction_offset = 0
105
+ cop.offenses.reject(&:disabled?).each do |rubocop_offense|
106
+ if rubocop_offense.corrected?
107
+ correction = cop.corrections[correction_offset]
108
+ correction_offset += 1
109
+ end
110
+
111
+ offense_range = processed_source
112
+ .to_source_range(rubocop_offense.location)
113
+ .offset(offset)
114
+
115
+ add_offense(rubocop_offense, offense_range, correction, offset, code_node.loc.range)
116
+ end
117
+ end
118
+ end
88
119
  end
89
120
 
90
121
  def tempfile_from(filename, content)
@@ -97,7 +128,7 @@ module ERBLint
97
128
  end
98
129
 
99
130
  def rubocop_processed_source(content, filename)
100
- RuboCop::ProcessedSource.new(
131
+ ::RuboCop::ProcessedSource.new(
101
132
  content,
102
133
  @rubocop_config.target_ruby_version,
103
134
  filename
@@ -106,15 +137,15 @@ module ERBLint
106
137
 
107
138
  def cop_classes
108
139
  if @only_cops.present?
109
- selected_cops = RuboCop::Cop::Cop.all.select { |cop| cop.match?(@only_cops) }
110
- RuboCop::Cop::Registry.new(selected_cops)
140
+ selected_cops = ::RuboCop::Cop::Cop.all.select { |cop| cop.match?(@only_cops) }
141
+ ::RuboCop::Cop::Registry.new(selected_cops)
111
142
  else
112
- RuboCop::Cop::Registry.new(RuboCop::Cop::Cop.all)
143
+ ::RuboCop::Cop::Registry.new(::RuboCop::Cop::Cop.all)
113
144
  end
114
145
  end
115
146
 
116
147
  def build_team
117
- RuboCop::Cop::Team.new(
148
+ ::RuboCop::Cop::Team.new(
118
149
  cop_classes,
119
150
  @rubocop_config,
120
151
  extra_details: true,
@@ -129,7 +160,7 @@ module ERBLint
129
160
  resolve_inheritance(hash, inherit_from)
130
161
 
131
162
  tempfile_from('.erblint-rubocop', hash.to_yaml) do |tempfile|
132
- RuboCop::ConfigLoader.load_file(tempfile.path)
163
+ ::RuboCop::ConfigLoader.load_file(tempfile.path)
133
164
  end
134
165
  end
135
166
 
@@ -137,7 +168,7 @@ module ERBLint
137
168
  base_configs(inherit_from)
138
169
  .reverse_each do |base_config|
139
170
  base_config.each do |k, v|
140
- hash[k] = hash.key?(k) ? RuboCop::ConfigLoader.merge(v, hash[k]) : v if v.is_a?(Hash)
171
+ hash[k] = hash.key?(k) ? ::RuboCop::ConfigLoader.merge(v, hash[k]) : v if v.is_a?(Hash)
141
172
  end
142
173
  end
143
174
  end
@@ -146,7 +177,7 @@ module ERBLint
146
177
  regex = URI::DEFAULT_PARSER.make_regexp(%w(http https))
147
178
  configs = Array(inherit_from).compact.map do |base_name|
148
179
  if base_name =~ /\A#{regex}\z/
149
- RuboCop::ConfigLoader.load_file(RuboCop::RemoteConfig.new(base_name, Dir.pwd))
180
+ ::RuboCop::ConfigLoader.load_file(::RuboCop::RemoteConfig.new(base_name, Dir.pwd))
150
181
  else
151
182
  config_from_hash(@file_loader.yaml(base_name))
152
183
  end
@@ -28,9 +28,9 @@ module ERBLint
28
28
  end
29
29
 
30
30
  def cop_classes
31
- selected_cops = RuboCop::Cop::Cop.all.select { |cop| cop.match?(@only_cops) }
31
+ selected_cops = ::RuboCop::Cop::Cop.all.select { |cop| cop.match?(@only_cops) }
32
32
 
33
- RuboCop::Cop::Registry.new(selected_cops)
33
+ ::RuboCop::Cop::Registry.new(selected_cops)
34
34
  end
35
35
  end
36
36
  end
@@ -45,24 +45,30 @@ module ERBLint
45
45
  end
46
46
 
47
47
  class << self
48
- def default
48
+ def default(default_enabled: nil)
49
+ default_enabled = default_enabled.nil? ? true : default_enabled
49
50
  new(
50
51
  linters: {
51
- AllowedScriptType: { enabled: true },
52
- ClosingErbTagIndent: { enabled: true },
53
- ExtraNewline: { enabled: true },
54
- FinalNewline: { enabled: true },
55
- NoJavascriptTagHelper: { enabled: true },
56
- ParserErrors: { enabled: true },
57
- RightTrim: { enabled: true },
58
- SelfClosingTag: { enabled: true },
59
- SpaceAroundErbTag: { enabled: true },
60
- SpaceIndentation: { enabled: true },
61
- SpaceInHtmlTag: { enabled: true },
62
- TrailingWhitespace: { enabled: true },
52
+ AllowedScriptType: { enabled: default_enabled },
53
+ ClosingErbTagIndent: { enabled: default_enabled },
54
+ ExtraNewline: { enabled: default_enabled },
55
+ FinalNewline: { enabled: default_enabled },
56
+ NoJavascriptTagHelper: { enabled: default_enabled },
57
+ ParserErrors: { enabled: default_enabled },
58
+ RightTrim: { enabled: default_enabled },
59
+ SelfClosingTag: { enabled: default_enabled },
60
+ SpaceAroundErbTag: { enabled: default_enabled },
61
+ SpaceIndentation: { enabled: default_enabled },
62
+ SpaceInHtmlTag: { enabled: default_enabled },
63
+ TrailingWhitespace: { enabled: default_enabled },
63
64
  },
64
65
  )
65
66
  end
67
+
68
+ def default_for(config)
69
+ default_linters_enabled = config.to_hash.dig("EnableDefaultLinters")
70
+ default(default_enabled: default_linters_enabled).merge(config)
71
+ end
66
72
  end
67
73
 
68
74
  private
@@ -38,7 +38,9 @@ module ERBLint
38
38
  @corrector.remove_trailing(range_with_offset(range), size)
39
39
  end
40
40
 
41
- def range_with_offset(range)
41
+ def range_with_offset(node_or_range)
42
+ range = to_range(node_or_range)
43
+
42
44
  @processed_source.to_source_range(
43
45
  bound(@offset + range.begin_pos)..bound(@offset + (range.end_pos - 1))
44
46
  )
@@ -50,6 +52,21 @@ module ERBLint
50
52
  @bound_range.max,
51
53
  ].min
52
54
  end
55
+
56
+ private
57
+
58
+ def to_range(node_or_range)
59
+ case node_or_range
60
+ when ::RuboCop::AST::Node, ::Parser::Source::Comment
61
+ node_or_range.loc.expression
62
+ when ::Parser::Source::Range
63
+ node_or_range
64
+ else
65
+ raise TypeError,
66
+ 'Expected a Parser::Source::Range, Comment or ' \
67
+ "Rubocop::AST::Node, got #{node_or_range.class}"
68
+ end
69
+ end
53
70
  end
54
71
  end
55
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ERBLint
4
- VERSION = '0.0.30'
4
+ VERSION = '0.0.35'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.30
4
+ version: 0.0.35
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Chan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-01 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html
@@ -44,14 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.51'
47
+ version: '0.79'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.51'
54
+ version: '0.79'
55
+ - !ruby/object:Gem::Dependency
56
+ name: parser
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.1.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.7.1.4
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: activesupport
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,20 @@ dependencies:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-shopify
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
125
153
  description: ERB Linter tool.
126
154
  email:
127
155
  - justin.the.c@gmail.com
@@ -167,7 +195,8 @@ files:
167
195
  homepage: https://github.com/Shopify/erb-lint
168
196
  licenses:
169
197
  - MIT
170
- metadata: {}
198
+ metadata:
199
+ allowed_push_host: https://rubygems.org
171
200
  post_install_message:
172
201
  rdoc_options: []
173
202
  require_paths:
@@ -176,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
176
205
  requirements:
177
206
  - - ">="
178
207
  - !ruby/object:Gem::Version
179
- version: 2.4.0
208
+ version: 2.5.0
180
209
  required_rubygems_version: !ruby/object:Gem::Requirement
181
210
  requirements:
182
211
  - - ">="