Skip to content

Commit

Permalink
feat: tssert equalTo and not.equalTo
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 13, 2022
1 parent ffbdfa6 commit ade3db7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/tssert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function notIdenticalTo<TargetType>(_target?: unknown) {}
function subtypeOf<TargetType>(_target?: unknown) {}
function notSubtypeOf<TargetType>(_target?: unknown) {}

function equalTo<TargetType>(_target?: unknown) {}

expectType[API_PROPERTY_KEY] = 'tssert:expectType' as const;

assignableTo[API_PROPERTY_KEY] = 'tssert:assignableTo' as const;
Expand All @@ -24,14 +26,18 @@ notIdenticalTo[API_PROPERTY_KEY] = 'tssert:notIdenticalTo' as const;
subtypeOf[API_PROPERTY_KEY] = 'tssert:subtypeOf' as const;
notSubtypeOf[API_PROPERTY_KEY] = 'tssert:notSubtypeOf' as const;

equalTo[API_PROPERTY_KEY] = 'tssert:equalTo' as const;

const api = {
assignableTo,
identicalTo,
subtypeOf,
equalTo,
not: {
assignableTo: notAssignableTo,
identicalTo: notIdenticalTo,
subtypeOf: notSubtypeOf,
equalTo: notIdenticalTo,
},
} as const;

Expand Down
55 changes: 55 additions & 0 deletions src/plugin/assert/tssert/equal-to.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type ts from 'unleashed-typescript';
import type { Assertion } from '../../types';
import { getTypes, typeError } from './util';
import { ErrorCode } from '../../../common/error';
import type { Compiler } from '../../../typescript/types';

export function equalTo({ node }: Assertion, { sourceFile, typeChecker }: Compiler): ts.Diagnostic | undefined {
const expectedNode = node.expression.getChildAt(0) as ts.CallExpression;
const expected = getTypes(expectedNode, typeChecker);

if (expected.diagnostic) {
return expected.diagnostic;
}

const argument = getTypes(node, typeChecker);

if (argument.diagnostic) {
return argument.diagnostic;
}

if (!typeChecker.isTypeAssignableTo(argument.type, expected.type)) {
return typeError(
ErrorCode.ASSERT_TYPE_NOT_ASSIGNABLE,
typeChecker,
argument.type,
expected.type,
sourceFile,
expectedNode,
);
}

if (!typeChecker.isTypeAssignableTo(expected.type, argument.type)) {
return typeError(
ErrorCode.ASSERT_TYPE_TOO_WIDE,
typeChecker,
argument.type,
expected.type,
sourceFile,
expectedNode,
);
}

if (!typeChecker.isTypeIdenticalTo(argument.type, expected.type)) {
return typeError(
ErrorCode.ASSERT_TYPE_NOT_IDENTICAL,
typeChecker,
argument.type,
expected.type,
sourceFile,
expectedNode,
);
}

return;
}
1 change: 1 addition & 0 deletions src/plugin/assert/tssert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './identical-to';
export * from './not-identical-to';
export * from './subtype-of';
export * from './not-subtype-of';
export * from './equal-to';
5 changes: 5 additions & 0 deletions test/tssert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ test('test-9', () => {
expectType<string>().subtypeOf('hello');
expectType<string>().not.subtypeOf('hello');
});

test('test-9', () => {
expectType<'hello'>().equalTo('hello');
expectType<'hello'>().not.equalTo('hello');
});

0 comments on commit ade3db7

Please sign in to comment.