Skip to content

Commit

Permalink
feat: tsd printType
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 12, 2022
1 parent e9daa32 commit af1eda0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
10 changes: 1 addition & 9 deletions src/plugin/assert/tsd/index.ts
@@ -1,8 +1,3 @@
import type ts from 'unleashed-typescript';
import type { Assertion } from '../../types';
import type { Compiler } from '../../../typescript/types';
import { createAssertionDiagnostic } from '../../diagnostics';

// All credits go to tsd! Most of the logic here comme from their code:
// https://github.com/SamVerschueren/tsd/blob/main/source/lib/assertions/index.ts
// https://github.com/SamVerschueren/tsd/tree/main/source/lib/assertions/handlers
Expand All @@ -14,7 +9,4 @@ export * from './expect-not-assignable';
export * from './expect-error';
export * from './expect-deprecated';
export * from './expect-not-deprecated';

export function printType(assertion: Assertion, compiler: Compiler): ts.Diagnostic | undefined {
return createAssertionDiagnostic('Not yet implemented.', compiler.sourceFile, assertion.node.getStart());
}
export * from './print-type';
23 changes: 23 additions & 0 deletions src/plugin/assert/tsd/print-type.ts
@@ -0,0 +1,23 @@
import { missingArgument } from './util';
import type ts from 'unleashed-typescript';
import type { Assertion } from '../../types';
import type { Compiler } from '../../../typescript/types';
import { createAssertionDiagnostic } from '../../diagnostics';

export function printType({ node }: Assertion, { sourceFile, typeChecker }: Compiler): ts.Diagnostic | undefined {
const argument = node.arguments[0];

if (!argument) {
return missingArgument(node, sourceFile);
}

const argumentExpression = argument.getText();
const argumentType = typeChecker.getTypeAtLocation(argument);
const argumentString = typeChecker.typeToString(argumentType);

return createAssertionDiagnostic(
`Type for expression \`${argumentExpression}\` is: \`${argumentString}\``,
sourceFile,
node.getStart(),
);
}
6 changes: 0 additions & 6 deletions src/plugin/assert/tsd/util.ts
Expand Up @@ -72,12 +72,6 @@ export function hasDeprecatedTag(argument: ts.Expression, typeChecker: ts.TypeCh
}

export function expressionToString(typeChecker: ts.TypeChecker, expression: ts.Expression): string | undefined {
if (ts.isTypeNode(expression)) {
const type = typeChecker.getTypeAtLocation(expression);

return typeChecker.typeToString(type);
}

if (ts.isCallLikeExpression(expression)) {
const signature = typeChecker.getResolvedSignature(expression);

Expand Down
13 changes: 13 additions & 0 deletions test/tsd.test.ts
Expand Up @@ -44,6 +44,10 @@ test('test-6', () => {
tsd.expectNotDeprecated(UnicornClass);
});

test('test-7', () => {
tsd.printType(prout);
});

/**
* @deprecated
*/
Expand All @@ -62,3 +66,12 @@ export interface Options {
readonly separator: string;
readonly delimiter: string;
}

export const prout = {
hello: 'world',
life: 42,
data: {
id: 385643984,
items: [1, 2, 3],
},
};

0 comments on commit af1eda0

Please sign in to comment.