Skip to content

Commit 14f2812

Browse files
Allow for aliased importing (#156)
1 parent 5506a63 commit 14f2812

File tree

14 files changed

+85
-2
lines changed

14 files changed

+85
-2
lines changed

source/lib/parser.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Program, Node, CallExpression, forEachChild, isCallExpression, Identifier} from '@tsd/typescript';
1+
import {Program, Node, CallExpression, forEachChild, isCallExpression, isPropertyAccessExpression, SymbolFlags} from '@tsd/typescript';
22
import {Assertion} from './assertions';
33
import {Location, Diagnostic} from './interfaces';
44

@@ -11,13 +11,24 @@ const assertionFnNames = new Set<string>(Object.values(Assertion));
1111
*/
1212
export const extractAssertions = (program: Program): Map<Assertion, Set<CallExpression>> => {
1313
const assertions = new Map<Assertion, Set<CallExpression>>();
14+
const checker = program.getTypeChecker();
1415

1516
/**
1617
* Recursively loop over all the nodes and extract all the assertions out of the source files.
1718
*/
1819
function walkNodes(node: Node) {
1920
if (isCallExpression(node)) {
20-
const identifier = (node.expression as Identifier).getText();
21+
const expression = isPropertyAccessExpression(node.expression) ?
22+
node.expression.name :
23+
node.expression;
24+
25+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
26+
const maybeAlias = checker.getSymbolAtLocation(expression)!;
27+
const symbol = maybeAlias.flags & SymbolFlags.Alias ?
28+
checker.getAliasedSymbol(maybeAlias) :
29+
maybeAlias;
30+
31+
const identifier = symbol.getName();
2132

2233
// Check if the call type is a valid assertion
2334
if (assertionFnNames.has(identifier)) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare const one: {
2+
(foo: string, bar: string): string;
3+
(foo: number, bar: number): number;
4+
<T extends string>(foo: T, bar: T): string;
5+
};
6+
7+
export default one;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = (foo, bar) => foo + bar;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {expectType as et} from '../../../..';
2+
import one from '.';
3+
4+
et<number>(one(1, 1));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "foo"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare const one: {
2+
(foo: string, bar: string): string;
3+
(foo: number, bar: number): number;
4+
<T extends string>(foo: T, bar: T): string;
5+
};
6+
7+
export default one;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = (foo, bar) => foo + bar;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as tsd from '../../../..';
2+
import one from '.';
3+
4+
const x = tsd;
5+
x.expectError(one(true, true));
6+
7+
const y = {z: tsd};
8+
y.z.expectError(one(true, true));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "foo"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare const one: {
2+
(foo: string, bar: string): string;
3+
(foo: number, bar: number): number;
4+
<T extends string>(foo: T, bar: T): string;
5+
};
6+
7+
export default one;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = (foo, bar) => foo + bar;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as tsd from '../../../..';
2+
import {expectType, expectError} from '../../../..';
3+
import one from '.';
4+
5+
expectType<number>(one(1, 1));
6+
tsd.expectType<number>(one(1, 1));
7+
8+
expectError(one(true, true));
9+
tsd.expectError(one(true, true));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "foo"
3+
}

source/test/test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,21 @@ test('prints the types of expressions passed to `printType` helper', async t =>
455455
[10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'],
456456
]);
457457
});
458+
459+
test('assertions should be identified if imported as an aliased module', async t => {
460+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-module')});
461+
462+
verify(t, diagnostics, []);
463+
});
464+
465+
test('assertions should be identified if imported as an alias', async t => {
466+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-assertion')});
467+
468+
verify(t, diagnostics, []);
469+
});
470+
471+
test('assertions should be identified if aliased', async t => {
472+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-const')});
473+
474+
verify(t, diagnostics, []);
475+
});

0 commit comments

Comments
 (0)