haml_i18n_lint 0.8.0 → 0.9.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: 5baf9d78c58e54ef28dc02d47853a0ad5cc47e48
4
- data.tar.gz: 663f20d241cc959ac781f1128d6c63fe430e8fea
3
+ metadata.gz: 39727a8149db51ef830a77e981f889ffafe595e4
4
+ data.tar.gz: ca228553e73a66d68b0e73ac2dee226f754fa7e8
5
5
  SHA512:
6
- metadata.gz: 0cc2d75ab100f45eea52c2f74af8d72d2924259d552a3b559f9adb13c67215d29c622d82d8723e427caaa34fb25ebf8886872e6766490d0e4ea1c945a9e0bbb3
7
- data.tar.gz: 0cd123a39fee2444e314af5d651b5cbaedc7c99f147486d01a0e49f7651273287e8d5f1629ed74679c66a803d5f83103244813951c8617f760b8b76cdc7be596
6
+ metadata.gz: e4ed255dac1812a7dfaca0e92f15f688ae389028b4470462e173efc4609732dc774d3d36022df107a604117f5a2be67d1ae6ea9caa235149f25eef392403409d
7
+ data.tar.gz: 8b18fd3d44b73d9499625179d6dd29a7dc87bf7e538f82e361b377374579c7e7a2bac5a36f2e3d684bca4e326178caba9a002a80c07e7a1b408ab4985ce4560e
data/README.md CHANGED
@@ -33,36 +33,7 @@ Or install it yourself as:
33
33
  -c, --config=FILE configuration file
34
34
  -f, --files=PATTERN pattern to find Haml template files, default: -f '**/*.haml'
35
35
 
36
- The configuration file sample:
37
-
38
- # You can override Config#need_i18n? that returns the content in Haml template need i18n or not.
39
- def need_i18n?(content)
40
- # the default behaviours is ignore white spaces and digits
41
- /^[\s]+$/ !~ content && /\p{Alpha}/ =~ content
42
- end
43
-
44
- # You can override Config#report in configuration file
45
- # to customize output format or send result to other location.
46
- #
47
- # The default output format is like following:
48
- #
49
- # $ haml_i18n_lint
50
- # test/fixtures/hi.html.haml:4
51
- # 3: %head
52
- # 4: %title Hi
53
- # 5: %body
54
- #
55
- # For example, to use short format:
56
- def report(result)
57
- result.matched_nodes.each do |node|
58
- puts "#{result.filename}:#{node.line}"
59
- end
60
- end
61
-
62
- # You can override Config#files for complex file pattern.
63
- def files
64
- Dir['**/*.haml'].reject { |path| path.start_with?('app/assets/') || path.start_with?('node_modules') }
65
- end
36
+ The configuration file examples are in the `examples` directory.
66
37
 
67
38
  ## Development
68
39
 
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+
3
+ def report(result_set)
4
+ t = {}
5
+ result_set.each_with_object(t['en'] = {}) do |result, t|
6
+ keys = result.filename.gsub(%r{^app/views/}, '').gsub(/(\.html)?\.haml$/, '').split('/')
7
+ t = keys.inject(t) { |t, key| t[key] ||= {} }
8
+ key = "L#{result.node.line}"
9
+ unless t.key?(key)
10
+ t[key] = result.text
11
+ next
12
+ end
13
+ key << "-1"
14
+ key.next! while t.key?(key)
15
+ t[key] = result.text
16
+ end
17
+ puts t.to_yaml
18
+ end
data/examples/react.rb ADDED
@@ -0,0 +1,7 @@
1
+ def ignore_methods
2
+ super + %w(react_component)
3
+ end
4
+
5
+ def ignore_keys
6
+ super + %w(tag)
7
+ end
@@ -0,0 +1,28 @@
1
+ # You can override Config#need_i18n? that returns the content in Haml template need i18n or not.
2
+ def need_i18n?(content)
3
+ # the default behaviours is ignore white spaces and digits
4
+ /^[\s]+$/ !~ content && /\p{Alpha}/ =~ content
5
+ end
6
+
7
+ # You can override Config#report in configuration file
8
+ # to customize output format or send result to other location.
9
+ #
10
+ # The default output format is like following:
11
+ #
12
+ # $ haml_i18n_lint
13
+ # test/fixtures/hi.html.haml:4
14
+ # 3: %head
15
+ # 4: %title Hi
16
+ # 5: %body
17
+ #
18
+ # For example, to use short format:
19
+ def report(result)
20
+ result.each do |r|
21
+ puts "%30s\t%s" % ["#{r.filename}:#{r.node.line}", r.text]
22
+ end
23
+ end
24
+
25
+ # You can override Config#files for complex file pattern.
26
+ def files
27
+ Dir['**/*.haml'].reject { |path| path.start_with?('app/assets/') || path.start_with?('node_modules') }
28
+ end
@@ -0,0 +1,9 @@
1
+ result_count = 0
2
+ define_method(:report) do |result_set|
3
+ result_count += result_set.count
4
+ super(result_set)
5
+ end
6
+
7
+ at_exit do
8
+ puts "Total: #{result_count}"
9
+ end
@@ -8,8 +8,6 @@ Gem::Specification.new do |spec|
8
8
  spec.version = HamlI18nLint::VERSION
9
9
  spec.authors = ["Seiei Miyagi"]
10
10
  spec.email = ["hanachin@gmail.com"]
11
- spec.cert_chain = ["certs/hanachin.pem"]
12
- spec.signing_key = File.expand_path("~/.ssh/gem-private_key.pem")
13
11
 
14
12
  spec.summary = "find out not translated yet plain text from your Haml template"
15
13
  spec.homepage = "https://github.com/okinawarb/haml_i18n_lint"
@@ -19,13 +19,15 @@ module HamlI18nLint
19
19
 
20
20
  # Output the formatted result
21
21
  #
22
- # @param result [Linter::Result] the lint result
23
- def report(result)
24
- print '.' and return if result.success?
22
+ # @param result [Linter::ResultSet] the lint result
23
+ def report(result_set)
24
+ print '.' and return if result_set.success?
25
25
 
26
26
  puts
27
- file = File.readlines(result.filename)
28
- result.matched_nodes.each do |node|
27
+ files = Hash.new { |h, k| h[k] = File.readlines(k) }
28
+ result_set.each do |result|
29
+ file = files[result.filename]
30
+ node = result.node
29
31
  puts "#{result.filename}:#{node.line}"
30
32
  puts "#{node.line-1}: #{file[node.line - 2]}" if file[node.line - 2] && !(node.line - 2).negative?
31
33
  puts "#{node.line}: #{file[node.line - 1]}"
@@ -4,36 +4,41 @@ module HamlI18nLint
4
4
 
5
5
  def compile_script
6
6
  super
7
- lint_add_matched_node(@node) if script_need_i18n?(@node.value[:text])
7
+ lint_script(@node.value[:text])
8
8
  end
9
9
 
10
10
  def compile_plain
11
11
  super
12
- lint_add_matched_node(@node) if lint_config.need_i18n?(@node.value[:text])
12
+ text = @node.value[:text]
13
+ lint_add(text) if lint_config.need_i18n?(text)
13
14
  end
14
15
 
15
16
  def lint_attributes_hashes
16
17
  @node.value[:attributes_hashes]
17
18
  end
18
19
 
19
- def lint_attribute_need_i18n?
20
- attributes_hashes = lint_attributes_hashes
21
- attributes_hashes.any? do |attributes_hash|
22
- script_need_i18n?("{#{attributes_hash}}")
20
+ def lint_attributes
21
+ lint_attributes_hashes.any? do |attributes_hash|
22
+ lint_script("{#{attributes_hash}}")
23
23
  end
24
24
  end
25
25
 
26
26
  def compile_tag
27
27
  super
28
28
  if @node.value[:parse]
29
- lint_add_matched_node(@node) if script_need_i18n?(@node.value[:value])
30
-
29
+ lint_script(@node.value[:value])
31
30
  else
32
- lint_add_matched_node(@node) if lint_config.need_i18n?(@node.value[:value])
31
+ value = @node.value[:value]
32
+ lint_add(value) if lint_config.need_i18n?(value)
33
33
  end
34
- lint_add_matched_node(@node) if lint_config.need_i18n?(@node.value.dig(:attributes, 'placeholder') || "")
35
- lint_add_matched_node(@node) if lint_config.need_i18n?(@node.value.dig(:attributes, 'value') || "")
36
- lint_add_matched_node(@node) if lint_attribute_need_i18n?
34
+
35
+ placeholder = @node.value.dig(:attributes, 'placeholder') || ""
36
+ lint_add(placeholder) if lint_config.need_i18n?(placeholder)
37
+
38
+ value = @node.value.dig(:attributes, 'value') || ""
39
+ lint_add(value) if lint_config.need_i18n?(value)
40
+
41
+ lint_attributes
37
42
  end
38
43
 
39
44
  private
@@ -101,7 +106,7 @@ module HamlI18nLint
101
106
  lint_command?(sexp, m) || lint_fcall?(sexp, m) || lint_call?(sexp, m)
102
107
  end
103
108
 
104
- def script_need_i18n?(script)
109
+ def lint_script(script)
105
110
  script = script.dup
106
111
  if Ripper.lex(script.rstrip).any? { |(_, on_kw, kw_do)| on_kw == :on_kw && kw_do == "do" }
107
112
  script << "\nend\n"
@@ -127,7 +132,7 @@ module HamlI18nLint
127
132
 
128
133
  walk.(sexp)
129
134
 
130
- string_literal_found
135
+ lint_add(script) if string_literal_found
131
136
  end
132
137
 
133
138
  end
@@ -6,17 +6,42 @@ module HamlI18nLint
6
6
  # Raised if failed to parse the attributes hash
7
7
  class AttributesParseError < StandardError; end
8
8
 
9
- # The lint result of the file
10
- class Result < Struct.new(:filename, :matched_nodes)
11
- # @!attribute [r] matched_nodes
12
- # @return [Array<Haml::Parser::ParseNode>] the nodes that needs i18n.
13
-
9
+ # The lint result
10
+ class Result < Struct.new(:filename, :node, :text)
14
11
  # @!attribute [r] filename
15
12
  # @return [String] name of the linted file
16
13
 
14
+ # @!attribute [r] node
15
+ # @return [Haml::Parser::ParseNode] the node that needs i18n.
16
+
17
+ # @!attribute [r] text
18
+ # @return [String] the text that needs i18n.
19
+ end
20
+
21
+ # The lint results
22
+ class ResultSet
23
+ include Enumerable
24
+
25
+ # @param filename [String]
26
+ def initialize
27
+ @results = []
28
+ end
29
+
30
+ # @param result [Result] the result
31
+ def add_result(result)
32
+ @results << result
33
+ end
34
+
35
+ # @yield [result] Gives each result to a block.
36
+ def each
37
+ @results.each do |r|
38
+ yield r
39
+ end
40
+ end
41
+
17
42
  # @return [true, false] passed lint or not.
18
43
  def success?
19
- matched_nodes.empty?
44
+ count.zero?
20
45
  end
21
46
  end
22
47
 
@@ -30,7 +55,7 @@ module HamlI18nLint
30
55
  #
31
56
  # @param filename [String] the filename
32
57
  # @param template [String] the Haml template
33
- # @return [Linter::Result] the result of lint
58
+ # @return [Linter::ResultSet] the result of lint
34
59
  # @raise [Linter::AttributesParseError] if failed to parse attributes hash in the template.
35
60
  def lint(filename:, template:)
36
61
  haml_options = ::Haml::Options.new
@@ -39,7 +64,6 @@ module HamlI18nLint
39
64
  compiler(haml_options).compile(node)
40
65
  end
41
66
 
42
-
43
67
  private
44
68
 
45
69
  def parse(haml_options, template)
@@ -52,7 +76,7 @@ module HamlI18nLint
52
76
 
53
77
  def compiler(haml_options)
54
78
  config = @config
55
- result = Result.new(haml_options[:filename], [])
79
+ result_set = ResultSet.new
56
80
 
57
81
  ext = compiler_extension
58
82
  compiler_result_extension = Module.new do
@@ -60,7 +84,7 @@ module HamlI18nLint
60
84
 
61
85
  define_method(:compile) do |node|
62
86
  super(node)
63
- result
87
+ result_set
64
88
  end
65
89
 
66
90
  private
@@ -69,8 +93,8 @@ module HamlI18nLint
69
93
  config
70
94
  end
71
95
 
72
- define_method(:lint_add_matched_node) do |node|
73
- result.matched_nodes << node
96
+ define_method(:lint_add) do |text|
97
+ result_set.add_result(Result.new(haml_options[:filename], @node, text))
74
98
  end
75
99
  end
76
100
 
@@ -1,3 +1,3 @@
1
1
  module HamlI18nLint
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,36 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml_i18n_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Miyagi
8
8
  autorequire:
9
9
  bindir: exe
10
- cert_chain:
11
- - |
12
- -----BEGIN CERTIFICATE-----
13
- MIIDcDCCAligAwIBAgIBATANBgkqhkiG9w0BAQUFADA/MREwDwYDVQQDDAhoYW5h
14
- Y2hpbjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
15
- MB4XDTE3MDUwNDE0NDYxOVoXDTE4MDUwNDE0NDYxOVowPzERMA8GA1UEAwwIaGFu
16
- YWNoaW4xFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkWA2Nv
17
- bTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAN03A8Jvk7k0aVLmIHrj
18
- soZLOppADifbdH+FoXUfWXFeu9x/hq8UsuMyTuSF6oNZRihzk7yakKBcv8B44wc2
19
- Kfui97h+UOqBib5oPZOrJjW34dSfnHgdsPskZRK1yvMW0X88q7K+9iBT78Xpkf40
20
- XkDh7mEyA0sC25n8BBTg3HpPMNXtQazR0UrtSH/Uyu2t7Sy4QQVKFYfVdfITfMoG
21
- i7X/2cXs0ao6dLOK8H1lodxZ+2Dc5UQCrerVXKvVjAgZhJIN7qgbpAfuc+KFpGhq
22
- pvkFaoOJ/NCg54DDiJYhZMm2X3NtjRRE3Ujt4bwO6vVlr5aPU2/vPCvsiyF1PmO+
23
- a+UCAwEAAaN3MHUwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFMug
24
- kMbzYl73rLFxRlmgKeNAiSZjMB0GA1UdEQQWMBSBEmhhbmFjaGluQGdtYWlsLmNv
25
- bTAdBgNVHRIEFjAUgRJoYW5hY2hpbkBnbWFpbC5jb20wDQYJKoZIhvcNAQEFBQAD
26
- ggEBAFBV/OUPBF6DhEe/Q1bojFYwRdLI+JbDAoH97GRuBzKokbW7a4k5EGMw4aLx
27
- RXH75W+vmsG1z/RE7lpD+T7Uf+ZuGwCIhFsGiZBcbBbinz4MEsqjGwu2/OPOgbYK
28
- alk+o48ier71CaSsfz83hSAzklJ7g6BocJCWROXuVzX9eCw7YB3F4xNzdw8HxHkA
29
- WbyMQMURxOX5Em9t+EgSU9Odx0tJgnhygUSdTJknavnpaZUa2odWS4+wagh8nXxS
30
- +zDzgwp9Z4A8i47ioz1YEGIkQhDKZeGQznwkht0zsrtswEAiOisL5uJDtWvQiwt6
31
- a9nBgrpUm8NHrucdUDtMYjixgmU=
32
- -----END CERTIFICATE-----
33
- date: 2017-05-06 00:00:00.000000000 Z
10
+ cert_chain: []
11
+ date: 2017-05-13 00:00:00.000000000 Z
34
12
  dependencies:
35
13
  - !ruby/object:Gem::Dependency
36
14
  name: haml
@@ -135,6 +113,10 @@ files:
135
113
  - bin/console
136
114
  - bin/setup
137
115
  - certs/hanachin.pem
116
+ - examples/output_yaml.rb
117
+ - examples/react.rb
118
+ - examples/sample.rb
119
+ - examples/total_num.rb
138
120
  - exe/haml_i18n_lint
139
121
  - gemfiles/haml_4.gemfile
140
122
  - gemfiles/haml_5.gemfile
checksums.yaml.gz.sig DELETED
Binary file
data.tar.gz.sig DELETED
Binary file
metadata.gz.sig DELETED
@@ -1 +0,0 @@
1
- eL�'XU�*�e��ˠ�0������m-x�4��kQ�yw[���N3 Տ>F����h�LO �vX*����$���L�Kz` ��{@�(�eH ���_�Y���j����r�?���z�P>�����l�>��õ�ߧ���C_Q��ƫ�\�Y�p(�9V�#�y?z3���k������ ����sJ1=����W�8Y����>|Qc�0�`�ΖRr�����j���݌1�^% �R66Ə��q�Y��s,t����