Skip to content

Commit

Permalink
Merge pull request #1190 from prettier/new-backend
Browse files Browse the repository at this point in the history
New backend
  • Loading branch information
kddnewton authored Apr 22, 2022
2 parents 08abaa6 + 2b7c573 commit 56efce2
Show file tree
Hide file tree
Showing 130 changed files with 395 additions and 18,156 deletions.
8 changes: 1 addition & 7 deletions .github/ISSUE_TEMPLATE/bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ about: Issues with prettier crashing
- Ruby version: latest
- Node version: latest
- `@prettier/plugin-ruby` or `prettier` gem version: latest
- Options:
- `rubyArrayLiteral` - `true`
- `rubyHashLabel` - `true`
- `rubyModifier` - `true`
- `rubySingleQuote` - `true`
- `rubyToProc` - `false`
- `trailingComma` - `"none"`
- `syntax_tree` version: latest

## Input

Expand Down
8 changes: 1 addition & 7 deletions .github/ISSUE_TEMPLATE/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ about: Issues for ugly or incorrect code
- Ruby version: latest
- Node version: latest
- `@prettier/plugin-ruby` or `prettier` gem version: latest
- Options:
- `rubyArrayLiteral` - `true`
- `rubyHashLabel` - `true`
- `rubyModifier` - `true`
- `rubySingleQuote` - `true`
- `rubyToProc` - `false`
- `trailingComma` - `"none"`
- `syntax_tree` version: latest

## Input

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
- ubuntu-latest
- windows-latest
ruby:
- "2.6"
- "2.7"
- "3.0"
- "3.1"
runs-on: ${{ matrix.platform }}
env:
PLUGIN_RUBY_CI: true
Expand Down Expand Up @@ -59,8 +59,8 @@ jobs:
fail-fast: false
matrix:
platform:
- macos-latest
- ubuntu-latest
# - macos-latest
# - ubuntu-latest
- windows-latest
yarn:
- 2.x
Expand All @@ -73,7 +73,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: "3.0"
ruby-version: "3.1"
- uses: actions/setup-node@v2
with:
node-version: 12.x
Expand Down Expand Up @@ -106,7 +106,7 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: "3.0"
ruby-version: "3.1"
- uses: actions/setup-node@v2
with:
node-version: 12.x
Expand Down
4 changes: 0 additions & 4 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@

*.txt
*.gem

# I'm ignoring this for now because it's a dump of a bunch of types that I don't
# want running onto multiple lines at the moment.
/src/types
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ The comment in the above example should stay in place.

```ruby
begin

rescue Foo, Bar
# comment
end
Expand Down Expand Up @@ -476,7 +475,8 @@ return (a or b) if c?
- kddnewton - Support for the `nokw_param` node for specifying when methods should no accept keywords, as in:

```ruby
def foo(**nil); end
def foo(**nil)
end
```

- kddnewton - Support for the `args_forward` node for forwarding all types of arguments, as in:
Expand Down Expand Up @@ -522,9 +522,9 @@ will now be printed as:

```ruby
Config::Download.new(
'prettier',
filename: 'prettier.yml',
url: 'https://raw.githubusercontent.com/...'
"prettier",
filename: "prettier.yml",
url: "https://raw.githubusercontent.com/..."
).perform
```

Expand Down
8 changes: 1 addition & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

gemspec

gem 'bundler', '~> 2.1'
gem 'haml', '~> 5.2'
gem 'minitest', '~> 5.14'
gem 'rake', '~> 13.0'
gem 'rbs', '~> 1.0'
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ d = [
30_643_069_058
]
a, s = [], $*[0]
s.each_byte { |b| a << ('%036b' % d[b.chr.to_i]).scan(/\d{6}/) }
s.each_byte { |b| a << ("%036b" % d[b.chr.to_i]).scan(/\d{6}/) }
a.transpose.each do |a|
a.join.each_byte { |i| print i == 49 ? ($*[1] || '#') : 32.chr }
a.join.each_byte { |i| printi == 49 ? ($*[1] || "#") : 32.chr }
puts
end
```
Expand All @@ -83,7 +83,7 @@ This plugin currently supports formatting the following kinds of files:
Add this line to your application's Gemfile:

```ruby
gem 'prettier'
gem "prettier"
```

And then execute:
Expand Down Expand Up @@ -128,31 +128,25 @@ The `prettier` executable is now installed and ready for use:

Below are the options (from [`src/plugin.js`](src/plugin.js)) that `@prettier/plugin-ruby` currently supports:

| API Option | CLI Option | Default | Description |
| ------------------ | ---------------------- | :------: | ------------------------------------------------------------------------------------------------------------------------------------ |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `requirePragma` | `--require-pragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)). |
| `rubyArrayLiteral` | `--ruby-array-literal` | `true` | When possible, favor the use of string and symbol array literals. |
| `rubyHashLabel` | `--ruby-hash-label` | `true` | When possible, uses the shortened hash key syntax, as opposed to hash rockets. |
| `rubyModifier` | `--ruby-modifier` | `true` | When it fits on one line, allows while and until statements to use the modifier form. |
| `rubySingleQuote` | `--ruby-single-quote` | `true` | When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals. |
| `rubyToProc` | `--ruby-to-proc` | `false` | When possible, convert blocks to the more concise `Symbol#to_proc` syntax. |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| `trailingComma` | `--trailing-comma` | `"none"` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#trailing-comma)). `"es5"` is equivalent to `true`. |
| API Option | CLI Option | Default | Description |
| --------------- | ------------------ | :-----: | --------------------------------------------------------------------------------------------------- |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `requirePragma` | `--require-pragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)). |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |

Any of these can be added to your existing [prettier configuration
file](https://prettier.io/docs/en/configuration.html). For example:

```json
{
"rubySingleQuote": false
"tabWidth": 4
}
```

Or, they can be passed to `prettier` as arguments:

```bash
bundle exec rbprettier --ruby-single-quote false --write '**/*'
bundle exec rbprettier --tab-width 4 --write '**/*'
```

### Usage with RuboCop
Expand Down
10 changes: 5 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'
require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << 'test/rb'
t.libs << 'lib'
t.test_files = FileList['test/rb/**/*_test.rb']
t.libs << "test/rb"
t.libs << "lib"
t.test_files = FileList["test/rb/**/*_test.rb"]
end

task default: :test
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'prettier'
require "bundler/setup"
require "prettier"

require 'irb'
require "irb"
IRB.start(__FILE__)
14 changes: 7 additions & 7 deletions bin/debug
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/usr/bin/env ruby

require 'bundler/inline'
require "bundler/inline"

gemfile do
source 'https://rubygems.org'
gem 'sinatra', require: 'sinatra/base'
gem 'webrick'
source "https://rubygems.org"
gem "sinatra", require: "sinatra/base"
gem "webrick"
end

require_relative '../src/ruby/parser'
require_relative "../src/ruby/parser"

class App < Sinatra::Base
HTML = DATA.read

get '/' do
get "/" do
HTML
end

post '/ast' do
post "/ast" do
response = Prettier::Parser.parse(request.body.read)
response ? JSON.fast_generate(response) : halt(422)
rescue Prettier::Parser::ParserError
Expand Down
2 changes: 1 addition & 1 deletion bin/lex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

require 'ripper'
require "ripper"

source = File.file?(ARGV[0]) ? File.read(ARGV[0]) : ARGV[0].gsub('\\n', "\n")
pp Ripper.lex(source)
3 changes: 1 addition & 2 deletions bin/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ if (fs.existsSync(process.argv[contentIdx])) {
const { formatted } = prettier.formatWithCursor(content, {
parser,
plugins: [plugin as any as string], // hacky, but it works
cursorOffset: 1,
rubySingleQuote: false
cursorOffset: 1
} as any);

console.log(formatted);
6 changes: 3 additions & 3 deletions bin/sexp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env ruby

require_relative '../src/ruby/parser'
require_relative "../src/ruby/parser"

source =
if !ARGV[0]
File.read('test.rb')
File.read("test.rb")
elsif File.file?(ARGV[0])
File.read(ARGV[0])
else
Expand All @@ -13,7 +13,7 @@ source =

parsed = SyntaxTree.parse(source)

puts '=== SOURCE === '
puts "=== SOURCE === "
puts source

puts "\n=== COMMENTS ==="
Expand Down
4 changes: 2 additions & 2 deletions exe/rbprettier
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$:.unshift(File.expand_path(File.join('..', 'lib'), __dir__))
require 'prettier'
$:.unshift(File.expand_path(File.join("..", "lib"), __dir__))
require "prettier"

exit(Prettier.run(ARGV))
18 changes: 9 additions & 9 deletions lib/prettier.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

require 'json' unless defined?(JSON)
require 'open3'
require "json" unless defined?(JSON)
require "open3"

module Prettier
PLUGIN = -File.expand_path('..', __dir__)
BINARY = -File.join(PLUGIN, 'node_modules', 'prettier', 'bin-prettier.js')
VERSION = -JSON.parse(File.read(File.join(PLUGIN, 'package.json')))['version']
PLUGIN = -File.expand_path("..", __dir__)
BINARY = -File.join(PLUGIN, "node_modules", "prettier", "bin-prettier.js")
VERSION = -JSON.parse(File.read(File.join(PLUGIN, "package.json")))["version"]

def self.run(args)
quoted = args.map { |arg| arg.start_with?('-') ? arg : "\"#{arg}\"" }
command = "node #{BINARY} --plugin \"#{PLUGIN}\" #{quoted.join(' ')}"
quoted = args.map { |arg| arg.start_with?("-") ? arg : "\"#{arg}\"" }
command = "node #{BINARY} --plugin \"#{PLUGIN}\" #{quoted.join(" ")}"

stdout, stderr, status =
Open3.capture3({ 'RBPRETTIER' => '1' }, command, stdin_data: STDIN)
Open3.capture3({ "RBPRETTIER" => "1" }, command, stdin_data: STDIN)
STDOUT.puts(stdout)

# If we completed successfully, then just exit out.
Expand All @@ -30,7 +30,7 @@ def self.run(args)
If you installed this dependency through git instead of from rubygems,
it does not install the necessary files by default. To fix this you can
either install them yourself by cd-ing into the directory where this gem
is located (#{File.expand_path('..', __dir__)}) and running:
is located (#{File.expand_path("..", __dir__)}) and running:
`yarn && yarn prepublishOnly`
or
Expand Down
10 changes: 5 additions & 5 deletions lib/prettier/rake/task.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'rake'
require 'rake/tasklib'
require "rake"
require "rake/tasklib"

module Prettier
module Rake
Expand Down Expand Up @@ -35,7 +35,7 @@ class Task < ::Rake::TaskLib
def initialize(name = :prettier)
@name = name
@write = true
@source_files = 'lib/**/*.rb'
@source_files = "lib/**/*.rb"

yield self if block_given?
define_task
Expand All @@ -44,12 +44,12 @@ def initialize(name = :prettier)
private

def define_task
desc 'Runs prettier over source files'
desc "Runs prettier over source files"
task(name) { run_task }
end

def run_task
Prettier.run([('--write' if write), source_files].compact)
Prettier.run([("--write" if write), source_files].compact)
exit($?.exitstatus) if $?&.exited?
end
end
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"checkFormat": "prettier --check '**/*'",
"lint": "eslint --cache .",
"prepublishOnly": "tsc -p tsconfig.build.json && cp src/parser/getInfo.js dist/parser && cp src/parser/netcat.js dist/parser && cp src/parser/server.rb dist/parser && cp src/haml/parser.rb dist/haml && cp src/rbs/parser.rb dist/rbs && cp src/ruby/parser.rb dist/ruby",
"prepublishOnly": "tsc -p tsconfig.build.json && cp src/getInfo.js dist && cp src/netcat.js dist && cp src/server.rb dist",
"test": "jest"
},
"repository": {
Expand Down
Loading

0 comments on commit 56efce2

Please sign in to comment.