syntax_tree 2.7.0 → 2.7.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: 126d8e2e86fc77aeab82d0130da3ed5d20b85ecbfb6cfd84ea4056f48c9c5cb9
4
- data.tar.gz: c7abdbc2427c75561ae5e08b55345ecce65c4ff4cf6c5b6c02151734f9e54f5c
3
+ metadata.gz: 4e90bde4f4aa60ce6784fd2c03234853bc281b7012cdb496d3f2bd97a1d825a9
4
+ data.tar.gz: 24c4a344934acc5fb0b8b7d8931f689f98185f00fd9b71726f5aeab2499dea98
5
5
  SHA512:
6
- metadata.gz: 0b06e13f4f4ff47b250a5423e3e47de324c77ca4cd965539a040edc0b02b4aa9e78367cd5a8a5fad504f80dea82029798e8001d82eddf042e745b6080041a389
7
- data.tar.gz: '0910deb51a5d6a458530923f5014b02030092b957f056e6339ef252ab6cb951014f738e2443d44d28e836dbb399567f934ac21b1b26e8ea75ddb8782475f4647'
6
+ metadata.gz: 70d191270dbe2efd07300169410e4e0674b7abd8ec1ed4d95e87f7bb986b790195f8366f27b77f38eeeb94de1a08ce3467d64fbb126910922857366389ce66c0
7
+ data.tar.gz: c3994813de2fa53eb132dce7e4d176e58a83071232bcdefe3ded77a3fdd35ed52639d99989878b4cf5ca3d13c0a5bc14cbdfb95555b9705e2dd08c88d2abf360
data/.rubocop.yml CHANGED
@@ -78,3 +78,6 @@ Style/PerlBackrefs:
78
78
 
79
79
  Style/SpecialGlobalVars:
80
80
  Enabled: false
81
+
82
+ Style/StructInheritance:
83
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.7.1] - 2022-05-25
10
+
11
+ ### Added
12
+
13
+ - [#92](https://github.com/ruby-syntax-tree/syntax_tree/pull/92) - (Internal) Drastically increase test coverage, including many more tests for the language server and the CLI.
14
+
15
+ ### Changed
16
+
17
+ - [#87](https://github.com/ruby-syntax-tree/syntax_tree/pull/87) - Don't convert quotes on strings if it would result in more escapes.
18
+ - [#91](https://github.com/ruby-syntax-tree/syntax_tree/pull/91) - Always use `[]` with array patterns. There are just too many edge cases where you have to use them anyway. This simplifies the look and makes it more consistent.
19
+ - [#92](https://github.com/ruby-syntax-tree/syntax_tree/pull/92) - Remodel the currently shipped plugins such that they're modifying an options hash instead of overriding methods. This should make it easier for other plugins to reference the already loaded plugins, e.g., the RBS plugin referencing the quotes.
20
+ - [#92](https://github.com/ruby-syntax-tree/syntax_tree/pull/92) - Fix up the language server inlay hints to continue walking the tree once a pattern is found. This should increase useability.
21
+
9
22
  ## [2.7.0] - 2022-05-19
10
23
 
11
24
  ### Added
@@ -246,7 +259,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
246
259
 
247
260
  - 🎉 Initial release! 🎉
248
261
 
249
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.7.0...HEAD
262
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.7.1...HEAD
263
+ [2.7.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.7.0...v2.7.1
250
264
  [2.7.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.6.0...v2.7.0
251
265
  [2.6.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.5.0...v2.6.0
252
266
  [2.5.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.1...v2.5.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (2.7.0)
4
+ syntax_tree (2.7.1)
5
5
  prettier_print
6
6
 
7
7
  GEM
@@ -4,6 +4,18 @@ module SyntaxTree
4
4
  # A slightly enhanced PP that knows how to format recursively including
5
5
  # comments.
6
6
  class Formatter < PrettierPrint
7
+ # We want to minimize as much as possible the number of options that are
8
+ # available in syntax tree. For the most part, if users want non-default
9
+ # formatting, they should override the format methods on the specific nodes
10
+ # themselves. However, because of some history with prettier and the fact
11
+ # that folks have become entrenched in their ways, we decided to provide a
12
+ # small amount of configurability.
13
+ #
14
+ # Note that we're keeping this in a global-ish hash instead of just
15
+ # overriding methods on classes so that other plugins can reference this if
16
+ # necessary. For example, the RBS plugin references the quote style.
17
+ OPTIONS = { quote: "\"", trailing_comma: false }
18
+
7
19
  COMMENT_PRIORITY = 1
8
20
  HEREDOC_PRIORITY = 2
9
21
 
@@ -14,13 +26,20 @@ module SyntaxTree
14
26
  attr_reader :quote, :trailing_comma
15
27
  alias trailing_comma? trailing_comma
16
28
 
17
- def initialize(source, ...)
18
- super(...)
29
+ def initialize(
30
+ source,
31
+ *args,
32
+ quote: OPTIONS[:quote],
33
+ trailing_comma: OPTIONS[:trailing_comma]
34
+ )
35
+ super(*args)
19
36
 
20
37
  @source = source
21
38
  @stack = []
22
- @quote = "\""
23
- @trailing_comma = false
39
+
40
+ # Memoizing these values per formatter to make access faster.
41
+ @quote = quote
42
+ @trailing_comma = trailing_comma
24
43
  end
25
44
 
26
45
  def self.format(source, node)
@@ -38,6 +38,7 @@ module SyntaxTree
38
38
  #
39
39
  def visit_assign(node)
40
40
  parentheses(node.location) if stack[-2].is_a?(Params)
41
+ super
41
42
  end
42
43
 
43
44
  # Adds parentheses around binary expressions to make it clear which
@@ -57,6 +58,8 @@ module SyntaxTree
57
58
  parentheses(node.location)
58
59
  else
59
60
  end
61
+
62
+ super
60
63
  end
61
64
 
62
65
  # Adds parentheses around ternary operators contained within certain
@@ -70,9 +73,13 @@ module SyntaxTree
70
73
  # a ? b : ₍c ? d : e₎
71
74
  #
72
75
  def visit_if_op(node)
73
- if stack[-2] in Assign | Binary | IfOp | OpAssign
76
+ case stack[-2]
77
+ in Assign | Binary | IfOp | OpAssign
74
78
  parentheses(node.location)
79
+ else
75
80
  end
81
+
82
+ super
76
83
  end
77
84
 
78
85
  # Adds the implicitly rescued StandardError into a bare rescue clause. For
@@ -92,6 +99,8 @@ module SyntaxTree
92
99
  if node.exception.nil?
93
100
  after[node.location.start_char + "rescue".length] << " StandardError"
94
101
  end
102
+
103
+ super
95
104
  end
96
105
 
97
106
  # Adds parentheses around unary statements using the - operator that are
@@ -107,6 +116,8 @@ module SyntaxTree
107
116
  if stack[-2].is_a?(Binary) && (node.operator == "-")
108
117
  parentheses(node.location)
109
118
  end
119
+
120
+ super
110
121
  end
111
122
 
112
123
  def self.find(program)
@@ -70,13 +70,11 @@ module SyntaxTree
70
70
  id:,
71
71
  params: { textDocument: { uri: } }
72
72
  }
73
- output = []
74
- PP.pp(SyntaxTree.parse(store[uri]), output)
75
- write(id: id, result: output.join)
73
+ write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +""))
76
74
  in method: %r{\$/.+}
77
75
  # ignored
78
76
  else
79
- raise "Unhandled: #{request}"
77
+ raise ArgumentError, "Unhandled: #{request}"
80
78
  end
81
79
  end
82
80
  end
@@ -109,10 +107,6 @@ module SyntaxTree
109
107
  }
110
108
  end
111
109
 
112
- def log(message)
113
- write(method: "window/logMessage", params: { type: 4, message: message })
114
- end
115
-
116
110
  def inlay_hints(source)
117
111
  inlay_hints = InlayHints.find(SyntaxTree.parse(source))
118
112
  serialize = ->(position, text) { { position: position, text: text } }
@@ -1123,38 +1123,20 @@ module SyntaxTree
1123
1123
  end
1124
1124
 
1125
1125
  def format(q)
1126
- parts = [*requireds]
1127
- parts << RestFormatter.new(rest) if rest
1128
- parts += posts
1129
-
1130
- if constant
1131
- q.group do
1132
- q.format(constant)
1133
- q.text("[")
1134
- q.indent do
1135
- q.breakable("")
1136
- q.seplist(parts) { |part| q.format(part) }
1137
- end
1138
- q.breakable("")
1139
- q.text("]")
1140
- end
1141
-
1142
- return
1143
- end
1144
-
1145
- parent = q.parent
1146
- if parts.length == 1 || PATTERNS.include?(parent.class)
1126
+ q.group do
1127
+ q.format(constant) if constant
1147
1128
  q.text("[")
1148
1129
  q.indent do
1149
1130
  q.breakable("")
1131
+
1132
+ parts = [*requireds]
1133
+ parts << RestFormatter.new(rest) if rest
1134
+ parts += posts
1135
+
1150
1136
  q.seplist(parts) { |part| q.format(part) }
1151
1137
  end
1152
1138
  q.breakable("")
1153
1139
  q.text("]")
1154
- elsif parts.empty?
1155
- q.text("[]")
1156
- else
1157
- q.group { q.seplist(parts) { |part| q.format(part) } }
1158
1140
  end
1159
1141
  end
1160
1142
  end
@@ -2147,11 +2129,13 @@ module SyntaxTree
2147
2129
  #
2148
2130
  # break
2149
2131
  #
2150
- in [Paren[
2151
- contents: {
2152
- body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
2153
- }
2154
- ]]
2132
+ in [
2133
+ Paren[
2134
+ contents: {
2135
+ body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
2136
+ }
2137
+ ]
2138
+ ]
2155
2139
  # Here we have a single argument that is a set of parentheses wrapping
2156
2140
  # an array literal that has at least 2 elements. We're going to print
2157
2141
  # the contents of the array directly. This would be like if we had:
@@ -3879,9 +3863,9 @@ module SyntaxTree
3879
3863
  # whichever quote the user chose. (If they chose single quotes, then double
3880
3864
  # quoting would activate the escape sequence, and if they chose double
3881
3865
  # quotes, then single quotes would deactivate it.)
3882
- def self.locked?(node)
3866
+ def self.locked?(node, quote)
3883
3867
  node.parts.any? do |part|
3884
- !part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]/)
3868
+ !part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]|#{quote}/)
3885
3869
  end
3886
3870
  end
3887
3871
 
@@ -3996,12 +3980,12 @@ module SyntaxTree
3996
3980
 
3997
3981
  if matched
3998
3982
  [quote, matching]
3999
- elsif Quotes.locked?(self)
3983
+ elsif Quotes.locked?(self, q.quote)
4000
3984
  ["#{":" unless hash_key}'", "'"]
4001
3985
  else
4002
3986
  ["#{":" unless hash_key}#{q.quote}", q.quote]
4003
3987
  end
4004
- elsif Quotes.locked?(self)
3988
+ elsif Quotes.locked?(self, q.quote)
4005
3989
  if quote.start_with?(":")
4006
3990
  [hash_key ? quote[1..] : quote, quote[1..]]
4007
3991
  else
@@ -5490,12 +5474,14 @@ module SyntaxTree
5490
5474
  q.format(predicate)
5491
5475
  q.text(" ?")
5492
5476
 
5493
- q.breakable
5494
- q.format(truthy)
5495
- q.text(" :")
5477
+ q.indent do
5478
+ q.breakable
5479
+ q.format(truthy)
5480
+ q.text(" :")
5496
5481
 
5497
- q.breakable
5498
- q.format(falsy)
5482
+ q.breakable
5483
+ q.format(falsy)
5484
+ end
5499
5485
  end
5500
5486
  end
5501
5487
 
@@ -8429,7 +8415,7 @@ module SyntaxTree
8429
8415
  end
8430
8416
 
8431
8417
  opening_quote, closing_quote =
8432
- if !Quotes.locked?(self)
8418
+ if !Quotes.locked?(self, q.quote)
8433
8419
  [q.quote, q.quote]
8434
8420
  elsif quote.start_with?("%")
8435
8421
  [quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]
@@ -548,13 +548,6 @@ module SyntaxTree
548
548
  parts[0].location.to(parts[-1].location)
549
549
  end
550
550
 
551
- # If there's the optional then keyword, then we'll delete that and use it
552
- # as the end bounds of the location.
553
- if (token = find_token(Kw, "then", consume: false))
554
- tokens.delete(token)
555
- location = location.to(token.location)
556
- end
557
-
558
551
  # If there is a plain *, then we're going to fix up the location of it
559
552
  # here because it currently doesn't have anything to use for its precise
560
553
  # location. If we hit a comma, then we've gone too far.
@@ -1698,12 +1691,6 @@ module SyntaxTree
1698
1691
  end
1699
1692
  end
1700
1693
 
1701
- # Delete the optional then keyword
1702
- if (token = find_token(Kw, "then", consume: false))
1703
- parts << token
1704
- tokens.delete(token)
1705
- end
1706
-
1707
1694
  HshPtn.new(
1708
1695
  constant: constant,
1709
1696
  keywords: keywords || [],
@@ -3013,6 +3000,11 @@ module SyntaxTree
3013
3000
  # (StringEmbExpr | StringDVar | TStringContent) part
3014
3001
  # ) -> StringContent
3015
3002
  def on_string_add(string, part)
3003
+ # Due to some eccentricities in how ripper works, you need this here in
3004
+ # case you have a syntax error with an embedded expression that doesn't
3005
+ # finish, as in: "#{"
3006
+ return string if part.is_a?(String)
3007
+
3016
3008
  location =
3017
3009
  string.parts.any? ? string.location.to(part.location) : part.location
3018
3010
 
@@ -1,4 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "syntax_tree/formatter/single_quotes"
4
- SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::SingleQuotes)
3
+ SyntaxTree::Formatter::OPTIONS[:quote] = "'"
@@ -1,4 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "syntax_tree/formatter/trailing_comma"
4
- SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::TrailingComma)
3
+ SyntaxTree::Formatter::OPTIONS[:trailing_comma] = true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "2.7.0"
4
+ VERSION = "2.7.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-20 00:00:00.000000000 Z
11
+ date: 2022-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prettier_print
@@ -124,8 +124,6 @@ files:
124
124
  - lib/syntax_tree/basic_visitor.rb
125
125
  - lib/syntax_tree/cli.rb
126
126
  - lib/syntax_tree/formatter.rb
127
- - lib/syntax_tree/formatter/single_quotes.rb
128
- - lib/syntax_tree/formatter/trailing_comma.rb
129
127
  - lib/syntax_tree/language_server.rb
130
128
  - lib/syntax_tree/language_server/inlay_hints.rb
131
129
  - lib/syntax_tree/node.rb
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SyntaxTree
4
- class Formatter
5
- # This module overrides the quote method on the formatter to use single
6
- # quotes for everything instead of double quotes.
7
- module SingleQuotes
8
- def quote
9
- "'"
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SyntaxTree
4
- class Formatter
5
- # This module overrides the trailing_comma? method on the formatter to
6
- # return true.
7
- module TrailingComma
8
- def trailing_comma?
9
- true
10
- end
11
- end
12
- end
13
- end