syntax_tree 2.4.0 → 2.6.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: '0804ba6e87e03ec83616511679283c17dc5c6250c5718c9e25aa218117abc2a1'
4
- data.tar.gz: 485b0e850086f9b3cc949af3308edbd9f6a75b9ae0c13dda56a1eaf9847d2a28
3
+ metadata.gz: f3d700916c1fab01ddc92117fd2b99c26b699df9e03f06b83eafa1415167ef7d
4
+ data.tar.gz: f5499912fe7bf422b44360bf66ce3f7e74145f3a2cc3ef5668e20fc97bd61948
5
5
  SHA512:
6
- metadata.gz: f85fd10aec38d5d66f77359620ee37ad8b3494546763d96effc5ff0395a2bfdaacb3ade3698238d09201955a6e452a6b8ee52b575c9d17b00ca6fda17dfdbc79
7
- data.tar.gz: 61faf8fc45245d9795717aa9a54c7bb4ccd2bc197facf0f8c9eeb9e55ce5dd04ddea63697d4e1c1cd7cf2663361ba8a078bf664f9407349860840958ce7d3c17
6
+ metadata.gz: e287bcace78e800140ce6b30044a684587e8514f7f0b4c8a57f7b4f3926717a96eb966828e42325a44c1af23715cc602c3cb327dcab27d5390b0353b6a595744
7
+ data.tar.gz: 7cb34f0d9d423b3129033f04dc543d1bb9b19e62dcc7a9a140eb5c3a3159caaf389746930d15e6556df753e10f0651e38ab5e4db95bad63737836e14f14a86c1
@@ -38,7 +38,7 @@ jobs:
38
38
  ruby-version: '3.1'
39
39
  - name: Check
40
40
  run: |
41
- bundle exec rake check
41
+ bundle exec rake stree:check
42
42
  bundle exec rubocop
43
43
 
44
44
  automerge:
data/CHANGELOG.md CHANGED
@@ -6,6 +6,33 @@ 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.6.0] - 2022-05-16
10
+
11
+ ### Added
12
+
13
+ - [#74](https://github.com/ruby-syntax-tree/syntax_tree/pull/74) - Add Rake test to run check and format commands.
14
+ - [#83](https://github.com/ruby-syntax-tree/syntax_tree/pull/83) - Add a trailing commas plugin.
15
+ - [#84](https://github.com/ruby-syntax-tree/syntax_tree/pull/84) - Handle lambda block-local variables.
16
+
17
+ ### Changed
18
+
19
+ - [#85](https://github.com/ruby-syntax-tree/syntax_tree/pull/85) - Better handle trailing operators on command calls.
20
+
21
+ ## [2.5.0] - 2022-05-13
22
+
23
+ ### Added
24
+
25
+ - [#79](https://github.com/ruby-syntax-tree/syntax_tree/pull/79) - Support an optional `maxwidth` second argument to `SyntaxTree.format`.
26
+
27
+ ### Changed
28
+
29
+ - [#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.
30
+ - [#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.
31
+
32
+ ## [2.4.1] - 2022-05-10
33
+
34
+ - [#73](https://github.com/ruby-syntax-tree/syntax_tree/pull/73) - Fix nested hash patterns from accidentally adding a `then` to their output.
35
+
9
36
  ## [2.4.0] - 2022-05-07
10
37
 
11
38
  ### Added
@@ -209,7 +236,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
209
236
 
210
237
  - 🎉 Initial release! 🎉
211
238
 
212
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...HEAD
239
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.6.0...HEAD
240
+ [2.6.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.5.0...v2.6.0
241
+ [2.5.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.1...v2.5.0
242
+ [2.4.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...v2.4.1
213
243
  [2.4.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.1...v2.4.0
214
244
  [2.3.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.0...v2.3.1
215
245
  [2.3.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.2.0...v2.3.0
data/Gemfile CHANGED
@@ -3,5 +3,3 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
7
- gem "rubocop"
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (2.4.0)
4
+ syntax_tree (2.6.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.3.1)
19
+ regexp_parser (2.4.0)
18
20
  rexml (3.2.5)
19
- rubocop (1.29.0)
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/README.md CHANGED
@@ -37,7 +37,10 @@ It is built with only standard library dependencies. It additionally ships with
37
37
  - [textDocument/inlayHints](#textdocumentinlayhints)
38
38
  - [syntaxTree/visualizing](#syntaxtreevisualizing)
39
39
  - [Plugins](#plugins)
40
+ - [Configuration](#configuration)
41
+ - [Languages](#languages)
40
42
  - [Integration](#integration)
43
+ - [Rake](#rake)
41
44
  - [RuboCop](#rubocop)
42
45
  - [VSCode](#vscode)
43
46
  - [Contributing](#contributing)
@@ -223,7 +226,7 @@ This function takes an input string containing Ruby code and returns the syntax
223
226
 
224
227
  ### SyntaxTree.format(source)
225
228
 
226
- This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string.
229
+ This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string. You can optionally pass a second argument to this method as well that is the maximum width to print. It defaults to `80`.
227
230
 
228
231
  ## Nodes
229
232
 
@@ -404,7 +407,20 @@ The language server additionally includes this custom request to return a textua
404
407
 
405
408
  ## Plugins
406
409
 
407
- You can register additional languages that can flow through the same CLI with Syntax Tree's plugin system. To register a new language, call:
410
+ You can register additional configuration and additional languages that can flow through the same CLI with Syntax Tree's plugin system. When invoking the CLI, you pass through the list of plugins with the `--plugins` options to the commands that accept them. They should be a comma-delimited list. When the CLI first starts, it will require the files corresponding to those names.
411
+
412
+ ### Configuration
413
+
414
+ To register additional configuration, define a file somewhere in your load path named `syntax_tree/my_plugin`. Then when invoking the CLI, you will pass `--plugins=my_plugin`. To require multiple, separate them by a comma. In this way, you can modify Syntax Tree however you would like. Some plugins ship with Syntax Tree itself. They are:
415
+
416
+ * `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
417
+ * `plugin/trailing_comma` - This will put trailing commas into multiline array literals, hash literals, and method calls that can support trailing commas.
418
+
419
+ If you're using Syntax Tree as a library, you should require those files directly.
420
+
421
+ ### Languages
422
+
423
+ To register a new language, call:
408
424
 
409
425
  ```ruby
410
426
  SyntaxTree.register_handler(".mylang", MyLanguage)
@@ -416,18 +432,56 @@ In this case, whenever the CLI encounters a filepath that ends with the given ex
416
432
  * `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface.
417
433
  * `MyLanguage.format(source)` - this should return the formatted version of the given source.
418
434
 
419
- 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.
435
+ Below are listed all of the "official" language plugins hosted under the same GitHub organization, which can be used as references for how to implement other plugins.
420
436
 
421
- * [SyntaxTree::Haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
422
- * [SyntaxTree::JSON](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
423
- * [SyntaxTree::RBS](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
424
-
425
- When invoking the CLI, you pass through the list of plugins with the `--plugins` options to the commands that accept them. They should be a comma-delimited list. When the CLI first starts, it will require the files corresponding to those names.
437
+ * [haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
438
+ * [json](https://github.com/ruby-syntax-tree/syntax_tree-json) for JSON.
439
+ * [rbs](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
426
440
 
427
441
  ## Integration
428
442
 
429
443
  Syntax Tree's goal is to seemlessly integrate into your workflow. To this end, it provides a couple of additional tools beyond the CLI and the Ruby library.
430
444
 
445
+ ### Rake
446
+
447
+ Syntax Tree ships with the ability to define [rake](https://github.com/ruby/rake) tasks that will trigger runs of the CLI. To define them in your application, add the following configuration to your `Rakefile`:
448
+
449
+ ```ruby
450
+ require "syntax_tree/rake_tasks"
451
+ SyntaxTree::Rake::CheckTask.new
452
+ SyntaxTree::Rake::WriteTask.new
453
+ ```
454
+
455
+ These calls will define `rake stree:check` and `rake stree:write` (equivalent to calling `stree check` and `stree write` with the CLI respectively). You can configure them by either passing arguments to the `new` method or by using a block.
456
+
457
+ #### `name`
458
+
459
+ If you'd like to change the default name of the rake task, you can pass that as the first argument, as in:
460
+
461
+ ```ruby
462
+ SyntaxTree::Rake::WriteTask.new(:format)
463
+ ```
464
+
465
+ #### `source_files`
466
+
467
+ If you wanted to configure Syntax Tree to check or write different files than the default (`lib/**/*.rb`), you can set the `source_files` field, as in:
468
+
469
+ ```ruby
470
+ SyntaxTree::Rake::WriteTask.new do |t|
471
+ t.source_files = FileList[%w[Gemfile Rakefile lib/**/*.rb test/**/*.rb]]
472
+ end
473
+ ```
474
+
475
+ #### `plugins`
476
+
477
+ If you're running Syntax Tree with plugins (either your own or the pre-built ones), you can pass that to the `plugins` field, as in:
478
+
479
+ ```ruby
480
+ SyntaxTree::Rake::WriteTask.new do |t|
481
+ t.plugins = ["plugin/single_quotes"]
482
+ end
483
+ ```
484
+
431
485
  ### RuboCop
432
486
 
433
487
  RuboCop and Syntax Tree serve different purposes, but there is overlap with some of RuboCop's functionality. Syntax Tree provides a RuboCop configuration file to disable rules that are redundant with Syntax Tree. To use this configuration file, add the following snippet to the top of your project's `.rubocop.yml`:
data/Rakefile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/gem_tasks"
4
4
  require "rake/testtask"
5
+ require "syntax_tree/rake_tasks"
5
6
 
6
7
  Rake::TestTask.new(:test) do |t|
7
8
  t.libs << "test"
@@ -11,24 +12,8 @@ end
11
12
 
12
13
  task default: :test
13
14
 
14
- FILEPATHS = %w[
15
- Gemfile
16
- Rakefile
17
- syntax_tree.gemspec
18
- lib/**/*.rb
19
- test/*.rb
20
- ].freeze
15
+ SOURCE_FILES =
16
+ FileList[%w[Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb test/*.rb]]
21
17
 
22
- task :syntax_tree do
23
- $:.unshift File.expand_path("lib", __dir__)
24
- require "syntax_tree"
25
- require "syntax_tree/cli"
26
- end
27
-
28
- task check: :syntax_tree do
29
- exit SyntaxTree::CLI.run(["check"] + FILEPATHS)
30
- end
31
-
32
- task format: :syntax_tree do
33
- exit SyntaxTree::CLI.run(["write"] + FILEPATHS)
34
- end
18
+ SyntaxTree::Rake::CheckTask.new { |t| t.source_files = SOURCE_FILES }
19
+ SyntaxTree::Rake::WriteTask.new { |t| t.source_files = SOURCE_FILES }
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class Formatter
5
+ # This module overrides the quote method on the formatter to use single
6
+ # quotes for everything instead of double quotes.
7
+ module SingleQuotes
8
+ def quote
9
+ "'"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class Formatter
5
+ # This module overrides the trailing_comma? method on the formatter to
6
+ # return true.
7
+ module TrailingComma
8
+ def trailing_comma?
9
+ true
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,11 +3,16 @@
3
3
  module SyntaxTree
4
4
  # A slightly enhanced PP that knows how to format recursively including
5
5
  # comments.
6
- class Formatter < PP
6
+ class Formatter < PrettierPrint
7
7
  COMMENT_PRIORITY = 1
8
8
  HEREDOC_PRIORITY = 2
9
9
 
10
- attr_reader :source, :stack, :quote
10
+ attr_reader :source, :stack
11
+
12
+ # These options are overridden in plugins to we need to make sure they are
13
+ # available here.
14
+ attr_reader :quote, :trailing_comma
15
+ alias trailing_comma? trailing_comma
11
16
 
12
17
  def initialize(source, ...)
13
18
  super(...)
@@ -15,6 +20,7 @@ module SyntaxTree
15
20
  @source = source
16
21
  @stack = []
17
22
  @quote = "\""
23
+ @trailing_comma = false
18
24
  end
19
25
 
20
26
  def self.format(source, node)