Skip to content

Commit

Permalink
Merge pull request #140 from Shopify/uk-do-not-sort-tstruct-fields
Browse files Browse the repository at this point in the history
Do not sort `T::Struct` fields (`prop` and `const` attributes)
  • Loading branch information
paracycle authored Oct 4, 2022
2 parents 2d0e014 + 1fb2e6c commit 425abe8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/rbi/rewriters/sort_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ def group_rank(kind)
sig { params(node: Node).returns(T.nilable(String)) }
def node_name(node)
case node
when Module, Class, Struct, Const, Method, Helper, TStructField, RequiresAncestor
when Module, Class, Struct, Const, Method, Helper, RequiresAncestor
node.name
when Attr
node.names.first.to_s
when TStructField, Mixin
nil # we never want to sort these nodes by their name
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/rbi/rewriters/group_nodes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ module Scope
send2
send1
const :SC, Type
prop :SP, Type
const :SC, Type
def m1; end
def self.m2; end
Expand Down Expand Up @@ -350,10 +350,10 @@ def test_group_sort_groups_in_tree
mixes_in_class_methods MICM2
mixes_in_class_methods MICM1
const :SC1, Type
prop :SP2, Type
const :SC2, Type
prop :SP1, Type
prop :SP2, Type
const :SC1, Type
def m1; end
def m2; end
Expand Down Expand Up @@ -487,10 +487,10 @@ class Scope1
mixes_in_class_methods MICM2
mixes_in_class_methods MICM1
const :SC1, Type
prop :SP2, Type
const :SC2, Type
prop :SP1, Type
prop :SP2, Type
const :SC1, Type
def m1; end
def m2; end
Expand Down Expand Up @@ -524,10 +524,10 @@ class Scope2
mixes_in_class_methods MICM2
mixes_in_class_methods MICM1
const :SC1, Type
prop :SP2, Type
const :SC2, Type
prop :SP1, Type
prop :SP2, Type
const :SC1, Type
def m1; end
def m2; end
Expand Down Expand Up @@ -556,10 +556,10 @@ class Scope2.1
mixes_in_class_methods MICM2
mixes_in_class_methods MICM1
const :SC1, Type
prop :SP2, Type
const :SC2, Type
prop :SP1, Type
prop :SP2, Type
const :SC1, Type
def m1; end
def m2; end
Expand Down
10 changes: 5 additions & 5 deletions test/rbi/rewriters/sort_nodes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ def test_sort_struct_properties
rbi.sort_nodes!

assert_equal(<<~RBI, rbi.string)
prop :a, T
const :b, T
prop :c, T
const :d, T
prop :c, T
const :b, T
prop :a, T
RBI
end

Expand Down Expand Up @@ -290,10 +290,10 @@ def test_sort_all_nodes_in_tree
requires_ancestor { RA }
h!
mixes_in_class_methods MICM
prop :SP1, T
const :SP2, T
prop :SP3, T
const :SP4, T
prop :SP1, T
prop :SP3, T
attr_accessor :a, :foo, :z
attr_writer :b, :baz
attr_reader :bar
Expand Down

0 comments on commit 425abe8

Please sign in to comment.