-
-
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
12 changed files
with
331 additions
and
81 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 +1,9 @@ | ||
export function expectType<T = undefined>(value: unknown): T { | ||
return value as T; | ||
} | ||
export function expectType<SourceType>(source?: unknown) { | ||
const dumbFunction = <TargetType>(target?: unknown) => { | ||
return [source, target] as [SourceType, TargetType]; | ||
}; | ||
|
||
export function expectTypeEqual<T = undefined>(value: unknown): T { | ||
return value as T; | ||
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,32 @@ | ||
export function createErrorString({ | ||
message, | ||
file, | ||
line, | ||
column, | ||
}: { | ||
message: string; | ||
file: string; | ||
line: number; | ||
column: number; | ||
}) { | ||
return ` | ||
// error generated by vite-plugin-vitest-typescript-assert | ||
(() => { | ||
const err = new TypeError("${message}"); | ||
err.name = "TypeError"; | ||
err.stack = ""; | ||
err.stackStr = ""; | ||
err.stacks = [{ | ||
file: "${file}", | ||
line: ${line + 1}, | ||
column: ${column}, | ||
sourcePos: { | ||
source: "${file}", | ||
line: ${line + 1}, | ||
column: ${column} | ||
} | ||
}]; | ||
throw err; | ||
})() | ||
`; | ||
} |
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,37 +1,43 @@ | ||
import type { Plugin } from 'vite'; | ||
import { typeCheck } from './program'; | ||
import MagicString from 'magic-string'; | ||
import { createErrorString } from './error'; | ||
|
||
export function vitestTypescriptAssertPlugin(): Plugin { | ||
return { | ||
name: 'vitest:typescript-assert', | ||
apply: 'serve', | ||
enforce: 'pre', | ||
// configureServer(server) { | ||
// const prout = (path: string) => { | ||
// setTimeout(() => { | ||
// console.log('>>>>>>>>>>', path); | ||
// }, 2000); | ||
// }; | ||
|
||
// server.watcher.on('add', prout); | ||
// server.watcher.on('change', prout); | ||
// server.watcher.on('unlink', prout); | ||
// }, | ||
transform(code, fileName) { | ||
if (!fileName.endsWith('.test.ts')) { | ||
// TODO get from config, with glob pattern | ||
return; | ||
} | ||
|
||
// WTF is appening here ???? | ||
code = code.replace(/\t/g, ' '); | ||
|
||
const result = typeCheck({ | ||
configName: 'tsconfig.check.json', | ||
input: { fileName, code }, | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(result); | ||
const newCode = new MagicString(code); | ||
|
||
result.assertionDiagnostics?.forEach((diagnostic) => { | ||
const column = diagnostic.position.character; | ||
const line = diagnostic.position.line; | ||
const message = diagnostic.message.replace(/"/g, '\\"'); | ||
|
||
const lastBlockNode = diagnostic.path[diagnostic.path.length - 1]?.node; | ||
const lastBlockPosition = lastBlockNode?.getEnd() ?? newCode.length(); | ||
newCode.appendLeft(lastBlockPosition - 2, createErrorString({ message, file: fileName, line, column })); | ||
}); | ||
|
||
return code; | ||
return { | ||
code: newCode.toString(), | ||
map: newCode.generateMap({ hires: true }), | ||
}; | ||
}, | ||
}; | ||
} |
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,34 @@ | ||
import type ts from 'typescript'; | ||
import { AssertionNode, getTypes } from '../index'; | ||
import { createAssertionDiagnostic } from '../diagnostic'; | ||
|
||
export function assignableTo(path: AssertionNode[], target: ts.CallExpression, typeChecker: ts.TypeChecker) { | ||
const types = getTypes(target, typeChecker); | ||
|
||
const sourceString = typeChecker.typeToString(types.source.type); | ||
const targetString = typeChecker.typeToString(types.target.type); | ||
|
||
// @ts-expect-error access to internal API | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
const pass = typeChecker.isTypeAssignableTo(types.source.type, types.target.type) as boolean; | ||
|
||
const position = types.source.node.getSourceFile().getLineAndCharacterOfPosition(types.source.position); | ||
|
||
let diagnostic = undefined; | ||
|
||
if (!pass) { | ||
diagnostic = createAssertionDiagnostic({ | ||
message: `Type \`${sourceString}\` is not assignable to type \`${targetString}\`.`, | ||
position, | ||
path, | ||
}); | ||
} | ||
|
||
return { | ||
pass, | ||
fail: !pass, | ||
source: sourceString, | ||
target: targetString, | ||
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 @@ | ||
export * from './assignableTo'; |
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,12 @@ | ||
import type ts from 'typescript'; | ||
import type { AssertionNode } from '.'; | ||
|
||
export interface AssertionDiagnostic { | ||
message: string; | ||
position: ts.LineAndCharacter; | ||
path: AssertionNode[]; | ||
} | ||
|
||
export function createAssertionDiagnostic(diagnostic: AssertionDiagnostic): AssertionDiagnostic { | ||
return { ...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
Oops, something went wrong.