JetBrains / resharper-fsharp

F# support in JetBrains Rider
Apache License 2.0
328 stars 53 forks source link

Suggest using explicit "this" qualifier #592

Open En3Tho opened 6 months ago

En3Tho commented 6 months ago

I propose adding a code fixer that can automatically introduce "this" variable if symbol is not recognized but there is an accessible instance method or property with the same name.

Consider this example from Playwright dotnet (note Page and Expect calls):

[Test]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{
    await Page.GotoAsync("https://playwright.dev");

    // Expect a title "to contain" a substring.
    await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));

    // create a locator
    var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });

    // Expect an attribute "to be strictly equal" to the value.
    await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");

    // Click the get started link.
    await getStarted.ClickAsync();

    // Expects the URL to contain intro.
    await Expect(Page).ToHaveURLAsync(new Regex(".*intro"));
}

Naive translation to F# resulted in this which is broken because Page and Expect are instance members. IDE doesn't really help here because it can't really find what an "Expect" is for example. There isn't such a type.

[<Test>]
member _.HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() = task {
    do! Page.GotoAsync("https://playwright.dev")

    // Expect a title "to contain" a substring.
    do! Expect(Page).ToHaveTitleAsync(new Regex("Playwright"))

    // create a locator
    let getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" })

    // Expect an attribute "to be strictly equal" to the value.
    do! Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro")

    // Click the get started link.
    do! getStarted.ClickAsync()

    // Expects the URL to contain intro.
    do! Expect(Page).ToHaveURLAsync(new Regex(".*intro"))
}

While correct one is this (note "this" binding). It would be nice to have a suggestion of introducing "this" binding if member exists in the current type.

[<Test>]
member this.HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage() = task {
    let! _ = this.Page.GotoAsync("https://playwright.dev")

    // Expect a title "to contain" a substring.
    do! this.Expect(this.Page).ToHaveTitleAsync(Regex("Playwright"))

    // create a locator
    let getStarted = this.Page.GetByRole(AriaRole.Link, PageGetByRoleOptions(Name = "Get started"))

    // Expect an attribute "to be strictly equal" to the value.
    do! this.Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro")

    // Click the get started link.
    do! getStarted.ClickAsync()

    // Expects the URL to contain intro.
    do! this.Expect(this.Page).ToHaveURLAsync(Regex(".*intro"))
}

P.S. it would be also nice to suggest "Introduce let! (var_name)" for cases when do! is used with non-unit expressions but that's another issue I guess,