slim_lint 0.3.0 → 0.4.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
  SHA1:
3
- metadata.gz: 66e3790d144eb2141cb93b53835a447cfe9e1db8
4
- data.tar.gz: 7b07131e272e1b913926061eb37703d7e0738f01
3
+ metadata.gz: 3389992065a4202ee3f1541ccade01b424df4115
4
+ data.tar.gz: 27e0e7ef9d0155dd54b5729ecf237b72f4b24464
5
5
  SHA512:
6
- metadata.gz: d776fde028a429d5a44ae2cdc4092705e759a96f82dca6c2b59ad6a3fc1280823e86ecee38fa642f3720d048567ab31a3dd1bd5bf72a48ef3737c2b1d300843f
7
- data.tar.gz: e617bc89440a83feefec3beabaa186ef57fb8c5433fd7b134610deaf631a4b4ad3c0e1e70474255145d2b40fc5d19ee3f19191ab102329b4f5a5dc27d9995717
6
+ metadata.gz: f419a7cd896f9dec484719c95e4b7ceb6fac3b3c37cdf7432de40ce9581381256d4b108dc20da1e30391bf6128bbe3519ae6cfac6875669dac7b39e4851779ef
7
+ data.tar.gz: adc31508f162b7182d79eb81641d8ac5648f498d43f4decbbd38240fd720def57349979cfb9eba76504a9e3c4f97336b9d65fe7d9a25b14530762661a817fb28
data/config/default.yml CHANGED
@@ -34,9 +34,11 @@ linters:
34
34
  - Lint/EndAlignment
35
35
  - Lint/Void
36
36
  - Metrics/LineLength
37
+ - Style/AlignHash
37
38
  - Style/AlignParameters
38
39
  - Style/BlockNesting
39
40
  - Style/FileName
41
+ - Style/FirstParameterIndentation
40
42
  - Style/IfUnlessModifier
41
43
  - Style/IndentationConsistency
42
44
  - Style/IndentationWidth
@@ -0,0 +1,29 @@
1
+ module SlimLint::Filters
2
+ # A dumbed-down version of {Slim::CodeAttributes} which doesn't introduce any
3
+ # temporary variables or other cruft.
4
+ class AttributeProcessor < Slim::Filter
5
+ define_options :merge_attrs
6
+
7
+ # Handle attributes expression `[:html, :attrs, *attrs]`
8
+ #
9
+ # @param attrs [Array]
10
+ # @return [Array]
11
+ def on_html_attrs(*attrs)
12
+ [:multi, *attrs.map { |a| compile(a) }]
13
+ end
14
+
15
+ # Handle attribute expression `[:html, :attr, name, value]`
16
+ #
17
+ # @param name [String] name of the attribute
18
+ # @param value [Array] Sexp representing the value
19
+ def on_html_attr(name, value)
20
+ if value[0] == :slim && value[1] == :attrvalue
21
+ code = value[3]
22
+ [:code, code]
23
+ else
24
+ @attr = name
25
+ super
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ module SlimLint::Filters
2
+ # A dumbed-down version of {Slim::Controls} which doesn't introduce temporary
3
+ # variables and other cruft (which in the context of extracting Ruby code,
4
+ # results in a lot of weird cops reported by RuboCop).
5
+ class ControlProcessor < Slim::Filter
6
+ BLOCK_RE = /\A(if|unless)\b|\bdo\s*(\|[^\|]*\|)?\s*$/
7
+
8
+ # Handle control expression `[:slim, :control, code, content]`
9
+ #
10
+ # @param code [String]
11
+ # @param content [Array]
12
+ def on_slim_control(code, content)
13
+ [:multi,
14
+ [:code, code],
15
+ compile(content)]
16
+ end
17
+
18
+ # Handle output expression `[:slim, :output, escape, code, content]`
19
+ #
20
+ # @param _escape [Boolean]
21
+ # @param code [String]
22
+ # @param content [Array]
23
+ # @return [Array
24
+ def on_slim_output(_escape, code, content)
25
+ if code[BLOCK_RE]
26
+ [:multi,
27
+ [:code, code, compile(content)],
28
+ [:code, 'end']]
29
+ else
30
+ [:multi, [:dynamic, code], compile(content)]
31
+ end
32
+ end
33
+
34
+ # Handle text expression `[:slim, :text, type, content]`
35
+ #
36
+ # @param _type [Symbol]
37
+ # @param content [Array]
38
+ # @return [Array]
39
+ def on_slim_text(_type, content)
40
+ # Ensures :newline expressions from static output are still represented in
41
+ # the final expression
42
+ compile(content)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module SlimLint::Filters
2
+ # A dumbed-down version of {Slim::Splat::Filter} which doesn't introduced
3
+ # temporary variables or other cruft.
4
+ class SplatProcessor < Slim::Filter
5
+ # Handle slim splat expressions `[:slim, :splat, code]`
6
+ #
7
+ # @param code [String]
8
+ # @return [Array]
9
+ def on_slim_splat(code)
10
+ [:code, code]
11
+ end
12
+ end
13
+ end
@@ -6,12 +6,19 @@ module SlimLint
6
6
 
7
7
  on [:multi] do |sexp|
8
8
  Utils.for_consecutive_items(sexp,
9
- ->(nested_sexp) { nested_sexp.match?([:slim, :control]) },
9
+ method(:flat_control_statement?),
10
10
  config['max_consecutive'] + 1) do |group|
11
11
  report_lint(group.first,
12
12
  "#{group.count} consecutive control statements can be " \
13
13
  'merged into a single `ruby:` filter')
14
14
  end
15
15
  end
16
+
17
+ private
18
+
19
+ def flat_control_statement?(sexp)
20
+ sexp.match?([:slim, :control]) &&
21
+ sexp[3] == [:multi, [:newline]]
22
+ end
16
23
  end
17
24
  end
@@ -12,9 +12,11 @@ module SlimLint
12
12
  processed_sexp = SlimLint::RubyExtractEngine.new.call(document.source)
13
13
 
14
14
  extractor = SlimLint::RubyExtractor.new
15
- extracted_ruby = extractor.extract(processed_sexp)
15
+ extracted_source = extractor.extract(processed_sexp)
16
16
 
17
- find_lints(extracted_ruby, extractor.source_map) unless extracted_ruby.empty?
17
+ next if extracted_source.source.empty?
18
+
19
+ find_lints(extracted_source.source, extracted_source.source_map)
18
20
  end
19
21
 
20
22
  private
@@ -5,12 +5,6 @@ module SlimLint
5
5
  # This is mostly copied from Slim::Engine, with some filters and generators
6
6
  # omitted.
7
7
  class RubyExtractEngine < Temple::Engine
8
- define_options sort_attrs: true,
9
- format: :xhtml,
10
- attr_quote: '"',
11
- merge_attrs: { 'class' => ' ' },
12
- default_tag: 'div'
13
-
14
8
  filter :Encoding
15
9
  filter :RemoveBOM
16
10
 
@@ -23,14 +17,11 @@ module SlimLint
23
17
  # matter in this case.
24
18
  use Slim::Embedded
25
19
  use Slim::Interpolation
26
- use Slim::Splat::Filter
20
+ use SlimLint::Filters::SplatProcessor
27
21
  use Slim::DoInserter
28
22
  use Slim::EndInserter
29
- use Slim::Controls
30
- html :AttributeSorter
31
- html :AttributeMerger
32
- use Slim::CodeAttributes
33
- filter :ControlFlow
23
+ use SlimLint::Filters::ControlProcessor
24
+ use SlimLint::Filters::AttributeProcessor
34
25
  filter :MultiFlattener
35
26
  filter :StaticMerger
36
27
 
@@ -30,16 +30,20 @@ module SlimLint
30
30
  include SexpVisitor
31
31
  extend SexpVisitor::DSL
32
32
 
33
- # Map of generated Ruby source code lines and their corresponding lines in
34
- # the original document.
35
- attr_reader :source_map
33
+ # Stores the extracted source and a map of lines of generated source to the
34
+ # original source that created them.
35
+ #
36
+ # @attr_reader source [String] generated source code
37
+ # @attr_reader source_map [Hash] map of line numbers from generated source
38
+ # to original source line number
39
+ RubySource = Struct.new(:source, :source_map)
36
40
 
37
41
  # Extracts Ruby code from Sexp representing a Slim document.
38
42
  #
39
43
  # @param sexp [SlimLint::Sexp]
40
44
  def extract(sexp)
41
45
  trigger_pattern_callbacks(sexp)
42
- @source_lines.join("\n")
46
+ RubySource.new(@source_lines.join("\n"), @source_map)
43
47
  end
44
48
 
45
49
  on_start do |_sexp|
@@ -79,14 +83,14 @@ module SlimLint
79
83
  def append(code, sexp)
80
84
  return if code.empty?
81
85
 
82
- @source_lines << code
83
86
  original_line = sexp.line
84
87
 
85
88
  # For code that spans multiple lines, the resulting code will span
86
89
  # multiple lines, so we need to create a mapping for each line.
87
- (code.count("\n") + 1).times do
90
+ code.split("\n").each_with_index do |line, index|
91
+ @source_lines << line
88
92
  @line_count += 1
89
- @source_map[@line_count] = original_line
93
+ @source_map[@line_count] = original_line + index
90
94
  end
91
95
  end
92
96
  end
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module SlimLint
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slim
@@ -101,8 +101,11 @@ files:
101
101
  - lib/slim_lint/engine.rb
102
102
  - lib/slim_lint/exceptions.rb
103
103
  - lib/slim_lint/file_finder.rb
104
+ - lib/slim_lint/filters/attribute_processor.rb
105
+ - lib/slim_lint/filters/control_processor.rb
104
106
  - lib/slim_lint/filters/inject_line_numbers.rb
105
107
  - lib/slim_lint/filters/sexp_converter.rb
108
+ - lib/slim_lint/filters/splat_processor.rb
106
109
  - lib/slim_lint/lint.rb
107
110
  - lib/slim_lint/linter.rb
108
111
  - lib/slim_lint/linter/comment_control_statement.rb
@@ -159,3 +162,4 @@ signing_key:
159
162
  specification_version: 4
160
163
  summary: Slim template linting tool
161
164
  test_files: []
165
+ has_rdoc: