erb_lint 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/erb_lint/linters/hard_coded_string.rb +62 -0
- data/lib/erb_lint/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ae1dbd574b00046985466aacce7a126e9a61b34
|
4
|
+
data.tar.gz: 1649ae3b899e23ee9c02f5f40dfd6a9db02e2008
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6571d993f2acc94689f7711adb041dbca684b696f22548d85ed5af44a530143adf0acca04de2f19b46313223fbdeb8a3d539ac75827fd07d010c9280ef809d1
|
7
|
+
data.tar.gz: 1bd62fde864706db8442b6f03290ab71dcf4e94571aee526c425781469f5010f4d78ce8091cae3264f3a4d8927d5f4584f3d75c5770e056458f3f55decc4a69a
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'better_html/tree/tag'
|
4
|
+
|
5
|
+
module ERBLint
|
6
|
+
module Linters
|
7
|
+
# Checks for hardcoded strings. Useful if you want to ensure a string can be translated using i18n.
|
8
|
+
class HardCodedString < Linter
|
9
|
+
include LinterRegistry
|
10
|
+
|
11
|
+
def offenses(processed_source)
|
12
|
+
hardcoded_strings = processed_source.ast.descendants(:text).each_with_object([]) do |text_node, to_check|
|
13
|
+
next if javascript?(processed_source, text_node)
|
14
|
+
|
15
|
+
offended_str = text_node.to_a.find { |node| relevant_node(node) }
|
16
|
+
to_check << [text_node, offended_str] if offended_str
|
17
|
+
end
|
18
|
+
|
19
|
+
hardcoded_strings.compact.map do |text_node, offended_str|
|
20
|
+
Offense.new(
|
21
|
+
self,
|
22
|
+
processed_source.to_source_range(text_node.loc.start, text_node.loc.stop),
|
23
|
+
message(offended_str)
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def javascript?(processed_source, text_node)
|
31
|
+
ast = processed_source.parser.ast.to_a
|
32
|
+
index = ast.find_index(text_node)
|
33
|
+
|
34
|
+
previous_node = ast[index - 1]
|
35
|
+
|
36
|
+
if previous_node.type == :tag
|
37
|
+
tag = BetterHtml::Tree::Tag.from_node(previous_node)
|
38
|
+
|
39
|
+
tag.name == "script" && !tag.closing?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def relevant_node(inner_node)
|
44
|
+
if inner_node.is_a?(String)
|
45
|
+
inner_node.strip.empty? ? false : inner_node
|
46
|
+
else
|
47
|
+
false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def message(string)
|
52
|
+
stripped_string = string.strip
|
53
|
+
|
54
|
+
if stripped_string.length > 1
|
55
|
+
"String not translated: #{stripped_string}"
|
56
|
+
else
|
57
|
+
"Consider using Rails helpers to move out the single character `#{stripped_string}` from the html."
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/erb_lint/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb_lint
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Chan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: better_html
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- lib/erb_lint/linters/deprecated_classes.rb
|
142
142
|
- lib/erb_lint/linters/erb_safety.rb
|
143
143
|
- lib/erb_lint/linters/final_newline.rb
|
144
|
+
- lib/erb_lint/linters/hard_coded_string.rb
|
144
145
|
- lib/erb_lint/linters/rubocop.rb
|
145
146
|
- lib/erb_lint/linters/rubocop_text.rb
|
146
147
|
- lib/erb_lint/offense.rb
|