syntax_tree 2.4.1 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -1
- data/Gemfile +0 -2
- data/Gemfile.lock +5 -3
- data/lib/syntax_tree/formatter.rb +1 -1
- data/lib/syntax_tree/node.rb +12 -92
- data/lib/syntax_tree/version.rb +1 -1
- data/lib/syntax_tree/visitor/match_visitor.rb +2 -2
- data/lib/syntax_tree.rb +3 -27
- data/syntax_tree.gemspec +3 -0
- metadata +31 -4
- data/lib/syntax_tree/prettyprint.rb +0 -1159
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b1505fca44f379d951132dafb8122a2591fe46607c8402d096bab67bd53d0a3
|
4
|
+
data.tar.gz: 0d58e60b6779ec1acd22ca776f3610f290c4b048cc1bf41b425f9c5a74e80db8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42d4dea2571b21b3cf877a129f0a840f30125eb918c558b25b5bc303c915bb758b731151cdf2cfb6e70e92cbc4957d915095d1284312529675fec71f3a6af3a7
|
7
|
+
data.tar.gz: 5178b395ddcfdcf795c18f7c986e86a12f6ca2bdec871792de2c21220dffc7c2a2238a02926cece3021635f42859ab5084ba4dba21932ce7f1791c2a17a5bc49
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,17 @@ 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.5.0] - 2022-05-13
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- [#79](https://github.com/ruby-syntax-tree/syntax_tree/pull/79) - Support an optional `maxwidth` second argument to `SyntaxTree.format`.
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
|
17
|
+
- [#77](https://github.com/ruby-syntax-tree/syntax_tree/pull/77) - Correct the pattern for checking if a dynamic symbol can be converted into a label as a hash key.
|
18
|
+
- [#72](https://github.com/ruby-syntax-tree/syntax_tree/pull/72) - Disallow conditionals with `not` without parentheses in the predicate from turning into a ternary.
|
19
|
+
|
9
20
|
## [2.4.1] - 2022-05-10
|
10
21
|
|
11
22
|
- [#73](https://github.com/ruby-syntax-tree/syntax_tree/pull/73) - Fix nested hash patterns from accidentally adding a `then` to their output.
|
@@ -213,7 +224,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
213
224
|
|
214
225
|
- 🎉 Initial release! 🎉
|
215
226
|
|
216
|
-
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.
|
227
|
+
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.5.0...HEAD
|
228
|
+
[2.5.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.1...v2.5.0
|
217
229
|
[2.4.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...v2.4.1
|
218
230
|
[2.4.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.1...v2.4.0
|
219
231
|
[2.3.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.0...v2.3.1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
syntax_tree (2.
|
4
|
+
syntax_tree (2.5.0)
|
5
|
+
prettier_print
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
@@ -12,11 +13,12 @@ GEM
|
|
12
13
|
parallel (1.22.1)
|
13
14
|
parser (3.1.2.0)
|
14
15
|
ast (~> 2.4.1)
|
16
|
+
prettier_print (0.1.0)
|
15
17
|
rainbow (3.1.1)
|
16
18
|
rake (13.0.6)
|
17
|
-
regexp_parser (2.
|
19
|
+
regexp_parser (2.4.0)
|
18
20
|
rexml (3.2.5)
|
19
|
-
rubocop (1.29.
|
21
|
+
rubocop (1.29.1)
|
20
22
|
parallel (~> 1.10)
|
21
23
|
parser (>= 3.1.0.0)
|
22
24
|
rainbow (>= 2.2.2, < 4.0)
|
data/lib/syntax_tree/node.rb
CHANGED
@@ -123,7 +123,7 @@ module SyntaxTree
|
|
123
123
|
end
|
124
124
|
|
125
125
|
def construct_keys
|
126
|
-
|
126
|
+
PrettierPrint.format(+"") { |q| Visitor::MatchVisitor.new(q).visit(self) }
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
@@ -1388,7 +1388,7 @@ module SyntaxTree
|
|
1388
1388
|
module HashKeyFormatter
|
1389
1389
|
# Formats the keys of a hash literal using labels.
|
1390
1390
|
class Labels
|
1391
|
-
LABEL =
|
1391
|
+
LABEL = /\A[A-Za-z_](\w*[\w!?])?\z/
|
1392
1392
|
|
1393
1393
|
def format_key(q, key)
|
1394
1394
|
case key
|
@@ -1666,52 +1666,6 @@ module SyntaxTree
|
|
1666
1666
|
end
|
1667
1667
|
end
|
1668
1668
|
|
1669
|
-
# This module will remove any breakables from the list of contents so that no
|
1670
|
-
# newlines are present in the output.
|
1671
|
-
module RemoveBreaks
|
1672
|
-
class << self
|
1673
|
-
def call(doc)
|
1674
|
-
marker = Object.new
|
1675
|
-
stack = [doc]
|
1676
|
-
|
1677
|
-
while stack.any?
|
1678
|
-
doc = stack.pop
|
1679
|
-
|
1680
|
-
if doc == marker
|
1681
|
-
stack.pop
|
1682
|
-
next
|
1683
|
-
end
|
1684
|
-
|
1685
|
-
stack += [doc, marker]
|
1686
|
-
|
1687
|
-
case doc
|
1688
|
-
when PrettyPrint::Align, PrettyPrint::Indent, PrettyPrint::Group
|
1689
|
-
doc.contents.map! { |child| remove_breaks(child) }
|
1690
|
-
stack += doc.contents.reverse
|
1691
|
-
when PrettyPrint::IfBreak
|
1692
|
-
doc.flat_contents.map! { |child| remove_breaks(child) }
|
1693
|
-
stack += doc.flat_contents.reverse
|
1694
|
-
end
|
1695
|
-
end
|
1696
|
-
end
|
1697
|
-
|
1698
|
-
private
|
1699
|
-
|
1700
|
-
def remove_breaks(doc)
|
1701
|
-
case doc
|
1702
|
-
when PrettyPrint::Breakable
|
1703
|
-
text = PrettyPrint::Text.new
|
1704
|
-
text.add(object: doc.force? ? "; " : doc.separator, width: doc.width)
|
1705
|
-
text
|
1706
|
-
when PrettyPrint::IfBreak
|
1707
|
-
PrettyPrint::Align.new(indent: 0, contents: doc.flat_contents)
|
1708
|
-
else
|
1709
|
-
doc
|
1710
|
-
end
|
1711
|
-
end
|
1712
|
-
end
|
1713
|
-
end
|
1714
|
-
|
1715
1669
|
# BlockVar represents the parameters being declared for a block. Effectively
|
1716
1670
|
# this node is everything contained within the pipes. This includes all of the
|
1717
1671
|
# various parameter types, as well as block-local variable declarations.
|
@@ -1752,8 +1706,7 @@ module SyntaxTree
|
|
1752
1706
|
|
1753
1707
|
def format(q)
|
1754
1708
|
q.group(0, "|", "|") do
|
1755
|
-
|
1756
|
-
RemoveBreaks.call(doc)
|
1709
|
+
q.remove_breaks(q.format(params))
|
1757
1710
|
|
1758
1711
|
if locals.any?
|
1759
1712
|
q.text("; ")
|
@@ -3096,31 +3049,6 @@ module SyntaxTree
|
|
3096
3049
|
|
3097
3050
|
private
|
3098
3051
|
|
3099
|
-
# This is a somewhat naive method that is attempting to sum up the width of
|
3100
|
-
# the doc nodes that make up the given doc node. This is used to align
|
3101
|
-
# content.
|
3102
|
-
def doc_width(parent)
|
3103
|
-
queue = [parent]
|
3104
|
-
width = 0
|
3105
|
-
|
3106
|
-
until queue.empty?
|
3107
|
-
doc = queue.shift
|
3108
|
-
|
3109
|
-
case doc
|
3110
|
-
when PrettyPrint::Text
|
3111
|
-
width += doc.width
|
3112
|
-
when PrettyPrint::Indent, PrettyPrint::Align, PrettyPrint::Group
|
3113
|
-
queue = doc.contents + queue
|
3114
|
-
when PrettyPrint::IfBreak
|
3115
|
-
queue = doc.break_contents + queue
|
3116
|
-
when PrettyPrint::Breakable
|
3117
|
-
width = 0
|
3118
|
-
end
|
3119
|
-
end
|
3120
|
-
|
3121
|
-
width
|
3122
|
-
end
|
3123
|
-
|
3124
3052
|
def argument_alignment(q, doc)
|
3125
3053
|
# Very special handling case for rspec matchers. In general with rspec
|
3126
3054
|
# matchers you expect to see something like:
|
@@ -3138,7 +3066,7 @@ module SyntaxTree
|
|
3138
3066
|
if %w[to not_to to_not].include?(message.value)
|
3139
3067
|
0
|
3140
3068
|
else
|
3141
|
-
width =
|
3069
|
+
width = q.last_position(doc) + 1
|
3142
3070
|
width > (q.maxwidth / 2) ? 0 : width
|
3143
3071
|
end
|
3144
3072
|
end
|
@@ -4891,17 +4819,9 @@ module SyntaxTree
|
|
4891
4819
|
end
|
4892
4820
|
|
4893
4821
|
def format(q)
|
4894
|
-
# This is a very specific behavior
|
4895
|
-
#
|
4896
|
-
|
4897
|
-
breakable = -> do
|
4898
|
-
q.target << PrettyPrint::Breakable.new(
|
4899
|
-
" ",
|
4900
|
-
1,
|
4901
|
-
indent: false,
|
4902
|
-
force: true
|
4903
|
-
)
|
4904
|
-
end
|
4822
|
+
# This is a very specific behavior where you want to force a newline, but
|
4823
|
+
# don't want to force the break parent.
|
4824
|
+
breakable = -> { q.breakable(indent: false, force: :skip_break_parent) }
|
4905
4825
|
|
4906
4826
|
q.group do
|
4907
4827
|
q.format(beginning)
|
@@ -5197,6 +5117,8 @@ module SyntaxTree
|
|
5197
5117
|
case node
|
5198
5118
|
in predicate: Assign | Command | CommandCall | MAssign | OpAssign
|
5199
5119
|
false
|
5120
|
+
in predicate: Not[parentheses: false]
|
5121
|
+
false
|
5200
5122
|
in {
|
5201
5123
|
statements: { body: [truthy] },
|
5202
5124
|
consequent: Else[statements: { body: [falsy] }]
|
@@ -5325,9 +5247,8 @@ module SyntaxTree
|
|
5325
5247
|
# force it into the output but we _don't_ want to explicitly
|
5326
5248
|
# break the parent. If a break-parent shows up in the tree, then
|
5327
5249
|
# it's going to force it all the way up to the tree, which is
|
5328
|
-
# going to negate the ternary.
|
5329
|
-
|
5330
|
-
q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
|
5250
|
+
# going to negate the ternary.
|
5251
|
+
q.breakable(force: :skip_break_parent)
|
5331
5252
|
q.format(node.consequent.statements)
|
5332
5253
|
end
|
5333
5254
|
end
|
@@ -8314,8 +8235,7 @@ module SyntaxTree
|
|
8314
8235
|
# same line in the source, then we're going to leave them in place and
|
8315
8236
|
# assume that's the way the developer wanted this expression
|
8316
8237
|
# represented.
|
8317
|
-
|
8318
|
-
RemoveBreaks.call(doc)
|
8238
|
+
q.remove_breaks(q.group(0, '#{', "}") { q.format(statements) })
|
8319
8239
|
else
|
8320
8240
|
q.group do
|
8321
8241
|
q.text('#{')
|
data/lib/syntax_tree/version.rb
CHANGED
@@ -22,7 +22,7 @@ module SyntaxTree
|
|
22
22
|
# entire value into the output buffer.
|
23
23
|
q.text(node.inspect)
|
24
24
|
else
|
25
|
-
|
25
|
+
node.pretty_print(q)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -114,7 +114,7 @@ module SyntaxTree
|
|
114
114
|
q.nest(0) do
|
115
115
|
q.text(name)
|
116
116
|
q.text(": ")
|
117
|
-
|
117
|
+
value.pretty_print(q)
|
118
118
|
end
|
119
119
|
end
|
120
120
|
end
|
data/lib/syntax_tree.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require "json"
|
4
4
|
require "pp"
|
5
|
-
require "
|
5
|
+
require "prettier_print"
|
6
6
|
require "ripper"
|
7
7
|
require "stringio"
|
8
8
|
|
@@ -16,30 +16,6 @@ require_relative "syntax_tree/visitor/json_visitor"
|
|
16
16
|
require_relative "syntax_tree/visitor/match_visitor"
|
17
17
|
require_relative "syntax_tree/visitor/pretty_print_visitor"
|
18
18
|
|
19
|
-
# If PrettyPrint::Align isn't defined, then we haven't gotten the updated
|
20
|
-
# version of prettyprint. In that case we'll define our own. This is going to
|
21
|
-
# overwrite a bunch of methods, so silencing them as well.
|
22
|
-
unless PrettyPrint.const_defined?(:Align)
|
23
|
-
verbose = $VERBOSE
|
24
|
-
$VERBOSE = nil
|
25
|
-
|
26
|
-
begin
|
27
|
-
require_relative "syntax_tree/prettyprint"
|
28
|
-
ensure
|
29
|
-
$VERBOSE = verbose
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# When PP is running, it expects that everything that interacts with it is going
|
34
|
-
# to flow through PP.pp, since that's the main entry into the module from the
|
35
|
-
# perspective of its uses in core Ruby. In doing so, it calls guard_inspect_key
|
36
|
-
# at the top of the PP.pp method, which establishes some thread-local hashes to
|
37
|
-
# check for cycles in the pretty printed tree. This means that if you want to
|
38
|
-
# manually call pp on some object _before_ you have established these hashes,
|
39
|
-
# you're going to break everything. So this call ensures that those hashes have
|
40
|
-
# been set up before anything uses pp manually.
|
41
|
-
PP.new(+"", 0).guard_inspect_key {}
|
42
|
-
|
43
19
|
# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
|
44
20
|
# provides the ability to generate a syntax tree from source, as well as the
|
45
21
|
# tools necessary to inspect and manipulate that syntax tree. It can be used to
|
@@ -64,8 +40,8 @@ module SyntaxTree
|
|
64
40
|
end
|
65
41
|
|
66
42
|
# Parses the given source and returns the formatted source.
|
67
|
-
def self.format(source)
|
68
|
-
formatter = Formatter.new(source, [])
|
43
|
+
def self.format(source, maxwidth = 80)
|
44
|
+
formatter = Formatter.new(source, [], maxwidth)
|
69
45
|
parse(source).format(formatter)
|
70
46
|
|
71
47
|
formatter.flush
|
data/syntax_tree.gemspec
CHANGED
@@ -25,8 +25,11 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
26
|
spec.require_paths = %w[lib]
|
27
27
|
|
28
|
+
spec.add_dependency "prettier_print"
|
29
|
+
|
28
30
|
spec.add_development_dependency "bundler"
|
29
31
|
spec.add_development_dependency "minitest"
|
30
32
|
spec.add_development_dependency "rake"
|
33
|
+
spec.add_development_dependency "rubocop"
|
31
34
|
spec.add_development_dependency "simplecov"
|
32
35
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syntax_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
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-
|
11
|
+
date: 2022-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: prettier_print
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +66,20 @@ dependencies:
|
|
52
66
|
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: simplecov
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -101,7 +129,6 @@ files:
|
|
101
129
|
- lib/syntax_tree/node.rb
|
102
130
|
- lib/syntax_tree/parser.rb
|
103
131
|
- lib/syntax_tree/plugin/single_quotes.rb
|
104
|
-
- lib/syntax_tree/prettyprint.rb
|
105
132
|
- lib/syntax_tree/version.rb
|
106
133
|
- lib/syntax_tree/visitor.rb
|
107
134
|
- lib/syntax_tree/visitor/field_visitor.rb
|
@@ -129,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
156
|
- !ruby/object:Gem::Version
|
130
157
|
version: '0'
|
131
158
|
requirements: []
|
132
|
-
rubygems_version: 3.
|
159
|
+
rubygems_version: 3.3.3
|
133
160
|
signing_key:
|
134
161
|
specification_version: 4
|
135
162
|
summary: A parser based on ripper
|