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

Enforce that parameters position with sorbet-static #1273

Merged
merged 3 commits into from
Jul 27, 2019

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Jul 11, 2019

Motivation

There are three mismatch between sorbet-static and sorbet-runtime regarding parameters:

  1. sorbet-runtime ask for required parameters to be defined before the optional ones
  2. sorbet-runtime ask for required keyword parameters to be defined before the optional ones
  3. sorbet-runtime ask for the parameters order in the method to be the same than the signature

None of these checks are implemented in sorbet-static yet.

This pull-request implements 2. and 3..
For 1., it is possible to implement it but it conflicts with some definitions in socket.rbi.

Test plan

Few examples from the tests:

sig {params(x: T1, y: T2, z: T3).void}
def f2(x:, y: T2.new, z:); end
#                     ^^ error: Malformed `sig`. Required parameter `z` must be declared before all the optional ones
sig { params(a: Integer, b: String).void }
def f2(b, a); end
#      ^ error: Bad parameter ordering for `b`, expected `a` instead
#         ^ error: Bad parameter ordering for `a`, expected `b` instead

sig {params(v: T1, w: T2, x: T1, y: T2, z: T3).void}
def f3(v, w = T2.new, *y, z:, x: T1.new); end
#                      ^ error: Bad parameter ordering for `y`, expected `x` instead
#                         ^^ error: Bad parameter ordering for `z`, expected `y` instead
#                             ^^ error: Bad parameter ordering for `x`, expected `z` instead

See included automated tests.

Closes #174.

@Morriar Morriar requested a review from a team July 11, 2019 21:12
@ghost ghost requested review from aisamanra and removed request for a team July 11, 2019 21:12
@DarkDimius
Copy link
Collaborator

While I do like the proposed requirement, as it makes the code more readable.
That said ruby does accept those method definitions. Thus we should prohibit them outright.

WDYT about making this into a separate error class that is enabled in strict?

BTW: thank you for great tests

@DarkDimius DarkDimius requested review from DarkDimius and removed request for aisamanra July 15, 2019 16:32
Copy link
Collaborator

@DarkDimius DarkDimius left a comment

Choose a reason for hiding this comment

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

due to ^^^ I expect this will break a lot of people.
Let's make it less invasive by making it a:

  • separate error code
  • that's only enabled at strict and higher

@Morriar
Copy link
Collaborator Author

Morriar commented Jul 16, 2019

@DarkDimius:

WDYT about making this into a separate error class that is enabled in strict?

The problem we have with the current state is that sorbet-runtime will raise for errors not caught by sorbet-static. So we're at risk of something raising in production while we could not catch it on CI.

To avoid this problem we have two cops ran on CI especially for the parameters ordering:

My goal with this PR was to remove the need of these cops.

Moving this check to strict or strong doesn't really fixes it for us as false and true are still at risk of raising in production.

I added a commit to bind these errors to another class especially for the parameters ordering: 5045: BadParameterOrdering.

WDYT about leaving this in false level and let users blacklist it if needed?

@DarkDimius
Copy link
Collaborator

WDYT about leaving this in false level and let users blacklist it if needed?

our current policy is that the moment you blacklist an error, you're not supported, thus I'd like to not introduce a common error that open-source folks will blacklist.

Moving this check to strict or strong doesn't really fixes it for us as false and true are still at risk of raising in production.

Let me carefully see how many breackages does this have in open-source & in our repo to get indication if this is problematic in practice and get inspiration for how can we address all usecases here.

@DarkDimius DarkDimius self-assigned this Jul 17, 2019
@DarkDimius
Copy link
Collaborator

Hi Alexandre,
Would you kindly rebase and run ./tools/scripts/update_exp_files.sh?
Other than that, I think we can merge! 🎉
Thank you!

…ones

Also add a test showing the new spec.

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

Morriar commented Jul 19, 2019

^ @DarkDimius rebased on master.

Thank you!

@Morriar
Copy link
Collaborator Author

Morriar commented Jul 23, 2019

@DarkDimius is there something else to do with this pull request?

Copy link
Collaborator

@DarkDimius DarkDimius left a comment

Choose a reason for hiding this comment

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

Working on landing it

@DarkDimius
Copy link
Collaborator

Thank you, sorry it took so long!

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.

Sorbet doesn't complain if params and def list parameters out-of-order
2 participants