-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
193 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { getTypes } from './util'; | ||
import ts from 'unleashed-typescript'; | ||
import type { Assertion } from '../../types'; | ||
import { isArgumentInDiagnostic } from '../util'; | ||
import { newLine } from '../../../typescript/util'; | ||
import type { Compiler } from '../../../typescript/types'; | ||
import { createAssertionDiagnostic } from '../../diagnostics'; | ||
import { ErrorCode, errorMessage } from '../../../common/error'; | ||
|
||
export function toThrowError({ node }: Assertion, compiler: Compiler): ts.Diagnostic | undefined { | ||
const expectedNode = node.expression.getChildAt(0) as ts.CallExpression; | ||
const expected = getTypes(expectedNode, compiler.typeChecker); | ||
|
||
if (expected.diagnostic) { | ||
return expected.diagnostic; | ||
} | ||
|
||
const argument = getTypes(node, compiler.typeChecker); | ||
|
||
if (argument.diagnostic && argument.errorCode !== ErrorCode.ASSERT_MISSING_TYPE_OR_VALUE) { | ||
return argument.diagnostic; | ||
} | ||
|
||
if (!compiler.diagnostics.length) { | ||
return createAssertionDiagnostic(errorMessage(ErrorCode.ASSERT_ERROR), compiler.sourceFile, expected.position); | ||
} | ||
|
||
const diagnostic = compiler.diagnostics.find((diagnostic) => isArgumentInDiagnostic(expected.argument, diagnostic)); | ||
|
||
if (!diagnostic) { | ||
return createAssertionDiagnostic(errorMessage(ErrorCode.ASSERT_ERROR), compiler.sourceFile, expected.position); | ||
} | ||
|
||
if (argument.argument) { | ||
if (ts.isStringLiteral(argument.argument)) { | ||
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, newLine); | ||
|
||
if (!message.includes(argument.argument.text)) { | ||
return createAssertionDiagnostic( | ||
errorMessage(ErrorCode.ASSERT_ERROR_MESSAGE, { expected: argument.argument.text, received: message }), | ||
compiler.sourceFile, | ||
expected.position, | ||
); | ||
} | ||
} else if (ts.isNumericLiteral(argument.argument)) { | ||
if (Number(argument.argument.text) !== diagnostic.code) { | ||
return createAssertionDiagnostic( | ||
errorMessage(ErrorCode.ASSERT_ERROR_CODE, { expected: argument.argument.text, received: diagnostic.code }), | ||
compiler.sourceFile, | ||
expected.position, | ||
); | ||
} | ||
} else { | ||
// TODO: resolve value | ||
return createAssertionDiagnostic( | ||
errorMessage(ErrorCode.ASSERT_STRING_OR_NUMBER_LITERAL), | ||
compiler.sourceFile, | ||
argument.argument.getStart(), | ||
); | ||
} | ||
} | ||
|
||
compiler.diagnostics = compiler.diagnostics.filter((d) => d !== diagnostic); | ||
|
||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { test } from 'vitest'; | ||
import * as tsd from '../src/api/tsd'; | ||
import { expectType, expectType as assertType } from '../src/api/tsd'; | ||
|
||
test('test-1', () => { | ||
expectType<string>('hello'); | ||
}); | ||
|
||
test('test-2', () => { | ||
assertType<string>('hello'); | ||
}); | ||
|
||
test('test-3', () => { | ||
tsd.expectNotType<string>('hello'); | ||
}); | ||
|
||
test('test-4', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
tsd.expectError(42 === 'life'); | ||
}); | ||
|
||
test('test-5', () => { | ||
tsd.expectError(true); | ||
}); | ||
|
||
test('test-6', () => { | ||
tsd.expectDeprecated(UnicornClass); | ||
tsd.expectNotDeprecated(UnicornClass); | ||
}); | ||
|
||
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 | ||
*/ | ||
export class UnicornClass { | ||
readonly key = '🦄'; | ||
} | ||
|
||
export class RainbowClass { | ||
readonly key = '🌈'; | ||
} | ||
|
||
export interface Options { | ||
/** | ||
* @deprecated | ||
*/ | ||
readonly separator: string; | ||
readonly delimiter: string; | ||
} | ||
|
||
export const prout = { | ||
hello: 'world', | ||
life: 42, | ||
data: { | ||
id: 385643984, | ||
items: [1, 2, 3], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters