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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions source/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Program, Node, CallExpression, forEachChild, isCallExpression, Identifier} from '@tsd/typescript';
import {Program, Node, CallExpression, forEachChild, isCallExpression, isPropertyAccessExpression, SymbolFlags} from '@tsd/typescript';
import {Assertion} from './assertions';
import {Location, Diagnostic} from './interfaces';

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

/**
* Recursively loop over all the nodes and extract all the assertions out of the source files.
*/
function walkNodes(node: Node) {
if (isCallExpression(node)) {
const identifier = (node.expression as Identifier).getText();
const expression = isPropertyAccessExpression(node.expression) ?
node.expression.name :
node.expression;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const maybeAlias = checker.getSymbolAtLocation(expression)!;
const symbol = maybeAlias.flags & SymbolFlags.Alias ?
checker.getAliasedSymbol(maybeAlias) :
maybeAlias;

const identifier = symbol.getName();

// Check if the call type is a valid assertion
if (assertionFnNames.has(identifier)) {
Expand Down
7 changes: 7 additions & 0 deletions source/test/fixtures/aliased/aliased-assertion/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
<T extends string>(foo: T, bar: T): string;
};

export default one;
1 change: 1 addition & 0 deletions source/test/fixtures/aliased/aliased-assertion/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.default = (foo, bar) => foo + bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType as et} from '../../../..';
import one from '.';

et<number>(one(1, 1));
3 changes: 3 additions & 0 deletions source/test/fixtures/aliased/aliased-assertion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
7 changes: 7 additions & 0 deletions source/test/fixtures/aliased/aliased-const/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
<T extends string>(foo: T, bar: T): string;
};

export default one;
1 change: 1 addition & 0 deletions source/test/fixtures/aliased/aliased-const/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.default = (foo, bar) => foo + bar;
8 changes: 8 additions & 0 deletions source/test/fixtures/aliased/aliased-const/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as tsd from '../../../..';
import one from '.';

const x = tsd;
x.expectError(one(true, true));

const y = {z: tsd};
y.z.expectError(one(true, true));
3 changes: 3 additions & 0 deletions source/test/fixtures/aliased/aliased-const/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
7 changes: 7 additions & 0 deletions source/test/fixtures/aliased/aliased-module/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
<T extends string>(foo: T, bar: T): string;
};

export default one;
1 change: 1 addition & 0 deletions source/test/fixtures/aliased/aliased-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.default = (foo, bar) => foo + bar;
9 changes: 9 additions & 0 deletions source/test/fixtures/aliased/aliased-module/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as tsd from '../../../..';
import {expectType, expectError} from '../../../..';
import one from '.';

expectType<number>(one(1, 1));
tsd.expectType<number>(one(1, 1));

expectError(one(true, true));
tsd.expectError(one(true, true));
3 changes: 3 additions & 0 deletions source/test/fixtures/aliased/aliased-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
18 changes: 18 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,21 @@ test('prints the types of expressions passed to `printType` helper', async t =>
[10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'],
]);
});

test('assertions should be identified if imported as an aliased module', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-module')});

verify(t, diagnostics, []);
});

test('assertions should be identified if imported as an alias', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-assertion')});

verify(t, diagnostics, []);
});

test('assertions should be identified if aliased', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/aliased/aliased-const')});

verify(t, diagnostics, []);
});