Skip to content

Commit 8566d42

Browse files
committed
Switch parser over to prism
1 parent 476435e commit 8566d42

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

+5828
-20345
lines changed

.gitattributes

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

.github/workflows/main.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12+
os:
13+
- ubuntu-latest
14+
- macos-latest
15+
- windows-latest
1216
ruby:
13-
- '2.7.0'
14-
- '3.0'
15-
- '3.1'
1617
- '3.2'
1718
- '3.3'
1819
- '3.4'
1920
- truffleruby-head
21+
exclude:
22+
- os: windows-latest
23+
ruby: truffleruby-head
2024
name: CI
21-
runs-on: ubuntu-latest
22-
env:
23-
CI: true
24-
# TESTOPTS: --verbose
25+
runs-on: ${{ matrix.os }}
2526
steps:
2627
- uses: actions/checkout@master
2728
- uses: ruby/setup-ruby@v1
@@ -34,8 +35,6 @@ jobs:
3435
check:
3536
name: Check
3637
runs-on: ubuntu-latest
37-
env:
38-
CI: true
3938
steps:
4039
- uses: actions/checkout@master
4140
- uses: ruby/setup-ruby@v1

.rubocop.yml

Lines changed: 14 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

@@ -79,12 +82,18 @@ Naming/VariableNumber:
7982
Security/Eval:
8083
Enabled: false
8184

85+
Style/AccessModifierDeclarations:
86+
Enabled: false
87+
8288
Style/AccessorGrouping:
8389
Enabled: false
8490

8591
Style/Alias:
8692
Enabled: false
8793

94+
Style/BlockDelimiters:
95+
Enabled: false
96+
8897
Style/CaseEquality:
8998
Enabled: false
9099

@@ -151,6 +160,9 @@ Style/Next:
151160
Style/NumericPredicate:
152161
Enabled: false
153162

163+
Style/OptionalBooleanParameter:
164+
Enabled: false
165+
154166
Style/ParallelAssignment:
155167
Enabled: false
156168

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: 7 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,24 @@ 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
52+
x64-mingw-ucrt
5753
x86_64-linux
5854

5955
DEPENDENCIES
6056
bundler
61-
fiddle
6257
minitest
6358
rake
6459
rubocop
6560
simplecov
6661
syntax_tree!
6762

6863
BUNDLED WITH
69-
2.3.6
64+
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: 2 additions & 18 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"
@@ -14,23 +14,7 @@ task default: :test
1414

1515
configure = ->(task) do
1616
task.source_files =
17-
FileList[
18-
%w[
19-
Gemfile
20-
Rakefile
21-
syntax_tree.gemspec
22-
lib/**/*.rb
23-
tasks/*.rake
24-
test/*.rb
25-
]
26-
]
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")
17+
FileList[%w[Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb tasks/*.rake test/*.rb]]
3418
end
3519

3620
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

0 commit comments

Comments
 (0)