-
-
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
3 changed files
with
126 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import path from 'path'; | ||
import ts from 'typescript'; | ||
|
||
const fileExists = (path: string) => ts.sys.fileExists(path); | ||
const readFile = (path: string) => ts.sys.readFile(path); | ||
|
||
export function parseConfigFile(fileName: string, compilerOptions: ts.CompilerOptions = {}) { | ||
const configFile = readFile(fileName); | ||
|
||
if (!configFile) { | ||
// TODO better error handling, create diagnostic ? | ||
throw new Error('tsconfig.json not readable'); | ||
} | ||
|
||
const configObject = ts.parseConfigFileTextToJson(fileName, configFile); | ||
|
||
return ts.parseJsonConfigFileContent(configObject.config, ts.sys, path.dirname(fileName), compilerOptions); | ||
} | ||
|
||
export interface DiagnosticMessage { | ||
file?: string | undefined; | ||
line?: number; | ||
column?: number; | ||
message: string; | ||
code: number; | ||
category: number; | ||
} | ||
|
||
export function typeCheck(fileName: string) { | ||
const configFile = ts.findConfigFile(__dirname, fileExists, 'tsconfig.json'); | ||
|
||
if (!configFile) { | ||
// TODO better error handling, create diagnostic ? | ||
throw new Error('tsconfig.json not found'); | ||
} | ||
|
||
const config = parseConfigFile(configFile); | ||
const program = ts.createProgram([fileName], { | ||
...ts.getDefaultCompilerOptions(), | ||
...config.options, | ||
}); | ||
|
||
const messages: DiagnosticMessage[] = []; | ||
|
||
ts.getPreEmitDiagnostics(program).forEach((diagnostic) => { | ||
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | ||
const payload = { code: diagnostic.code, category: diagnostic.category, message }; | ||
|
||
if (!diagnostic.file) { | ||
messages.push(payload); | ||
return; | ||
} | ||
|
||
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, Number(diagnostic.start)); | ||
|
||
messages.push({ ...payload, file: diagnostic.file.fileName, line, column: character }); | ||
}); | ||
|
||
return messages; | ||
} |
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,9 +1,65 @@ | ||
import type { Plugin } from 'vite'; | ||
import { typeCheck, type DiagnosticMessage } from './compiler'; | ||
|
||
export function vitestTypescriptAssertPlugin(): Plugin { | ||
return { | ||
name: 'vitest:typescript-assert', | ||
apply: 'serve', | ||
enforce: 'pre', | ||
transform(code, id) { | ||
if (!id.endsWith('.test.ts')) { | ||
// TODO get from config, with glob pattern | ||
return; | ||
} | ||
|
||
return `${code}${error(typeCheck(id))}`; | ||
}, | ||
}; | ||
} | ||
|
||
function error(messages?: DiagnosticMessage[]) { | ||
const fistMessage = messages ? messages[0] : undefined; | ||
|
||
if (!fistMessage) { | ||
return ''; | ||
} | ||
|
||
const file = String(fistMessage.file); | ||
const line = Number(fistMessage.line) + 1; | ||
const column = Number(fistMessage.column) * 2; | ||
|
||
return `\ntest.concurrent('type-check', () => {throw error()}); | ||
function error() { | ||
const err = new TypeError("${fistMessage.message} ts(${fistMessage.code})"); | ||
err.name = "TypeError"; | ||
err.nameStr = "TypeError"; | ||
// err.showDiff = true; | ||
// err.actual = "life"; | ||
// err.expected = "42"; | ||
// err.stackStr = "???"; | ||
// err.operator = "???"; | ||
// err.type = "???"; | ||
err.stacks = [{ | ||
method: "", | ||
file: "${file}", | ||
line: ${line}, | ||
column: ${column}, | ||
sourcePos: { | ||
source: "${file}", | ||
line: ${line}, | ||
column: ${column}, | ||
} | ||
}] | ||
// console.log(err.stack) | ||
// console.log(err.stacks) | ||
return 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