syntax_tree 2.0.1 → 2.2.0

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: 3595c635a0aa27ef38bab6d57816ece35357b4c16d6c7dd6e460cf0dc601acfd
4
- data.tar.gz: 2d5bcca68c0340efd9bade62ce2c933036a9c711cad68b0dd494d9eaf24eb16a
3
+ metadata.gz: adfcb697cee07c1c1fa0ad15538079a1fb7f40cfe622317b4cff91546555e463
4
+ data.tar.gz: 27fc9fe177d07ba066314a8b80f542ae8e6e94fff85f9edaf82cefc32616a7d1
5
5
  SHA512:
6
- metadata.gz: a4e2f4e06886a26e32627389e7fa178a2a2991b9e8dda80cc234804659320797cfdb4d76646d293668f8ac64748f5991023ca33efd68f9771e67653f64662cd2
7
- data.tar.gz: ae29dcc05bb1e83a4c43405d9044766b896fb4c5af9262c909bfdf5d4824228426c493fd3345c4ff4f64d57384068b3afba78913fa4bf2b02d866deed1c40ef5
6
+ metadata.gz: 975dc25af26617665dec05c4a902732b693045be67917dba0f0b1e4c8a7e3016a8c69d1556f850c8be2b9f2d6fb07d06d6ee0d69245cd71ab5067f4d0690e909
7
+ data.tar.gz: 302d3744abc6cea5ce13d094322ff4071dc3c9487442f7c74fccd2a1ff7bb93ddc98017a5b5997369795df80b098981f8cbbe34c54315db4ccffc7a8e92f546c
@@ -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: '3.1'
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,44 @@ 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.2.0] - 2022-04-19
10
+
11
+ ### Added
12
+
13
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::Location` nodes now have pattern matching.
14
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::Heredoc` now have a `dedent` field that indicates the number of spaces to strip from the beginning of the string content.
15
+
16
+ ### Changed
17
+
18
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::HshPtn` will now add a `then` if you use a bare `**` and `SyntaxTree::AryPtn` will do the same for a bare `*` on the end.
19
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::MLHSParen` now has a comma field in case a trailing comma has been added to a parenthesis destructuring, as in `((foo,))`.
20
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::FndPtn` has much improved parsing now.
21
+
22
+ ## [2.1.1] - 2022-04-16
23
+
24
+ ### Changed
25
+
26
+ - [#45](https://github.com/ruby-syntax-tree/syntax_tree/issues/45) - Fix parsing expressions like `foo.instance_exec(&T.must(block))`, where there are two `args_add_block` calls with a single `&`. Previously it was associating the `&` with the wrong block.
27
+ - [#47](https://github.com/ruby-syntax-tree/syntax_tree/pull/47) - Handle expressions like `not()`.
28
+ - [#48](https://github.com/ruby-syntax-tree/syntax_tree/pull/48) - Handle special call syntax with `::` operator.
29
+ - [#49](https://github.com/ruby-syntax-tree/syntax_tree/pull/49) - Handle expressions like `case foo; in {}; end`.
30
+ - [#50](https://github.com/ruby-syntax-tree/syntax_tree/pull/50) - Parsing expressions like `case foo; in **nil; end`.
31
+
32
+ ## [2.1.0] - 2022-04-12
33
+
34
+ ### Added
35
+
36
+ - The `SyntaxTree::Visitor` class now implements the visitor pattern for Ruby nodes.
37
+ - The `SyntaxTree::Visitor.visit_method(name)` method.
38
+ - Support for Ruby 2.7.
39
+ - Support for comments on `rescue` and `else` keywords.
40
+ - `SyntaxTree::Location` now additionally has `start_column` and `end_column`.
41
+ - The CLI now accepts content over STDIN for the `ast`, `check`, `debug`, `doc`, `format`, and `write` commands.
42
+
43
+ ### Removed
44
+
45
+ - The missing hash value inlay hints have been removed.
46
+
9
47
  ## [2.0.1] - 2022-03-31
10
48
 
11
49
  ### Changed
@@ -128,7 +166,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
128
166
 
129
167
  - 🎉 Initial release! 🎉
130
168
 
131
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.0...HEAD
169
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.2.0...HEAD
170
+ [2.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.1...v2.2.0
171
+ [2.1.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.0...v2.1.1
172
+ [2.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.1...v2.1.0
173
+ [2.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.0...v2.0.1
132
174
  [2.0.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.2.0...v2.0.0
133
175
  [1.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.1.1...v1.2.0
134
176
  [1.1.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.1.0...v1.1.1
data/Gemfile CHANGED
@@ -3,8 +3,3 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
7
- gem "benchmark-ips"
8
- gem "parser"
9
- gem "ruby_parser"
10
- gem "stackprof"
data/Gemfile.lock CHANGED
@@ -1,44 +1,30 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (2.0.1)
4
+ syntax_tree (2.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.2)
10
- benchmark-ips (2.10.0)
11
9
  docile (1.4.0)
12
10
  minitest (5.15.0)
13
- parser (3.1.1.0)
14
- ast (~> 2.4.1)
15
11
  rake (13.0.6)
16
- ruby_parser (3.19.0)
17
- sexp_processor (~> 4.16)
18
- sexp_processor (4.16.0)
19
12
  simplecov (0.21.2)
20
13
  docile (~> 1.1)
21
14
  simplecov-html (~> 0.11)
22
15
  simplecov_json_formatter (~> 0.1)
23
16
  simplecov-html (0.12.3)
24
- simplecov_json_formatter (0.1.3)
25
- stackprof (0.2.19)
17
+ simplecov_json_formatter (0.1.4)
26
18
 
27
19
  PLATFORMS
28
- x86_64-darwin-19
29
20
  x86_64-darwin-21
30
- x86_64-linux
31
21
 
32
22
  DEPENDENCIES
33
- benchmark-ips
34
23
  bundler
35
24
  minitest
36
- parser
37
25
  rake
38
- ruby_parser
39
26
  simplecov
40
- stackprof
41
27
  syntax_tree!
42
28
 
43
29
  BUNDLED WITH
44
- 2.2.31
30
+ 2.3.6
data/README.md CHANGED
@@ -7,11 +7,51 @@
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
- A fast Ruby parser and formatter with only standard library dependencies.
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
+ - [Plugins](#plugins)
37
+ - [Contributing](#contributing)
38
+ - [License](#license)
11
39
 
12
40
  ## Installation
13
41
 
14
- Add this line to your application's Gemfile:
42
+ 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:
43
+
44
+ ```sh
45
+ gem install syntax_tree
46
+ ```
47
+
48
+ To run the CLI with the gem installed globally, you would run:
49
+
50
+ ```sh
51
+ stree version
52
+ ```
53
+
54
+ 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
55
 
16
56
  ```ruby
17
57
  gem "syntax_tree"
@@ -19,52 +59,274 @@ gem "syntax_tree"
19
59
 
20
60
  And then execute:
21
61
 
22
- $ bundle install
62
+ ```sh
63
+ bundle install
64
+ ```
65
+
66
+ To run the CLI with the gem installed in your gem bundle, you would run:
23
67
 
24
- Or install it yourself as:
68
+ ```sh
69
+ bundle exec stree version
70
+ ```
25
71
 
26
- $ gem install syntax_tree
72
+ ## CLI
27
73
 
28
- ## Usage
74
+ 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.
29
75
 
30
- From code:
76
+ ### ast
31
77
 
32
- ```ruby
33
- require "syntax_tree"
78
+ This command will print out a textual representation of the syntax tree associated with each of the files it finds. To execute, run:
79
+
80
+ ```sh
81
+ stree ast path/to/file.rb
82
+ ```
83
+
84
+ For a file that contains `1 + 1`, you will receive:
34
85
 
35
- pp SyntaxTree.parse(source) # print out the AST
36
- puts SyntaxTree.format(source) # format the AST
37
86
  ```
87
+ (program (statements (binary (int "1") + (int "1"))))
88
+ ```
89
+
90
+ ### check
38
91
 
39
- From the CLI:
92
+ 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.
40
93
 
41
94
  ```sh
42
- $ stree ast program.rb
43
- (program
44
- (statements
45
- ...
95
+ stree check path/to/file.rb
96
+ ```
97
+
98
+ For a file that matches the expected format, you will receive:
99
+
100
+ ```
101
+ All files matched expected format.
102
+ ```
103
+
104
+ If there are files with unformatted code, you will receive:
105
+
106
+ ```
107
+ [warn] path/to/file.rb
108
+ The listed files did not match the expected format.
46
109
  ```
47
110
 
48
- or
111
+ ### format
112
+
113
+ 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.
49
114
 
50
115
  ```sh
51
- $ stree format program.rb
52
- class MyClass
53
- ...
116
+ stree format path/to/file.rb
54
117
  ```
55
118
 
56
- or
119
+ For a file that contains `1 + 1`, you will receive:
120
+
121
+ ```ruby
122
+ 1 + 1
123
+ ```
124
+
125
+ ### write
126
+
127
+ 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.
57
128
 
58
129
  ```sh
59
- $ stree write program.rb
60
- program.rb 1ms
130
+ stree write path/to/file.rb
131
+ ```
132
+
133
+ 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.
134
+
135
+ ```
136
+ path/to/file.rb 0ms
61
137
  ```
62
138
 
63
- ## Development
139
+ ## Library
140
+
141
+ Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.
142
+
143
+ ### SyntaxTree.read(filepath)
144
+
145
+ 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).
146
+
147
+ ### SyntaxTree.parse(source)
148
+
149
+ 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.
150
+
151
+ ### SyntaxTree.format(source)
152
+
153
+ This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string.
154
+
155
+ ## Nodes
156
+
157
+ 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.
158
+
159
+ ### child_nodes
160
+
161
+ 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.
162
+
163
+ ```ruby
164
+ program = SyntaxTree.parse("1 + 1")
165
+ program.child_nodes.first.child_nodes.first
166
+ # => (binary (int "1") :+ (int "1"))
167
+ ```
168
+
169
+ ### Pattern matching
170
+
171
+ 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:
172
+
173
+ ```ruby
174
+ program = SyntaxTree.parse("1 + 1")
175
+ program => { statements: { body: [binary] } }
176
+ binary
177
+ # => (binary (int "1") :+ (int "1"))
178
+ ```
179
+
180
+ Or, with more constraints on the types to ensure we're getting exactly what we expect:
181
+
182
+ ```ruby
183
+ program = SyntaxTree.parse("1 + 1")
184
+ program => SyntaxTree::Program[statements: SyntaxTree::Statements[body: [SyntaxTree::Binary => binary]]]
185
+ binary
186
+ # => (binary (int "1") :+ (int "1"))
187
+ ```
188
+
189
+ ### pretty_print(q)
190
+
191
+ 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:
192
+
193
+ ```ruby
194
+ pp SyntaxTree.parse("1 + 1")
195
+ # (program (statements (binary (int "1") + (int "1"))))
196
+ ```
197
+
198
+ ### to_json(*opts)
199
+
200
+ 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:
201
+
202
+ ```ruby
203
+ program = SyntaxTree.parse("1 + 1")
204
+ program => { statements: { body: [{ left: }] } }
205
+ puts JSON.dump(left)
206
+ # {"type":"int","value":"1","loc":[1,0,1,1],"cmts":[]}
207
+ ```
208
+
209
+ ### format(q)
210
+
211
+ 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:
212
+
213
+ ```ruby
214
+ source = "1+1"
215
+ program = SyntaxTree.parse(source)
216
+ program => { statements: { body: [binary] } }
217
+
218
+ formatter = SyntaxTree::Formatter.new(source, [])
219
+ binary.format(formatter)
220
+
221
+ formatter.flush
222
+ formatter.output.join
223
+ # => "1 + 1"
224
+ ```
225
+
226
+ ## Visitor
227
+
228
+ 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.
229
+
230
+ 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:
231
+
232
+ ```ruby
233
+ class ArithmeticVisitor < SyntaxTree::Visitor
234
+ def visit_binary(node)
235
+ if node in { left: SyntaxTree::Int, operator: :+ | :- | :* | :/, right: SyntaxTree::Int }
236
+ puts "The result is: #{node.left.value.to_i.public_send(node.operator, node.right.value.to_i)}"
237
+ end
238
+ end
239
+ end
240
+
241
+ visitor = ArithmeticVisitor.new
242
+ visitor.visit(SyntaxTree.parse("1 + 1"))
243
+ # The result is: 2
244
+ ```
245
+
246
+ 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:
247
+
248
+ * call `super` (which will do the default and visit all child nodes)
249
+ * call `visit_child_nodes` manually
250
+ * call `visit(child)` with each child that you want to visit
251
+ * call nothing if you're sure you don't want to descend further
252
+
253
+ 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.
254
+
255
+ ### visit_method
256
+
257
+ 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:
258
+
259
+ ```ruby
260
+ class ArithmeticVisitor < SyntaxTree::Visitor
261
+ visit_method def visit_binary(node)
262
+ # ...
263
+ end
264
+ end
265
+ ```
266
+
267
+ 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:
268
+
269
+ ```
270
+ ~/syntax_tree/lib/syntax_tree/visitor.rb:46:in `visit_method': Invalid visit method: visit_binar (SyntaxTree::Visitor::VisitMethodError)
271
+ Did you mean? visit_binary
272
+ visit_in
273
+ visit_ivar
274
+ from (irb):2:in `<class:ArithmeticVisitor>'
275
+ from (irb):1:in `<main>'
276
+ from bin/console:8:in `<main>'
277
+ ```
278
+
279
+ ## Language server
280
+
281
+ 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:
282
+
283
+ ```sh
284
+ stree lsp
285
+ ```
286
+
287
+ 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).
288
+
289
+ ### textDocument/formatting
290
+
291
+ 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.
292
+
293
+ ### textDocument/inlayHints
294
+
295
+ 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:
296
+
297
+ ```ruby
298
+ 1 + 2 * 3
299
+ ```
300
+
301
+ 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:
302
+
303
+ ```ruby
304
+ 1 + ₍2 * 3₎
305
+ ```
306
+
307
+ ### syntaxTree/visualizing
308
+
309
+ 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.
310
+
311
+ ## Plugins
312
+
313
+ You can register additional languages that can flow through the same CLI with Syntax Tree's plugin system. To register a new language, call:
314
+
315
+ ```ruby
316
+ SyntaxTree.register_handler(".mylang", MyLanguage)
317
+ ```
318
+
319
+ In this case, whenever the CLI encounters a filepath that ends with the given extension, it will invoke methods on `MyLanguage` instead of `SyntaxTree` itself. To make sure your object conforms to each of the necessary APIs, it should implement:
320
+
321
+ * `MyLanguage.read(filepath)` - usually this is just an alias to `File.read(filepath)`, but if you need anything else that hook is here.
322
+ * `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface.
323
+ * `MyLanguage.format(source)` - this should return the formatted version of the given source.
64
324
 
65
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
325
+ Below are listed all of the "official" plugins hosted under the same GitHub organization, which can be used as references for how to implement other plugins.
66
326
 
67
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
327
+ * [SyntaxTree::Haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
328
+ * [SyntaxTree::JSON](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
329
+ * [SyntaxTree::RBS](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
68
330
 
69
331
  ## Contributing
70
332
 
data/bin/bench CHANGED
@@ -1,12 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "benchmark/ips"
4
+ require "bundler/inline"
6
5
 
7
- require_relative "../lib/syntax_tree"
8
- require "ruby_parser"
9
- require "parser/current"
6
+ gemfile do
7
+ source "https://rubygems.org"
8
+ gem "benchmark-ips"
9
+ gem "parser", require: "parser/current"
10
+ gem "ruby_parser"
11
+ end
12
+
13
+ $:.unshift(File.expand_path("../lib", __dir__))
14
+ require "syntax_tree"
10
15
 
11
16
  def compare(filepath)
12
17
  prefix = "#{File.expand_path("..", __dir__)}/"
@@ -30,7 +35,7 @@ filepaths = ARGV
30
35
  if filepaths.empty?
31
36
  filepaths = [
32
37
  File.expand_path("bench", __dir__),
33
- File.expand_path("../lib/syntax_tree.rb", __dir__)
38
+ File.expand_path("../lib/syntax_tree/node.rb", __dir__)
34
39
  ]
35
40
  end
36
41
 
data/bin/profile CHANGED
@@ -1,19 +1,28 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "stackprof"
4
+ require "bundler/inline"
6
5
 
7
- filepath = File.expand_path("../lib/syntax_tree", __dir__)
8
- require_relative filepath
6
+ gemfile do
7
+ source "https://rubygems.org"
8
+ gem "stackprof"
9
+ end
10
+
11
+ $:.unshift(File.expand_path("../lib", __dir__))
12
+ require "syntax_tree"
9
13
 
10
14
  GC.disable
11
15
 
12
16
  StackProf.run(mode: :cpu, out: "tmp/profile.dump", raw: true) do
13
- SyntaxTree.format(File.read("#{filepath}.rb"))
17
+ filepath = File.expand_path("../lib/syntax_tree/node.rb", __dir__)
18
+ SyntaxTree.format(File.read(filepath))
14
19
  end
15
20
 
16
21
  GC.enable
17
22
 
18
- `bundle exec stackprof --d3-flamegraph tmp/profile.dump > tmp/flamegraph.html`
19
- puts "open tmp/flamegraph.html"
23
+ File.open("tmp/flamegraph.html", "w") do |file|
24
+ report = Marshal.load(IO.binread("tmp/profile.dump"))
25
+ StackProf::Report.new(report).print_d3_flamegraph(file)
26
+ end
27
+
28
+ `open tmp/flamegraph.html`