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

fix(FastCheck): missing namespace on function objects #428

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Apr 8, 2024

This PR addresses an issue with FastCheck where the following snippet would lead to a type error:

export interface it {
  skip(): void
}
export function it() {}
it.skip = () => {}

it.skip()

Internally, TypeScript generates a namespace for it which tells the compiler that the function has a skip property. The inteface itself is interestingly ignored. This lead to us erroring.

The solution is to generate the namespace declaration which we need to anyway for the dts transform.

export interface it {
    skip(): void;
}
export function it(): void {}
export namespace it {
    var skip: () => void;
}

This matches the exact output that tsc generates.

Fixes denoland/deno#23276

@marvinhagemeister
Copy link
Contributor Author

This fixes the issue, but it likely needs further iteration. Especially in regards to maybe_infer_type_from_expr which I feel like should return a Return type. What do you think @dsherret ?

@marvinhagemeister
Copy link
Contributor Author

Okay, went ahead and made maybe_infer_type_from_expr return a Result. To fully support the original pattern in the issue I also expanded our capabilities to infer the type from as const expressions.

Copy link
Member

@dsherret dsherret 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, but we need to make this work for namespaces.

Copy link
Member

Choose a reason for hiding this comment

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

We should add this crazy test case and make it work:

export function test() {
}

if (true) {
    test.skip = () => {};
}

test.skip();

export namespace Test {
    export function test() {
    }

    test.other = () => {};

    test.other();
}

export namespace Test {
    test.other2 = () => {};

    test.other2();
}

This is TypeScript's dts output:

export declare function test(): void;
export declare namespace test {
    var skip: () => void;
}
export declare namespace Test {
    function test(): void;
    namespace test {
        var other: () => void;
        var other2: () => void;
    }
}
export declare namespace Test {
}

Copy link
Member

Choose a reason for hiding this comment

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

We'll save this one for later.

tests/specs/graph/jsr/FastCheck_Vars.txt Outdated Show resolved Hide resolved
src/fast_check/transform.rs Outdated Show resolved Hide resolved
src/fast_check/transform.rs Outdated Show resolved Hide resolved
src/fast_check/transform.rs Outdated Show resolved Hide resolved
src/fast_check/transform.rs Outdated Show resolved Hide resolved
src/fast_check/transform.rs Outdated Show resolved Hide resolved
},
exports: {
"skip": 2,
"prop": 3,
Copy link
Member

Choose a reason for hiding this comment

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

See here how the myFunc symbol now has these expando properties as exports.

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.

bug: type error when using it.skip from @std/testing/bdd on jsr.io
2 participants