Skip to content

Commit

Permalink
feat: type check
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 6, 2022
1 parent 43acfac commit 7ec0912
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/compiler.ts
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;
}
56 changes: 56 additions & 0 deletions src/index.ts
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;
}
`;
}
11 changes: 10 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { expect, test } from 'vitest';
test('test-1', () => {
expect(42).toBe(42);
expect(42).toBe(25); // <- fail
});

test('test-2', () => {
expect(true).toBe(false); // <- fail

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const prout = 'pouet'; // <- shoul fail
const prout = 'pouet'; // <- should fail

// eslint-disable-next-line no-console
console.log('plop'); // <- should fail

throw new Error('Prout'); // <- fail
});

0 comments on commit 7ec0912

Please sign in to comment.