haml_lint 0.53.0 → 0.54.0

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: 49bb0bec15e96c047439560e80f83eaf6f4c3baec24a12b1f10248f1d2f347d3
4
- data.tar.gz: 153f65eda76c5751bbae1d02fc49d0d4477f401bb273096ad9308868b9d21288
3
+ metadata.gz: b017823b15563d033480ee99317c4d0c007624b10ad2ed3b53c254c5686289c8
4
+ data.tar.gz: cf83ac415fb7749551466fcc0b23a5a2019bbecb99a67b2c54051a78523f1a33
5
5
  SHA512:
6
- metadata.gz: a4d2c2682e7b12d9aaf8164054f3533c0667f5f6035bfbf5ef6bbb694a40f9a70d9d13a9aa56dabf27131be27e74d346c9eff642a65d0e2aac8992ebc1999fba
7
- data.tar.gz: 343aee9ed9bf4a766640aabbbaf96af9a5c36261fb6e6943b9a0b9eadcb5c75bc5a15665cae30c3c4c8925b307ecca837130fb04cf1d14a74549978db235f60c
6
+ metadata.gz: 17ea23cce399f12712d0f73560553b82068e2ac3be9124c590bbea78948a330811402c15fd2b5f8cbc0b5e8e044bc044e69df78f2e05462580665db814298595
7
+ data.tar.gz: d619d3018e139b00ce539da9b9f7e1bfef644c71c8e102d146778c3c322770458a16feb89bb44a1a3699eadacc6038c7186407fc8361e6b354f0c4bdbb2dcd57
@@ -6,5 +6,5 @@ module HamlLint
6
6
  APP_NAME = 'haml-lint'
7
7
 
8
8
  REPO_URL = 'https://github.com/sds/haml-lint'
9
- BUG_REPORT_URL = "#{REPO_URL}/issues"
9
+ BUG_REPORT_URL = "#{REPO_URL}/issues".freeze
10
10
  end
@@ -3,7 +3,7 @@
3
3
  module HamlLint
4
4
  # Handles linter configuration transformation via Haml comments.
5
5
  class Directive
6
- LINTER_REGEXP = /(?:[A-Z]\w+)/.freeze
6
+ LINTER_REGEXP = /(?:[A-Z]\w+)/
7
7
 
8
8
  DIRECTIVE_REGEXP = /
9
9
  # "haml-lint:" with optional spacing
@@ -14,7 +14,7 @@ module HamlLint
14
14
 
15
15
  # "all" or a comma-separated list (with optional spaces) of linters
16
16
  (?<linters>all | (?:#{LINTER_REGEXP}\s*,\s*)* #{LINTER_REGEXP})
17
- /x.freeze
17
+ /x
18
18
 
19
19
  # Constructs a directive from source code as a given line.
20
20
  #
@@ -14,6 +14,9 @@ module HamlLint
14
14
  # @return [String] Haml template file path
15
15
  attr_reader :file
16
16
 
17
+ # @return [Boolean] true if source changes (from autocorrect) should be written to stdout instead of disk
18
+ attr_reader :write_to_stdout
19
+
17
20
  # @return [HamlLint::Tree::Node] Root of the parse tree
18
21
  attr_reader :tree
19
22
 
@@ -36,10 +39,12 @@ module HamlLint
36
39
  # @param source [String] Haml code to parse
37
40
  # @param options [Hash]
38
41
  # @option options :file [String] file name of document that was parsed
42
+ # @option options :write_to_stdout [Boolean] true if source changes should be written to stdout
39
43
  # @raise [Haml::Parser::Error] if there was a problem parsing the document
40
44
  def initialize(source, options)
41
45
  @config = options[:config]
42
46
  @file = options.fetch(:file, STRING_SOURCE)
47
+ @write_to_stdout = options[:write_to_stdout]
43
48
  @source_was_changed = false
44
49
  process_source(source)
45
50
  end
@@ -82,7 +87,11 @@ module HamlLint
82
87
  if file == STRING_SOURCE
83
88
  raise HamlLint::Exceptions::InvalidFilePath, 'Cannot write without :file option'
84
89
  end
85
- File.write(file, unstrip_frontmatter(source))
90
+ if @write_to_stdout
91
+ $stdout << unstrip_frontmatter(source)
92
+ else
93
+ File.write(file, unstrip_frontmatter(source))
94
+ end
86
95
  @source_was_changed = false
87
96
  end
88
97
 
@@ -3,7 +3,7 @@
3
3
  module HamlLint
4
4
  # Checks for tabs that are placed for alignment of tag content
5
5
  class Linter::AlignmentTabs < Linter
6
- REGEX = /[^\s*]\t+/.freeze
6
+ REGEX = /[^\s*]\t+/
7
7
 
8
8
  def visit_tag(node)
9
9
  if REGEX.match?(node.source_code)
@@ -20,7 +20,7 @@ module HamlLint
20
20
 
21
21
  STATIC_TYPES = %i[str sym].freeze
22
22
 
23
- VALID_CLASS_REGEX = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/.freeze
23
+ VALID_CLASS_REGEX = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/
24
24
 
25
25
  def visit_tag(node)
26
26
  return unless contains_class_attribute?(node.dynamic_attributes_sources)
@@ -11,7 +11,7 @@ module HamlLint
11
11
  tab: /^\t*(?! )/,
12
12
  }.freeze
13
13
 
14
- LEADING_SPACES_REGEX = /^( +)(?! )/.freeze
14
+ LEADING_SPACES_REGEX = /^( +)(?! )/
15
15
 
16
16
  def visit_root(root)
17
17
  character = config['character'].to_sym
@@ -31,7 +31,7 @@ module HamlLint
31
31
 
32
32
  private
33
33
 
34
- MULTILINE_PIPE_REGEX = /\s+\|\s*$/.freeze
34
+ MULTILINE_PIPE_REGEX = /\s+\|\s*$/
35
35
 
36
36
  def line_text_for_node(node)
37
37
  document.source_lines[node.line - 1]
@@ -6,8 +6,8 @@ module HamlLint
6
6
  include LinterRegistry
7
7
 
8
8
  MSG = 'Placeholders attributes should not be used.'
9
- HASH_REGEXP = /:?['"]?placeholder['"]?(?::| *=>)/.freeze
10
- HTML_REGEXP = /placeholder=/.freeze
9
+ HASH_REGEXP = /:?['"]?placeholder['"]?(?::| *=>)/
10
+ HTML_REGEXP = /placeholder=/
11
11
 
12
12
  def visit_tag(node)
13
13
  return unless node.hash_attributes_source =~ HASH_REGEXP || node.html_attributes_source =~ HTML_REGEXP
@@ -32,7 +32,7 @@ module HamlLint
32
32
 
33
33
  private
34
34
 
35
- def add_linter_options(parser) # rubocop:disable Metrics/MethodLength
35
+ def add_linter_options(parser) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
36
36
  parser.on('--auto-gen-config', 'Generate a configuration file acting as a TODO list') do
37
37
  @options[:auto_gen_config] = true
38
38
  end
@@ -70,7 +70,13 @@ module HamlLint
70
70
  @options[:autocorrect] ||= :safe
71
71
  end
72
72
 
73
- parser.on('--stderr', 'Write all output to stderr') do
73
+ parser.on('-s', '--stdin FILE', 'Pipe source from STDIN, using FILE in ' \
74
+ 'offense when combined with --auto-correct and --stdin.') do |file_path|
75
+ @options[:stdin] = file_path
76
+ end
77
+
78
+ parser.on('--stderr', 'Write all output to stderr except for the autocorrected source. ' \
79
+ 'This is especially useful when combined with --auto-correct and --stdin') do
74
80
  @options[:stderr] = true
75
81
  end
76
82
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'parallel'
4
+ require_relative 'source'
4
5
 
5
6
  module HamlLint
6
7
  # Responsible for running the applicable linters against the desired files.
@@ -20,12 +21,13 @@ module HamlLint
20
21
  # @return [HamlLint::Report] a summary of all lints found
21
22
  def run(options = {})
22
23
  @config = load_applicable_config(options)
23
- @files = extract_applicable_files(config, options)
24
+ @sources = extract_applicable_sources(config, options)
24
25
  @linter_selector = HamlLint::LinterSelector.new(config, options)
25
26
  @fail_fast = options.fetch(:fail_fast, false)
26
27
  @cache = {}
27
28
  @autocorrect = options[:autocorrect]
28
29
  @autocorrect_only = options[:autocorrect_only]
30
+ @autocorrect_stdout = options[:stdin] && options[:stderr]
29
31
 
30
32
  report(options)
31
33
  end
@@ -48,10 +50,10 @@ module HamlLint
48
50
  # @return [true, false]
49
51
  alias fail_fast? fail_fast
50
52
 
51
- # The list of files to lint during this run.
53
+ # The list of sources to lint during this run.
52
54
  #
53
- # @return [Array<String>]
54
- attr_reader :files
55
+ # @return [Array<HamlLint::Source>]
56
+ attr_reader :sources
55
57
 
56
58
  # The selector for which linters to run during this run.
57
59
  #
@@ -79,18 +81,20 @@ module HamlLint
79
81
  # Runs all provided linters using the specified config against the given
80
82
  # file.
81
83
  #
82
- # @param file [String] path to file to lint
84
+ # @param source [HamlLint::Source] source to lint
83
85
  # @param linter_selector [HamlLint::LinterSelector]
84
86
  # @param config [HamlLint::Configuration]
85
- def collect_lints(file, linter_selector, config)
87
+ def collect_lints(source, linter_selector, config)
86
88
  begin
87
- document = HamlLint::Document.new(File.read(file), file: file, config: config)
89
+ document = HamlLint::Document.new source.contents, file: source.path,
90
+ config: config,
91
+ write_to_stdout: @autocorrect_stdout
88
92
  rescue HamlLint::Exceptions::ParseError => e
89
- return [HamlLint::Lint.new(HamlLint::Linter::Syntax.new(config), file,
93
+ return [HamlLint::Lint.new(HamlLint::Linter::Syntax.new(config), source.path,
90
94
  e.line, e.to_s, :error)]
91
95
  end
92
96
 
93
- linters = linter_selector.linters_for_file(file)
97
+ linters = linter_selector.linters_for_file(source.path)
94
98
  lint_arrays = []
95
99
 
96
100
  if @autocorrect
@@ -125,40 +129,46 @@ module HamlLint
125
129
  lint_arrays
126
130
  end
127
131
 
128
- # Returns the list of files that should be linted given the specified
132
+ # Returns the list of sources that should be linted given the specified
129
133
  # configuration and options.
130
134
  #
131
135
  # @param config [HamlLint::Configuration]
132
136
  # @param options [Hash]
133
- # @return [Array<String>]
134
- def extract_applicable_files(config, options)
135
- included_patterns = options[:files]
136
- excluded_patterns = config['exclude']
137
- excluded_patterns += options.fetch(:excluded_files, [])
137
+ # @return [Array<HamlLint::Source>]
138
+ def extract_applicable_sources(config, options)
139
+ if options[:stdin]
140
+ [HamlLint::Source.new($stdin, options[:stdin])]
141
+ else
142
+ included_patterns = options[:files]
143
+ excluded_patterns = config['exclude']
144
+ excluded_patterns += options.fetch(:excluded_files, [])
138
145
 
139
- HamlLint::FileFinder.new(config).find(included_patterns, excluded_patterns)
146
+ HamlLint::FileFinder.new(config).find(included_patterns, excluded_patterns).map do |file_path|
147
+ HamlLint::Source.new File.new(file_path), file_path
148
+ end
149
+ end
140
150
  end
141
151
 
142
- # Process the files and add them to the given report.
152
+ # Process the sources and add them to the given report.
143
153
  #
144
154
  # @param report [HamlLint::Report]
145
155
  # @return [void]
146
- def process_files(report)
147
- files.each do |file|
148
- process_file(file, report)
156
+ def process_sources(report)
157
+ sources.each do |source|
158
+ process_source(source, report)
149
159
  break if report.failed? && fail_fast?
150
160
  end
151
161
  end
152
162
 
153
163
  # Process a file and add it to the given report.
154
164
  #
155
- # @param file [String] the name of the file to process
165
+ # @param source [HamlLint::Source] the source to process
156
166
  # @param report [HamlLint::Report]
157
167
  # @return [void]
158
- def process_file(file, report)
159
- lints = @cache[file] || collect_lints(file, linter_selector, config)
168
+ def process_source(source, report)
169
+ lints = @cache[source.path] || collect_lints(source, linter_selector, config)
160
170
  lints.each { |lint| report.add_lint(lint) }
161
- report.finish_file(file, lints)
171
+ report.finish_file(source.path, lints)
162
172
  end
163
173
 
164
174
  # Generates a report based on the given options.
@@ -168,9 +178,9 @@ module HamlLint
168
178
  # @return [HamlLint::Report]
169
179
  def report(options)
170
180
  report = HamlLint::Report.new(reporter: options[:reporter], fail_level: options[:fail_level])
171
- report.start(@files)
181
+ report.start(sources.map(&:path))
172
182
  warm_cache if options[:parallel]
173
- process_files(report)
183
+ process_sources(report)
174
184
  report
175
185
  end
176
186
 
@@ -178,9 +188,9 @@ module HamlLint
178
188
  #
179
189
  # @return [void]
180
190
  def warm_cache
181
- results = Parallel.map(files) do |file|
182
- lints = collect_lints(file, linter_selector, config)
183
- [file, lints]
191
+ results = Parallel.map(sources) do |source|
192
+ lints = collect_lints(source, linter_selector, config)
193
+ [source.path, lints]
184
194
  end
185
195
  @cache = results.to_h
186
196
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HamlLint
4
+ # Wrapper class representing a single target for HamlLint::Runner to run against, comprised of an IO object
5
+ # containing haml code, as well as a file path.
6
+ class Source
7
+ # @return [String] File path associated with the given IO object.
8
+ attr_reader :path
9
+
10
+ # Wraps an IO object and file path to a source object.
11
+ #
12
+ # @param [IO] io
13
+ # @param [String] path
14
+ def initialize(io, path)
15
+ @io = io
16
+ @path = path
17
+ end
18
+
19
+ # @return [String] Contents of the given IO object.
20
+ def contents
21
+ @contents ||= @io.read
22
+ end
23
+ end
24
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module HamlLint
5
- VERSION = '0.53.0'
5
+ VERSION = '0.54.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.53.0
4
+ version: 0.54.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-08 00:00:00.000000000 Z
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -176,6 +176,7 @@ files:
176
176
  - lib/haml_lint/ruby_parser.rb
177
177
  - lib/haml_lint/runner.rb
178
178
  - lib/haml_lint/severity.rb
179
+ - lib/haml_lint/source.rb
179
180
  - lib/haml_lint/spec.rb
180
181
  - lib/haml_lint/spec/matchers/report_lint.rb
181
182
  - lib/haml_lint/spec/normalize_indent.rb
@@ -199,7 +200,7 @@ homepage: https://github.com/sds/haml-lint
199
200
  licenses:
200
201
  - MIT
201
202
  metadata: {}
202
- post_install_message:
203
+ post_install_message:
203
204
  rdoc_options: []
204
205
  require_paths:
205
206
  - lib
@@ -207,15 +208,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
207
208
  requirements:
208
209
  - - ">="
209
210
  - !ruby/object:Gem::Version
210
- version: 2.7.0
211
+ version: '3.0'
211
212
  required_rubygems_version: !ruby/object:Gem::Requirement
212
213
  requirements:
213
214
  - - ">="
214
215
  - !ruby/object:Gem::Version
215
216
  version: '0'
216
217
  requirements: []
217
- rubygems_version: 3.0.3.1
218
- signing_key:
218
+ rubygems_version: 3.4.10
219
+ signing_key:
219
220
  specification_version: 4
220
221
  summary: HAML lint tool
221
222
  test_files: []