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

Suggest using explicit "this" qualifier #592

Open
En3Tho opened this issue Jan 10, 2024 · 0 comments
Open

Suggest using explicit "this" qualifier #592

En3Tho opened this issue Jan 10, 2024 · 0 comments

Comments

@En3Tho
Copy link
Contributor

En3Tho commented Jan 10, 2024

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,

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

No branches or pull requests

1 participant