syntax_tree 2.0.1 → 2.1.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 +9 -1
- data/CHANGELOG.md +18 -1
- data/Gemfile.lock +5 -3
- data/README.md +267 -26
- data/lib/syntax_tree/cli.rb +46 -34
- data/lib/syntax_tree/language_server/inlay_hints.rb +0 -16
- data/lib/syntax_tree/node.rb +1022 -3151
- data/lib/syntax_tree/parser.rb +233 -108
- data/lib/syntax_tree/version.rb +1 -1
- data/lib/syntax_tree/visitor/json_visitor.rb +1335 -0
- data/lib/syntax_tree/visitor/pretty_print_visitor.rb +1213 -0
- data/lib/syntax_tree/visitor.rb +548 -0
- data/lib/syntax_tree.rb +4 -0
- metadata +6 -4
- data/bin/setup +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46b573afdda378e664e11b1f5acab2c89ce0fdd2c8b3d4c6400d74e133aaffc3
|
4
|
+
data.tar.gz: 6be0570ce471fe3f34c53730d7095d9b81be5b7e3e2e84ac5724ba95d7ac7362
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36236536a1dda5e6e2907b82c383b74f207f5e2b64f282490cf26161d62d748d1cdf51b68c393cd60f1777d2c5434c294ad2e243f2575faae34c42ac44155f76
|
7
|
+
data.tar.gz: 5c2e0ee682e5847287cee5a6ca4357c51e4172bacf7aa98b780e427fb7d0912b6bd829bbf6236baa294a919be9be5730d8c7e74b27a0ca630a29a91c37540324
|
data/.github/workflows/main.yml
CHANGED
@@ -4,6 +4,14 @@ on:
|
|
4
4
|
- pull_request_target
|
5
5
|
jobs:
|
6
6
|
ci:
|
7
|
+
strategy:
|
8
|
+
fail-fast: false
|
9
|
+
matrix:
|
10
|
+
ruby:
|
11
|
+
- '2.7'
|
12
|
+
- '3.0'
|
13
|
+
- '3.1'
|
14
|
+
- head
|
7
15
|
name: CI
|
8
16
|
runs-on: ubuntu-latest
|
9
17
|
env:
|
@@ -13,7 +21,7 @@ jobs:
|
|
13
21
|
- uses: ruby/setup-ruby@v1
|
14
22
|
with:
|
15
23
|
bundler-cache: true
|
16
|
-
ruby-version:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
17
25
|
- name: Test
|
18
26
|
run: bundle exec rake test
|
19
27
|
automerge:
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,21 @@ 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.1.0] - 2022-04-12
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- The `SyntaxTree::Visitor` class now implements the visitor pattern for Ruby nodes.
|
14
|
+
- The `SyntaxTree::Visitor.visit_method(name)` method.
|
15
|
+
- Support for Ruby 2.7.
|
16
|
+
- Support for comments on `rescue` and `else` keywords.
|
17
|
+
- `SyntaxTree::Location` now additionally has `start_column` and `end_column`.
|
18
|
+
- The CLI now accepts content over STDIN for the `ast`, `check`, `debug`, `doc`, `format`, and `write` commands.
|
19
|
+
|
20
|
+
### Removed
|
21
|
+
|
22
|
+
- The missing hash value inlay hints have been removed.
|
23
|
+
|
9
24
|
## [2.0.1] - 2022-03-31
|
10
25
|
|
11
26
|
### Changed
|
@@ -128,7 +143,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
128
143
|
|
129
144
|
- 🎉 Initial release! 🎉
|
130
145
|
|
131
|
-
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.
|
146
|
+
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.0...HEAD
|
147
|
+
[2.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.1...v2.1.0
|
148
|
+
[2.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.0...v2.0.1
|
132
149
|
[2.0.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.2.0...v2.0.0
|
133
150
|
[1.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.1.1...v1.2.0
|
134
151
|
[1.1.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.1.0...v1.1.1
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
syntax_tree (2.0
|
4
|
+
syntax_tree (2.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -10,10 +10,10 @@ GEM
|
|
10
10
|
benchmark-ips (2.10.0)
|
11
11
|
docile (1.4.0)
|
12
12
|
minitest (5.15.0)
|
13
|
-
parser (3.1.
|
13
|
+
parser (3.1.2.0)
|
14
14
|
ast (~> 2.4.1)
|
15
15
|
rake (13.0.6)
|
16
|
-
ruby_parser (3.19.
|
16
|
+
ruby_parser (3.19.1)
|
17
17
|
sexp_processor (~> 4.16)
|
18
18
|
sexp_processor (4.16.0)
|
19
19
|
simplecov (0.21.2)
|
@@ -25,6 +25,8 @@ GEM
|
|
25
25
|
stackprof (0.2.19)
|
26
26
|
|
27
27
|
PLATFORMS
|
28
|
+
arm64-darwin-21
|
29
|
+
ruby
|
28
30
|
x86_64-darwin-19
|
29
31
|
x86_64-darwin-21
|
30
32
|
x86_64-linux
|
data/README.md
CHANGED
@@ -7,11 +7,50 @@
|
|
7
7
|
[![Build Status](https://github.com/ruby-syntax-tree/syntax_tree/actions/workflows/main.yml/badge.svg)](https://github.com/ruby-syntax-tree/syntax_tree/actions/workflows/main.yml)
|
8
8
|
[![Gem Version](https://img.shields.io/gem/v/syntax_tree.svg)](https://rubygems.org/gems/syntax_tree)
|
9
9
|
|
10
|
-
|
10
|
+
Syntax Tree is a suite of tools built on top of the internal CRuby parser. It provides the ability to generate a syntax tree from source, as well as the tools necessary to inspect and manipulate that syntax tree. It can be used to build formatters, linters, language servers, and more.
|
11
|
+
|
12
|
+
It is built with only standard library dependencies. It additionally ships with a plugin system so that you can build your own syntax trees from other languages and incorporate these tools.
|
13
|
+
|
14
|
+
- [Installation](#installation)
|
15
|
+
- [CLI](#cli)
|
16
|
+
- [ast](#ast)
|
17
|
+
- [check](#check)
|
18
|
+
- [format](#format)
|
19
|
+
- [write](#write)
|
20
|
+
- [Library](#library)
|
21
|
+
- [SyntaxTree.read(filepath)](#syntaxtreereadfilepath)
|
22
|
+
- [SyntaxTree.parse(source)](#syntaxtreeparsesource)
|
23
|
+
- [SyntaxTree.format(source)](#syntaxtreeformatsource)
|
24
|
+
- [Nodes](#nodes)
|
25
|
+
- [child_nodes](#child_nodes)
|
26
|
+
- [Pattern matching](#pattern-matching)
|
27
|
+
- [pretty_print(q)](#pretty_printq)
|
28
|
+
- [to_json(*opts)](#to_jsonopts)
|
29
|
+
- [format(q)](#formatq)
|
30
|
+
- [Visitor](#visitor)
|
31
|
+
- [visit_method](#visit_method)
|
32
|
+
- [Language server](#language-server)
|
33
|
+
- [textDocument/formatting](#textdocumentformatting)
|
34
|
+
- [textDocument/inlayHints](#textdocumentinlayhints)
|
35
|
+
- [syntaxTree/visualizing](#syntaxtreevisualizing)
|
36
|
+
- [Contributing](#contributing)
|
37
|
+
- [License](#license)
|
11
38
|
|
12
39
|
## Installation
|
13
40
|
|
14
|
-
|
41
|
+
Syntax Tree is both a command-line interface and a library. If you're only looking to use the command-line interface, then we recommend installing the gem globally, as in:
|
42
|
+
|
43
|
+
```sh
|
44
|
+
gem install syntax_tree
|
45
|
+
```
|
46
|
+
|
47
|
+
To run the CLI with the gem installed globally, you would run:
|
48
|
+
|
49
|
+
```sh
|
50
|
+
stree version
|
51
|
+
```
|
52
|
+
|
53
|
+
If you're planning on using Syntax Tree as a library within your own project, we recommend installing it as part of your gem bundle. First, add this line to your application's Gemfile:
|
15
54
|
|
16
55
|
```ruby
|
17
56
|
gem "syntax_tree"
|
@@ -19,52 +58,254 @@ gem "syntax_tree"
|
|
19
58
|
|
20
59
|
And then execute:
|
21
60
|
|
22
|
-
|
61
|
+
```sh
|
62
|
+
bundle install
|
63
|
+
```
|
23
64
|
|
24
|
-
|
65
|
+
To run the CLI with the gem installed in your gem bundle, you would run:
|
25
66
|
|
26
|
-
|
67
|
+
```sh
|
68
|
+
bundle exec stree version
|
69
|
+
```
|
27
70
|
|
28
|
-
##
|
71
|
+
## CLI
|
29
72
|
|
30
|
-
|
73
|
+
Syntax Tree ships with the `stree` CLI, which can be used to inspect and manipulate Ruby code. Below are listed all of the commands built into the CLI that you can use. Note that for all commands that operate on files, you can also pass in content through STDIN.
|
31
74
|
|
32
|
-
|
33
|
-
|
75
|
+
### ast
|
76
|
+
|
77
|
+
This command will print out a textual representation of the syntax tree associated with each of the files it finds. To execute, run:
|
78
|
+
|
79
|
+
```sh
|
80
|
+
stree ast path/to/file.rb
|
81
|
+
```
|
82
|
+
|
83
|
+
For a file that contains `1 + 1`, you will receive:
|
84
|
+
|
85
|
+
```
|
86
|
+
(program (statements (binary (int "1") + (int "1"))))
|
87
|
+
```
|
88
|
+
|
89
|
+
### check
|
90
|
+
|
91
|
+
This command is meant to be used in the context of a continuous integration or git hook. It checks each file given to make sure that it matches the expected format. It can be used to ensure unformatted content never makes it into a codebase.
|
92
|
+
|
93
|
+
```sh
|
94
|
+
stree check path/to/file.rb
|
95
|
+
```
|
96
|
+
|
97
|
+
For a file that matches the expected format, you will receive:
|
34
98
|
|
35
|
-
pp SyntaxTree.parse(source) # print out the AST
|
36
|
-
puts SyntaxTree.format(source) # format the AST
|
37
99
|
```
|
100
|
+
All files matched expected format.
|
101
|
+
```
|
102
|
+
|
103
|
+
If there are files with unformatted code, you will receive:
|
104
|
+
|
105
|
+
```
|
106
|
+
[warn] path/to/file.rb
|
107
|
+
The listed files did not match the expected format.
|
108
|
+
```
|
109
|
+
|
110
|
+
### format
|
38
111
|
|
39
|
-
|
112
|
+
This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
|
40
113
|
|
41
114
|
```sh
|
42
|
-
|
43
|
-
(program
|
44
|
-
(statements
|
45
|
-
...
|
115
|
+
stree format path/to/file.rb
|
46
116
|
```
|
47
117
|
|
48
|
-
|
118
|
+
For a file that contains `1 + 1`, you will receive:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
1 + 1
|
122
|
+
```
|
123
|
+
|
124
|
+
### write
|
125
|
+
|
126
|
+
This command will format the listed files and write that formatted version back to the source files. Note that this overwrites the original content, to be sure to be using a version control system.
|
49
127
|
|
50
128
|
```sh
|
51
|
-
|
52
|
-
|
53
|
-
|
129
|
+
stree write path/to/file.rb
|
130
|
+
```
|
131
|
+
|
132
|
+
This will list every file that is being formatted. It will output light gray if the file already matches the expected format. It will output in regular color if it does not.
|
133
|
+
|
134
|
+
```
|
135
|
+
path/to/file.rb 0ms
|
136
|
+
```
|
137
|
+
|
138
|
+
## Library
|
139
|
+
|
140
|
+
Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.
|
141
|
+
|
142
|
+
### SyntaxTree.read(filepath)
|
143
|
+
|
144
|
+
This function takes a filepath and returns a string associated with the content of that file. It is similar in functionality to `File.read`, except htat it takes into account Ruby-level file encoding (through magic comments at the top of the file).
|
145
|
+
|
146
|
+
### SyntaxTree.parse(source)
|
147
|
+
|
148
|
+
This function takes an input string containing Ruby code and returns the syntax tree associated with it. The top-level node is always a `SyntaxTree::Program`, which contains a list of top-level expression nodes.
|
149
|
+
|
150
|
+
### SyntaxTree.format(source)
|
151
|
+
|
152
|
+
This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string.
|
153
|
+
|
154
|
+
## Nodes
|
155
|
+
|
156
|
+
There are many different node types in the syntax tree. They are meant to be treated as immutable structs containing links to child nodes with minimal logic contained within their implementation. However, for the most part they all respond to a certain set of APIs, listed below.
|
157
|
+
|
158
|
+
### child_nodes
|
159
|
+
|
160
|
+
One of the easiest ways to descend the tree is to use the `child_nodes` function. It is implemented on every node type (leaf nodes return an empty array). If the goal is to simply walk through the tree, this is the easiest way to go.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
program = SyntaxTree.parse("1 + 1")
|
164
|
+
program.child_nodes.first.child_nodes.first
|
165
|
+
# => (binary (int "1") :+ (int "1"))
|
166
|
+
```
|
167
|
+
|
168
|
+
### Pattern matching
|
169
|
+
|
170
|
+
Pattern matching is another way to descend the tree which is more specific than using `child_nodes`. Using Ruby's built-in pattern matching, you can extract the same information but be as specific about your constraints as you like. For example, with minimal constraints:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
program = SyntaxTree.parse("1 + 1")
|
174
|
+
program => { statements: { body: [binary] } }
|
175
|
+
binary
|
176
|
+
# => (binary (int "1") :+ (int "1"))
|
177
|
+
```
|
178
|
+
|
179
|
+
Or, with more constraints on the types to ensure we're getting exactly what we expect:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
program = SyntaxTree.parse("1 + 1")
|
183
|
+
program => SyntaxTree::Program[statements: SyntaxTree::Statements[body: [SyntaxTree::Binary => binary]]]
|
184
|
+
binary
|
185
|
+
# => (binary (int "1") :+ (int "1"))
|
186
|
+
```
|
187
|
+
|
188
|
+
### pretty_print(q)
|
189
|
+
|
190
|
+
Every node responds to the `pretty_print` Ruby interface, which makes it usable by the `pp` library. You _can_ use this API manually, but it's mostly there for compatibility and not meant to be directly invoked. For example:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
pp SyntaxTree.parse("1 + 1")
|
194
|
+
# (program (statements (binary (int "1") + (int "1"))))
|
195
|
+
```
|
196
|
+
|
197
|
+
### to_json(*opts)
|
198
|
+
|
199
|
+
Every node responds to the `to_json` Ruby interface, which makes it usable by the `json` library. Much like `pretty_print`, you could use this API manually, but it's mostly used by `JSON` to dump the nodes to a serialized format. For example:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
program = SyntaxTree.parse("1 + 1")
|
203
|
+
program => { statements: { body: [{ left: }] } }
|
204
|
+
puts JSON.dump(left)
|
205
|
+
# {"type":"int","value":"1","loc":[1,0,1,1],"cmts":[]}
|
206
|
+
```
|
207
|
+
|
208
|
+
### format(q)
|
209
|
+
|
210
|
+
Every node responds to `format`, which formats the content nicely. The API mirrors that used by the `pretty_print` gem in that it accepts a formatter object and calls methods on it to generate its own internal representation of the text that will be outputted. Because of this, it's easier to not use this API directly and instead to call `SyntaxTree.format`. You _can_ however use this directly if you create the formatter yourself, as in:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
source = "1+1"
|
214
|
+
program = SyntaxTree.parse(source)
|
215
|
+
program => { statements: { body: [binary] } }
|
216
|
+
|
217
|
+
formatter = SyntaxTree::Formatter.new(source, [])
|
218
|
+
binary.format(formatter)
|
219
|
+
|
220
|
+
formatter.flush
|
221
|
+
formatter.output.join
|
222
|
+
# => "1 + 1"
|
223
|
+
```
|
224
|
+
|
225
|
+
## Visitor
|
226
|
+
|
227
|
+
If you want to operate over a set of nodes in the tree but don't want to walk the tree manually, the `Visitor` class makes it easy. `SyntaxTree::Visitor` is an implementation of the double dispatch visitor pattern. It works by the user defining visit methods that process nodes in the tree, which then call back to other visit methods to continue the descent. This is easier shown in code.
|
228
|
+
|
229
|
+
Let's say, for instance, that you wanted to find every place in source where you have an arithmetic problem between two integers (this is pretty contrived, but it's just for illustration). You could define a visitor that only explicitly visits the `SyntaxTree::Binary` node, as in:
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
class ArithmeticVisitor < SyntaxTree::Visitor
|
233
|
+
def visit_binary(node)
|
234
|
+
if node in { left: SyntaxTree::Int, operator: :+ | :- | :* | :/, right: SyntaxTree::Int }
|
235
|
+
puts "The result is: #{node.left.value.to_i.public_send(node.operator, node.right.value.to_i)}"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
visitor = ArithmeticVisitor.new
|
241
|
+
visitor.visit(SyntaxTree.parse("1 + 1"))
|
242
|
+
# The result is: 2
|
54
243
|
```
|
55
244
|
|
56
|
-
|
245
|
+
With visitors, you only define handlers for the nodes that you need. You can find the names of the methods that you will need to define within the base visitor, as they're all aliased to the default behavior (visiting the child nodes). Note that when you define a handler for a node, you have to tell Syntax Tree how to walk further. In the example above, we don't need to go any further because we already know the child nodes are `SyntaxTree::Int`, so they can't possibly contain more `SyntaxTree::Binary` nodes. In other circumstances you may not know though, so you can either:
|
246
|
+
|
247
|
+
* call `super` (which will do the default and visit all child nodes)
|
248
|
+
* call `visit_child_nodes` manually
|
249
|
+
* call `visit(child)` with each child that you want to visit
|
250
|
+
* call nothing if you're sure you don't want to descend further
|
251
|
+
|
252
|
+
There are a couple of visitors that ship with Syntax Tree that can be used as examples. They live in the [lib/syntax_tree/visitor](lib/syntax_tree/visitor) directory.
|
253
|
+
|
254
|
+
### visit_method
|
255
|
+
|
256
|
+
When you're creating a visitor, it's very easy to accidentally mistype a visit method. Unfortunately, there's no way to tell Ruby to explicitly override a parent method, so it would then be easy to define a method that never gets called. To mitigate this risk, there's `Visitor.visit_method(name)`. This method accepts a symbol that is checked against the list of known visit methods. If it's not in the list, then an error will be raised. It's meant to be used like:
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
class ArithmeticVisitor < SyntaxTree::Visitor
|
260
|
+
visit_method def visit_binary(node)
|
261
|
+
# ...
|
262
|
+
end
|
263
|
+
end
|
264
|
+
```
|
265
|
+
|
266
|
+
This will only be checked once when the file is first required. If there is a typo in your method name (or the method no longer exists for whatever reason), you will receive an error like so:
|
267
|
+
|
268
|
+
```
|
269
|
+
~/syntax_tree/lib/syntax_tree/visitor.rb:46:in `visit_method': Invalid visit method: visit_binar (SyntaxTree::Visitor::VisitMethodError)
|
270
|
+
Did you mean? visit_binary
|
271
|
+
visit_in
|
272
|
+
visit_ivar
|
273
|
+
from (irb):2:in `<class:ArithmeticVisitor>'
|
274
|
+
from (irb):1:in `<main>'
|
275
|
+
from bin/console:8:in `<main>'
|
276
|
+
```
|
277
|
+
|
278
|
+
## Language server
|
279
|
+
|
280
|
+
Syntax Tree additionally ships with a language server conforming to the [language server protocol](https://microsoft.github.io/language-server-protocol/). It can be invoked through the CLI by running:
|
57
281
|
|
58
282
|
```sh
|
59
|
-
|
60
|
-
|
283
|
+
stree lsp
|
284
|
+
```
|
285
|
+
|
286
|
+
By default, the language server is relatively minimal, mostly meant to provide a registered formatter for the Ruby language. However there are a couple of additional niceties baked in. There are related projects that configure and use this language server within IDEs. For example, to use this code with VSCode, see [ruby-syntax-tree/vscode-syntax-tree](https://github.com/ruby-syntax-tree/vscode-syntax-tree).
|
287
|
+
|
288
|
+
### textDocument/formatting
|
289
|
+
|
290
|
+
As mentioned above, the language server responds to formatting requests with the formatted document. It typically responds on the order of tens of milliseconds, so it should be fast enough for any IDE.
|
291
|
+
|
292
|
+
### textDocument/inlayHints
|
293
|
+
|
294
|
+
The language server also responds to the relatively new inlay hints request. This request allows the language server to define additional information that should exist in the source code as helpful hints to the developer. In our case we use it to display things like implicit parentheses. For example, if you had the following code:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
1 + 2 * 3
|
61
298
|
```
|
62
299
|
|
63
|
-
|
300
|
+
Implicity, the `2 * 3` is going to be executed first because the `*` operator has higher precedence than the `+` operator. However, to ease mental overhead, our language server includes small parentheses to make this explicit, as in:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
1 + ₍2 * 3₎
|
304
|
+
```
|
64
305
|
|
65
|
-
|
306
|
+
### syntaxTree/visualizing
|
66
307
|
|
67
|
-
|
308
|
+
The language server additionally includes this custom request to return a textual representation of the syntax tree underlying the source code of a file. Language server clients can use this to (for example) open an additional tab with this information displayed.
|
68
309
|
|
69
310
|
## Contributing
|
70
311
|
|
data/lib/syntax_tree/cli.rb
CHANGED
@@ -124,7 +124,7 @@ module SyntaxTree
|
|
124
124
|
start = Time.now
|
125
125
|
|
126
126
|
formatted = handler.format(source)
|
127
|
-
File.write(filepath, formatted)
|
127
|
+
File.write(filepath, formatted) if filepath != :stdin
|
128
128
|
|
129
129
|
color = source == formatted ? Color.gray(filepath) : filepath
|
130
130
|
delta = ((Time.now - start) * 1000).round
|
@@ -191,11 +191,6 @@ module SyntaxTree
|
|
191
191
|
return 0
|
192
192
|
end
|
193
193
|
|
194
|
-
if arguments.empty?
|
195
|
-
warn(HELP)
|
196
|
-
return 1
|
197
|
-
end
|
198
|
-
|
199
194
|
action =
|
200
195
|
case name
|
201
196
|
when "a", "ast"
|
@@ -215,6 +210,13 @@ module SyntaxTree
|
|
215
210
|
return 1
|
216
211
|
end
|
217
212
|
|
213
|
+
# If we're not reading from stdin and the user didn't supply and
|
214
|
+
# filepaths to be read, then we exit with the usage message.
|
215
|
+
if STDIN.tty? && arguments.empty?
|
216
|
+
warn(HELP)
|
217
|
+
return 1
|
218
|
+
end
|
219
|
+
|
218
220
|
# If there are any plugins specified on the command line, then load them
|
219
221
|
# by requiring them here. We do this by transforming something like
|
220
222
|
#
|
@@ -224,40 +226,34 @@ module SyntaxTree
|
|
224
226
|
#
|
225
227
|
# require "syntax_tree/haml"
|
226
228
|
#
|
227
|
-
if arguments.first
|
229
|
+
if arguments.first&.start_with?("--plugins=")
|
228
230
|
plugins = arguments.shift[/^--plugins=(.*)$/, 1]
|
229
231
|
plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
|
230
232
|
end
|
231
233
|
|
234
|
+
# Track whether or not there are any errors from any of the files that
|
235
|
+
# we take action on so that we can properly clean up and exit.
|
232
236
|
errored = false
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
if error.lineno
|
246
|
-
highlight_error(error, source)
|
247
|
-
else
|
248
|
-
warn(error.message)
|
249
|
-
warn(error.backtrace)
|
250
|
-
end
|
251
|
-
|
252
|
-
errored = true
|
253
|
-
rescue Check::UnformattedError, Debug::NonIdempotentFormatError
|
254
|
-
errored = true
|
255
|
-
rescue => error
|
256
|
-
warn(error.message)
|
257
|
-
warn(error.backtrace)
|
258
|
-
errored = true
|
259
|
-
end
|
237
|
+
|
238
|
+
each_file(arguments) do |handler, filepath, source|
|
239
|
+
action.run(handler, filepath, source)
|
240
|
+
rescue Parser::ParseError => error
|
241
|
+
warn("Error: #{error.message}")
|
242
|
+
|
243
|
+
if error.lineno
|
244
|
+
highlight_error(error, source)
|
245
|
+
else
|
246
|
+
warn(error.message)
|
247
|
+
warn(error.backtrace)
|
260
248
|
end
|
249
|
+
|
250
|
+
errored = true
|
251
|
+
rescue Check::UnformattedError, Debug::NonIdempotentFormatError
|
252
|
+
errored = true
|
253
|
+
rescue => error
|
254
|
+
warn(error.message)
|
255
|
+
warn(error.backtrace)
|
256
|
+
errored = true
|
261
257
|
end
|
262
258
|
|
263
259
|
if errored
|
@@ -271,6 +267,22 @@ module SyntaxTree
|
|
271
267
|
|
272
268
|
private
|
273
269
|
|
270
|
+
def each_file(arguments)
|
271
|
+
if STDIN.tty?
|
272
|
+
arguments.each do |pattern|
|
273
|
+
Dir.glob(pattern).each do |filepath|
|
274
|
+
next unless File.file?(filepath)
|
275
|
+
|
276
|
+
handler = HANDLERS[File.extname(filepath)]
|
277
|
+
source = handler.read(filepath)
|
278
|
+
yield handler, filepath, source
|
279
|
+
end
|
280
|
+
end
|
281
|
+
else
|
282
|
+
yield HANDLERS[".rb"], :stdin, STDIN.read
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
274
286
|
# Highlights a snippet from a source and parse error.
|
275
287
|
def highlight_error(error, source)
|
276
288
|
lines = source.lines
|
@@ -27,20 +27,6 @@ module SyntaxTree
|
|
27
27
|
after[location.start_char + "rescue".length] << " StandardError"
|
28
28
|
end
|
29
29
|
|
30
|
-
# Adds the implicitly referenced value (local variable or method call)
|
31
|
-
# that is added into a hash when the value of a key-value pair is omitted.
|
32
|
-
# For example,
|
33
|
-
#
|
34
|
-
# { value: }
|
35
|
-
#
|
36
|
-
# becomes
|
37
|
-
#
|
38
|
-
# { value: value }
|
39
|
-
#
|
40
|
-
def missing_hash_value(key, location)
|
41
|
-
after[location.end_char] << " #{key}"
|
42
|
-
end
|
43
|
-
|
44
30
|
# Adds implicit parentheses around certain expressions to make it clear
|
45
31
|
# which subexpression will be evaluated first. For example,
|
46
32
|
#
|
@@ -69,8 +55,6 @@ module SyntaxTree
|
|
69
55
|
case [parent_node, child_node]
|
70
56
|
in _, Rescue[exception: nil, location:]
|
71
57
|
inlay_hints.bare_rescue(location)
|
72
|
-
in _, Assoc[key: Label[value: key], value: nil, location:]
|
73
|
-
inlay_hints.missing_hash_value(key[0...-1], location)
|
74
58
|
in Assign | Binary | IfOp | OpAssign, IfOp[location:]
|
75
59
|
inlay_hints.precedence_parentheses(location)
|
76
60
|
in Assign | OpAssign, Binary[location:]
|