super-slim 3.4.1 → 3.6.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 +4 -4
- data/CHANGELOG.md +55 -1
- data/README.md +20 -5
- data/lib/super_slim/cli.rb +4 -0
- data/lib/super_slim/compiler.rb +31 -26
- data/lib/super_slim/formatter/code_wrapper.rb +5 -1
- data/lib/super_slim/formatter.rb +31 -6
- data/lib/super_slim/linter/rules.rb +272 -21
- data/lib/super_slim/linter.rb +60 -7
- data/lib/super_slim/parser.rb +2 -1
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +4 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 80dad2f87ffeca714cec78be16f94bea2271a13f0c095a9d4b6809f1fd256b85
|
|
4
|
+
data.tar.gz: 7b7da5b1350e4ddb1e27519d2bf6d80136ce27b9ce55b492ec06b6e8fb67deaa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f5836cf9c4c31086aee7e4de49fce6179796ff8858e5f3f78851a091b0489fdc31e39b66dfcf32ae0513cb622af8299d4bc125aaec1b8a5af3c113adfeb6698f
|
|
7
|
+
data.tar.gz: 396e25566bd11a16442861680472c09b5deffccac68b0c47ed5fbc49917422802e3d04646853a78e08b67ca74f47b6f13f2e7a87ca5e406df2102c39c4f6f121
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.6.0] — 2026-11-11
|
|
6
|
+
|
|
7
|
+
The linter reads all the Ruby of a template and `.super-slim.yml` is checked when it is read: `super-slim lint` may
|
|
8
|
+
report lines it passed before, and a configuration with a misspelt name or a wrong value now stops with an error.
|
|
9
|
+
|
|
10
|
+
**New**
|
|
11
|
+
- A rule, an option or a setting that `.super-slim.yml` misspells stops `lint` and `format` with a usage error that
|
|
12
|
+
names it and lists the known ones, instead of being ignored: `LineLenght:` left the limit at 120, and a rule carried
|
|
13
|
+
over from slim-lint, such as `RuboCop:`, did nothing. Rename or remove the entry.
|
|
14
|
+
- A wrong value in `.super-slim.yml` is reported with the setting it belongs to. `max: "120"`, `classes: [close]`,
|
|
15
|
+
`enabled: "false"`, `tailwind_classes: maybe`, `max_line_length: 0` and `attribute_delimiters: "<>"` failed on
|
|
16
|
+
every template, ended in a backtrace or were taken at their word.
|
|
17
|
+
|
|
18
|
+
**Fixed**
|
|
19
|
+
- `InstanceVariables` reads interpolated text, quoted attributes, inline HTML and `ruby:` blocks, not only `-` and `=`
|
|
20
|
+
lines and code attributes, and reports each variable on its own line when code spreads over several. An `@` in a
|
|
21
|
+
string, as in `"npm/@scope/package"`, is no longer taken for one.
|
|
22
|
+
- `UnusedLocals` no longer reports a local read in inline HTML, in an interpolation holding braces such as
|
|
23
|
+
`#{tags.map { |tag| tag.name }}`, in an attribute of `javascript:` or `css:`, or through
|
|
24
|
+
`binding.local_variable_get`.
|
|
25
|
+
- `UnescapedOutput` reports `href=="#{url}"`, `#{{}}` in a quoted attribute or in inline HTML, and `html_safe` or
|
|
26
|
+
`raw` inside an interpolation. `DatabaseQueries` and `UnnecessaryInterpolation` read interpolations holding braces,
|
|
27
|
+
and `HardcodedText` no longer takes their code for text.
|
|
28
|
+
- An offense inside an HTML comment (`/!`) at the top of a template is reported on its line, not without one.
|
|
29
|
+
- With `max_line_length`, `super-slim format` writes a template that `format --check` accepts. A second run could
|
|
30
|
+
rewrite a hash the first had spread, a call with two spaces before its first argument, the children of an output
|
|
31
|
+
moved onto its tag, or a `-` line joined beside a `ruby:` block. A template the wrapper changes takes about 30%
|
|
32
|
+
longer to format; one already formatted takes the same time.
|
|
33
|
+
- With `max_line_length`, a `- rescue` or `- ensure` line longer than the limit no longer makes `super-slim format`
|
|
34
|
+
refuse the template with a `NoMethodError`.
|
|
35
|
+
- Outside Rails, a dynamic tag with content, `*{tag: "section"} Hello`, no longer compiles to Ruby that warns under
|
|
36
|
+
`ruby -w`.
|
|
37
|
+
|
|
38
|
+
## [3.5.0] — 2026-11-08
|
|
39
|
+
|
|
40
|
+
Six lint rules and a page that documents every rule. Four of the new rules are on by default, so `super-slim lint` may
|
|
41
|
+
report lines it passed before.
|
|
42
|
+
|
|
43
|
+
**New**
|
|
44
|
+
- The website gives each lint rule its own entry: why it exists, a flagged and a clean example, its settings with
|
|
45
|
+
their defaults, and whether `super-slim format` fixes it. `super-slim lint` prints the link to each rule it reports,
|
|
46
|
+
on stderr.
|
|
47
|
+
- `AnchorHref` also flags `href="#"` and `javascript:` URLs, links that stand in for a button.
|
|
48
|
+
- `UnusedLocals`, on by default, flags a local declared in `/# locals:` that the partial never reads. The shorthand
|
|
49
|
+
`render "card", post:` counts as a read; a template that reads `local_assigns` is left alone.
|
|
50
|
+
- `SoleNestedConditional`, `NegatedIfElseCondition` and `UnlessElse`, on by default, flag an `if` alone inside
|
|
51
|
+
another, `- if !x` with an `else`, and `- unless` with an `else`: RuboCop's cops for these can't see `-` lines.
|
|
52
|
+
- `InlineEventHandlers`, off by default, flags `onclick`, `onchange` and the other `on*` attributes, which a strict
|
|
53
|
+
Content Security Policy blocks.
|
|
54
|
+
- `DatabaseQueries`, off by default, flags `where`, `find_by`, `pluck`, `exists?` and the like, and `Model.all`,
|
|
55
|
+
`Model.order` and the like, written in a template.
|
|
56
|
+
|
|
5
57
|
## [3.4.1] — 2026-11-07
|
|
6
58
|
|
|
7
59
|
One fix: `super-slim format` no longer writes `-` lines that `super-slim lint` flags.
|
|
@@ -247,7 +299,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
247
299
|
- Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
|
|
248
300
|
newer than the one Ruby 3.3 ships.
|
|
249
301
|
|
|
250
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.
|
|
302
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.6.0...HEAD
|
|
303
|
+
[3.6.0]: https://github.com/levups/super-slim/compare/v3.5.0...v3.6.0
|
|
304
|
+
[3.5.0]: https://github.com/levups/super-slim/compare/v3.4.1...v3.5.0
|
|
251
305
|
[3.4.1]: https://github.com/levups/super-slim/compare/v3.4.0...v3.4.1
|
|
252
306
|
[3.4.0]: https://github.com/levups/super-slim/compare/v3.3.0...v3.4.0
|
|
253
307
|
[3.3.0]: https://github.com/levups/super-slim/compare/v3.2.2...v3.3.0
|
data/README.md
CHANGED
|
@@ -46,6 +46,16 @@ Rails then renders every `.slim` view with SuperSlim, and the digest of a `cache
|
|
|
46
46
|
renders changes. A partial whose name is computed needs a `/# Template Dependency: orders/line` comment, as in ERB.
|
|
47
47
|
There is nothing to configure. Rails itself is optional, and SuperSlim is tested on Rails 8.0 and later.
|
|
48
48
|
|
|
49
|
+
To keep Slim as your engine and use only the [linter](#linter), the [formatter](#formatter) or the
|
|
50
|
+
[RuboCop plugin](#rubocop-plugin), add SuperSlim with `require: false`. Rails then never loads it and goes on
|
|
51
|
+
rendering with Slim, while `super-slim lint`, `super-slim format` and `rubocop` still find it:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
gem "super-slim", require: false
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
They read templates as SuperSlim does, [additions](#additions-to-slim) included, which Slim can't render.
|
|
58
|
+
|
|
49
59
|
Without Rails, `SuperSlim.compile` turns a template into Ruby code, which you evaluate where the template's
|
|
50
60
|
variables and methods are defined:
|
|
51
61
|
|
|
@@ -202,20 +212,24 @@ linters, so most will look familiar:
|
|
|
202
212
|
|---|---|---|
|
|
203
213
|
| Indentation, Tab, TrailingWhitespace, EmptyLines, TrailingBlankLines, ControlStatementSpacing, RedundantDiv | slim-lint (`super-slim format` fixes them) | on |
|
|
204
214
|
| LineLength (`max: 120`), CommentControlStatement, EmptyControlStatement, ConsecutiveControlStatements (`max: 2`), TagCase | slim-lint | on |
|
|
215
|
+
| SoleNestedConditional, NegatedIfElseCondition, UnlessElse (conditionals spread over `-` lines, out of the RuboCop plugin's reach) | RuboCop | on |
|
|
205
216
|
| InstanceVariables (in partials) | haml-lint, Rails conventions | on |
|
|
206
217
|
| StrictLocals (partials declare `/# locals:`) | Rails 7.1 strict locals | off |
|
|
207
|
-
|
|
|
208
|
-
|
|
|
218
|
+
| UnusedLocals (a local declared in `/# locals:` that the partial never reads) | Rails 7.1 strict locals | on |
|
|
219
|
+
| DuplicateId, ImageAlt, IframeTitle, AnchorHref (also `href="#"` and `javascript:`), NestedLinks, PositiveTabindex, BooleanAttributeValue, AriaBooleanValue (`aria-*=true` renders no value, `aria-*=false` no attribute), AriaRole, AriaAttribute | Herb, eslint-plugin-jsx-a11y | on |
|
|
220
|
+
| ButtonType, InlineStyles, InlineEventHandlers (`onclick` and other `on*` attributes) | eslint-plugin-react, haml-lint | off |
|
|
209
221
|
| DeprecatedClasses (`classes: {old: new}`, `old-*: new-*` for a prefix), DeprecatedAttributes (`attributes: {old: new}`), both with `preset: bootstrap5` | erb_lint | on, but empty |
|
|
210
222
|
| UnescapedOutput (`==`, `html_safe`, `raw`, `#{{}}`) | Brakeman, erb_lint | off |
|
|
223
|
+
| DatabaseQueries (`where`, `find_by`, `pluck`, `exists?`, `Model.all`, `Model.order`… in a template) | Rails guides | off |
|
|
211
224
|
| SlimSyntax (code continued after an opening bracket, a comment after code: Slim and Brakeman can't read them) | Brakeman | on |
|
|
212
225
|
| EmbeddedEngines (`forbidden_engines: [javascript, css]`) | slim-lint | off |
|
|
213
226
|
| HardcodedText (text that should be translated) | erb_lint | off |
|
|
214
227
|
| UnnecessaryInterpolation, UnnecessaryStringOutput | haml-lint | on |
|
|
215
228
|
| Zwsp (invisible characters) | slim-lint | on |
|
|
216
229
|
|
|
217
|
-
The website
|
|
218
|
-
|
|
230
|
+
The website gives [each rule](https://levups.github.io/super-slim/tools.html#rules) its own entry: why it exists,
|
|
231
|
+
an example, its settings with their defaults, and whether `super-slim format` fixes it. `super-slim lint` prints
|
|
232
|
+
the link to each rule it reports, on stderr. A configuration file looks like this:
|
|
219
233
|
|
|
220
234
|
```yaml
|
|
221
235
|
# .super-slim.yml
|
|
@@ -240,7 +254,8 @@ linters:
|
|
|
240
254
|
```
|
|
241
255
|
|
|
242
256
|
Every rule accepts `include:` and `exclude:`, lists of glob patterns matched against paths relative to
|
|
243
|
-
`.super-slim.yml` (`**/` matches any number of directories).
|
|
257
|
+
`.super-slim.yml` (`**/` matches any number of directories). A rule, an option or a setting the file misspells
|
|
258
|
+
stops `lint` and `format` with an error that names it, rather than being ignored.
|
|
244
259
|
|
|
245
260
|
A comment silences rules on the next tag or code line, blank lines and comments skipped, and on the lines it goes on
|
|
246
261
|
over. The linter reports a rule name it doesn't know:
|
data/lib/super_slim/cli.rb
CHANGED
|
@@ -55,6 +55,10 @@ module SuperSlim
|
|
|
55
55
|
[Linter::Offense.new(path, 0, "Error", "#{exception.class}: #{exception.message.lines.first&.strip}")]
|
|
56
56
|
end.flatten
|
|
57
57
|
offenses.each { |offense| puts printable("#{offense.path}:#{offense.lineno}: [#{offense.rule}] #{offense.message}") }
|
|
58
|
+
# On stderr, so that stdout stays one offense per line for the tools that read it.
|
|
59
|
+
$stdout.flush
|
|
60
|
+
reported = offenses.to_set(&:rule)
|
|
61
|
+
Linter::Rules.all.each { |rule| warn("#{rule.rule_name}: #{Linter::RULES_URL}##{rule.rule_name}") if reported.include?(rule.rule_name) }
|
|
58
62
|
offenses.empty? ? 0 : 1
|
|
59
63
|
end
|
|
60
64
|
|
data/lib/super_slim/compiler.rb
CHANGED
|
@@ -83,6 +83,29 @@ module SuperSlim
|
|
|
83
83
|
closing_by_opening
|
|
84
84
|
end
|
|
85
85
|
|
|
86
|
+
# The text cut at its interpolations, as [:static, text] and [:dynamic, code, escape] parts: `#{{code}}` is not
|
|
87
|
+
# escaped, and its code comes without the inner braces.
|
|
88
|
+
def self.interpolation_parts(text)
|
|
89
|
+
return [[:static, text]] unless text.include?('#{')
|
|
90
|
+
|
|
91
|
+
closing_by_opening = closing_braces(text)
|
|
92
|
+
scanner = StringScanner.new(text)
|
|
93
|
+
parts = []
|
|
94
|
+
until scanner.eos?
|
|
95
|
+
if scanner.skip(ESCAPED_INTERPOLATION)
|
|
96
|
+
parts << [:static, '#{']
|
|
97
|
+
elsif scanner.match?(INTERPOLATION_START) && (closing = closing_by_opening[scanner.pos + 1])
|
|
98
|
+
code = text.byteslice(scanner.pos + 2...closing)
|
|
99
|
+
scanner.pos = closing + 1
|
|
100
|
+
escape = !code.match?(UNESCAPED_INTERPOLATION_CODE)
|
|
101
|
+
parts << [:dynamic, escape ? code : code[1..-2], escape]
|
|
102
|
+
else
|
|
103
|
+
parts << [:static, scanner.scan(INTERPOLATION_FREE_TEXT)]
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
parts
|
|
107
|
+
end
|
|
108
|
+
|
|
86
109
|
private
|
|
87
110
|
|
|
88
111
|
def compile_children(nodes)
|
|
@@ -142,7 +165,10 @@ module SuperSlim
|
|
|
142
165
|
if @rails
|
|
143
166
|
compile_children(output.children)
|
|
144
167
|
else
|
|
145
|
-
|
|
168
|
+
# The block returns what its children render.
|
|
169
|
+
captured = new_variable
|
|
170
|
+
capture_into(captured) { compile_children(output.children) }
|
|
171
|
+
emit_code(captured)
|
|
146
172
|
end
|
|
147
173
|
emit_code("end")
|
|
148
174
|
emit_output(returned_value, output.escape)
|
|
@@ -307,7 +333,7 @@ module SuperSlim
|
|
|
307
333
|
emit_code("#{value} = #{attribute.code}")
|
|
308
334
|
[[:raw, "::SuperSlim::Runtime.class_value(#{value}, #{attribute.escape}, #{@html_safe})"]]
|
|
309
335
|
when QuotedAttribute
|
|
310
|
-
interpolation_parts(attribute.text).map do |kind, value, escape|
|
|
336
|
+
Compiler.interpolation_parts(attribute.text).map do |kind, value, escape|
|
|
311
337
|
(kind == :static && attribute.escape) ? [:static, ERB::Escape.html_escape(value)] : [kind, value, escape]
|
|
312
338
|
end
|
|
313
339
|
end
|
|
@@ -351,28 +377,7 @@ module SuperSlim
|
|
|
351
377
|
end
|
|
352
378
|
|
|
353
379
|
def emit_interpolated(text)
|
|
354
|
-
emit_parts(interpolation_parts(text))
|
|
355
|
-
end
|
|
356
|
-
|
|
357
|
-
def interpolation_parts(text)
|
|
358
|
-
return [[:static, text]] unless text.include?('#{')
|
|
359
|
-
|
|
360
|
-
closing_by_opening = Compiler.closing_braces(text)
|
|
361
|
-
scanner = StringScanner.new(text)
|
|
362
|
-
parts = []
|
|
363
|
-
until scanner.eos?
|
|
364
|
-
if scanner.skip(ESCAPED_INTERPOLATION)
|
|
365
|
-
parts << [:static, '#{']
|
|
366
|
-
elsif scanner.match?(INTERPOLATION_START) && (closing = closing_by_opening[scanner.pos + 1])
|
|
367
|
-
code = text.byteslice(scanner.pos + 2...closing)
|
|
368
|
-
scanner.pos = closing + 1
|
|
369
|
-
escape = !code.match?(UNESCAPED_INTERPOLATION_CODE)
|
|
370
|
-
parts << [:dynamic, escape ? code : code[1..-2], escape]
|
|
371
|
-
else
|
|
372
|
-
parts << [:static, scanner.scan(INTERPOLATION_FREE_TEXT)]
|
|
373
|
-
end
|
|
374
|
-
end
|
|
375
|
-
parts
|
|
380
|
+
emit_parts(Compiler.interpolation_parts(text))
|
|
376
381
|
end
|
|
377
382
|
|
|
378
383
|
def compile_embedded(embedded)
|
|
@@ -401,7 +406,7 @@ module SuperSlim
|
|
|
401
406
|
|
|
402
407
|
placeholder = "%#{object_id.abs.to_s(36)}%"
|
|
403
408
|
dynamic_parts = []
|
|
404
|
-
markdown = children.grep(Text).flat_map { interpolation_parts(_1.text) }.map do |part|
|
|
409
|
+
markdown = children.grep(Text).flat_map { Compiler.interpolation_parts(_1.text) }.map do |part|
|
|
405
410
|
next part[1] if part.first == :static
|
|
406
411
|
|
|
407
412
|
dynamic_parts << part
|
|
@@ -437,7 +442,7 @@ module SuperSlim
|
|
|
437
442
|
@buffer = StringBuffer.new(variable, @escape_function)
|
|
438
443
|
emit_code(@buffer.open)
|
|
439
444
|
yield
|
|
440
|
-
|
|
445
|
+
flush_static
|
|
441
446
|
@buffer = outer_buffer
|
|
442
447
|
end
|
|
443
448
|
end
|
|
@@ -51,7 +51,11 @@ module SuperSlim
|
|
|
51
51
|
parsed = Prism.parse("#{TEMPLATE_METHOD}#{code}\nend")
|
|
52
52
|
opens_block = !parsed.success?
|
|
53
53
|
parsed = Prism.parse("#{TEMPLATE_METHOD}#{code}\nend\nend") if opens_block
|
|
54
|
-
|
|
54
|
+
return unless parsed.success? && parsed.comments.empty?
|
|
55
|
+
|
|
56
|
+
# `- rescue` and `- ensure` belong to the method itself: they leave it no statements to spread.
|
|
57
|
+
body = parsed.value.statements.body.first.body
|
|
58
|
+
[body.is_a?(Prism::StatementsNode) ? body.body : [], opens_block]
|
|
55
59
|
end
|
|
56
60
|
|
|
57
61
|
# Positional and keyword arguments, then the block argument; nil when the call takes none.
|
data/lib/super_slim/formatter.rb
CHANGED
|
@@ -25,7 +25,10 @@ module SuperSlim
|
|
|
25
25
|
RUBY_BLOCK_MIN_LINES = 3
|
|
26
26
|
SKIPPED_KINDS = %i[blank continuation].freeze
|
|
27
27
|
RUBY_BLOCK = /\A\s*ruby:\s*\z/
|
|
28
|
-
|
|
28
|
+
# Three `-` lines, or one beside a `ruby:` block.
|
|
29
|
+
CODE_RUN = /^( *)- .*\n\1- .*\n\1- |^( *)- .*\n\2ruby:$|^( *)ruby:\n(?:\3 .*\n)+\3- /
|
|
30
|
+
# Two passes settle every template of the corpus, and a third sees that nothing changes: the rest is margin.
|
|
31
|
+
MAX_PASSES = 5
|
|
29
32
|
# `while x do` is valid Ruby that nobody writes.
|
|
30
33
|
LOOP = /\A(?:while|until|for)\b/
|
|
31
34
|
END_KEYWORD = /\Aend\b/
|
|
@@ -60,7 +63,31 @@ module SuperSlim
|
|
|
60
63
|
@line_wrapper = LineWrapper.new(max_line_length, tailwind_classes:, attribute_delimiters:) if max_line_length
|
|
61
64
|
end
|
|
62
65
|
|
|
66
|
+
# With max_line_length, a pass can leave work for the next: brackets it spread fit once joined back without the
|
|
67
|
+
# spaces inside them, and the children of an output it moved onto its tag stand one level too deep. Formatted
|
|
68
|
+
# until nothing changes, so that `format --check` passes right after `format`.
|
|
63
69
|
def call(source)
|
|
70
|
+
formatted = format_once(source)
|
|
71
|
+
return formatted if @line_wrapper.nil? || formatted == source
|
|
72
|
+
|
|
73
|
+
(MAX_PASSES - 1).times do
|
|
74
|
+
previous = formatted
|
|
75
|
+
formatted = format_again(previous)
|
|
76
|
+
break if formatted == previous
|
|
77
|
+
end
|
|
78
|
+
formatted
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
# Each pass is checked against the template it reads: one that can't be leaves the pass before it.
|
|
84
|
+
def format_again(formatted)
|
|
85
|
+
format_once(formatted)
|
|
86
|
+
rescue UnsafeFormattingError
|
|
87
|
+
formatted
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def format_once(source)
|
|
64
91
|
parser = Parser.new(outline: true, tailwind_classes: @tailwind_classes)
|
|
65
92
|
nodes = parser.call(source)
|
|
66
93
|
formatted = layout(source, parser)
|
|
@@ -79,9 +106,9 @@ module SuperSlim
|
|
|
79
106
|
wrapped = @line_wrapper.call(formatted, parser:)
|
|
80
107
|
return formatted if wrapped == formatted
|
|
81
108
|
|
|
82
|
-
# `-` code joined by the wrapper may now make a run long enough for a `ruby:` block
|
|
83
|
-
# refused below.
|
|
84
|
-
if wrapped.match?(
|
|
109
|
+
# `-` code joined by the wrapper may now make a run long enough for a `ruby:` block, or stand beside one. A wrap
|
|
110
|
+
# Slim can't read is refused below.
|
|
111
|
+
if wrapped.match?(CODE_RUN)
|
|
85
112
|
wrapped_parser = parsed_template(wrapped).last
|
|
86
113
|
wrapped = layout(wrapped, wrapped_parser) if wrapped_parser
|
|
87
114
|
end
|
|
@@ -97,8 +124,6 @@ module SuperSlim
|
|
|
97
124
|
raise UnsafeFormattingError, "Code nested too deep to check that formatting changes nothing"
|
|
98
125
|
end
|
|
99
126
|
|
|
100
|
-
private
|
|
101
|
-
|
|
102
127
|
def layout(source, parser)
|
|
103
128
|
source = source.delete_prefix("")
|
|
104
129
|
lines = Parser.split_lines(source)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
3
5
|
module SuperSlim
|
|
4
6
|
class Linter
|
|
5
7
|
module Rules
|
|
@@ -76,6 +78,57 @@ module SuperSlim
|
|
|
76
78
|
def literal_attributes(tag, name)
|
|
77
79
|
tag.attributes.select { _1.respond_to?(:name) && _1.name == name && literal_value(_1) }
|
|
78
80
|
end
|
|
81
|
+
|
|
82
|
+
# Each piece of Ruby in the template, with the line it starts on: `-` and `=` lines, attribute values, splats,
|
|
83
|
+
# `ruby:` blocks, and the interpolations of text, inline HTML and quoted attributes.
|
|
84
|
+
def each_ruby(document)
|
|
85
|
+
document.each_node do |node, ancestors|
|
|
86
|
+
case node
|
|
87
|
+
when AST::Control, AST::Output then yield node.code, document.line_of(node)
|
|
88
|
+
when AST::Tag then each_attribute_ruby(node.attributes) { |code| yield code, document.line_of(node) }
|
|
89
|
+
when AST::InlineHtml then each_interpolation(node.text) { |code| yield code, document.line_of(node) }
|
|
90
|
+
when AST::Embedded
|
|
91
|
+
each_attribute_ruby(node.attributes) { |code| yield code, document.line_of(node) }
|
|
92
|
+
yield ruby_block_code(node), document.line_of(node) + 1 if ruby_block?(node)
|
|
93
|
+
when AST::Text
|
|
94
|
+
next if ruby_block?(ancestors.last)
|
|
95
|
+
|
|
96
|
+
each_interpolation(node.text) { |code| yield code, line_near(document, node, ancestors) }
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def each_attribute_ruby(attributes, &)
|
|
102
|
+
attributes.each do |attribute|
|
|
103
|
+
case attribute
|
|
104
|
+
when AST::CodeAttribute, AST::Splat then yield attribute.code
|
|
105
|
+
when AST::QuotedAttribute then each_interpolation(attribute.text, &)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def each_interpolation(text)
|
|
111
|
+
Compiler.interpolation_parts(text).each { |kind, code| yield code if kind == :dynamic }
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def ruby_block?(node)
|
|
115
|
+
node.is_a?(AST::Embedded) && node.engine == "ruby"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def ruby_block_code(embedded)
|
|
119
|
+
embedded.children.grep(AST::Text).map(&:text).join
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Prism reads a `- if` or `- each do` line without its `end`, as it reads code with errors: the tree it gives
|
|
123
|
+
# holds the calls written.
|
|
124
|
+
def each_prism_node(code_or_node)
|
|
125
|
+
stack = [code_or_node.is_a?(String) ? Prism.parse(code_or_node).value : code_or_node]
|
|
126
|
+
until stack.empty?
|
|
127
|
+
node = stack.pop
|
|
128
|
+
yield node
|
|
129
|
+
stack.concat(node.compact_child_nodes)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
79
132
|
end
|
|
80
133
|
|
|
81
134
|
# Layout: everything `super-slim format` rewrites.
|
|
@@ -247,20 +300,28 @@ module SuperSlim
|
|
|
247
300
|
end
|
|
248
301
|
|
|
249
302
|
class InstanceVariables < Rule
|
|
250
|
-
INSTANCE_VARIABLE = /(?<![\w@])@[a-z_]\w*/
|
|
251
|
-
|
|
252
303
|
def offenses(document, _source)
|
|
253
304
|
return [] unless document.partial?
|
|
254
305
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
306
|
+
# One offense a line of Ruby, for its first variable: a tag may read the same one in two attributes.
|
|
307
|
+
enum_for(:each_ruby, document).flat_map do |code, lineno|
|
|
308
|
+
first_variable_by_line(code).map do |line, variable|
|
|
309
|
+
offense(document, lineno + line - 1, "Pass `#{variable}` to the partial as a local")
|
|
259
310
|
end
|
|
260
|
-
|
|
311
|
+
end.uniq
|
|
312
|
+
end
|
|
261
313
|
|
|
262
|
-
|
|
314
|
+
private
|
|
315
|
+
|
|
316
|
+
# Lexed rather than matched: "npm/@scope/package" and "@alice" hold no variable.
|
|
317
|
+
def first_variable_by_line(code)
|
|
318
|
+
variable_by_line = {}
|
|
319
|
+
return variable_by_line unless code.include?("@")
|
|
320
|
+
|
|
321
|
+
Prism.lex(code).value.each do |token,|
|
|
322
|
+
variable_by_line[token.location.start_line] ||= token.value if token.type == :INSTANCE_VARIABLE
|
|
263
323
|
end
|
|
324
|
+
variable_by_line
|
|
264
325
|
end
|
|
265
326
|
end
|
|
266
327
|
|
|
@@ -274,6 +335,42 @@ module SuperSlim
|
|
|
274
335
|
end
|
|
275
336
|
end
|
|
276
337
|
|
|
338
|
+
class UnusedLocals < Rule
|
|
339
|
+
SIGNATURE = %r{\A\s*/#\s*locals:\s*(\(.*\))\s*\z}
|
|
340
|
+
|
|
341
|
+
def offenses(document, _source)
|
|
342
|
+
return [] unless (lineno = document.lines.index { _1.match?(SIGNATURE) })
|
|
343
|
+
|
|
344
|
+
definition = Prism.parse("def _#{document.lines[lineno][SIGNATURE, 1]}; end").value.statements&.body&.first
|
|
345
|
+
return [] unless definition.is_a?(Prism::DefNode) && definition.parameters
|
|
346
|
+
|
|
347
|
+
declared = definition.parameters.keywords.map(&:name)
|
|
348
|
+
# A default can read another local: `(post:, title: post.title)`.
|
|
349
|
+
read = Set.new
|
|
350
|
+
definition.parameters.keywords.each { read.merge(names_read(_1.value)) if _1.respond_to?(:value) }
|
|
351
|
+
each_ruby(document) { |code,| read.merge(names_read(code)) }
|
|
352
|
+
# `binding.local_variable_get(:class)` reads a local named after a keyword.
|
|
353
|
+
return [] if read.include?(:local_assigns) || read.include?(:binding)
|
|
354
|
+
|
|
355
|
+
(declared - read.to_a).map { offense(document, lineno + 1, "The local `#{_1}` is declared but never read") }
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
private
|
|
359
|
+
|
|
360
|
+
# A partial reads its locals as methods without receiver nor arguments, the shorthand `post:` included, or as
|
|
361
|
+
# variables once code has assigned them.
|
|
362
|
+
def names_read(code_or_node)
|
|
363
|
+
names = Set.new
|
|
364
|
+
each_prism_node(code_or_node) do |node|
|
|
365
|
+
case node
|
|
366
|
+
when Prism::LocalVariableReadNode then names << node.name
|
|
367
|
+
when Prism::CallNode then names << node.name if node.receiver.nil? && node.arguments.nil?
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
names
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
277
374
|
# HTML and accessibility, after Herb, erb_lint and eslint-plugin-jsx-a11y.
|
|
278
375
|
|
|
279
376
|
class DuplicateId < Rule
|
|
@@ -306,9 +403,20 @@ module SuperSlim
|
|
|
306
403
|
end
|
|
307
404
|
end
|
|
308
405
|
|
|
406
|
+
# After eslint-plugin-jsx-a11y's anchor-is-valid: `href="#"` and `javascript:` only stand in for a click handler.
|
|
309
407
|
class AnchorHref < Rule
|
|
408
|
+
PLACEHOLDER = /\A\s*(?:#|javascript:.*)\z/im
|
|
409
|
+
|
|
310
410
|
def offenses(document, _source)
|
|
311
|
-
|
|
411
|
+
placeholders = enum_for(:each_tag, document, "a").flat_map do |tag,|
|
|
412
|
+
literal_attributes(tag, "href").filter_map do |attribute|
|
|
413
|
+
next unless (href = literal_value(attribute)).match?(PLACEHOLDER)
|
|
414
|
+
|
|
415
|
+
target = href.strip.start_with?("#") ? '`href="#"`' : "A `javascript:` URL"
|
|
416
|
+
offense(document, document.line_of(tag), "#{target} goes nowhere: use a `button` for actions")
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
missing_attribute(document, "a", "href", "A link without `href` is not a link: use a `button` for actions") + placeholders
|
|
312
420
|
end
|
|
313
421
|
end
|
|
314
422
|
|
|
@@ -481,27 +589,42 @@ module SuperSlim
|
|
|
481
589
|
end
|
|
482
590
|
end
|
|
483
591
|
|
|
592
|
+
class InlineEventHandlers < Rule
|
|
593
|
+
DEFAULTS = {enabled: false}.freeze
|
|
594
|
+
HANDLER = /\Aon[a-z]+\z/
|
|
595
|
+
|
|
596
|
+
def offenses(document, _source)
|
|
597
|
+
enum_for(:each_tag, document).flat_map do |tag,|
|
|
598
|
+
attribute_names(tag).grep(HANDLER).map do |name|
|
|
599
|
+
offense(document, document.line_of(tag), "Move the `#{name}` handler to JavaScript: a strict Content Security Policy blocks it")
|
|
600
|
+
end
|
|
601
|
+
end
|
|
602
|
+
end
|
|
603
|
+
end
|
|
604
|
+
|
|
484
605
|
class UnescapedOutput < Rule
|
|
485
606
|
DEFAULTS = {enabled: false}.freeze
|
|
486
607
|
SKIPS_ESCAPING = /\.html_safe\b|(?<![\w.:@])raw[ (]/
|
|
487
|
-
UNESCAPED_INTERPOLATION = /#\{\{/
|
|
488
608
|
MESSAGE = "skips HTML escaping: output with `=`, or render HTML from a translation key ending in `_html`"
|
|
489
609
|
|
|
490
610
|
def offenses(document, _source)
|
|
491
611
|
document.enum_for(:each_node).flat_map do |node, ancestors|
|
|
492
|
-
reasons_of(node).map { offense(document, line_near(document, node, ancestors), "#{_1} #{MESSAGE}") }
|
|
612
|
+
reasons_of(node, ancestors).map { offense(document, line_near(document, node, ancestors), "#{_1} #{MESSAGE}") }
|
|
493
613
|
end
|
|
494
614
|
end
|
|
495
615
|
|
|
496
616
|
private
|
|
497
617
|
|
|
498
|
-
def reasons_of(node)
|
|
618
|
+
def reasons_of(node, ancestors)
|
|
499
619
|
case node
|
|
500
620
|
when AST::Output then [*("`==`" unless node.escape), *code_reason(node.code)]
|
|
501
621
|
when AST::Control then code_reason(node.code)
|
|
502
622
|
when AST::Tag then node.attributes.flat_map { attribute_reasons(_1) }
|
|
503
|
-
when AST::
|
|
504
|
-
|
|
623
|
+
when AST::InlineHtml then text_reasons(node.text)
|
|
624
|
+
# The text of a `ruby:` block is code, read whole with the block.
|
|
625
|
+
when AST::Text then ruby_block?(ancestors.last) ? [] : text_reasons(node.text)
|
|
626
|
+
when AST::Embedded
|
|
627
|
+
[*node.attributes.flat_map { attribute_reasons(_1) }, *(code_reason(ruby_block_code(node)) if ruby_block?(node))]
|
|
505
628
|
else []
|
|
506
629
|
end
|
|
507
630
|
end
|
|
@@ -509,11 +632,21 @@ module SuperSlim
|
|
|
509
632
|
def attribute_reasons(attribute)
|
|
510
633
|
case attribute
|
|
511
634
|
when AST::CodeAttribute then [*("`==`" unless attribute.escape), *code_reason(attribute.code)]
|
|
635
|
+
when AST::QuotedAttribute then text_reasons(attribute.text, escape: attribute.escape)
|
|
512
636
|
when AST::Splat then code_reason(attribute.code)
|
|
513
637
|
else []
|
|
514
638
|
end
|
|
515
639
|
end
|
|
516
640
|
|
|
641
|
+
# `==` before literal text skips escaping nothing but what the author wrote: only interpolations count.
|
|
642
|
+
def text_reasons(text, escape: true)
|
|
643
|
+
Compiler.interpolation_parts(text).flat_map do |kind, code, escape_interpolation|
|
|
644
|
+
next [] unless kind == :dynamic
|
|
645
|
+
|
|
646
|
+
[*("`==`" unless escape), *("`\#{{}}`" unless escape_interpolation), *code_reason(code)]
|
|
647
|
+
end.uniq
|
|
648
|
+
end
|
|
649
|
+
|
|
517
650
|
def code_reason(code)
|
|
518
651
|
return [] unless (call = code[SKIPS_ESCAPING])
|
|
519
652
|
|
|
@@ -536,22 +669,61 @@ module SuperSlim
|
|
|
536
669
|
|
|
537
670
|
class HardcodedText < Rule
|
|
538
671
|
DEFAULTS = {enabled: false}.freeze
|
|
539
|
-
NOT_WORDS =
|
|
672
|
+
NOT_WORDS = /&(?:[a-z]+|#\d+|#x\h+);|<!\[CDATA\[|\]\]>|<[^<>]+>/i
|
|
540
673
|
EXCERPT_LENGTH = 40
|
|
541
674
|
|
|
542
675
|
def offenses(document, _source)
|
|
543
676
|
document.enum_for(:each_node).filter_map do |node, ancestors|
|
|
544
677
|
next unless node.is_a?(AST::TextBlock) && !ancestors.inside_comment
|
|
545
678
|
|
|
546
|
-
|
|
547
|
-
|
|
679
|
+
texts = node.children.grep(AST::Text).map(&:text)
|
|
680
|
+
literal_texts = texts.flat_map { |text| Compiler.interpolation_parts(text).filter_map { |kind, literal| literal if kind == :static } }
|
|
681
|
+
next unless literal_texts.join(" ").gsub(NOT_WORDS, "").match?(/\p{L}/)
|
|
548
682
|
|
|
683
|
+
text = texts.join(" ").gsub(/\s+/, " ").strip
|
|
549
684
|
excerpt = (text.length > EXCERPT_LENGTH) ? "#{text[0, EXCERPT_LENGTH]}…" : text
|
|
550
685
|
offense(document, line_near(document, node, ancestors), "Translate `#{excerpt}` instead of writing it in the template")
|
|
551
686
|
end
|
|
552
687
|
end
|
|
553
688
|
end
|
|
554
689
|
|
|
690
|
+
# Matched by method name: an off-by-default rule, since a few of these also run on arrays.
|
|
691
|
+
class DatabaseQueries < Rule
|
|
692
|
+
DEFAULTS = {enabled: false}.freeze
|
|
693
|
+
QUERY_METHODS = %i[where find_by find_by! pluck pick exists? find_each find_in_batches in_batches].to_set.freeze
|
|
694
|
+
# Called on a constant, these read a model's table: `Tag.order(:name)`.
|
|
695
|
+
MODEL_METHODS = %i[all order find first last count limit includes joins sum minimum maximum].to_set.freeze
|
|
696
|
+
|
|
697
|
+
def offenses(document, _source)
|
|
698
|
+
calls_by_lineno = Hash.new { |hash, lineno| hash[lineno] = [] }
|
|
699
|
+
each_ruby(document) do |code, lineno|
|
|
700
|
+
each_prism_node(code) do |node|
|
|
701
|
+
next unless node.is_a?(Prism::CallNode) && (call = query(node))
|
|
702
|
+
|
|
703
|
+
calls_by_lineno[lineno + node.location.start_line - 1] << [node.message_loc.start_offset, call]
|
|
704
|
+
end
|
|
705
|
+
end
|
|
706
|
+
# One offense a line, its calls in the order they are written: the tree is walked from the outermost.
|
|
707
|
+
calls_by_lineno.keys.sort!.map! do |lineno|
|
|
708
|
+
calls = calls_by_lineno[lineno].sort.map!(&:last).uniq
|
|
709
|
+
offense(document, lineno, "#{calls.map { "`#{_1}`" }.join(", ")} #{(calls.size == 1) ? "queries" : "query"} " \
|
|
710
|
+
"the database from the template: load the records in the controller")
|
|
711
|
+
end
|
|
712
|
+
end
|
|
713
|
+
|
|
714
|
+
private
|
|
715
|
+
|
|
716
|
+
def query(node)
|
|
717
|
+
return unless node.receiver
|
|
718
|
+
|
|
719
|
+
if node.receiver.is_a?(Prism::ConstantReadNode) || node.receiver.is_a?(Prism::ConstantPathNode)
|
|
720
|
+
"#{node.receiver.slice}.#{node.name}" if MODEL_METHODS.include?(node.name) || QUERY_METHODS.include?(node.name)
|
|
721
|
+
elsif QUERY_METHODS.include?(node.name)
|
|
722
|
+
node.name.to_s
|
|
723
|
+
end
|
|
724
|
+
end
|
|
725
|
+
end
|
|
726
|
+
|
|
555
727
|
# Brakeman parses views with Slim, which refuses these lines: it reports the template and checks nothing in it.
|
|
556
728
|
class SlimSyntax < Rule
|
|
557
729
|
MESSAGE = "Slim, and so Brakeman, cannot read"
|
|
@@ -579,19 +751,26 @@ module SuperSlim
|
|
|
579
751
|
# Ruby that a plainer line of Slim says better, after haml-lint.
|
|
580
752
|
|
|
581
753
|
class UnnecessaryInterpolation < Rule
|
|
582
|
-
LONE_INTERPOLATION = /\A#\{([^{}]+)\}\z/
|
|
583
|
-
|
|
584
754
|
def offenses(document, _source)
|
|
585
755
|
document.enum_for(:each_node).filter_map do |node, ancestors|
|
|
586
756
|
next unless node.is_a?(AST::TextBlock)
|
|
587
757
|
next unless (lineno = document.line_of(node) || document.line_of(ancestors.last))
|
|
588
758
|
|
|
589
759
|
texts = node.children.grep(AST::Text)
|
|
590
|
-
next unless texts.size == 1 && texts.first.text
|
|
760
|
+
next unless texts.size == 1 && (code = lone_interpolation(texts.first.text))
|
|
591
761
|
|
|
592
|
-
offense(document, lineno, "Write `= #{
|
|
762
|
+
offense(document, lineno, "Write `= #{code}` instead of interpolating it alone")
|
|
593
763
|
end
|
|
594
764
|
end
|
|
765
|
+
|
|
766
|
+
private
|
|
767
|
+
|
|
768
|
+
# The code of an escaped interpolation that is the whole text: `#{{}}` would need `==`.
|
|
769
|
+
def lone_interpolation(text)
|
|
770
|
+
parts = Compiler.interpolation_parts(text)
|
|
771
|
+
kind, code, escape = parts.first
|
|
772
|
+
code.strip if parts.size == 1 && kind == :dynamic && escape && !code.strip.empty?
|
|
773
|
+
end
|
|
595
774
|
end
|
|
596
775
|
|
|
597
776
|
class UnnecessaryStringOutput < Rule
|
|
@@ -615,6 +794,78 @@ module SuperSlim
|
|
|
615
794
|
end
|
|
616
795
|
end
|
|
617
796
|
|
|
797
|
+
# Conditionals RuboCop would rewrite, but its plugin hands it each `-` line without the lines around it.
|
|
798
|
+
|
|
799
|
+
module Conditionals
|
|
800
|
+
CONDITIONAL = /\A\s*(?:if|unless)\s/
|
|
801
|
+
ELSE = /\A\s*else\s*\z/
|
|
802
|
+
|
|
803
|
+
private
|
|
804
|
+
|
|
805
|
+
# Each conditional `-` line, the line after it at its depth, comments skipped, and its sub-lines.
|
|
806
|
+
def each_conditional(document)
|
|
807
|
+
[document.nodes, *document.visits.filter_map { |node,| Document.children_of(node) }].each do |siblings|
|
|
808
|
+
lines = significant(siblings)
|
|
809
|
+
lines.each_with_index do |node, index|
|
|
810
|
+
yield node, lines[index + 1], significant(node.children) if node.is_a?(AST::Control) && node.code.match?(CONDITIONAL)
|
|
811
|
+
end
|
|
812
|
+
end
|
|
813
|
+
end
|
|
814
|
+
|
|
815
|
+
def significant(nodes)
|
|
816
|
+
(nodes || []).reject { _1 == AST::NEWLINE || (_1.is_a?(AST::Control) && _1.code.match?(/\A\s*#/)) }
|
|
817
|
+
end
|
|
818
|
+
|
|
819
|
+
def else?(node)
|
|
820
|
+
node.is_a?(AST::Control) && node.code.match?(ELSE)
|
|
821
|
+
end
|
|
822
|
+
|
|
823
|
+
def branch?(node)
|
|
824
|
+
node.is_a?(AST::Control) && node.code.match?(/\A\s*(?:else|elsif)\b/)
|
|
825
|
+
end
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
class SoleNestedConditional < Rule
|
|
829
|
+
include Conditionals
|
|
830
|
+
|
|
831
|
+
def offenses(document, _source)
|
|
832
|
+
enum_for(:each_conditional, document).filter_map do |_node, following, children|
|
|
833
|
+
next if branch?(following) || children.size != 1
|
|
834
|
+
next unless (inner = children.first).is_a?(AST::Control) && inner.code.match?(CONDITIONAL)
|
|
835
|
+
|
|
836
|
+
keyword = inner.code[/\w+/]
|
|
837
|
+
offense(document, document.line_of(inner), "Merge this `#{keyword}` into the one around it with `&&`")
|
|
838
|
+
end
|
|
839
|
+
end
|
|
840
|
+
end
|
|
841
|
+
|
|
842
|
+
class NegatedIfElseCondition < Rule
|
|
843
|
+
include Conditionals
|
|
844
|
+
|
|
845
|
+
NEGATED_IF = /\A\s*if\s+!(?!=)\s*(\S.*)\z/m
|
|
846
|
+
|
|
847
|
+
def offenses(document, _source)
|
|
848
|
+
enum_for(:each_conditional, document).filter_map do |node, following|
|
|
849
|
+
next unless else?(following) && (condition = node.code[NEGATED_IF, 1])
|
|
850
|
+
next if condition.match?(Formatter::COMPOUND_CONDITION)
|
|
851
|
+
|
|
852
|
+
offense(document, document.line_of(node), "Write `if #{condition.strip}` and swap the branches")
|
|
853
|
+
end
|
|
854
|
+
end
|
|
855
|
+
end
|
|
856
|
+
|
|
857
|
+
class UnlessElse < Rule
|
|
858
|
+
include Conditionals
|
|
859
|
+
|
|
860
|
+
def offenses(document, _source)
|
|
861
|
+
enum_for(:each_conditional, document).filter_map do |node, following|
|
|
862
|
+
next unless else?(following) && node.code.match?(/\A\s*unless\s/)
|
|
863
|
+
|
|
864
|
+
offense(document, document.line_of(node), "Write `if` instead of `unless`, and swap the branches")
|
|
865
|
+
end
|
|
866
|
+
end
|
|
867
|
+
end
|
|
868
|
+
|
|
618
869
|
class Zwsp < Rule
|
|
619
870
|
INVISIBLE = /[\u200B-\u200D\u2060\uFEFF]/
|
|
620
871
|
# The zero-width joiner of 🐈⬛ or 👩🏻💻: between two pictographs, after an optional skin tone or emoji style.
|
data/lib/super_slim/linter.rb
CHANGED
|
@@ -70,10 +70,23 @@ module SuperSlim
|
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
CONFIG_FILE = ".super-slim.yml"
|
|
73
|
+
# Each rule has an entry there, its name as the fragment: `rake docs` writes them.
|
|
74
|
+
RULES_URL = "https://levups.github.io/super-slim/tools.html"
|
|
73
75
|
DISABLE_DIRECTIVE = %r{\A?\s*/\s*super-slim:disable\s+(\S.*?)\s*\z}
|
|
74
76
|
# The layout rules: what `super-slim format` rewrites.
|
|
75
77
|
LAYOUT_RULE_NAMES = %w[Indentation Tab TrailingWhitespace EmptyLines ControlStatementSpacing RedundantDiv].freeze
|
|
76
78
|
GLOB_OPTIONS = %w[include exclude].freeze
|
|
79
|
+
SETTINGS = %w[linters format fmt tailwind_classes].freeze
|
|
80
|
+
FORMAT_SETTINGS = %w[max_line_length attribute_delimiters].freeze
|
|
81
|
+
# What the default of a rule's option asks of the value that replaces it.
|
|
82
|
+
VALUE_KIND_BY_DEFAULT_CLASS = {
|
|
83
|
+
TrueClass => ["true or false", [TrueClass, FalseClass]],
|
|
84
|
+
FalseClass => ["true or false", [TrueClass, FalseClass]],
|
|
85
|
+
Integer => ["a whole number", [Integer]],
|
|
86
|
+
# Left empty under a preset, `classes:` is nil.
|
|
87
|
+
Hash => ["a mapping", [Hash, NilClass]],
|
|
88
|
+
Array => ["a list", [Array]]
|
|
89
|
+
}.freeze
|
|
77
90
|
|
|
78
91
|
def self.load(directory = Dir.pwd)
|
|
79
92
|
new(configuration(directory).fetch("linters", {}), directory)
|
|
@@ -86,22 +99,62 @@ module SuperSlim
|
|
|
86
99
|
configuration
|
|
87
100
|
end
|
|
88
101
|
|
|
89
|
-
# A value of the wrong shape would fail later, inside a rule,
|
|
102
|
+
# A value of the wrong shape would fail later, inside a rule or the formatter, on every template and with an error
|
|
103
|
+
# naming none of this file: `max: "120"` compares a number with text.
|
|
90
104
|
def self.check_configuration(configuration)
|
|
91
105
|
raise Error, "#{CONFIG_FILE}: expected a mapping of settings" unless configuration.is_a?(Hash)
|
|
92
106
|
|
|
107
|
+
check_names(configuration.keys, SETTINGS, "a setting")
|
|
93
108
|
%w[linters format fmt].each do |key|
|
|
94
109
|
raise Error, "#{CONFIG_FILE}: `#{key}` must be a mapping" unless configuration.fetch(key, {}).is_a?(Hash)
|
|
95
110
|
end
|
|
96
|
-
|
|
97
|
-
raise Error, "#{CONFIG_FILE}: `linters: #{rule_name}` must be a mapping of options" unless options.is_a?(Hash)
|
|
111
|
+
raise Error, "#{CONFIG_FILE}: `tailwind_classes` must be true or false" unless [true, false, nil].include?(configuration["tailwind_classes"])
|
|
98
112
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
113
|
+
configuration.fetch("linters", {}).each { |rule_name, options| check_rule_options(rule_name, options) }
|
|
114
|
+
configuration.slice("format", "fmt").each { |key, options| check_format_options(key, options) }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def self.check_rule_options(rule_name, options)
|
|
118
|
+
raise Error, "#{CONFIG_FILE}: `linters: #{rule_name}` must be a mapping of options" unless options.is_a?(Hash)
|
|
119
|
+
|
|
120
|
+
GLOB_OPTIONS.each do |key|
|
|
121
|
+
globs = Array(options[key])
|
|
122
|
+
raise Error, "#{CONFIG_FILE}: `#{rule_name}: #{key}` must be a list of globs" unless globs.all?(String)
|
|
123
|
+
end
|
|
124
|
+
rule_class = Rules.all.find { _1.rule_name == rule_name }
|
|
125
|
+
raise Error, "#{CONFIG_FILE}: `#{rule_name}` is not a rule of super-slim lint" unless rule_class
|
|
126
|
+
|
|
127
|
+
preset_option = (PRESETS.any? { |_preset, options_by_rule| options_by_rule.key?(rule_name) }) ? ["preset"] : []
|
|
128
|
+
check_names(options.keys, rule_class::DEFAULTS.keys.map(&:to_s) + GLOB_OPTIONS + preset_option, "an option of #{rule_name}")
|
|
129
|
+
rule_class::DEFAULTS.each do |key, default|
|
|
130
|
+
next unless options.key?(key.to_s)
|
|
131
|
+
|
|
132
|
+
kind, classes = VALUE_KIND_BY_DEFAULT_CLASS.fetch(default.class)
|
|
133
|
+
raise Error, "#{CONFIG_FILE}: `#{rule_name}: #{key}` must be #{kind}" unless classes.any? { options[key.to_s].is_a?(_1) }
|
|
103
134
|
end
|
|
104
135
|
end
|
|
136
|
+
private_class_method :check_rule_options
|
|
137
|
+
|
|
138
|
+
def self.check_format_options(key, options)
|
|
139
|
+
check_names(options.keys, FORMAT_SETTINGS, "a setting of `#{key}`")
|
|
140
|
+
max_line_length = options["max_line_length"]
|
|
141
|
+
wraps_nothing_or_at_a_length = max_line_length.nil? || (max_line_length.is_a?(Integer) && max_line_length.positive?)
|
|
142
|
+
raise Error, "#{CONFIG_FILE}: `#{key}: max_line_length` must be a whole number above 0" unless wraps_nothing_or_at_a_length
|
|
143
|
+
|
|
144
|
+
delimiters = Formatter::LineWrapper::OPENING_BY_ATTRIBUTE_DELIMITERS.keys
|
|
145
|
+
return if !options.key?("attribute_delimiters") || delimiters.include?(options["attribute_delimiters"])
|
|
146
|
+
|
|
147
|
+
raise Error, "#{CONFIG_FILE}: `#{key}: attribute_delimiters` must be one of #{delimiters.join(", ")}"
|
|
148
|
+
end
|
|
149
|
+
private_class_method :check_format_options
|
|
150
|
+
|
|
151
|
+
# A misspelt name is a setting that does nothing and says nothing: the rule it meant to turn off stays on.
|
|
152
|
+
def self.check_names(names, known_names, kind)
|
|
153
|
+
unknown_name = (names - known_names).first or return
|
|
154
|
+
|
|
155
|
+
raise Error, "#{CONFIG_FILE}: `#{unknown_name}` is not #{kind}, use one of: #{known_names.join(", ")}"
|
|
156
|
+
end
|
|
157
|
+
private_class_method :check_names
|
|
105
158
|
|
|
106
159
|
# include: and exclude: globs match paths relative to `root`, the directory of .super-slim.yml.
|
|
107
160
|
def initialize(options_by_rule_name = {}, root = Dir.pwd)
|
data/lib/super_slim/parser.rb
CHANGED
|
@@ -292,7 +292,8 @@ module SuperSlim
|
|
|
292
292
|
def parse_comment
|
|
293
293
|
if @scanner.skip(HTML_COMMENT)
|
|
294
294
|
text_indent = @indents.last + @scanner[1].size + 2
|
|
295
|
-
|
|
295
|
+
lineno = @lineno
|
|
296
|
+
@stacks.last << located(Comment.new(parse_text_block(@scanner.rest, text_indent)), lineno)
|
|
296
297
|
elsif @scanner.match?(CONDITIONAL_COMMENT)
|
|
297
298
|
syntax_error!("Conditional comments are not supported")
|
|
298
299
|
else
|
data/lib/super_slim/version.rb
CHANGED
data/lib/super_slim.rb
CHANGED
|
@@ -8,8 +8,10 @@ require_relative "super_slim/compiler"
|
|
|
8
8
|
require_relative "super_slim/runtime"
|
|
9
9
|
|
|
10
10
|
module SuperSlim
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
# By their full path: a bare "super_slim/formatter" is the first one on the load path, which is another copy of the
|
|
12
|
+
# gem when this one was loaded by its path, as `exe/super-slim` does in a checkout.
|
|
13
|
+
autoload :Formatter, File.join(__dir__, "super_slim/formatter")
|
|
14
|
+
autoload :RuboCop, File.join(__dir__, "super_slim/rubocop")
|
|
13
15
|
|
|
14
16
|
class Error < StandardError; end
|
|
15
17
|
class InvalidAttributeNameError < Error; end
|