-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
206 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,36 @@ | ||
export const VITE_PLUGIN_VITEST_TYPESCRIPT_ASSERT = 'tsd'; | ||
|
||
export function expectType() { | ||
return; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type ExplicitAny = any; | ||
|
||
export function expectType<T>(value: T) { | ||
return value; | ||
} | ||
|
||
export function expectNotType<T>(value: ExplicitAny) { | ||
return value as T; | ||
} | ||
|
||
export function expectAssignable<T>(value: T) { | ||
return value; | ||
} | ||
|
||
export function expectNotAssignable<T>(value: ExplicitAny) { | ||
return value as T; | ||
} | ||
|
||
export function expectError<T = ExplicitAny>(value: T) { | ||
return value; | ||
} | ||
|
||
export function expectDeprecated(expression: ExplicitAny) { | ||
return expression as unknown; | ||
} | ||
|
||
export function expectNotDeprecated(expression: ExplicitAny) { | ||
return expression as unknown; | ||
} | ||
|
||
export function printType(expression: ExplicitAny) { | ||
return expression as unknown; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
export const VITE_PLUGIN_VITEST_TYPESCRIPT_ASSERT = 'tssert'; | ||
|
||
export function expectType() { | ||
return; | ||
export function expectType<SourceType>(source?: unknown) { | ||
const dumbFunction = <TargetType>(target?: unknown) => { | ||
return [source, target] as [SourceType, TargetType]; | ||
}; | ||
|
||
return { | ||
assignableTo: dumbFunction, | ||
}; | ||
} |
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,57 @@ | ||
import type ts from 'byots'; | ||
import { getTypes } from './util'; | ||
import type { AssertionResult } from '../types'; | ||
import { createAssertionDiagnostic } from '../../util'; | ||
|
||
export function expectType( | ||
sourceFile: ts.SourceFile, | ||
node: ts.CallExpression, | ||
checker: ts.TypeChecker, | ||
): AssertionResult { | ||
const { source, target } = getTypes(node, checker); | ||
|
||
if (!source.type || !target.type) { | ||
throw new Error('Prout'); | ||
} | ||
|
||
if (!checker.isTypeAssignableTo(source.type, target.type)) { | ||
const sourceString = checker.typeToString(source.type); | ||
const targetString = checker.typeToString(target.type); | ||
|
||
return createAssertionDiagnostic( | ||
`Type \`${sourceString}\` is not assignable to type \`${targetString}\`.`, | ||
sourceFile, | ||
source.position, | ||
); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
// export function expectNotType(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function expectAssignable(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function expectNotAssignable(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function expectError(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function expectDeprecated(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function expectNotDeprecated(): AssertionResult { | ||
// return undefined; | ||
// } | ||
|
||
// export function printType(): AssertionResult { | ||
// return undefined; | ||
// } |
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 |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import type ts from 'byots'; | ||
import * as assert from './assert'; | ||
import type { ProcessCallExpressionReturn } from '../types'; | ||
|
||
const names = new Map(Object.entries(assert)); | ||
|
||
export function processCallExpression( | ||
sourceFile: ts.SourceFile, | ||
node: ts.CallExpression, | ||
checker: ts.TypeChecker, | ||
): ProcessCallExpressionReturn { | ||
let diagnostic: ts.Diagnostic | undefined = undefined; | ||
|
||
export function processCallExpression(node: ts.CallExpression) { | ||
const identifier = node.expression.getText(); | ||
const assertFunction = names.get(identifier); | ||
|
||
if (assertFunction) { | ||
diagnostic = assertFunction(sourceFile, node, checker); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('> tsd', { identifier }); | ||
return { success: !diagnostic, skipped: !assertFunction, diagnostic }; | ||
} |
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,37 @@ | ||
import type ts from 'byots'; | ||
import { getMiddle } from '../../../typescript/util'; | ||
|
||
export interface AssertionType { | ||
node: ts.CallExpression; | ||
type: ts.Type | undefined; | ||
position: number; | ||
} | ||
|
||
export function getTypes( | ||
node: ts.CallExpression, | ||
checker: ts.TypeChecker, | ||
): { | ||
source: AssertionType; | ||
target: AssertionType; | ||
} { | ||
let targetType = undefined; | ||
let sourceType = undefined; | ||
|
||
let targetMiddle = -1; | ||
let sourceMiddle = -1; | ||
|
||
if (node.typeArguments?.[0]) { | ||
targetType = checker.getTypeFromTypeNode(node.typeArguments[0]); | ||
targetMiddle = getMiddle(node.typeArguments[0]); | ||
} | ||
|
||
if (node.arguments[0]) { | ||
sourceType = checker.getTypeAtLocation(node.arguments[0]); | ||
sourceMiddle = getMiddle(node.arguments[0]); | ||
} | ||
|
||
return { | ||
source: { node, type: sourceType, position: sourceMiddle }, | ||
target: { node, type: targetType, position: targetMiddle }, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import type ts from 'byots'; | ||
import { createAssertionDiagnostic } from '../../util'; | ||
import type { ProcessCallExpressionReturn } from '../types'; | ||
|
||
export function processCallExpression( | ||
sourceFile: ts.SourceFile, | ||
node: ts.CallExpression, | ||
checker: ts.TypeChecker, | ||
): ProcessCallExpressionReturn { | ||
let diagnostic: ts.Diagnostic | undefined = undefined; | ||
|
||
export function processCallExpression(node: ts.CallExpression) { | ||
const identifier = node.expression.getText(); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('> tssert', { identifier }); | ||
console.log('> tssert', { identifier, checker, sourceFile }); | ||
|
||
if (identifier.length < 0) { | ||
diagnostic = createAssertionDiagnostic(`No implemented....`, sourceFile, 0); | ||
} | ||
|
||
return { success: !diagnostic, skipped: true, diagnostic }; | ||
} |
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,15 @@ | ||
import type ts from 'byots'; | ||
|
||
export interface ProcessCallExpressionReturn { | ||
success: boolean; | ||
skipped: boolean; | ||
diagnostic: ts.Diagnostic | undefined; | ||
} | ||
|
||
export type AssertionResult = ts.Diagnostic | undefined; | ||
|
||
export type AssertionFunction = ( | ||
sourceFile: ts.SourceFile, | ||
node: ts.CallExpression, | ||
checker: ts.TypeChecker, | ||
) => AssertionResult; |
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