yaml-sort 2.1.1 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 813b1d18ae4a2cb6b57ebde8dc720c06ff9125417ab245d6cc15e22467e8f5ef
4
- data.tar.gz: 5d334e4fd7ab15319c225e549a1e7e38319f62a3ecec62a855b40c8e00cba036
3
+ metadata.gz: 71ee5e9950197312df22f8cb27e87dd7fe223f8c320969edcb7130c2c9548ed5
4
+ data.tar.gz: ca1948fa942e772f7ed9caade6b91683c92f7c20b685f24e508d4f669826ebef
5
5
  SHA512:
6
- metadata.gz: 84b534524792c4218449a7724e1673d09058afb2b2c838001e2092e144d8caaeadf51ea5cf415d52a6f5b1ca284ac3977785871344e317d0c61a0414b715d2d8
7
- data.tar.gz: 211409b7ade1b5cace67a86ea71a3fe952398a03165a099a201760ad47e3a01a2da99e7ecd359bc80f63d988396b63afc7a42141899f7ef316ac1dd0aa2c1e5a
6
+ metadata.gz: 37f4ae4497b011da8e928f5d7c9ba315105e80a5de50f4052320fb7409de469a45a51c92d143bac5061706e7e5e3bcfb422a461ffcfa0ab4b2f1d2df7309c39c
7
+ data.tar.gz: 4ba38b243d77ea33aa89c933eea5a3b98e2d7bed158d664d8f1020636c609e99d48c10cf462ec5e7db822ff73f3abf4f20693fc4ac0ea7585ae3ee343a316644
data/.simplecov ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # vim:set syntax=ruby:
4
+
5
+ SimpleCov.start do
6
+ add_filter "/spec/"
7
+ end
data/CHANGELOG.md CHANGED
@@ -3,6 +3,30 @@ All notable changes to this project will be documented in this file.
3
3
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4
4
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
5
 
6
+ ## [v2.2.1](https://github.com/smortex/yaml-sort/tree/v2.2.1) (2023-01-04)
7
+
8
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v2.2.0...v2.2.1)
9
+
10
+ **Fixed bugs:**
11
+
12
+ - Fix parsing of dictionaries with quotes in KEY/VALUE [\#21](https://github.com/smortex/yaml-sort/pull/21) ([smortex](https://github.com/smortex))
13
+
14
+ ## [v2.2.0](https://github.com/smortex/yaml-sort/tree/v2.2.0) (2022-09-27)
15
+
16
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v2.1.1...v2.2.0)
17
+
18
+ **Implemented enhancements:**
19
+
20
+ - Add support for &anchor / \*alias [\#17](https://github.com/smortex/yaml-sort/pull/17) ([smortex](https://github.com/smortex))
21
+
22
+ **Fixed bugs:**
23
+
24
+ - Fix relocation of comments following UNINDENT [\#18](https://github.com/smortex/yaml-sort/pull/18) ([smortex](https://github.com/smortex))
25
+
26
+ **Merged pull requests:**
27
+
28
+ - Add CodeClimate integration [\#15](https://github.com/smortex/yaml-sort/pull/15) ([smortex](https://github.com/smortex))
29
+
6
30
  ## [v2.1.1](https://github.com/smortex/yaml-sort/tree/v2.1.1) (2022-07-03)
7
31
 
8
32
  [Full Changelog](https://github.com/smortex/yaml-sort/compare/v2.1.0...v2.1.1)
data/Gemfile CHANGED
@@ -12,3 +12,4 @@ gem "rspec", "~> 3.0"
12
12
  gem "rubocop", "~> 1.21"
13
13
  gem "rubocop-rake"
14
14
  gem "rubocop-rspec"
15
+ gem "simplecov"
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # yaml-sort
2
2
 
3
+ [![CI](https://github.com/smortex/yaml-sort/actions/workflows/ci.yaml/badge.svg)](https://github.com/smortex/yaml-sort/actions/workflows/ci.yaml)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/4df28a3babe44d1a83c0/maintainability)](https://codeclimate.com/github/smortex/yaml-sort/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/4df28a3babe44d1a83c0/test_coverage)](https://codeclimate.com/github/smortex/yaml-sort/test_coverage)
6
+
3
7
  An utility to manage YAML files and ensure their lines is in a predictable order.
4
8
 
5
9
  This is mainly indended to manage [Hiera files](https://puppet.com/docs/puppet/7/hiera_intro.html) for [Puppet](https://puppet.com/) and translation files in [Ruby on Rails](https://rubyonrails.org/) applications.
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ require "cucumber/rake/task"
13
13
 
14
14
  Cucumber::Rake::Task.new
15
15
 
16
- task default: %i[rubocop spec cucumber]
16
+ task default: %i[spec cucumber]
17
17
 
18
18
  file "lib/yaml/sort/parser.rb": ["lib/yaml/sort/parser.ra"] do
19
19
  sh "racc --output-status --output-file=lib/yaml/sort/parser.rb lib/yaml/sort/parser.ra"
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaml
4
+ module Sort
5
+ class Alias < Value
6
+ attr_reader :name
7
+
8
+ def initialize(anchors, name)
9
+ super()
10
+ @anchors = anchors
11
+ @name = name[:value]
12
+ end
13
+
14
+ def value?
15
+ @anchors[@name]
16
+ end
17
+
18
+ def delete_value
19
+ @anchors.delete(@name)
20
+ end
21
+
22
+ def to_s(*)
23
+ if (s = @anchors.delete(@name))
24
+ separator = case s
25
+ when List, Dictionary then "\n"
26
+ else " "
27
+ end
28
+ "&#{name}#{separator}#{s}"
29
+ else
30
+ "*#{name}"
31
+ end
32
+ end
33
+
34
+ def <=>(_other)
35
+ 0
36
+ end
37
+ end
38
+ end
39
+ end
@@ -25,10 +25,9 @@ module Yaml
25
25
  super + items.map do |k, v|
26
26
  n += 1
27
27
  case v
28
- when List, Dictionary
29
- "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)}\n#{v}"
30
- when Scalar
31
- "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)} #{v}"
28
+ when List, Dictionary then "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)}\n#{v}"
29
+ when Scalar, Alias then "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)} #{v}"
30
+ else raise "Unexpected type"
32
31
  end
33
32
  end.join("\n")
34
33
  end
@@ -13,7 +13,7 @@ module Yaml
13
13
  module Sort
14
14
  class Parser < Racc::Parser
15
15
 
16
- module_eval(<<'...end parser.ra/module_eval...', 'parser.ra', 33)
16
+ module_eval(<<'...end parser.ra/module_eval...', 'parser.ra', 36)
17
17
 
18
18
  def scan(text)
19
19
  text = "---\n#{text}" unless text.start_with?("---\n")
@@ -27,11 +27,16 @@ def scan(text)
27
27
  @fakelineno = 0
28
28
  @position = 0
29
29
  @indent_stack = []
30
+ @anchors = {}
30
31
 
31
32
  until s.eos?
32
33
  if scan_value
33
34
  @position += s.matched_size if s.scan(/[[:blank:]]*/)
34
35
  case
36
+ when s.scan(/&[[:alnum:]]+/)
37
+ emit(:ANCHOR, s.matched[1..-1])
38
+ when s.scan(/\*[[:alnum:]]+/)
39
+ emit(:ALIAS, s.matched[1..-1])
35
40
  when s.scan(/[|>][-+]?(?=\n)/)
36
41
  match = s.matched
37
42
 
@@ -77,10 +82,11 @@ def scan(text)
77
82
  when s.scan(/\n[[:blank:]]*#.*/) then emit(:COMMENT, s.matched)
78
83
  when s.scan(/\n([[:blank:]]*-) */) then emit(:ITEM, s.matched, indent: s.captures[0])
79
84
  when s.scan(/\n[[:blank:]]*\.\.\./) then emit(:END_OF_DOCUMENT, s.matched)
80
- when s.scan(/\n?([[:blank:]]*)(.+:)(?=[ \n])/)
85
+ when s.scan(/\n?([[:blank:]]*)(.+?:)(?=[ \n])/)
81
86
  indent = s.captures[0]
82
87
  indent = last_indent_value + indent + " " unless s.matched.start_with?("\n")
83
88
  emit(:KEY, s.matched, indent: indent, value: s.captures[1])
89
+ scan_value = true if s.rest
84
90
 
85
91
  when s.scan(/\n\z/)
86
92
  # Done
@@ -111,7 +117,7 @@ def scan(text)
111
117
  @tokens.each do |token|
112
118
  if token[0] == :COMMENT
113
119
  comment << token[1][:value] + "\n"
114
- elsif comment.any?
120
+ elsif comment.any? && token[0] != :UNINDENT
115
121
  token[1][:comment] = comment
116
122
  comment = []
117
123
  end
@@ -141,8 +147,19 @@ def emit(token, match, length: nil, indent: nil, value: nil)
141
147
  @indent_stack.push(indent) unless @indent_stack.last == indent
142
148
  end
143
149
 
150
+ value ||= match
151
+
152
+ if token == :KEY && value == '<<:'
153
+ message = <<~MESSAGE
154
+ #{@filename}:#{@lineno}: '<<:' references are not sortable:
155
+ #{@lines[@lineno - 1].chomp}
156
+ #{indent}#{" " * @position}^#{"~" * (length - indent.length - 1)}
157
+ MESSAGE
158
+ raise(Racc::ParseError, message)
159
+ end
160
+
144
161
  exvalue = {
145
- value: value || match,
162
+ value: value,
146
163
  lineno: @lineno,
147
164
  position: @position,
148
165
  length: length,
@@ -193,50 +210,61 @@ end
193
210
  ##### State transition tables begin ###
194
211
 
195
212
  racc_action_table = [
196
- 5, 9, 2, 11, 5, 9, 3, 11, 5, 9,
197
- 12, 11, 9, 14, 16, 11, 13 ]
213
+ 5, 10, 2, 12, 3, 8, 5, 10, 13, 12,
214
+ 19, 8, 5, 10, 14, 12, 21, 8, 5, 10,
215
+ nil, 12, nil, 8, 5, 10, nil, 12, nil, 8,
216
+ 10, 15, 17, 12 ]
198
217
 
199
218
  racc_action_check = [
200
- 2, 2, 0, 2, 9, 9, 1, 9, 11, 11,
201
- 3, 11, 6, 6, 7, 7, 4 ]
219
+ 2, 2, 0, 2, 1, 2, 10, 10, 3, 10,
220
+ 10, 10, 12, 12, 4, 12, 12, 12, 19, 19,
221
+ nil, 19, nil, 19, 21, 21, nil, 21, nil, 21,
222
+ 6, 6, 7, 7 ]
202
223
 
203
224
  racc_action_pointer = [
204
- 0, 6, -4, 10, 13, nil, 7, 8, nil, 0,
205
- nil, 4, nil, nil, nil, nil, nil, nil, nil, nil ]
225
+ 0, 4, -4, 8, 11, nil, 25, 26, nil, nil,
226
+ 2, nil, 8, nil, nil, nil, nil, nil, nil, 14,
227
+ nil, 20, nil, nil, nil ]
206
228
 
207
229
  racc_action_default = [
208
- -12, -12, -12, -12, -2, -3, -12, -12, -7, -12,
209
- -10, -12, 20, -1, -4, -6, -5, -9, -8, -11 ]
230
+ -15, -15, -15, -15, -2, -3, -15, -15, -6, -8,
231
+ -15, -12, -15, 25, -1, -4, -7, -5, -11, -15,
232
+ -10, -15, -14, -9, -13 ]
210
233
 
211
234
  racc_goto_table = [
212
- 4, 1, 15, 17, nil, nil, nil, 18, nil, 19 ]
235
+ 4, 1, 16, 18, nil, nil, nil, nil, 20, nil,
236
+ 22, nil, nil, nil, nil, nil, nil, 23, nil, 24 ]
213
237
 
214
238
  racc_goto_check = [
215
- 2, 1, 5, 6, nil, nil, nil, 2, nil, 2 ]
239
+ 2, 1, 5, 6, nil, nil, nil, nil, 2, nil,
240
+ 2, nil, nil, nil, nil, nil, nil, 2, nil, 2 ]
216
241
 
217
242
  racc_goto_pointer = [
218
243
  nil, 1, -2, nil, nil, -4, -4 ]
219
244
 
220
245
  racc_goto_default = [
221
- nil, nil, nil, 6, 7, 8, 10 ]
246
+ nil, nil, nil, 6, 7, 9, 11 ]
222
247
 
223
248
  racc_reduce_table = [
224
249
  0, 0, :racc_error,
225
- 3, 9, :_reduce_1,
226
- 2, 9, :_reduce_2,
227
- 1, 10, :_reduce_3,
228
- 2, 10, :_reduce_4,
229
- 2, 10, :_reduce_5,
230
- 2, 11, :_reduce_6,
231
- 1, 11, :_reduce_7,
232
- 2, 13, :_reduce_8,
233
- 2, 12, :_reduce_9,
234
- 1, 12, :_reduce_10,
235
- 2, 14, :_reduce_11 ]
236
-
237
- racc_reduce_n = 12
238
-
239
- racc_shift_n = 20
250
+ 3, 11, :_reduce_1,
251
+ 2, 11, :_reduce_2,
252
+ 1, 12, :_reduce_3,
253
+ 2, 12, :_reduce_4,
254
+ 2, 12, :_reduce_5,
255
+ 1, 12, :_reduce_6,
256
+ 2, 13, :_reduce_7,
257
+ 1, 13, :_reduce_8,
258
+ 3, 15, :_reduce_9,
259
+ 2, 15, :_reduce_10,
260
+ 2, 14, :_reduce_11,
261
+ 1, 14, :_reduce_12,
262
+ 3, 16, :_reduce_13,
263
+ 2, 16, :_reduce_14 ]
264
+
265
+ racc_reduce_n = 15
266
+
267
+ racc_shift_n = 25
240
268
 
241
269
  racc_token_table = {
242
270
  false => 0,
@@ -246,9 +274,11 @@ racc_token_table = {
246
274
  :VALUE => 4,
247
275
  :KEY => 5,
248
276
  :UNINDENT => 6,
249
- :ITEM => 7 }
277
+ :ITEM => 7,
278
+ :ANCHOR => 8,
279
+ :ALIAS => 9 }
250
280
 
251
- racc_nt_base = 8
281
+ racc_nt_base = 10
252
282
 
253
283
  racc_use_result_var = true
254
284
 
@@ -277,6 +307,8 @@ Racc_token_to_s_table = [
277
307
  "KEY",
278
308
  "UNINDENT",
279
309
  "ITEM",
310
+ "ANCHOR",
311
+ "ALIAS",
280
312
  "$start",
281
313
  "document",
282
314
  "value",
@@ -326,43 +358,64 @@ module_eval(<<'.,.,', 'parser.ra', 14)
326
358
  end
327
359
  .,.,
328
360
 
329
- module_eval(<<'.,.,', 'parser.ra', 16)
361
+ module_eval(<<'.,.,', 'parser.ra', 15)
330
362
  def _reduce_6(val, _values, result)
331
- val[0].add_item(*val[1]); result = val[0]
363
+ result = Alias.new(@anchors, val[0])
332
364
  result
333
365
  end
334
366
  .,.,
335
367
 
336
368
  module_eval(<<'.,.,', 'parser.ra', 17)
337
369
  def _reduce_7(val, _values, result)
338
- result = Dictionary.create(*val[0])
370
+ val[0].add_item(*val[1]); result = val[0]
339
371
  result
340
372
  end
341
373
  .,.,
342
374
 
343
- module_eval(<<'.,.,', 'parser.ra', 19)
375
+ module_eval(<<'.,.,', 'parser.ra', 18)
344
376
  def _reduce_8(val, _values, result)
345
- result = [Scalar.new(val[0]), val[1]]
377
+ result = Dictionary.create(*val[0])
346
378
  result
347
379
  end
348
380
  .,.,
349
381
 
350
- module_eval(<<'.,.,', 'parser.ra', 21)
382
+ module_eval(<<'.,.,', 'parser.ra', 20)
351
383
  def _reduce_9(val, _values, result)
352
- val[0].add_item(*val[1]); result = val[0]
384
+ @anchors[val[1][:value]] = val[2]; result = [Scalar.new(val[0]), Alias.new(@anchors, val[1])]
353
385
  result
354
386
  end
355
387
  .,.,
356
388
 
357
- module_eval(<<'.,.,', 'parser.ra', 22)
389
+ module_eval(<<'.,.,', 'parser.ra', 21)
358
390
  def _reduce_10(val, _values, result)
359
- result = List.create(*val[0])
391
+ result = [Scalar.new(val[0]), val[1]]
360
392
  result
361
393
  end
362
394
  .,.,
363
395
 
364
- module_eval(<<'.,.,', 'parser.ra', 24)
396
+ module_eval(<<'.,.,', 'parser.ra', 23)
365
397
  def _reduce_11(val, _values, result)
398
+ val[0].add_item(*val[1]); result = val[0]
399
+ result
400
+ end
401
+ .,.,
402
+
403
+ module_eval(<<'.,.,', 'parser.ra', 24)
404
+ def _reduce_12(val, _values, result)
405
+ result = List.create(*val[0])
406
+ result
407
+ end
408
+ .,.,
409
+
410
+ module_eval(<<'.,.,', 'parser.ra', 26)
411
+ def _reduce_13(val, _values, result)
412
+ @anchors[val[1][:value]] = val[2]; result = [Item.new(val[0]), Alias.new(@anchors, val[1])]
413
+ result
414
+ end
415
+ .,.,
416
+
417
+ module_eval(<<'.,.,', 'parser.ra', 27)
418
+ def _reduce_14(val, _values, result)
366
419
  result = [Item.new(val[0]), val[1]]
367
420
  result
368
421
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yaml
4
4
  module Sort
5
- VERSION = "2.1.1"
5
+ VERSION = "2.2.1"
6
6
  end
7
7
  end
data/lib/yaml/sort.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "sort/parser"
4
4
  require_relative "sort/value"
5
+ require_relative "sort/alias"
5
6
  require_relative "sort/dictionary"
6
7
  require_relative "sort/item"
7
8
  require_relative "sort/list"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Tartière
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-03 00:00:00.000000000 Z
11
+ date: 2023-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cri
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".rspec"
78
78
  - ".rubocop.yml"
79
+ - ".simplecov"
79
80
  - CHANGELOG.md
80
81
  - CODE_OF_CONDUCT.md
81
82
  - Gemfile
@@ -84,6 +85,7 @@ files:
84
85
  - Rakefile
85
86
  - exe/yaml-sort
86
87
  - lib/yaml/sort.rb
88
+ - lib/yaml/sort/alias.rb
87
89
  - lib/yaml/sort/cli.rb
88
90
  - lib/yaml/sort/dictionary.rb
89
91
  - lib/yaml/sort/item.rb
@@ -119,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
121
  - !ruby/object:Gem::Version
120
122
  version: '0'
121
123
  requirements: []
122
- rubygems_version: 3.3.16
124
+ rubygems_version: 3.3.26
123
125
  signing_key:
124
126
  specification_version: 4
125
127
  summary: Sort lines in YAML files in a predictable order