-
-
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.
feat: report typescript diagnostics (optional)
- Loading branch information
Showing
13 changed files
with
177 additions
and
23 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 |
---|---|---|
@@ -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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// TODO: should be set by user config?! | ||
export const testWrapperIdentifiers = [ | ||
'it', | ||
'it.skip', | ||
'it.skipIf', | ||
'it.runIf', | ||
'it.only', | ||
'it.concurrent', | ||
'it.todo', | ||
'it.fails', | ||
'it.each', | ||
'test', | ||
'test.skip', | ||
'test.skipIf', | ||
'test.runIf', | ||
'test.only', | ||
'test.concurrent', | ||
'test.todo', | ||
'test.fails', | ||
'test.each', | ||
'describe', | ||
'describe.skip', | ||
'describe.only', | ||
'describe.concurrent', | ||
'describe.shuffle', | ||
'describe.todo', | ||
'describe.each', | ||
]; |
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,53 @@ | ||
import ts from 'byots'; | ||
import type MagicString from 'magic-string'; | ||
import { createErrorString } from './error'; | ||
import { newLine } from '../typescript/util'; | ||
import { testWrapperIdentifiers } from './identifiers'; | ||
|
||
export function searchTestWrapperFromPosition(file: ts.SourceFile, position: number) { | ||
const token = ts.getTokenAtPosition(file, position); | ||
|
||
let parent: ts.Node | undefined = token.parent; | ||
|
||
while (parent) { | ||
if (ts.isCallExpression(parent)) { | ||
const expression = parent.expression; | ||
const identifier = expression.getText(); | ||
|
||
if (testWrapperIdentifiers.includes(identifier)) { | ||
const name = parent.arguments[0]?.getText().slice(1, -1); | ||
const handler = parent.arguments[1]; | ||
|
||
if (name && handler) { | ||
return { name, expression, handler }; | ||
} | ||
} | ||
|
||
parent = undefined; | ||
} | ||
|
||
parent = parent?.parent; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
export function reportDiagnostics(diagnostics: readonly ts.Diagnostic[], newCode: MagicString, fileName: string) { | ||
if (diagnostics[0]) { | ||
const { messageText, start, file } = diagnostics[0]; | ||
|
||
if (file) { | ||
const startPosition = start ?? -1; | ||
const message = ts.flattenDiagnosticMessageText(messageText, newLine); | ||
const { line, character } = ts.getLineAndCharacterOfPosition(file, startPosition); | ||
const token = searchTestWrapperFromPosition(file, startPosition); | ||
|
||
if (token) { | ||
const position = token.handler.getEnd() - 1; | ||
newCode.appendLeft(position, createErrorString({ message, file: fileName, line, column: character })); | ||
} else { | ||
newCode.append(createErrorString({ message, file: fileName, line, column: character })); | ||
} | ||
} | ||
} | ||
} |
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,10 @@ | ||
import ts from 'byots'; | ||
|
||
export function program({ fileName, config }: { fileName: string; config: ts.ParsedCommandLine }) { | ||
const compilerOptions = { ...ts.getDefaultCompilerOptions(), ...config.options }; | ||
const program = ts.createProgram([fileName], compilerOptions); | ||
const diagnostics = ts.getPreEmitDiagnostics(program); | ||
const checker = program.getTypeChecker(); | ||
|
||
return { program, diagnostics, checker }; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
import { test, expect, describe } from 'vitest'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const prout1 = 'plop'; | ||
|
||
test('test-1', () => { | ||
expect('Hello World').toBe(42); | ||
|
||
// const prout2 = 'plop'; | ||
}); | ||
|
||
describe('test-2', () => { | ||
test('test-2', () => { | ||
expect('Hello World').toBe(24); | ||
describe('describe-1', () => { | ||
// const prout3 = 'plop'; | ||
|
||
test('test-3', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const prout4 = 'plop'; | ||
|
||
// expect('Hello World').toBe(24); | ||
}); | ||
}); |
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