From 510230cd73f2eab45f4fe8367caa19a282d0a2d3 Mon Sep 17 00:00:00 2001 From: skarab42 Date: Tue, 12 Jul 2022 15:11:42 +0200 Subject: [PATCH] fix: tsd expectError --- src/plugin/assert/tsd/expect-error.ts | 30 ++++++++-------------- test/tsd.test.ts | 36 +++++++++++++++------------ 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/plugin/assert/tsd/expect-error.ts b/src/plugin/assert/tsd/expect-error.ts index 0068af7..2a54f48 100644 --- a/src/plugin/assert/tsd/expect-error.ts +++ b/src/plugin/assert/tsd/expect-error.ts @@ -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]; @@ -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()); } diff --git a/test/tsd.test.ts b/test/tsd.test.ts index 6021f9f..9325865 100644 --- a/test/tsd.test.ts +++ b/test/tsd.test.ts @@ -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('hello'); -// expectType('hello'); -// expectNotType('hello'); -// tsd.expectType('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'; @@ -48,6 +32,26 @@ test('test-7', () => { tsd.printType(prout); }); +test('test-8', () => { + tsd.expectType({ + 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 */