Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ on:
- pull_request_target
jobs:
ci:
strategy:
fail-fast: false
matrix:
ruby:
- '2.7.5'
- '3.0'
- '3.1'
gemfile:
- rbs1
- rbs2
name: CI
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
CI: true
steps:
- uses: actions/checkout@master
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: '3.1'
ruby-version: ${{ matrix.ruby }}
- name: Test
run: bundle exec rake test
automerge:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/tmp/

test.rbs
gemfiles/*.lock
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ GEM
syntax_tree (2.3.0)

PLATFORMS
x86_64-darwin-19
x86_64-darwin-21
x86_64-linux

Expand Down
7 changes: 7 additions & 0 deletions gemfiles/rbs1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec path: ".."

gem "rbs", "~> 1"
7 changes: 7 additions & 0 deletions gemfiles/rbs2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec path: ".."

gem "rbs", "~> 2"
4 changes: 4 additions & 0 deletions lib/syntax_tree/rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
require_relative "rbs/utils"
require_relative "rbs/version"

if Gem::Version.new(RBS::VERSION) < Gem::Version.new("2.0.0")
require_relative "rbs/shims"
end

module SyntaxTree
module RBS
# A slight extension to the default PP formatter that keeps track of the
Expand Down
30 changes: 30 additions & 0 deletions lib/syntax_tree/rbs/shims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "delegate"

# Previously there were specialized types that didn't include some additional
# information for type params. So here we wrap them up in order to maintain the
# same API.
module TypeParams
class TypeParam < SimpleDelegator
def unchecked?
false
end

def upper_bound
nil
end
end

# Overriding the type params method to return an array of wrapped objects.
def type_params
super.params.map { |param| TypeParam.new(param) }
end
end

module RBS::AST::Declarations
Alias.prepend(TypeParams)
Class.prepend(TypeParams)
Interface.prepend(TypeParams)
Module.prepend(TypeParams)
end
2 changes: 1 addition & 1 deletion lib/syntax_tree/rbs/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def pretty_print(q)

class Literal
def format(q)
unless literal in String
unless literal.is_a?(String)
q.text(literal.inspect)
return
end
Expand Down
5 changes: 3 additions & 2 deletions lib/syntax_tree/rbs/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def initialize(node)

def format(q)
node.name.format(q)
return if node.type_params.length == 0
return if node.type_params.empty?

q.text("[")
q.seplist(node.type_params, -> { q.text(", ") }) do |param|
Expand Down Expand Up @@ -308,7 +308,8 @@ def format(q)
if node.respond_to?(:type_params) && node.type_params.any?
q.text("[")
q.seplist(node.type_params, -> { q.text(", ") }) do |param|
q.text(param.name)
# We need to do a type check here to support RBS 1.0
q.text(param.is_a?(Symbol) ? param.to_s : param.name)
end
q.text("] ")
end
Expand Down
88 changes: 47 additions & 41 deletions test/rbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,6 @@ def test_interface_with_type_params
RBS
end

def test_interface_with_bounded_type_param
assert_format(<<~RBS)
interface _Foo[A < B]
end
RBS
end

def test_interface_with_fancy_bounded_type_params
assert_format(<<~RBS)
interface _Foo[U < singleton(::Hash), V < W[X, Y]]
end
RBS
end

def test_class
assert_format(<<~RBS)
class Foo
Expand All @@ -185,27 +171,6 @@ class Foo[A, B]
RBS
end

def test_class_with_complicated_type_params
assert_format(<<~RBS)
class Foo[unchecked in A, unchecked out B, in C, out D, unchecked E, unchecked F, G, H]
end
RBS
end

def test_class_with_bounded_type_param
assert_format(<<~RBS)
class Foo[A < B]
end
RBS
end

def test_class_with_fancy_bounded_type_params
assert_format(<<~RBS)
class Foo[U < singleton(::Hash), V < W[X, Y]]
end
RBS
end

def test_class_with_annotations_that_cannot_be_switched_to_braces
assert_format(<<~RBS)
%a<This is {an} annotation.>
Expand Down Expand Up @@ -313,6 +278,43 @@ def test_kanji
assert_format("T: { \"日本語\" => Integer }")
end

if Gem::Version.new(RBS::VERSION) >= Gem::Version.new("2.0.0")
def test_class_with_bounded_type_param
assert_format(<<~RBS)
class Foo[A < B]
end
RBS
end

def test_class_with_fancy_bounded_type_params
assert_format(<<~RBS)
class Foo[U < singleton(::Hash), V < W[X, Y]]
end
RBS
end

def test_class_with_complicated_type_params
assert_format(<<~RBS)
class Foo[unchecked in A, unchecked out B, in C, out D, unchecked E, unchecked F, G, H]
end
RBS
end

def test_interface_with_bounded_type_param
assert_format(<<~RBS)
interface _Foo[A < B]
end
RBS
end

def test_interface_with_fancy_bounded_type_params
assert_format(<<~RBS)
interface _Foo[U < singleton(::Hash), V < W[X, Y]]
end
RBS
end
end

private

def assert_format(expected, original = expected)
Expand All @@ -325,11 +327,15 @@ def assert_format(expected, original = expected)
assert_equal(expected.strip, formatted.strip)

# Next, check that the pretty-print functions are implemented all of the way
# down the tree.
formatter = PP.new(+"")
SyntaxTree::RBS.parse(original).pretty_print(formatter)

formatter.flush
refute_includes(formatter.output, "#")
# down the tree. We're only going to do this on Ruby >= 3.0.0 because
# there's some issue with prettyprint below that that I don't want to deal
# with.
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")
formatter = PP.new(+"")
SyntaxTree::RBS.parse(original).pretty_print(formatter)

formatter.flush
refute_includes(formatter.output, "#")
end
end
end