tsdjs / tsd

Check TypeScript type definitions
MIT License
2.4k stars 67 forks source link

Allow for aliased importing #156

Closed tommy-mitchell closed 2 years ago

tommy-mitchell commented 2 years ago

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();

    // ...
}