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

Allow for aliased importing #156

Merged
merged 3 commits into from
Sep 11, 2022
Merged

Conversation

tommy-mitchell
Copy link
Contributor

Resolves #146.

Currently, the parser checks if a given node is a CallExpression and then checks if it matches the name of an assertion:

https://github.com/SamVerschueren/tsd/blob/5506a634a65fcefef9c3027e9cf51eb60b579c1d/source/lib/parser.ts#L19-L32

However, this fails to recognize aliased importing of this module or its assertions:

import * as tsd from 'tsd';

// assertion not recognized by the parser
// identifier is `tsd.expectType`, should be `expectType`
tsd.expectType<true>(true);
import {expectError as expErr} from 'tsd';

const add = (a: number, b: number): number => a + b;

// assertion not recognized by the parser
// identifier is `expErr`, should be `expectError`
expErr(add(1, '1'));

Checking if the node is a PropertyAccessExpression solves the first issue, and getting its aliased Symbol solves the second:

// parser.ts

if (isCallExpression(node)) {
    // check for `X.Y.Z.assertion()`
    const expression = isPropertyAccessExpression(node.expression) ?
        node.expression.name :
        node.expression;

    // check for `import {assertion as alias} from 'tsd'`
    const maybeAlias = checker.getSymbolAtLocation(expression)!;
    const symbol = maybeAlias.flags & SymbolFlags.Alias ?
        checker.getAliasedSymbol(maybeAlias) :
        maybeAlias;

    const identifier = symbol.getName();

    // ...
}

@sindresorhus sindresorhus merged commit 14f2812 into tsdjs:main Sep 11, 2022
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.

import * as T from 'tsd'; doesn't work
2 participants