yaml-sort 1.0.1 → 2.0.2

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
  SHA256:
3
- metadata.gz: 232d936095f9aac0e3f4098fa2cc4f1d608ddc79be9cb4e757fb476fa75999e3
4
- data.tar.gz: be5949810ea40472c5d80e7f23d96886e964847b26d9526f67b337a77acfe490
3
+ metadata.gz: 125cdbf70a136c2968ab9c18f99cbfaa62e3f4117a577187a37f43298910a7c9
4
+ data.tar.gz: 58ed8f15a8cd027871e17dbc8ccd8293fd1be1e3242e2b12012ea1efc9f50c87
5
5
  SHA512:
6
- metadata.gz: e90a5d3ff86076d86b0629ec1a387aa179588f678d17c5f60fcae2d01ffda38eb28f308d5d3d7df1a29d6b1a194701caab02409d26d364f8c2c8fb7403ca2a0a
7
- data.tar.gz: e2fb3054b83242a9fad8c596c6f79ecfce1f66169234c7e1bc8a179214bd41288e08eb608981b08d2312dff721e3bfa18aadaa429dded7bae1eeb702dc06bdd3
6
+ metadata.gz: 3e2580f860075dd74a69c5e5a31097d906cbba4c9293a8f3181ee6fbf72cc931ca371cbf3b5258ed5ac93d356d2ed82f2805477f039799ea4cd293a39458a236
7
+ data.tar.gz: 00d35a112f355e53e0fca7d3bc63349dc6c1da2b87fc8195af480754b1e81f31049da97282679eef282d412c42d2ec7788689879c915a6697860408d01f9f5cd
data/CHANGELOG.md CHANGED
@@ -3,9 +3,33 @@ 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
- ## [1.0.1](https://github.com/smortex/yaml-sort/tree/1.0.1) (2022-04-24)
6
+ ## [2.0.2](https://github.com/smortex/yaml-sort/tree/2.0.2) (2022-06-10)
7
7
 
8
- [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.0...1.0.1)
8
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v2.0.1...2.0.2)
9
+
10
+ **Fixed bugs:**
11
+
12
+ - Fix infinite loops when processing files with trailing whitespace [\#9](https://github.com/smortex/yaml-sort/pull/9) ([smortex](https://github.com/smortex))
13
+
14
+ ## [v2.0.1](https://github.com/smortex/yaml-sort/tree/v2.0.1) (2022-06-01)
15
+
16
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v2.0.0...v2.0.1)
17
+
18
+ **Fixed bugs:**
19
+
20
+ - Fix parsing of keys with spaces [\#8](https://github.com/smortex/yaml-sort/pull/8) ([smortex](https://github.com/smortex))
21
+
22
+ ## [v2.0.0](https://github.com/smortex/yaml-sort/tree/v2.0.0) (2022-04-25)
23
+
24
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.1...v2.0.0)
25
+
26
+ **Merged pull requests:**
27
+
28
+ - Rework list sorting [\#7](https://github.com/smortex/yaml-sort/pull/7) ([smortex](https://github.com/smortex))
29
+
30
+ ## [v1.0.1](https://github.com/smortex/yaml-sort/tree/v1.0.1) (2022-04-24)
31
+
32
+ [Full Changelog](https://github.com/smortex/yaml-sort/compare/v1.0.0...v1.0.1)
9
33
 
10
34
  **Fixed bugs:**
11
35
 
@@ -20,13 +20,15 @@ module Yaml
20
20
  dict
21
21
  end
22
22
 
23
- def to_s
23
+ def to_s(skip_first_indent: false)
24
+ n = -1
24
25
  super + items.map do |k, v|
26
+ n += 1
25
27
  case v
26
28
  when List, Dictionary
27
- "#{k}\n#{v}"
29
+ "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)}\n#{v}"
28
30
  when Scalar
29
- "#{k} #{v}"
31
+ "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)} #{v}"
30
32
  end
31
33
  end.join("\n")
32
34
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "scalar"
4
+
5
+ module Yaml
6
+ module Sort
7
+ class Item < Scalar
8
+ def to_s(*)
9
+ comments + value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -22,15 +22,13 @@ module Yaml
22
22
 
23
23
  def to_s
24
24
  super + items.map do |item|
25
- "#{item[0]}#{item[1]}"
25
+ "#{item[0]}#{item[1].to_s(skip_first_indent: true)}"
26
26
  end.join("\n")
27
27
  end
28
28
 
29
29
  def sort
30
- List.new(items.sort { |a, b| a[1] <=> b[1] })
31
- rescue ArgumentError
32
- # Non-comparable items
33
- self
30
+ # TODO: Add an option to sort scalar values
31
+ List.new(items.map { |i| [i[0], i[1].sort] })
34
32
  end
35
33
  end
36
34
  end
@@ -30,43 +30,42 @@ def scan(text)
30
30
 
31
31
  until s.eos?
32
32
  if scan_value
33
- unless s.match?(/[[:space:]]*\n/)
34
- @position += s.matched_size if s.scan(/[[:blank:]]*/)
35
- case
36
- when s.scan(/[|>][-+]?(?=\n)/)
37
- match = s.matched
38
-
39
- while s.match?("\n" + last_indent_value + " ")
40
- match += s.scan(/\n[^\n]+(?=\n)/)
41
- end
42
- emit(:VALUE, match)
43
- when s.scan(/"/)
44
- match = s.matched
45
- loop do
46
- match += s.scan_until(/"|\\/)
47
- if match[-1] == "\\"
48
- match += s.scan(/.|\n/)
49
- else
50
- break
51
- end
52
- end
53
- emit(:VALUE, match)
54
- when s.scan(/'/)
55
- match = s.matched
56
- loop do
57
- match += s.scan_until(/'/)
58
- break unless s.match?(/'/)
59
- match += s.scan(/'/)
60
- end
61
- emit(:VALUE, match)
62
- when s.scan(/\S+/)
63
- match = s.matched
64
- until s.match?(/[\n]/) || s.eos?
65
- match += s.scan(/[^\n]+/)
33
+ @position += s.matched_size if s.scan(/[[:blank:]]*/)
34
+ case
35
+ when s.scan(/[|>][-+]?(?=\n)/)
36
+ match = s.matched
37
+
38
+ while s.match?("\n" + last_indent_value + " ")
39
+ match += s.scan(/\n[^\n]+(?=\n)/)
40
+ end
41
+ emit(:VALUE, match)
42
+ when s.scan(/"/)
43
+ match = s.matched
44
+ loop do
45
+ match += s.scan_until(/"|\\/)
46
+ if match[-1] == "\\"
47
+ match += s.scan(/.|\n/)
48
+ else
49
+ break
66
50
  end
67
- emit(:VALUE, match)
68
51
  end
52
+ emit(:VALUE, match)
53
+ when s.scan(/'/)
54
+ match = s.matched
55
+ loop do
56
+ match += s.scan_until(/'/)
57
+ break unless s.match?(/'/)
58
+ match += s.scan(/'/)
59
+ end
60
+ emit(:VALUE, match)
61
+ when s.scan(/\S+/)
62
+ match = s.matched
63
+ until s.match?(/[\n]/) || s.eos?
64
+ match += s.scan(/[^\n]+/)
65
+ end
66
+ emit(:VALUE, match)
69
67
  end
68
+ s.scan(/[[:blank:]]*/)
70
69
  scan_value = false
71
70
  else
72
71
  case
@@ -78,17 +77,30 @@ def scan(text)
78
77
  when s.scan(/\n[[:blank:]]*#.*/) then emit(:COMMENT, s.matched)
79
78
  when s.scan(/\n([[:blank:]]*-) */) then emit(:ITEM, s.matched, indent: s.captures[0])
80
79
  when s.scan(/\n[[:blank:]]*\.\.\./) then emit(:END_OF_DOCUMENT, s.matched)
81
- when s.scan(/\n?([[:blank:]]*)([^[[:space:]]\n]+):(?=[ \n])/)
80
+ when s.scan(/\n?([[:blank:]]*)(.+:)(?=[ \n])/)
82
81
  indent = s.captures[0]
83
82
  indent = last_indent_value + indent + " " unless s.matched.start_with?("\n")
84
- emit(:KEY, s.matched, indent: indent)
83
+ emit(:KEY, s.matched, indent: indent, value: s.captures[1])
85
84
 
86
85
  when s.scan(/\n\z/)
87
86
  # Done
88
87
  when s.match?(/./)
89
88
  scan_value = true
90
89
  else
91
- raise "LoST: #{s.rest.inspect} #{s.eos?}"
90
+ message = if @lines[@lineno - 1][@position] == "\n"
91
+ <<~MESSAGE
92
+ #{@filename}:#{@lineno + 1}: unexpected content
93
+ #{@lines[@lineno].chomp}
94
+ ^#{"~" * (@lines[@lineno].chomp.length - 1)}
95
+ MESSAGE
96
+ else
97
+ <<~MESSAGE
98
+ #{@filename}:#{@lineno}: unexpected content
99
+ #{@lines[@lineno - 1].chomp}
100
+ #{" " * @position}^
101
+ MESSAGE
102
+ end
103
+ raise(Racc::ParseError, message)
92
104
  end
93
105
  end
94
106
  end
@@ -110,16 +122,16 @@ def scan(text)
110
122
  @tokens
111
123
  end
112
124
 
113
- def emit(token, value, length: nil, indent: nil)
125
+ def emit(token, match, length: nil, indent: nil, value: nil)
114
126
  indent.gsub!("-", " ") if indent
115
127
  if token && length.nil?
116
- raise "length must be explicitly passed when value is not a String (#{value.class.name})" unless value.is_a?(String)
117
- length = value.length
128
+ raise "length must be explicitly passed when match is not a String (#{match.class.name})" unless match.is_a?(String)
129
+ length = match.length
118
130
  end
119
131
 
120
- if value.start_with?("\n")
132
+ if match.start_with?("\n")
121
133
  @lineno += 1
122
- value = value[1..-1]
134
+ match = match[1..-1]
123
135
  length -= 1
124
136
  @position = 0
125
137
  end
@@ -130,7 +142,7 @@ def emit(token, value, length: nil, indent: nil)
130
142
  end
131
143
 
132
144
  exvalue = {
133
- value: value,
145
+ value: value || match,
134
146
  lineno: @lineno,
135
147
  position: @position,
136
148
  length: length,
@@ -138,7 +150,7 @@ def emit(token, value, length: nil, indent: nil)
138
150
  }
139
151
  @tokens << [token, exvalue]
140
152
 
141
- @lineno += value.count("\n")
153
+ @lineno += match.count("\n")
142
154
 
143
155
  @position += length
144
156
  end
@@ -351,7 +363,7 @@ module_eval(<<'.,.,', 'parser.ra', 22)
351
363
 
352
364
  module_eval(<<'.,.,', 'parser.ra', 24)
353
365
  def _reduce_11(val, _values, result)
354
- result = [Scalar.new(val[0]), val[1]]
366
+ result = [Item.new(val[0]), val[1]]
355
367
  result
356
368
  end
357
369
  .,.,
@@ -3,12 +3,13 @@
3
3
  module Yaml
4
4
  module Sort
5
5
  class Scalar < Value
6
- attr_reader :value
6
+ attr_reader :value, :indent
7
7
 
8
8
  def initialize(value)
9
9
  super()
10
10
  @comment = value[:comment] || []
11
11
  @value = value[:value]
12
+ @indent = value[:indent] || ""
12
13
  end
13
14
 
14
15
  def <=>(other)
@@ -19,8 +20,12 @@ module Yaml
19
20
  end
20
21
  end
21
22
 
22
- def to_s
23
- super + value
23
+ def to_s(skip_first_indent: false)
24
+ if skip_first_indent
25
+ super + value
26
+ else
27
+ super + indent + value
28
+ end
24
29
  end
25
30
  end
26
31
  end
@@ -7,7 +7,11 @@ module Yaml
7
7
  @comment = []
8
8
  end
9
9
 
10
- def to_s
10
+ def to_s(*)
11
+ comments
12
+ end
13
+
14
+ def comments
11
15
  @comment.join
12
16
  end
13
17
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yaml
4
4
  module Sort
5
- VERSION = "1.0.1"
5
+ VERSION = "2.0.2"
6
6
  end
7
7
  end
data/lib/yaml/sort.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "sort/parser"
4
4
  require_relative "sort/value"
5
5
  require_relative "sort/dictionary"
6
+ require_relative "sort/item"
6
7
  require_relative "sort/list"
7
8
  require_relative "sort/scalar"
8
9
  require_relative "sort/version"
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: 1.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Tartière
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-24 00:00:00.000000000 Z
11
+ date: 2022-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cri
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description:
69
+ description:
70
70
  email:
71
71
  - romain@blogreen.org
72
72
  executables:
@@ -86,6 +86,7 @@ files:
86
86
  - lib/yaml/sort.rb
87
87
  - lib/yaml/sort/cli.rb
88
88
  - lib/yaml/sort/dictionary.rb
89
+ - lib/yaml/sort/item.rb
89
90
  - lib/yaml/sort/list.rb
90
91
  - lib/yaml/sort/parser.rb
91
92
  - lib/yaml/sort/scalar.rb
@@ -103,7 +104,7 @@ metadata:
103
104
  source_code_uri: https://github.com/smortex/yaml-sort
104
105
  changelog_uri: https://github.com/smortex/yaml-sort/blob/master/CHANGELOG.md
105
106
  rubygems_mfa_required: 'true'
106
- post_install_message:
107
+ post_install_message:
107
108
  rdoc_options: []
108
109
  require_paths:
109
110
  - lib
@@ -118,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  - !ruby/object:Gem::Version
119
120
  version: '0'
120
121
  requirements: []
121
- rubygems_version: 3.3.10
122
- signing_key:
122
+ rubygems_version: 3.2.5
123
+ signing_key:
123
124
  specification_version: 4
124
125
  summary: Sort lines in YAML files in a predictable order
125
126
  test_files: []