Skip to content

Commit

Permalink
Merge upstream/main
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Sep 11, 2022
1 parent 1761771 commit dbcbb8e
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 2 deletions.
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 @@ -441,3 +441,21 @@ test('allow specifying `rootDir` option in `tsconfig.json`', async t => {

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

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, []);
});

0 comments on commit dbcbb8e

Please sign in to comment.