super-slim 3.0.0 → 3.1.1
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 -14
- data/README.md +289 -117
- data/lib/super_slim/cli.rb +45 -9
- data/lib/super_slim/compiler.rb +29 -10
- data/lib/super_slim/formatter/line_wrapper.rb +19 -18
- data/lib/super_slim/formatter.rb +91 -21
- data/lib/super_slim/linter/presets.rb +72 -0
- data/lib/super_slim/linter/rules.rb +20 -18
- data/lib/super_slim/linter.rb +68 -6
- data/lib/super_slim/parallel.rb +31 -11
- data/lib/super_slim/parser/ruby_brackets.rb +115 -0
- data/lib/super_slim/parser.rb +60 -10
- data/lib/super_slim/rubocop.rb +27 -5
- data/lib/super_slim/runtime.rb +12 -4
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +2 -1
- metadata +4 -2
data/lib/super_slim/linter.rb
CHANGED
|
@@ -6,15 +6,42 @@ module SuperSlim
|
|
|
6
6
|
class Linter
|
|
7
7
|
Offense = Data.define(:path, :lineno, :rule, :message)
|
|
8
8
|
|
|
9
|
+
# What the rules ask of a node's ancestors, worked out as the tree is walked: a list of them, copied at each
|
|
10
|
+
# level, made linting quadratic in the depth of the tree.
|
|
11
|
+
Ancestors = Data.define(:last, :control, :located, :inside_svg, :inside_link, :inside_comment) do
|
|
12
|
+
def self.root = new(nil, nil, nil, false, false, false)
|
|
13
|
+
|
|
14
|
+
def under(node, line_by_node)
|
|
15
|
+
tag_name = node.name if node.is_a?(AST::Tag)
|
|
16
|
+
Ancestors.new(
|
|
17
|
+
last: node,
|
|
18
|
+
control: node.is_a?(AST::Control) ? node : control,
|
|
19
|
+
located: line_by_node.key?(node) ? node : located,
|
|
20
|
+
inside_svg: inside_svg || tag_name == "svg",
|
|
21
|
+
inside_link: inside_link || tag_name == "a",
|
|
22
|
+
inside_comment: inside_comment || node.is_a?(AST::Comment)
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
9
27
|
# The tree is walked once per template: every rule iterates `visits`, each node with its ancestors.
|
|
10
28
|
Document = Data.define(:path, :lines, :nodes, :outline, :line_by_node, :visits, :tag_visits) do
|
|
11
|
-
|
|
12
|
-
|
|
29
|
+
# Depth first, children before the next sibling, with a stack of its own: a deep template would exhaust Ruby's.
|
|
30
|
+
def self.visits_of(nodes, line_by_node)
|
|
31
|
+
visits = []
|
|
32
|
+
frames = [[nodes || [], 0, Ancestors.root]]
|
|
33
|
+
until frames.empty?
|
|
34
|
+
frame = frames.last
|
|
35
|
+
siblings, index, ancestors = frame
|
|
36
|
+
next frames.pop if index == siblings.size
|
|
37
|
+
|
|
38
|
+
frame[1] += 1
|
|
39
|
+
node = siblings[index]
|
|
13
40
|
next if node == AST::NEWLINE
|
|
14
41
|
|
|
15
42
|
visits << [node, ancestors]
|
|
16
43
|
children = children_of(node)
|
|
17
|
-
|
|
44
|
+
frames << [children, 0, ancestors.under(node, line_by_node)] if children
|
|
18
45
|
end
|
|
19
46
|
visits
|
|
20
47
|
end
|
|
@@ -43,6 +70,7 @@ module SuperSlim
|
|
|
43
70
|
end
|
|
44
71
|
|
|
45
72
|
CONFIG_FILE = ".super-slim.yml"
|
|
73
|
+
GLOB_OPTIONS = %w[include exclude].freeze
|
|
46
74
|
|
|
47
75
|
def self.load(directory = Dir.pwd)
|
|
48
76
|
new(configuration(directory).fetch("linters", {}), directory)
|
|
@@ -50,7 +78,26 @@ module SuperSlim
|
|
|
50
78
|
|
|
51
79
|
def self.configuration(directory = Dir.pwd)
|
|
52
80
|
path = File.join(directory, CONFIG_FILE)
|
|
53
|
-
File.exist?(path) ? YAML.safe_load_file(path) || {} : {}
|
|
81
|
+
configuration = File.exist?(path) ? YAML.safe_load_file(path) || {} : {}
|
|
82
|
+
check_configuration(configuration)
|
|
83
|
+
configuration
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A value of the wrong shape would fail later, inside a rule, with a NoMethodError naming none of this file.
|
|
87
|
+
def self.check_configuration(configuration)
|
|
88
|
+
raise Error, "#{CONFIG_FILE}: expected a mapping of settings" unless configuration.is_a?(Hash)
|
|
89
|
+
|
|
90
|
+
%w[linters format fmt].each do |key|
|
|
91
|
+
raise Error, "#{CONFIG_FILE}: `#{key}` must be a mapping" unless configuration.fetch(key, {}).is_a?(Hash)
|
|
92
|
+
end
|
|
93
|
+
configuration.fetch("linters", {}).each do |rule_name, options|
|
|
94
|
+
raise Error, "#{CONFIG_FILE}: `linters: #{rule_name}` must be a mapping of options" unless options.is_a?(Hash)
|
|
95
|
+
|
|
96
|
+
GLOB_OPTIONS.each do |key|
|
|
97
|
+
globs = Array(options[key])
|
|
98
|
+
raise Error, "#{CONFIG_FILE}: `#{rule_name}: #{key}` must be a list of globs" unless globs.all?(String)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
54
101
|
end
|
|
55
102
|
|
|
56
103
|
# include: and exclude: globs match paths relative to `root`, the directory of .super-slim.yml.
|
|
@@ -58,6 +105,7 @@ module SuperSlim
|
|
|
58
105
|
@root = File.join(File.expand_path(root), "")
|
|
59
106
|
@rules = Rules.all.filter_map do |rule_class|
|
|
60
107
|
options = rule_class::DEFAULTS.merge(options_by_rule_name.fetch(rule_class.rule_name, {}).transform_keys(&:to_sym))
|
|
108
|
+
options = with_preset(rule_class.rule_name, options) if options[:preset]
|
|
61
109
|
rule_class.new(options) if options[:enabled]
|
|
62
110
|
end
|
|
63
111
|
end
|
|
@@ -65,17 +113,31 @@ module SuperSlim
|
|
|
65
113
|
def call(path, source)
|
|
66
114
|
parser = Parser.new(file: path, outline: true)
|
|
67
115
|
nodes = parser.call(source)
|
|
68
|
-
visits = Document.visits_of(nodes)
|
|
116
|
+
visits = Document.visits_of(nodes, parser.line_by_node)
|
|
69
117
|
document = Document.new(path:, lines: source.split(/\r?\n/), nodes:, outline: parser.outline, line_by_node: parser.line_by_node,
|
|
70
118
|
visits:, tag_visits: visits.select { |node,| node.is_a?(AST::Tag) })
|
|
71
119
|
relative_path = File.expand_path(path).delete_prefix(@root)
|
|
72
120
|
offenses = @rules.select { _1.applies_to?(relative_path) }.flat_map { _1.offenses(document, source) }
|
|
73
121
|
# sort_by is not stable: the index keeps offenses of one line in rule order.
|
|
74
|
-
offenses.each_with_index.sort_by { |offense, index| [offense.lineno, index] }.map(&:first)
|
|
122
|
+
offenses.each_with_index.sort_by { |offense, index| [offense.lineno.to_i, index] }.map(&:first)
|
|
75
123
|
rescue SyntaxError => exception
|
|
76
124
|
[Offense.new(path, exception.lineno, "Syntax", exception.error)]
|
|
77
125
|
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
# The preset's table goes under the rule's own entries, where `~` drops one of the preset.
|
|
130
|
+
def with_preset(rule_name, options)
|
|
131
|
+
preset_options = PRESETS.dig(options[:preset], rule_name)
|
|
132
|
+
unless preset_options
|
|
133
|
+
presets = PRESETS.filter_map { |preset, options_by_rule| preset if options_by_rule.key?(rule_name) }
|
|
134
|
+
raise Error, "#{rule_name}: unknown preset `#{options[:preset]}`, use one of: #{presets.join(", ")}"
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
options.merge(preset_options.to_h { |key, entries| [key, entries.merge(Hash(options[key])).compact] })
|
|
138
|
+
end
|
|
78
139
|
end
|
|
79
140
|
end
|
|
80
141
|
|
|
142
|
+
require_relative "linter/presets"
|
|
81
143
|
require_relative "linter/rules"
|
data/lib/super_slim/parallel.rb
CHANGED
|
@@ -12,25 +12,45 @@ module SuperSlim
|
|
|
12
12
|
process_count = [Etc.nprocessors, items.size / minimum_per_process].min
|
|
13
13
|
return items.map(&block) if process_count < 2 || !Process.respond_to?(:fork)
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
workers = items.each_slice((items.size.to_f / process_count).ceil).map do |slice|
|
|
16
16
|
reader, writer = IO.pipe
|
|
17
|
-
fork do
|
|
17
|
+
pid = fork do
|
|
18
18
|
reader.close
|
|
19
|
-
|
|
20
|
-
slice.map(&block)
|
|
21
|
-
rescue Exception => exception # rubocop:disable Lint/RescueException -- re-raised by the parent
|
|
22
|
-
exception
|
|
23
|
-
end
|
|
24
|
-
writer.binmode.write(Marshal.dump(results))
|
|
19
|
+
writer.binmode.write(dump { slice.map(&block) })
|
|
25
20
|
exit!(0)
|
|
26
21
|
end
|
|
27
22
|
writer.close
|
|
28
|
-
reader
|
|
23
|
+
[pid, reader]
|
|
29
24
|
end
|
|
30
|
-
results_by_process =
|
|
31
|
-
Process.waitall
|
|
25
|
+
results_by_process = workers.map { |pid, reader| receive(pid, reader) }
|
|
32
26
|
results_by_process.each { |results| raise results if results.is_a?(Exception) }
|
|
33
27
|
results_by_process.flatten(1)
|
|
28
|
+
ensure
|
|
29
|
+
workers&.each do |pid, reader|
|
|
30
|
+
reader.close unless reader.closed?
|
|
31
|
+
Process.wait(pid)
|
|
32
|
+
rescue Errno::ECHILD # already reaped by receive
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# A result or an exception Marshal cannot dump (a Proc, an IO) is sent as a RuntimeError naming it.
|
|
37
|
+
def dump
|
|
38
|
+
Marshal.dump(yield)
|
|
39
|
+
rescue Exception => exception # rubocop:disable Lint/RescueException -- re-raised by the parent
|
|
40
|
+
begin
|
|
41
|
+
Marshal.dump(exception)
|
|
42
|
+
rescue TypeError
|
|
43
|
+
Marshal.dump(RuntimeError.new("#{exception.class}: #{exception.message}"))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def receive(pid, reader)
|
|
48
|
+
payload = reader.binmode.read
|
|
49
|
+
reader.close
|
|
50
|
+
Process.wait(pid)
|
|
51
|
+
raise Error, "Worker #{pid} died: #{$?}" unless $?.success? && !payload.empty?
|
|
52
|
+
|
|
53
|
+
Marshal.load(payload) # rubocop:disable Security/MarshalLoad -- written by our own child
|
|
34
54
|
end
|
|
35
55
|
end
|
|
36
56
|
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SuperSlim
|
|
4
|
+
class Parser
|
|
5
|
+
# Read by hand, a line at a time: Prism would cost every process that renders templates its load time and memory.
|
|
6
|
+
# Misread, all rare after a bracket that ends its line: heredocs, `?(`, `$'`, a regexp after a method name or a
|
|
7
|
+
# keyword (`when /'/`), `%` glued to a value (`"%d"%[a]`), interpolation in a %-literal and %-literals between
|
|
8
|
+
# other delimiters (`%|a(|`).
|
|
9
|
+
class RubyBrackets
|
|
10
|
+
PLAIN_RUBY = /[^()\[\]{}"'`#%\/]+/
|
|
11
|
+
QUOTED_TEXT_BY_DELIMITER = %w[" ` /].to_h { |delimiter| [delimiter, /(?:[^\\#{delimiter}#]|\\.|#(?!\{))*/m] }
|
|
12
|
+
.merge("'" => /(?:[^\\']|\\.)*/m).freeze
|
|
13
|
+
PERCENT_LITERAL_OPENING = /[qQwWiIrsx]?([(\[{<])/
|
|
14
|
+
BYTES_BEFORE_REGEXP = "\n(,[{|&=!~;:?<>+-*%^".bytes.freeze
|
|
15
|
+
LINE_END = /[ \t]*(?:\n|\z)/
|
|
16
|
+
LINE_END_OR_COMMENT = /[ \t]*(?:[#\n]|\z)/
|
|
17
|
+
|
|
18
|
+
# `%w[a) b(]` nests only its own kind of bracket.
|
|
19
|
+
PercentLiteral = Struct.new(:opening, :closing, :depth)
|
|
20
|
+
|
|
21
|
+
def initialize(code)
|
|
22
|
+
@scanner = StringScanner.new(code.dup)
|
|
23
|
+
# Whether each bracket still open ends its line.
|
|
24
|
+
@open_brackets = []
|
|
25
|
+
# The strings, regexps and %-literals the scanner is in, innermost last; an integer is an interpolation, the
|
|
26
|
+
# number of open brackets once its `#{` is counted.
|
|
27
|
+
@literals = []
|
|
28
|
+
read
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def <<(line)
|
|
32
|
+
@scanner << "\n" << line
|
|
33
|
+
read
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def open_at_line_end?
|
|
38
|
+
@open_brackets.any?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def read
|
|
44
|
+
until @scanner.eos?
|
|
45
|
+
case (literal = @literals.last)
|
|
46
|
+
when String then read_quoted(literal)
|
|
47
|
+
when PercentLiteral then read_percent_literal(literal)
|
|
48
|
+
else read_code(literal)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def read_code(interpolation_depth)
|
|
54
|
+
until @scanner.eos?
|
|
55
|
+
next if @scanner.skip(PLAIN_RUBY)
|
|
56
|
+
|
|
57
|
+
character = @scanner.getch
|
|
58
|
+
case character
|
|
59
|
+
when "(", "[", "{" then @open_brackets << @scanner.match?(LINE_END_OR_COMMENT)
|
|
60
|
+
when ")", "]", "}"
|
|
61
|
+
@open_brackets.pop
|
|
62
|
+
return @literals.pop if interpolation_depth && @open_brackets.size < interpolation_depth
|
|
63
|
+
when '"', "'", "`" then return @literals << character
|
|
64
|
+
when "/" then return @literals << character if regexp_start?
|
|
65
|
+
when "%" then return open_percent_literal if @scanner.scan(PERCENT_LITERAL_OPENING)
|
|
66
|
+
when "#" then @scanner.skip_until(LINE_END)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def read_quoted(delimiter)
|
|
72
|
+
until @scanner.eos?
|
|
73
|
+
@scanner.skip(QUOTED_TEXT_BY_DELIMITER[delimiter])
|
|
74
|
+
return @literals.pop if @scanner.skip(delimiter)
|
|
75
|
+
|
|
76
|
+
@scanner.getch
|
|
77
|
+
next unless @scanner.skip("{")
|
|
78
|
+
|
|
79
|
+
@open_brackets << @scanner.match?(LINE_END_OR_COMMENT)
|
|
80
|
+
return @literals << @open_brackets.size
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def regexp_start?
|
|
85
|
+
code = @scanner.string
|
|
86
|
+
offset = @scanner.pos - 2
|
|
87
|
+
offset -= 1 while offset >= 0 && (code.getbyte(offset) == SPACE_BYTE || code.getbyte(offset) == TAB_BYTE)
|
|
88
|
+
offset.negative? || BYTES_BEFORE_REGEXP.include?(code.getbyte(offset))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def open_percent_literal
|
|
92
|
+
opening = @scanner[1]
|
|
93
|
+
@open_brackets << @scanner.match?(LINE_END)
|
|
94
|
+
@literals << PercentLiteral.new(opening, DELIMITERS.fetch(opening, ">"), 1)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def read_percent_literal(literal)
|
|
98
|
+
until @scanner.eos?
|
|
99
|
+
character = @scanner.getch
|
|
100
|
+
if character == "\\"
|
|
101
|
+
@scanner.getch
|
|
102
|
+
elsif character == literal.opening
|
|
103
|
+
literal.depth += 1
|
|
104
|
+
elsif character == literal.closing
|
|
105
|
+
literal.depth -= 1
|
|
106
|
+
next unless literal.depth.zero?
|
|
107
|
+
|
|
108
|
+
@open_brackets.pop
|
|
109
|
+
return @literals.pop
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/lib/super_slim/parser.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "strscan"
|
|
4
|
+
require_relative "parser/ruby_brackets"
|
|
4
5
|
|
|
5
6
|
module SuperSlim
|
|
6
7
|
# Reads each line with one StringScanner that moves forward: no MatchData, and no copy of the rest of the
|
|
@@ -30,6 +31,8 @@ module SuperSlim
|
|
|
30
31
|
# At the scanner, the rest of the line is only this character; on a whole line, the line ends with it.
|
|
31
32
|
LINE_CONTINUATION = /[,\\]\z/
|
|
32
33
|
BACKSLASH_AT_END = /\\\z/
|
|
34
|
+
OPENING_BRACKET_AT_LINE_END = /[(\[{]\z/
|
|
35
|
+
MAX_NESTING = 500
|
|
33
36
|
WHITESPACE_MARKERS = /[<>']*/
|
|
34
37
|
TEXT_LINE_INDICATOR = /([|'])([<>]{1,2}(?: |\z)| ?)/
|
|
35
38
|
HTML_COMMENT = /\/!( ?)/
|
|
@@ -92,9 +95,16 @@ module SuperSlim
|
|
|
92
95
|
source.force_encoding(Encoding::UTF_8)
|
|
93
96
|
source.force_encoding(original_encoding) unless source.valid_encoding?
|
|
94
97
|
end
|
|
98
|
+
invalid_encoding!(source) unless source.encoding == Encoding::UTF_8 && source.valid_encoding?
|
|
95
99
|
source.delete_prefix("")
|
|
96
100
|
end
|
|
97
101
|
|
|
102
|
+
def invalid_encoding!(source)
|
|
103
|
+
lines = source.b.split("\n")
|
|
104
|
+
lineno = lines.index { |line| !line.dup.force_encoding(Encoding::UTF_8).valid_encoding? } || 0
|
|
105
|
+
raise SyntaxError.new("Invalid UTF-8", @file, lines[lineno].to_s.force_encoding(Encoding::UTF_8).scrub, lineno + 1, 0)
|
|
106
|
+
end
|
|
107
|
+
|
|
98
108
|
def line_starts(source, byte_order_mark_size)
|
|
99
109
|
line_starts = [nil, byte_order_mark_size]
|
|
100
110
|
source.each_line { |line| line_starts << line_starts.last + line.size }
|
|
@@ -127,12 +137,37 @@ module SuperSlim
|
|
|
127
137
|
|
|
128
138
|
# Characters of the line read so far, even when the scanner reads a stripped copy or attribute source.
|
|
129
139
|
def column
|
|
130
|
-
|
|
140
|
+
charpos = scanner_charpos
|
|
141
|
+
line_size - (@scanner_string_size - charpos)
|
|
131
142
|
end
|
|
132
143
|
|
|
133
144
|
# Where the scanner stands in the source, unlike `column` exact on a stripped line or on attribute source.
|
|
134
145
|
def source_offset
|
|
135
|
-
@line_starts[@lineno] + @scanner_column +
|
|
146
|
+
@line_starts[@lineno] + @scanner_column + scanner_charpos
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# StringScanner#charpos and String#size count from the start of the string at each call, and the outline asks
|
|
150
|
+
# for them at each attribute: counted on from where the last call left off, they stay linear in the line.
|
|
151
|
+
def scanner_charpos
|
|
152
|
+
string = @scanner.string
|
|
153
|
+
position = @scanner.pos
|
|
154
|
+
unless string.equal?(@charpos_string) && position >= @charpos_byte
|
|
155
|
+
@charpos_string = string
|
|
156
|
+
@charpos_byte = @charpos_char = 0
|
|
157
|
+
@scanner_string_size = string.size
|
|
158
|
+
@scanner_string_ascii = string.ascii_only?
|
|
159
|
+
end
|
|
160
|
+
return position if @scanner_string_ascii
|
|
161
|
+
|
|
162
|
+
@charpos_char += string.byteslice(@charpos_byte, position - @charpos_byte).size
|
|
163
|
+
@charpos_byte = position
|
|
164
|
+
@charpos_char
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def line_size
|
|
168
|
+
@line_size = @line.size unless @line.equal?(@sized_line)
|
|
169
|
+
@sized_line = @line
|
|
170
|
+
@line_size
|
|
136
171
|
end
|
|
137
172
|
|
|
138
173
|
def line_end_offset
|
|
@@ -226,7 +261,7 @@ module SuperSlim
|
|
|
226
261
|
record_ruby(:text, source_offset, line_end_offset) if @outline
|
|
227
262
|
children = []
|
|
228
263
|
@stacks.last << located(InlineHtml.new(@scanner.rest, children))
|
|
229
|
-
|
|
264
|
+
nest(children)
|
|
230
265
|
end
|
|
231
266
|
|
|
232
267
|
def parse_control
|
|
@@ -234,7 +269,7 @@ module SuperSlim
|
|
|
234
269
|
@scanner.pos += 1
|
|
235
270
|
children = []
|
|
236
271
|
@stacks.last << located(Control.new(parse_broken_line, children), lineno)
|
|
237
|
-
|
|
272
|
+
nest(children)
|
|
238
273
|
end
|
|
239
274
|
|
|
240
275
|
def parse_output
|
|
@@ -253,7 +288,7 @@ module SuperSlim
|
|
|
253
288
|
lineno = @lineno
|
|
254
289
|
@stacks.last << located(Output.new(parse_broken_line, escape, children), lineno)
|
|
255
290
|
@stacks.last << Static.new(" ") if trailing_whitespace
|
|
256
|
-
|
|
291
|
+
nest(children)
|
|
257
292
|
end
|
|
258
293
|
|
|
259
294
|
def parse_tag_line
|
|
@@ -359,9 +394,17 @@ module SuperSlim
|
|
|
359
394
|
first = source_offset + @scanner.rest[/\A\s*/].size if @outline
|
|
360
395
|
broken_line = @scanner.rest
|
|
361
396
|
broken_line.strip!
|
|
362
|
-
|
|
397
|
+
brackets = RubyBrackets.new(broken_line) if broken_line.match?(OPENING_BRACKET_AT_LINE_END)
|
|
398
|
+
line = broken_line
|
|
399
|
+
while line.match?(LINE_CONTINUATION) || brackets&.open_at_line_end?
|
|
363
400
|
expect_next_line
|
|
364
|
-
|
|
401
|
+
line = @scanner.string
|
|
402
|
+
broken_line << "\n" << line
|
|
403
|
+
if brackets
|
|
404
|
+
brackets << line
|
|
405
|
+
elsif line.match?(OPENING_BRACKET_AT_LINE_END)
|
|
406
|
+
brackets = RubyBrackets.new(broken_line)
|
|
407
|
+
end
|
|
365
408
|
end
|
|
366
409
|
record_ruby(:code, first, line_end_offset) if @outline
|
|
367
410
|
broken_line
|
|
@@ -403,18 +446,19 @@ module SuperSlim
|
|
|
403
446
|
siblings.insert(-2, Static.new(" ")) if output_leading_whitespace && !leading_whitespace
|
|
404
447
|
siblings << Static.new(" ") if output_trailing_whitespace && !trailing_whitespace
|
|
405
448
|
end
|
|
406
|
-
|
|
449
|
+
nest(block) if block
|
|
407
450
|
end
|
|
408
451
|
|
|
409
452
|
# Returns the tag content, the block expecting indented children, and the whitespace an `=<>` output adds.
|
|
410
453
|
def parse_tag_content
|
|
411
|
-
|
|
454
|
+
# Not `column`, which counts the spaces stripped from the end of a continuation line.
|
|
455
|
+
output_column = @scanner_column + scanner_charpos if @outline
|
|
412
456
|
if @scanner.skip(NESTED_TAG)
|
|
413
457
|
return [[parse_embedded(@scanner[1], @scanner[2], @scanner.rest)]] if @scanner.skip(ENGINE)
|
|
414
458
|
|
|
415
459
|
(tag = scan_tag) || syntax_error!("Expected tag")
|
|
416
460
|
content = []
|
|
417
|
-
|
|
461
|
+
nest(content)
|
|
418
462
|
stack_index = @stacks.size - 1
|
|
419
463
|
parse_tag(tag)
|
|
420
464
|
@stacks.delete_at(stack_index)
|
|
@@ -547,6 +591,12 @@ module SuperSlim
|
|
|
547
591
|
value
|
|
548
592
|
end
|
|
549
593
|
|
|
594
|
+
# The compiler, the linter and Ruby's own parser of the compiled code walk the tree recursively.
|
|
595
|
+
def nest(children)
|
|
596
|
+
syntax_error!("Tags nested more than #{MAX_NESTING} levels deep") if @stacks.size > MAX_NESTING
|
|
597
|
+
@stacks << children
|
|
598
|
+
end
|
|
599
|
+
|
|
550
600
|
def syntax_error!(message)
|
|
551
601
|
error = SyntaxError.new(message, @file, @line, @lineno, (@line && @scanner) ? column : 0)
|
|
552
602
|
error.set_backtrace(["#{@file}:#{@lineno}", *caller])
|
data/lib/super_slim/rubocop.rb
CHANGED
|
@@ -34,7 +34,7 @@ module SuperSlim
|
|
|
34
34
|
TRAILING_DO = /(?:\b[ \t]*|[ \t])do[ \t]*(\|[^|]*\|)?[ \t]*(#.*)?\z/
|
|
35
35
|
WHEN = /\A\s*when[ \t]/
|
|
36
36
|
THEN = /[ \t]then(?:[ \t].*)?\z/m
|
|
37
|
-
|
|
37
|
+
INTERPOLATION_START = /#\{/
|
|
38
38
|
ESCAPED_INTERPOLATION = /\\#\{/
|
|
39
39
|
UNESCAPED_INTERPOLATION_CODE = /\A\{.*\}\z/m
|
|
40
40
|
TEXT_WITHOUT_INTERPOLATION = /[^#\\]+|./m
|
|
@@ -63,8 +63,8 @@ module SuperSlim
|
|
|
63
63
|
parser.call(@source)
|
|
64
64
|
parser.ruby_ranges.flat_map do |kind, first, last|
|
|
65
65
|
# From the start of its first line, so that the indentation of a `ruby:` block is the same on every line.
|
|
66
|
-
first = (@source.
|
|
67
|
-
fragment = Fragment.new(@source
|
|
66
|
+
first = character_offset((@source.byterindex("\n", byte_offset(first) - 1) || -1) + 1) if kind == :embedded_ruby
|
|
67
|
+
fragment = Fragment.new(@source.byteslice(byte_offset(first)...byte_offset(last)), first)
|
|
68
68
|
case kind
|
|
69
69
|
when :text then interpolations(fragment)
|
|
70
70
|
when :code then without_block_syntax(fragment)
|
|
@@ -75,19 +75,41 @@ module SuperSlim
|
|
|
75
75
|
[]
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
# The ranges count characters, and slicing by characters counts them again from the start of the template
|
|
79
|
+
# at each slice: on a template that isn't ASCII, each character's byte offset is worked out once.
|
|
80
|
+
def byte_offset(character_offset)
|
|
81
|
+
byte_offsets ? byte_offsets[character_offset] : character_offset
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def character_offset(byte_offset)
|
|
85
|
+
byte_offsets ? byte_offsets.bsearch_index { _1 >= byte_offset } : byte_offset
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def byte_offsets
|
|
89
|
+
return if @source.ascii_only?
|
|
90
|
+
|
|
91
|
+
@byte_offsets ||= begin
|
|
92
|
+
offsets = [0]
|
|
93
|
+
@source.each_char { |character| offsets << offsets.last + character.bytesize }
|
|
94
|
+
offsets
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
78
98
|
def tailwind_classes?
|
|
79
99
|
configuration = Linter.configuration
|
|
80
100
|
configuration.key?("tailwind_classes") ? configuration["tailwind_classes"] : SuperSlim.tailwind_classes
|
|
81
101
|
end
|
|
82
102
|
|
|
83
103
|
def interpolations(text)
|
|
104
|
+
closing_by_opening = Compiler.closing_braces(text.code)
|
|
84
105
|
scanner = StringScanner.new(text.code)
|
|
85
106
|
fragments = []
|
|
86
107
|
until scanner.eos?
|
|
87
108
|
next if scanner.skip(ESCAPED_INTERPOLATION)
|
|
88
109
|
|
|
89
|
-
if scanner.
|
|
90
|
-
code = scanner
|
|
110
|
+
if scanner.match?(INTERPOLATION_START) && (closing = closing_by_opening[scanner.pos + 1])
|
|
111
|
+
code = text.code.byteslice(scanner.pos + 2...closing)
|
|
112
|
+
scanner.pos = closing + 1
|
|
91
113
|
offset = text.offset + scanner.charpos - code.size - 1
|
|
92
114
|
fragments << (code.match?(UNESCAPED_INTERPOLATION_CODE) ? Fragment.new(code[1..-2], offset + 1) : Fragment.new(code, offset))
|
|
93
115
|
else
|
data/lib/super_slim/runtime.rb
CHANGED
|
@@ -33,8 +33,13 @@ module SuperSlim
|
|
|
33
33
|
|
|
34
34
|
# Attributes only known at render time: splats (`*hash`), `data=`/`aria=` hashes and dynamic tags (`*{tag: "a"}`).
|
|
35
35
|
class Attributes
|
|
36
|
-
|
|
36
|
+
# Slim lets tabs and line breaks through: they end a name, so `data: {"x\tonclick" => code}` set onclick.
|
|
37
|
+
INVALID_NAME = /[[:cntrl:] "'>\/=]/
|
|
38
|
+
# Read after escaping, which turns markup into entities: `&` is refused with the rest.
|
|
39
|
+
VALID_TAG = /\A[a-zA-Z][^[:cntrl:] "'&<>\/=]*\z/
|
|
37
40
|
HYPHENATED = %w[data aria].freeze
|
|
41
|
+
# Far deeper than any page needs, and short of the stack a hash referring to itself would exhaust.
|
|
42
|
+
MAX_HYPHENATED_DEPTH = 100
|
|
38
43
|
|
|
39
44
|
def initialize(html_safe)
|
|
40
45
|
@html_safe = html_safe
|
|
@@ -57,7 +62,7 @@ module SuperSlim
|
|
|
57
62
|
end
|
|
58
63
|
|
|
59
64
|
def attr(name, value)
|
|
60
|
-
raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if name.match?(INVALID_NAME)
|
|
65
|
+
raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if name.empty? || name.match?(INVALID_NAME)
|
|
61
66
|
|
|
62
67
|
if !@value_by_name.key?(name)
|
|
63
68
|
@value_by_name[name] = value
|
|
@@ -71,6 +76,7 @@ module SuperSlim
|
|
|
71
76
|
def build_tag(content = nil)
|
|
72
77
|
tag = @value_by_name.delete("tag").to_s
|
|
73
78
|
tag = "div" if tag.empty?
|
|
79
|
+
raise InvalidTagNameError, "Invalid tag name '#{tag}' was rendered" unless tag.match?(VALID_TAG)
|
|
74
80
|
content ? "<#{tag}#{self}>#{content}</#{tag}>" : "<#{tag}#{self} />"
|
|
75
81
|
end
|
|
76
82
|
|
|
@@ -85,9 +91,11 @@ module SuperSlim
|
|
|
85
91
|
|
|
86
92
|
private
|
|
87
93
|
|
|
88
|
-
def hyphenated_attr(name, escape, value)
|
|
94
|
+
def hyphenated_attr(name, escape, value, depth = 0)
|
|
89
95
|
if value.is_a?(Hash)
|
|
90
|
-
|
|
96
|
+
raise Error, "A data or aria hash is nested more than #{MAX_HYPHENATED_DEPTH} levels deep" if depth > MAX_HYPHENATED_DEPTH
|
|
97
|
+
|
|
98
|
+
value.each { |key, nested_value| hyphenated_attr("#{name}-#{key}", escape, nested_value, depth + 1) }
|
|
91
99
|
else
|
|
92
100
|
attr(name, escape(escape, value))
|
|
93
101
|
end
|
data/lib/super_slim/version.rb
CHANGED
data/lib/super_slim.rb
CHANGED
|
@@ -13,6 +13,7 @@ module SuperSlim
|
|
|
13
13
|
|
|
14
14
|
class Error < StandardError; end
|
|
15
15
|
class InvalidAttributeNameError < Error; end
|
|
16
|
+
class InvalidTagNameError < Error; end
|
|
16
17
|
class UnsafeFormattingError < Error; end
|
|
17
18
|
|
|
18
19
|
class SyntaxError < Error
|
|
@@ -34,7 +35,7 @@ module SuperSlim
|
|
|
34
35
|
#{error}
|
|
35
36
|
#{file}, Line #{lineno}, Column #{@column}
|
|
36
37
|
#{line}
|
|
37
|
-
#{" " * column}^
|
|
38
|
+
#{" " * [column, 0].max}^
|
|
38
39
|
MESSAGE
|
|
39
40
|
end
|
|
40
41
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: super-slim
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Level UP Solutions
|
|
@@ -46,9 +46,11 @@ files:
|
|
|
46
46
|
- lib/super_slim/formatter.rb
|
|
47
47
|
- lib/super_slim/formatter/line_wrapper.rb
|
|
48
48
|
- lib/super_slim/linter.rb
|
|
49
|
+
- lib/super_slim/linter/presets.rb
|
|
49
50
|
- lib/super_slim/linter/rules.rb
|
|
50
51
|
- lib/super_slim/parallel.rb
|
|
51
52
|
- lib/super_slim/parser.rb
|
|
53
|
+
- lib/super_slim/parser/ruby_brackets.rb
|
|
52
54
|
- lib/super_slim/rails_handler.rb
|
|
53
55
|
- lib/super_slim/railtie.rb
|
|
54
56
|
- lib/super_slim/rubocop.rb
|
|
@@ -79,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
79
81
|
- !ruby/object:Gem::Version
|
|
80
82
|
version: '0'
|
|
81
83
|
requirements: []
|
|
82
|
-
rubygems_version: 4.0.
|
|
84
|
+
rubygems_version: 4.0.20
|
|
83
85
|
specification_version: 4
|
|
84
86
|
summary: 'Slim templates, rewritten: faster to compile and render, with a formatter
|
|
85
87
|
and a linter'
|