Skip to content

Commit

Permalink
feat: tsd expectNotType
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 11, 2022
1 parent d33866f commit c040f02
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum ErrorCode {
ASSERT_TYPE_NOT_ASSIGNABLE,
ASSERT_TYPE_TOO_WIDE,
ASSERT_TYPE_NOT_IDENTICAL,
ASSERT_TYPE_IDENTICAL,
}

export const errorMessages: Record<ErrorCode, string> = {
Expand All @@ -23,7 +24,8 @@ export const errorMessages: Record<ErrorCode, string> = {
[ErrorCode.ASSERT_MISSING_ARGUMENT]: 'Missing argument at position {position}',
[ErrorCode.ASSERT_TYPE_NOT_ASSIGNABLE]: "Type '{expected}' is not assignable to type '{argument}'.",
[ErrorCode.ASSERT_TYPE_TOO_WIDE]: "Type '{expected}' is declared too wide for argument type '{argument}'.",
[ErrorCode.ASSERT_TYPE_NOT_IDENTICAL]: "Type '${expected}' is not identical to argument type '${argument}'.",
[ErrorCode.ASSERT_TYPE_NOT_IDENTICAL]: "Type '{expected}' is not identical to argument type '{argument}'.",
[ErrorCode.ASSERT_TYPE_IDENTICAL]: "Type '{expected}' is identical to argument type '{argument}'.",
};

export function errorMessage(code: ErrorCode, data?: Record<string, unknown>): string {
Expand Down
19 changes: 17 additions & 2 deletions src/plugin/assert/tsd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,23 @@ export function expectType({ node }: Assertion, { sourceFile, typeChecker }: Com
return;
}

export function expectNotType(assertion: Assertion, compiler: Compiler): ts.Diagnostic | undefined {
return createAssertionDiagnostic('Not yet implemented.', compiler.sourceFile, assertion.node.getStart());
export function expectNotType({ node }: Assertion, { sourceFile, typeChecker }: Compiler): ts.Diagnostic | undefined {
if (!node.typeArguments?.[0]) {
return missingGeneric(node, sourceFile);
}

if (!node.arguments[0]) {
return missingArgument(node, sourceFile);
}

const expectedType = typeChecker.getTypeFromTypeNode(node.typeArguments[0]);
const argumentType = typeChecker.getTypeAtLocation(node.arguments[0]);

if (!typeChecker.isTypeIdenticalTo(expectedType, argumentType)) {
return typeError(ErrorCode.ASSERT_TYPE_IDENTICAL, typeChecker, expectedType, argumentType, sourceFile, node);
}

return;
}

export function expectAssignable(assertion: Assertion, compiler: Compiler): ts.Diagnostic | undefined {
Expand Down
8 changes: 7 additions & 1 deletion test/tsd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import { expectType, expectType as assertType } from '../src/api/tsd';

test('test-1', () => {
expectType<string>('hello');
});

test('test-2', () => {
assertType<string>('hello');
tsd.expectType<string>('hello');
});

test('test-3', () => {
tsd.expectNotType<string>('hello');
});

0 comments on commit c040f02

Please sign in to comment.