syntax_tree 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46b573afdda378e664e11b1f5acab2c89ce0fdd2c8b3d4c6400d74e133aaffc3
4
- data.tar.gz: 6be0570ce471fe3f34c53730d7095d9b81be5b7e3e2e84ac5724ba95d7ac7362
3
+ metadata.gz: 5db5f5652e0786475f56ea361b693340998d3f1505d57e2300182d2cc811bcf3
4
+ data.tar.gz: 3ee5db660541f5355e80fe7d63c4995e30c5089b45286ab1e7ac29ccc62c8bab
5
5
  SHA512:
6
- metadata.gz: 36236536a1dda5e6e2907b82c383b74f207f5e2b64f282490cf26161d62d748d1cdf51b68c393cd60f1777d2c5434c294ad2e243f2575faae34c42ac44155f76
7
- data.tar.gz: 5c2e0ee682e5847287cee5a6ca4357c51e4172bacf7aa98b780e427fb7d0912b6bd829bbf6236baa294a919be9be5730d8c7e74b27a0ca630a29a91c37540324
6
+ metadata.gz: 9be4902085676d0c4c76eaa29c5b00878916409f2b63a519376ab1a9521cf6204c7aa0a540e61cfa607079ebe17ef42c960c63ab752da300fbdf653be279e4e8
7
+ data.tar.gz: b2923a9ed6e61e3e14b353b200e49ffe39556081a25fb409cf3af4bd8de4e14aa6229c44c20bb76e67b0efbf63b463cdb0a307ff43975ca5a08a362e6af8aac6
data/CHANGELOG.md CHANGED
@@ -6,6 +6,50 @@ 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.3.0] - 2022-04-22
10
+
11
+ ### Added
12
+
13
+ - [#52](https://github.com/ruby-syntax-tree/syntax_tree/pull/52) - `SyntaxTree::Formatter.format` for formatting an already parsed node.
14
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - `if` and `unless` can now be transformed into ternaries if they're simple enough.
15
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Nicely format call chains by one indentation.
16
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Handle trailing operators in call chains when they are necessary because of comments.
17
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Add some specialized formatting for Sorbet `sig` blocks to make them appear nicer.
18
+
19
+ ### Changed
20
+
21
+ - [#53](https://github.com/ruby-syntax-tree/syntax_tree/pull/53) - Optional keyword arguments on method declarations have a value of `nil` now instead of `false`. This makes it easier to use the visitor.
22
+ - [#54](https://github.com/ruby-syntax-tree/syntax_tree/pull/54) - Flow control operators can now skip parentheses for simple, individual arguments. e.g., `break(1)` becomes `break 1`.
23
+ - [#54](https://github.com/ruby-syntax-tree/syntax_tree/pull/54) - Don't allow modifier conditionals to modify ternaries.
24
+ - [#55](https://github.com/ruby-syntax-tree/syntax_tree/pull/55) - Skip parentheses and brackets on arrays for flow control operators. e.g., `break([1, 2, 3])` becomes `break 1, 2, 3`.
25
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Don't add parentheses to method calls if you don't need them.
26
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Format comments on empty parameter sets. e.g., `def foo # bar` should keeps its comment.
27
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - `%s[]` symbols on assignments should not indent to the next line.
28
+ - [#56](https://github.com/ruby-syntax-tree/syntax_tree/pull/56) - Empty hash and array literals with comments inside of them should be formatted correctly.
29
+
30
+ ## [2.2.0] - 2022-04-19
31
+
32
+ ### Added
33
+
34
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::Location` nodes now have pattern matching.
35
+ - [#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.
36
+
37
+ ### Changed
38
+
39
+ - [#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.
40
+ - [#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,))`.
41
+ - [#51](https://github.com/ruby-syntax-tree/syntax_tree/pull/51) - `SyntaxTree::FndPtn` has much improved parsing now.
42
+
43
+ ## [2.1.1] - 2022-04-16
44
+
45
+ ### Changed
46
+
47
+ - [#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.
48
+ - [#47](https://github.com/ruby-syntax-tree/syntax_tree/pull/47) - Handle expressions like `not()`.
49
+ - [#48](https://github.com/ruby-syntax-tree/syntax_tree/pull/48) - Handle special call syntax with `::` operator.
50
+ - [#49](https://github.com/ruby-syntax-tree/syntax_tree/pull/49) - Handle expressions like `case foo; in {}; end`.
51
+ - [#50](https://github.com/ruby-syntax-tree/syntax_tree/pull/50) - Parsing expressions like `case foo; in **nil; end`.
52
+
9
53
  ## [2.1.0] - 2022-04-12
10
54
 
11
55
  ### Added
@@ -143,7 +187,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
143
187
 
144
188
  - 🎉 Initial release! 🎉
145
189
 
146
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.0...HEAD
190
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.0...HEAD
191
+ [2.3.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.2.0...v2.3.0
192
+ [2.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.1...v2.2.0
193
+ [2.1.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.1.0...v2.1.1
147
194
  [2.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.1...v2.1.0
148
195
  [2.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.0.0...v2.0.1
149
196
  [2.0.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v1.2.0...v2.0.0
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,28 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (2.1.0)
4
+ syntax_tree (2.3.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.2.0)
14
- ast (~> 2.4.1)
15
11
  rake (13.0.6)
16
- ruby_parser (3.19.1)
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
20
  arm64-darwin-21
@@ -32,15 +24,11 @@ PLATFORMS
32
24
  x86_64-linux
33
25
 
34
26
  DEPENDENCIES
35
- benchmark-ips
36
27
  bundler
37
28
  minitest
38
- parser
39
29
  rake
40
- ruby_parser
41
30
  simplecov
42
- stackprof
43
31
  syntax_tree!
44
32
 
45
33
  BUNDLED WITH
46
- 2.2.31
34
+ 2.3.6
data/README.md CHANGED
@@ -33,6 +33,7 @@ It is built with only standard library dependencies. It additionally ships with
33
33
  - [textDocument/formatting](#textdocumentformatting)
34
34
  - [textDocument/inlayHints](#textdocumentinlayhints)
35
35
  - [syntaxTree/visualizing](#syntaxtreevisualizing)
36
+ - [Plugins](#plugins)
36
37
  - [Contributing](#contributing)
37
38
  - [License](#license)
38
39
 
@@ -307,6 +308,26 @@ Implicity, the `2 * 3` is going to be executed first because the `*` operator ha
307
308
 
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.
309
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.
324
+
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.
326
+
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).
330
+
310
331
  ## Contributing
311
332
 
312
333
  Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree.
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`
@@ -17,6 +17,13 @@ module SyntaxTree
17
17
  @quote = "\""
18
18
  end
19
19
 
20
+ def self.format(source, node)
21
+ formatter = new(source, [])
22
+ node.format(formatter)
23
+ formatter.flush
24
+ formatter.output.join
25
+ end
26
+
20
27
  def format(node, stackable: true)
21
28
  stack << node if stackable
22
29
  doc = nil
@@ -43,7 +50,7 @@ module SyntaxTree
43
50
  # Print all comments that were found after the node.
44
51
  trailing.each do |comment|
45
52
  line_suffix(priority: COMMENT_PRIORITY) do
46
- text(" ")
53
+ comment.inline? ? text(" ") : breakable
47
54
  comment.format(self)
48
55
  break_parent
49
56
  end