Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use our internal RBI backend for DSL generators #388

Merged
merged 25 commits into from
Jul 20, 2021
Merged

Use our internal RBI backend for DSL generators #388

merged 25 commits into from
Jul 20, 2021

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Jul 14, 2021

Motivation

Use our RBI generation framework instead of Parlour.

Implementation

Added a RBI::Builder to make an interface compatible with our current generators and minimize the diffs.

Tests

See automated tests.

This was also tested by regenerating all Core DSL RBIs with success. Typecheking is passing and diff is minimal.

One interesting side effect is the time required to generate all the RBIs:

  • Before: 3m 31s
  • After: 2m 23s

Fixes #346.

@Morriar Morriar requested a review from a team July 14, 2021 23:48
def decorate(root, constant)
return if constant.state_machines.empty?

root.path(constant) do |klass|
root.create_path(T.unsafe(constant)) do |klass|
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tree#create_path expects a Module but we get a StateMachines::ClassMethods for argument constant which doesn't seem to be a module.

One notable problem here is that the redefinition of decorate does not follow Liskov substitution principle. We do some shady thing (https://github.com/Shopify/tapioca/blob/master/lib/tapioca/compilers/dsl/base.rb#L28-L36) to avoid detection but this is weird design.

We should refactor this part to offer a correct/safe interface.


sig { params(node: Node).returns(Node) }
def create_node(node)
cached = nodes_cache[node.to_s]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid recreating the scopes between two generators, if we already created it in a previous generator we return it and append to it.

@@ -451,47 +452,50 @@ def title=(title); end
assert_path_exists("#{outdir}/namespace/comment.rbi")
refute_path_exists("#{outdir}/user.rbi")

assert_equal(<<~CONTENTS.chomp, File.read("#{outdir}/baz/role.rbi"))
assert_equal(<<~CONTENTS, File.read("#{outdir}/baz/role.rbi"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to chomp since this PR fixes #346.

class Conversation
include EnumMethodsModule

sig { returns(T::Hash[T.any(String, Symbol), Integer]) }
Copy link
Collaborator Author

@Morriar Morriar Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like for gem RBIs, all methods are sorted before the subconstants

class Cart
sig { params(customer_id: Integer, shop_id: Integer).void }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in gems RBIs, initialize methods are always sorted first.

@@ -268,10 +268,10 @@ def to_bar; end

class String
include ::Comparable
include ::JSON::Ext::Generator::GeneratorMethods::String
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This order changes because we removed Parlour has a dependency.

include ::ActionDispatch::Routing::UrlFor
include ::ActionDispatch::Routing::PolymorphicRoutes
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now in the same order we generate them:
image

Not sure why Parlour was changing the order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thankfully, the order didn't matter here.


class ActionDispatch::IntegrationTest
include GeneratedUrlHelpersModule
include GeneratedPathHelpersModule
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now in the same order we want them to be generated:

image

Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great speedup 🏃

class Conversation
include EnumMethodsModule

sig { returns(T::Hash[T.any(String, Symbol), Integer]) }
def self.statuses; end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage of both class << self and def self. in the RBIs is inconsistent. Do we prefer it over sticking with def self. everywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm planning to make this configurable in a follow up.

My thinking is to always create them as def self.foo then let the use decide if they should be printed as such or inside a class << self.

For now I aimed at the smaller possible diff in generated files.

Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I just have one question.

spec/tapioca/cli_spec.rb Outdated Show resolved Hide resolved
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
…elf block

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar
Copy link
Collaborator Author

Morriar commented Jul 20, 2021

Pushed a few changed:

  • Rebased on master and fixed merge conflicts
  • Removed the class << self nesting
  • Added a last commit to always sort def self.x singleton methods after the non singleton ones

@Morriar Morriar requested a review from paracycle July 20, 2021 16:12
@Morriar Morriar merged commit af07710 into master Jul 20, 2021
@Morriar Morriar deleted the at-rbi-dsl branch July 20, 2021 16:29
@shopify-shipit shopify-shipit bot temporarily deployed to production September 7, 2021 16:35 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tapioca dsl are not adding trailing new lines to auto-generated files.
3 participants