syntax_tree 2.4.1 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/CHANGELOG.md +37 -1
- data/Gemfile +0 -2
- data/Gemfile.lock +5 -3
- data/README.md +63 -2
- data/Rakefile +5 -20
- data/lib/syntax_tree/basic_visitor.rb +74 -0
- data/lib/syntax_tree/formatter/trailing_comma.rb +13 -0
- data/lib/syntax_tree/formatter.rb +8 -2
- data/lib/syntax_tree/node.rb +174 -118
- data/lib/syntax_tree/parser.rb +112 -0
- data/lib/syntax_tree/plugin/trailing_comma.rb +4 -0
- data/lib/syntax_tree/rake/check_task.rb +66 -0
- data/lib/syntax_tree/rake/write_task.rb +66 -0
- data/lib/syntax_tree/rake_tasks.rb +4 -0
- data/lib/syntax_tree/version.rb +1 -1
- data/lib/syntax_tree/visitor/field_visitor.rb +9 -3
- data/lib/syntax_tree/visitor/match_visitor.rb +2 -2
- data/lib/syntax_tree/visitor.rb +4 -66
- data/lib/syntax_tree.rb +5 -27
- data/syntax_tree.gemspec +3 -0
- metadata +36 -3
- data/lib/syntax_tree/prettyprint.rb +0 -1159
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rake"
|
4
|
+
require "rake/tasklib"
|
5
|
+
|
6
|
+
require "syntax_tree"
|
7
|
+
require "syntax_tree/cli"
|
8
|
+
|
9
|
+
module SyntaxTree
|
10
|
+
module Rake
|
11
|
+
# A Rake task that runs format on a set of source files.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# require 'syntax_tree/rake/write_task'
|
16
|
+
#
|
17
|
+
# SyntaxTree::Rake::WriteTask.new do |t|
|
18
|
+
# t.source_files = '{app,config,lib}/**/*.rb'
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# This will create task that can be run with:
|
22
|
+
#
|
23
|
+
# rake stree_write
|
24
|
+
#
|
25
|
+
class WriteTask < ::Rake::TaskLib
|
26
|
+
# Name of the task.
|
27
|
+
# Defaults to :"stree:write".
|
28
|
+
attr_accessor :name
|
29
|
+
|
30
|
+
# Glob pattern to match source files.
|
31
|
+
# Defaults to 'lib/**/*.rb'.
|
32
|
+
attr_accessor :source_files
|
33
|
+
|
34
|
+
# The set of plugins to require.
|
35
|
+
# Defaults to [].
|
36
|
+
attr_accessor :plugins
|
37
|
+
|
38
|
+
def initialize(
|
39
|
+
name = :"stree:write",
|
40
|
+
source_files = ::Rake::FileList["lib/**/*.rb"],
|
41
|
+
plugins = []
|
42
|
+
)
|
43
|
+
@name = name
|
44
|
+
@source_files = source_files
|
45
|
+
@plugins = plugins
|
46
|
+
|
47
|
+
yield self if block_given?
|
48
|
+
define_task
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def define_task
|
54
|
+
desc "Runs `stree write` over source files"
|
55
|
+
task(name) { run_task }
|
56
|
+
end
|
57
|
+
|
58
|
+
def run_task
|
59
|
+
arguments = ["write"]
|
60
|
+
arguments << "--plugins=#{plugins.join(",")}" if plugins.any?
|
61
|
+
|
62
|
+
SyntaxTree::CLI.run(arguments + Array(source_files))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/syntax_tree/version.rb
CHANGED
@@ -49,9 +49,7 @@ module SyntaxTree
|
|
49
49
|
# of circumstances, like when visiting the list of optional parameters
|
50
50
|
# defined on a method.
|
51
51
|
#
|
52
|
-
class FieldVisitor <
|
53
|
-
attr_reader :q
|
54
|
-
|
52
|
+
class FieldVisitor < BasicVisitor
|
55
53
|
def visit_aref(node)
|
56
54
|
node(node, "aref") do
|
57
55
|
field("collection", node.collection)
|
@@ -586,6 +584,14 @@ module SyntaxTree
|
|
586
584
|
end
|
587
585
|
end
|
588
586
|
|
587
|
+
def visit_lambda_var(node)
|
588
|
+
node(node, "lambda_var") do
|
589
|
+
field("params", node.params)
|
590
|
+
list("locals", node.locals) if node.locals.any?
|
591
|
+
comments(node)
|
592
|
+
end
|
593
|
+
end
|
594
|
+
|
589
595
|
def visit_lbrace(node)
|
590
596
|
visit_token(node, "lbrace")
|
591
597
|
end
|
@@ -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/visitor.rb
CHANGED
@@ -4,72 +4,7 @@ module SyntaxTree
|
|
4
4
|
# Visitor is a parent class that provides the ability to walk down the tree
|
5
5
|
# and handle a subset of nodes. By defining your own subclass, you can
|
6
6
|
# explicitly handle a node type by defining a visit_* method.
|
7
|
-
class Visitor
|
8
|
-
# This is raised when you use the Visitor.visit_method method and it fails.
|
9
|
-
# It is correctable to through DidYouMean.
|
10
|
-
class VisitMethodError < StandardError
|
11
|
-
attr_reader :visit_method
|
12
|
-
|
13
|
-
def initialize(visit_method)
|
14
|
-
@visit_method = visit_method
|
15
|
-
super("Invalid visit method: #{visit_method}")
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# This class is used by DidYouMean to offer corrections to invalid visit
|
20
|
-
# method names.
|
21
|
-
class VisitMethodChecker
|
22
|
-
attr_reader :visit_method
|
23
|
-
|
24
|
-
def initialize(error)
|
25
|
-
@visit_method = error.visit_method
|
26
|
-
end
|
27
|
-
|
28
|
-
def corrections
|
29
|
-
@corrections ||=
|
30
|
-
DidYouMean::SpellChecker.new(
|
31
|
-
dictionary: Visitor.visit_methods
|
32
|
-
).correct(visit_method)
|
33
|
-
end
|
34
|
-
|
35
|
-
DidYouMean.correct_error(VisitMethodError, self)
|
36
|
-
end
|
37
|
-
|
38
|
-
class << self
|
39
|
-
# This method is here to help folks write visitors.
|
40
|
-
#
|
41
|
-
# It's not always easy to ensure you're writing the correct method name in
|
42
|
-
# the visitor since it's perfectly valid to define methods that don't
|
43
|
-
# override these parent methods.
|
44
|
-
#
|
45
|
-
# If you use this method, you can ensure you're writing the correct method
|
46
|
-
# name. It will raise an error if the visit method you're defining isn't
|
47
|
-
# actually a method on the parent visitor.
|
48
|
-
def visit_method(method_name)
|
49
|
-
return if visit_methods.include?(method_name)
|
50
|
-
|
51
|
-
raise VisitMethodError, method_name
|
52
|
-
end
|
53
|
-
|
54
|
-
# This is the list of all of the valid visit methods.
|
55
|
-
def visit_methods
|
56
|
-
@visit_methods ||=
|
57
|
-
Visitor.instance_methods.grep(/^visit_(?!child_nodes)/)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def visit(node)
|
62
|
-
node&.accept(self)
|
63
|
-
end
|
64
|
-
|
65
|
-
def visit_all(nodes)
|
66
|
-
nodes.map { |node| visit(node) }
|
67
|
-
end
|
68
|
-
|
69
|
-
def visit_child_nodes(node)
|
70
|
-
visit_all(node.child_nodes)
|
71
|
-
end
|
72
|
-
|
7
|
+
class Visitor < BasicVisitor
|
73
8
|
# Visit an ARef node.
|
74
9
|
alias visit_aref visit_child_nodes
|
75
10
|
|
@@ -301,6 +236,9 @@ module SyntaxTree
|
|
301
236
|
# Visit a Lambda node.
|
302
237
|
alias visit_lambda visit_child_nodes
|
303
238
|
|
239
|
+
# Visit a LambdaVar node.
|
240
|
+
alias visit_lambda_var visit_child_nodes
|
241
|
+
|
304
242
|
# Visit a LBrace node.
|
305
243
|
alias visit_lbrace visit_child_nodes
|
306
244
|
|
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
|
|
@@ -10,36 +10,14 @@ require_relative "syntax_tree/formatter"
|
|
10
10
|
require_relative "syntax_tree/node"
|
11
11
|
require_relative "syntax_tree/parser"
|
12
12
|
require_relative "syntax_tree/version"
|
13
|
+
|
14
|
+
require_relative "syntax_tree/basic_visitor"
|
13
15
|
require_relative "syntax_tree/visitor"
|
14
16
|
require_relative "syntax_tree/visitor/field_visitor"
|
15
17
|
require_relative "syntax_tree/visitor/json_visitor"
|
16
18
|
require_relative "syntax_tree/visitor/match_visitor"
|
17
19
|
require_relative "syntax_tree/visitor/pretty_print_visitor"
|
18
20
|
|
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
21
|
# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
|
44
22
|
# provides the ability to generate a syntax tree from source, as well as the
|
45
23
|
# tools necessary to inspect and manipulate that syntax tree. It can be used to
|
@@ -64,8 +42,8 @@ module SyntaxTree
|
|
64
42
|
end
|
65
43
|
|
66
44
|
# Parses the given source and returns the formatted source.
|
67
|
-
def self.format(source)
|
68
|
-
formatter = Formatter.new(source, [])
|
45
|
+
def self.format(source, maxwidth = 80)
|
46
|
+
formatter = Formatter.new(source, [], maxwidth)
|
69
47
|
parse(source).format(formatter)
|
70
48
|
|
71
49
|
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.7.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-20 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
|
@@ -93,15 +121,20 @@ files:
|
|
93
121
|
- doc/logo.svg
|
94
122
|
- exe/stree
|
95
123
|
- lib/syntax_tree.rb
|
124
|
+
- lib/syntax_tree/basic_visitor.rb
|
96
125
|
- lib/syntax_tree/cli.rb
|
97
126
|
- lib/syntax_tree/formatter.rb
|
98
127
|
- lib/syntax_tree/formatter/single_quotes.rb
|
128
|
+
- lib/syntax_tree/formatter/trailing_comma.rb
|
99
129
|
- lib/syntax_tree/language_server.rb
|
100
130
|
- lib/syntax_tree/language_server/inlay_hints.rb
|
101
131
|
- lib/syntax_tree/node.rb
|
102
132
|
- lib/syntax_tree/parser.rb
|
103
133
|
- lib/syntax_tree/plugin/single_quotes.rb
|
104
|
-
- lib/syntax_tree/
|
134
|
+
- lib/syntax_tree/plugin/trailing_comma.rb
|
135
|
+
- lib/syntax_tree/rake/check_task.rb
|
136
|
+
- lib/syntax_tree/rake/write_task.rb
|
137
|
+
- lib/syntax_tree/rake_tasks.rb
|
105
138
|
- lib/syntax_tree/version.rb
|
106
139
|
- lib/syntax_tree/visitor.rb
|
107
140
|
- lib/syntax_tree/visitor/field_visitor.rb
|