Skip to content

Commit

Permalink
fix: tsd expectError
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 12, 2022
1 parent c2bb6f6 commit 510230c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
30 changes: 10 additions & 20 deletions src/plugin/assert/tsd/expect-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { Compiler } from '../../../typescript/types';
import { createAssertionDiagnostic } from '../../diagnostics';
import { ErrorCode, errorMessage } from '../../../common/error';

function inRange(argument: ts.Expression, diagnostic: ts.Diagnostic): boolean {
return !!diagnostic.start && argument.getStart() <= diagnostic.start && diagnostic.start <= argument.getEnd();
}

// https://github.dev/SamVerschueren/tsd/blob/e4a398c1b47a4d2f914446b662840e2be5994997/source/lib/compiler.ts#L54-L55
export function expectError({ node }: Assertion, compiler: Compiler): ts.Diagnostic | undefined {
const argument = node.arguments[0];
Expand All @@ -13,30 +17,16 @@ export function expectError({ node }: Assertion, compiler: Compiler): ts.Diagnos
return missingArgument(node, compiler.sourceFile);
}

let assertDiagnostic: ts.Diagnostic | undefined = undefined;

if (!compiler.diagnostics.length) {
return createAssertionDiagnostic(errorMessage(ErrorCode.ASSERT_ERROR), compiler.sourceFile, argument.getStart());
}

// TODO: Create a method in Compiler for cleanly removing/filter a diagnostic.
compiler.diagnostics = compiler.diagnostics.filter((diagnostic) => {
if (assertDiagnostic || !diagnostic.start) {
return true;
}
const diagnostic = compiler.diagnostics.find((diagnostic) => inRange(argument, diagnostic));

if (diagnostic.start < argument.getStart() || diagnostic.start > argument.getEnd()) {
assertDiagnostic = createAssertionDiagnostic(
errorMessage(ErrorCode.ASSERT_ERROR),
compiler.sourceFile,
diagnostic.start,
);

return true;
}

return false;
});
if (diagnostic) {
compiler.diagnostics = compiler.diagnostics.filter((d) => d !== diagnostic);
return;
}

return assertDiagnostic;
return createAssertionDiagnostic(errorMessage(ErrorCode.ASSERT_ERROR), compiler.sourceFile, argument.getStart());
}
36 changes: 20 additions & 16 deletions test/tsd.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
// import { test, expect, describe } from 'vitest';
// import { expectType, expectType as pouet, expectNotType } from '../src/api/tsd';
// import * as tsd from '../src/api/tsd';

// describe('describe-1', () => {
// test('test-1', () => {
// pouet<string>('hello');
// expectType<string>('hello');
// expectNotType<number>('hello');
// tsd.expectType<string>('hello');

// expect(42).toBe(42);
// // expect("life").toBe(42);
// });
// });

import { test } from 'vitest';
import * as tsd from '../src/api/tsd';
import { expectType, expectType as assertType } from '../src/api/tsd';
Expand Down Expand Up @@ -48,6 +32,26 @@ test('test-7', () => {
tsd.printType(prout);
});

test('test-8', () => {
tsd.expectType<typeof prout>({
hello: 'you',
life: 42,
data: {
id: '385643984',
items: [1, 2, 3],
},
});
});

// test('test-9', () => {
// const plop = 42;
// tsd.expectError(true);
// });

// test('test-10', () => {
// const plop = 42;
// });

/**
* @deprecated
*/
Expand Down

0 comments on commit 510230c

Please sign in to comment.