Skip to content

Commit 016f0c5

Browse files
committed
Switch parser over to prism
1 parent 476435e commit 016f0c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+5687
-20330
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/main.yml

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
ruby:
13-
- '2.7.0'
14-
- '3.0'
15-
- '3.1'
1613
- '3.2'
1714
- '3.3'
1815
- '3.4'
1916
- truffleruby-head
2017
name: CI
2118
runs-on: ubuntu-latest
22-
env:
23-
CI: true
24-
# TESTOPTS: --verbose
2519
steps:
2620
- uses: actions/checkout@master
2721
- uses: ruby/setup-ruby@v1
@@ -31,18 +25,16 @@ jobs:
3125
- name: Test
3226
run: bundle exec rake test
3327

34-
check:
35-
name: Check
36-
runs-on: ubuntu-latest
37-
env:
38-
CI: true
39-
steps:
40-
- uses: actions/checkout@master
41-
- uses: ruby/setup-ruby@v1
42-
with:
43-
bundler-cache: true
44-
ruby-version: '3.2'
45-
- name: Check
46-
run: |
47-
bundle exec rake stree:check
48-
bundle exec rubocop
28+
# check:
29+
# name: Check
30+
# runs-on: ubuntu-latest
31+
# steps:
32+
# - uses: actions/checkout@master
33+
# - uses: ruby/setup-ruby@v1
34+
# with:
35+
# bundler-cache: true
36+
# ruby-version: '3.2'
37+
# - name: Check
38+
# run: |
39+
# bundle exec rake stree:check
40+
# bundle exec rubocop

.rubocop.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ AllCops:
55
DisplayStyleGuide: true
66
NewCops: enable
77
SuggestExtensions: false
8-
TargetRubyVersion: 2.7
8+
TargetRubyVersion: 3.2
99
Exclude:
1010
- '{.git,.github,.ruby-lsp,bin,coverage,doc,pkg,sorbet,spec,test/fixtures,vendor,tmp}/**/*'
1111
- test.rb
@@ -14,7 +14,7 @@ Gemspec/DevelopmentDependencies:
1414
Enabled: false
1515

1616
Layout/LineLength:
17-
Max: 80
17+
Max: 100
1818

1919
Lint/AmbiguousBlockAssociation:
2020
Enabled: false
@@ -70,6 +70,9 @@ Naming/MethodName:
7070
Naming/MethodParameterName:
7171
Enabled: false
7272

73+
Naming/PredicateMethod:
74+
Enabled: false
75+
7376
Naming/RescuedExceptionsVariableName:
7477
PreferredName: error
7578

Gemfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ source "https://rubygems.org"
44

55
gemspec
66

7-
gem "fiddle"
7+
gem "bundler"
8+
gem "minitest"
9+
gem "rake"
10+
gem "rubocop"
11+
gem "simplecov"

Gemfile.lock

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@ PATH
22
remote: .
33
specs:
44
syntax_tree (6.3.0)
5-
prettier_print (>= 1.2.0)
5+
prism
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
1010
ast (2.4.3)
1111
docile (1.4.1)
12-
fiddle (1.1.8)
13-
json (2.15.1)
12+
json (2.15.2)
1413
language_server-protocol (3.17.0.5)
1514
lint_roller (1.1.0)
1615
minitest (5.26.0)
1716
parallel (1.27.0)
18-
parser (3.3.9.0)
17+
parser (3.3.10.0)
1918
ast (~> 2.4.1)
2019
racc
21-
prettier_print (1.2.1)
2220
prism (1.6.0)
2321
racc (1.8.1)
2422
rainbow (3.1.1)
@@ -43,27 +41,23 @@ GEM
4341
docile (~> 1.1)
4442
simplecov-html (~> 0.11)
4543
simplecov_json_formatter (~> 0.1)
46-
simplecov-html (0.13.1)
44+
simplecov-html (0.13.2)
4745
simplecov_json_formatter (0.1.4)
4846
unicode-display_width (3.2.0)
4947
unicode-emoji (~> 4.1)
5048
unicode-emoji (4.1.0)
5149

5250
PLATFORMS
53-
arm64-darwin-21
54-
ruby
55-
x86_64-darwin-19
56-
x86_64-darwin-21
51+
arm64-darwin-23
5752
x86_64-linux
5853

5954
DEPENDENCIES
6055
bundler
61-
fiddle
6256
minitest
6357
rake
6458
rubocop
6559
simplecov
6660
syntax_tree!
6761

6862
BUNDLED WITH
69-
2.3.6
63+
2.4.19

README.md

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
<img alt="Syntax Tree" height="400px" src="./doc/logo.svg">
33
</div>
44

5-
# SyntaxTree
5+
# Syntax Tree
66

77
[![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)
88
[![Gem Version](https://img.shields.io/gem/v/syntax_tree.svg)](https://rubygems.org/gems/syntax_tree)
99

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.
10+
Syntax Tree is fast Ruby parser built on top of the [prism](https://github.com/ruby/prism) Ruby parser. It is built with only standard library dependencies.
1311

1412
- [Installation](#installation)
1513
- [CLI](#cli)
@@ -19,11 +17,9 @@ It is built with only standard library dependencies. It additionally ships with
1917
- [Configuration](#configuration)
2018
- [Globbing](#globbing)
2119
- [Language server](#language-server)
22-
- [textDocument/formatting](#textdocumentformatting)
2320
- [Customization](#customization)
2421
- [Ignoring code](#ignoring-code)
2522
- [Plugins](#plugins)
26-
- [Languages](#languages)
2723
- [Integration](#integration)
2824
- [Rake](#rake)
2925
- [RuboCop](#rubocop)
@@ -144,16 +140,28 @@ stree write --ignore-files='db/**/*.rb' '**/*.rb'
144140

145141
### Configuration
146142

147-
Any of the above CLI commands can also read configuration options from a `.streerc` file in the directory where the commands are executed.
143+
All of the above commands accept additional configuration options. Those are:
144+
145+
- `--print-width=?` - The print width is the suggested line length that should be used when formatting the source. Note that this is not a hard limit like a linter. Instead, it is used as a guideline for how long lines _should_ be. For example, if you have the following code:
146+
147+
```ruby
148+
foo do
149+
bar
150+
end
151+
```
152+
153+
In this case, the formatter will see that the block fits into the print width and will rewrite it using the `{}` syntax. This will actually make the line longer than originally written. This is why it is helpful to think of it as a suggestion, rather than a limit.
154+
- `--preferred-quote=?` - The quote to use for string and character literals. This can be either `"` or `'`. It is "preferred" because in the case that the formatter encounters a string that contains interpolation or certain escape sequences, it will not attempt to change the quote style to avoid accidentally changing the semantic meaning of the code.
155+
- `--[no-]trailing-comma` - Whether or not to add trailing commas to multiline array literals, hash literals, and method calls that can support trailing commas.
148156

149-
This should be a text file with each argument on a separate line.
157+
Any of the above CLI commands can also read configuration options from a `.streerc` file in the directory where the commands are executed. This should be a text file with each argument on a separate line.
150158

151159
```txt
152160
--print-width=100
153-
--plugins=plugin/trailing_comma
161+
--trailing-comma
154162
```
155163

156-
If this file is present, it will _always_ be used for CLI commands. You can also pass options from the command line as in the examples above. The options in the `.streerc` file are passed to the CLI first, then the arguments from the command line. In the case of exclusive options (e.g. `--print-width`), this means that the command line options override what's in the config file. In the case of options that can take multiple inputs (e.g. `--plugins`), the effect is additive. That is, the plugins passed from the command line will be loaded _in addition to_ the plugins in the config file.
164+
If this file is present, it will _always_ be used for CLI commands. The options in the `.streerc` file are passed to the CLI first, then the arguments from the command line. In the case of exclusive options (e.g. `--print-width`), this means that the command line options override what's in the config file. In the case of options that can take multiple inputs (e.g. `--plugins`), the effect is additive. That is, the plugins passed from the command line will be loaded _in addition to_ the plugins in the config file.
157165

158166
### Globbing
159167

@@ -220,32 +228,8 @@ To register plugins, define a file somewhere in your load path named `syntax_tre
220228

221229
* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
222230
* `plugin/trailing_comma` - This will put trailing commas into multiline array literals, hash literals, and method calls that can support trailing commas.
223-
* `plugin/disable_auto_ternary` - This will prevent the automatic conversion of `if ... else` to ternary expressions.
224-
225-
If you're using Syntax Tree as a library, you can require those files directly or manually pass those options to the formatter initializer through the `SyntaxTree::Formatter::Options` class.
226-
227-
### Languages
228-
229-
To register a new language, call:
230-
231-
```ruby
232-
SyntaxTree.register_handler(".mylang", MyLanguage)
233-
```
234-
235-
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:
236-
237-
* `MyLanguage.read(filepath)` - usually this is just an alias to `File.read(filepath)`, but if you need anything else that hook is here.
238-
* `MyLanguage.parse(source)` - this should return the syntax tree corresponding to the given source. Those objects should implement the `pretty_print` interface.
239-
* `MyLanguage.format(source)` - this should return the formatted version of the given source.
240231

241-
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.
242-
243-
* [bf](https://github.com/ruby-syntax-tree/syntax_tree-bf) for the [brainf*** language](https://esolangs.org/wiki/Brainfuck).
244-
* [css](https://github.com/ruby-syntax-tree/syntax_tree-css) for the [CSS stylesheet language](https://www.w3.org/Style/CSS/).
245-
* [haml](https://github.com/ruby-syntax-tree/syntax_tree-haml) for the [Haml template language](https://haml.info/).
246-
* [json](https://github.com/ruby-syntax-tree/syntax_tree-json) for the [JSON notation language](https://www.json.org/).
247-
* [rbs](https://github.com/ruby-syntax-tree/syntax_tree-rbs) for the [RBS type language](https://github.com/ruby/rbs).
248-
* [xml](https://github.com/ruby-syntax-tree/syntax_tree-xml) for the [XML markup language](https://www.w3.org/XML/).
232+
If you're using Syntax Tree as a library, you can require those files directly or manually pass those options to the formatter initializer through the `SyntaxTree::Options` class.
249233

250234
## Integration
251235

@@ -256,12 +240,12 @@ Syntax Tree's goal is to seamlessly integrate into your workflow. To this end, i
256240
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`:
257241

258242
```ruby
259-
require "syntax_tree/rake_tasks"
243+
require "syntax_tree/rake"
260244
SyntaxTree::Rake::CheckTask.new
261245
SyntaxTree::Rake::WriteTask.new
262246
```
263247

264-
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.
248+
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. In addition to the regular configuration options used for the formatter, there are a few additional options specific to the rake tasks.
265249

266250
#### `name`
267251

@@ -292,26 +276,6 @@ SyntaxTree::Rake::WriteTask.new do |t|
292276
end
293277
```
294278

295-
#### `print_width`
296-
297-
If you want to use a different print width from the default (80), you can pass that to the `print_width` field, as in:
298-
299-
```ruby
300-
SyntaxTree::Rake::WriteTask.new do |t|
301-
t.print_width = 100
302-
end
303-
```
304-
305-
#### `plugins`
306-
307-
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:
308-
309-
```ruby
310-
SyntaxTree::Rake::WriteTask.new do |t|
311-
t.plugins = ["plugin/single_quotes"]
312-
end
313-
```
314-
315279
### RuboCop
316280

317281
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`:

Rakefile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "bundler/gem_tasks"
44
require "rake/testtask"
5-
require "syntax_tree/rake_tasks"
5+
require "syntax_tree/rake"
66

77
Rake::TestTask.new(:test) do |t|
88
t.libs << "test"
@@ -24,13 +24,6 @@ configure = ->(task) do
2424
test/*.rb
2525
]
2626
]
27-
28-
# Since Syntax Tree supports back to Ruby 2.7.0, we need to make sure that we
29-
# format our code such that it's compatible with that version. This actually
30-
# has very little effect on the output, the only change at the moment is that
31-
# Ruby < 2.7.3 didn't allow a newline before the closing brace of a hash
32-
# pattern.
33-
task.target_ruby_version = Gem::Version.new("2.7.0")
3427
end
3528

3629
SyntaxTree::Rake::CheckTask.new(&configure)

bin/bench

Lines changed: 0 additions & 42 deletions
This file was deleted.

bin/profile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ require "bundler/inline"
66
gemfile do
77
source "https://rubygems.org"
88
gem "stackprof"
9-
gem "prettier_print"
9+
gem "prism"
1010
end
1111

1212
$:.unshift(File.expand_path("../lib", __dir__))
1313
require "syntax_tree"
1414

1515
StackProf.run(mode: :cpu, out: "tmp/profile.dump", raw: true) do
1616
Dir[File.join(RbConfig::CONFIG["libdir"], "**/*.rb")].each do |filepath|
17-
SyntaxTree.format(SyntaxTree.read(filepath))
17+
SyntaxTree.format_file(filepath)
1818
end
1919
end
2020

exe/stree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
$:.unshift(File.expand_path("../lib", __dir__))
55

66
require "syntax_tree"
7-
require "syntax_tree/cli"
87

98
exit(SyntaxTree::CLI.run(ARGV))

0 commit comments

Comments
 (0)