-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
parser.ts
234 lines (207 loc) · 6.79 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { rm } from 'fs/promises'
import { execaCommand } from 'execa'
import { resolve } from 'pathe'
import { SourceMapConsumer } from 'source-map-js'
import { ensurePackageInstalled } from '../utils'
import type { Awaitable, File, ParsedStack, Task, TscErrorInfo, Vitest } from '../types'
import { getRawErrsMapFromTsCompile, getTsconfigPath } from './parse'
import { createIndexMap } from './utils'
import type { FileInformation } from './collect'
import { collectTests } from './collect'
export class TypeCheckError extends Error {
name = 'TypeCheckError'
constructor(public message: string, public stacks: ParsedStack[]) {
super(message)
}
}
interface ErrorsCache {
files: File[]
sourceErrors: TypeCheckError[]
}
type Callback<Args extends Array<any> = []> = (...args: Args) => Awaitable<void>
export class Typechecker {
private _onParseStart?: Callback
private _onParseEnd?: Callback<[ErrorsCache]>
private _onWatcherRerun?: Callback
private _result: ErrorsCache = {
files: [],
sourceErrors: [],
}
private _tests: Record<string, FileInformation> | null = {}
private tmpConfigPath?: string
constructor(protected ctx: Vitest, protected files: string[]) {}
public onParseStart(fn: Callback) {
this._onParseStart = fn
}
public onParseEnd(fn: Callback<[ErrorsCache]>) {
this._onParseEnd = fn
}
public onWatcherRerun(fn: Callback) {
this._onWatcherRerun = fn
}
protected async collectFileTests(filepath: string): Promise<FileInformation | null> {
return collectTests(this.ctx, filepath)
}
public async collectTests() {
const tests = (await Promise.all(
this.files.map(filepath => this.collectFileTests(filepath)),
)).reduce((acc, data) => {
if (!data)
return acc
acc[data.filepath] = data
return acc
}, {} as Record<string, FileInformation>)
this._tests = tests
return tests
}
protected async prepareResults(output: string) {
const typeErrors = await this.parseTscLikeOutput(output)
const testFiles = new Set(this.files)
let tests = this._tests
if (!tests)
tests = await this.collectTests()
const sourceErrors: TypeCheckError[] = []
const files: File[] = []
testFiles.forEach((path) => {
const { file, definitions, map, parsed } = tests![path]
const errors = typeErrors.get(path)
files.push(file)
if (!errors)
return
const sortedDefinitions = [...definitions.sort((a, b) => b.start - a.start)]
// has no map for ".js" files that use // @ts-check
const mapConsumer = map && new SourceMapConsumer(map)
const indexMap = createIndexMap(parsed)
const markFailed = (task: Task) => {
task.result = {
state: task.mode === 'run' || task.mode === 'only' ? 'fail' : task.mode,
}
if (task.suite)
markFailed(task.suite)
}
errors.forEach(({ error, originalError }, idx) => {
const originalPos = mapConsumer?.generatedPositionFor({
line: originalError.line,
column: originalError.column,
source: path,
}) || originalError
const index = indexMap.get(`${originalPos.line}:${originalPos.column}`)
const definition = (index != null && sortedDefinitions.find(def => def.start <= index && def.end >= index)) || file
const suite = 'task' in definition ? definition.task : definition
const state = suite.mode === 'run' || suite.mode === 'only' ? 'fail' : suite.mode
const task: Task = {
type: 'typecheck',
id: idx.toString(),
name: `error expect ${idx + 1}`, // TODO naming
mode: suite.mode,
file,
suite,
result: {
state,
error: state === 'fail' ? error : undefined,
},
}
if (state === 'fail')
markFailed(suite)
suite.tasks.push(task)
})
})
typeErrors.forEach((errors, path) => {
if (!testFiles.has(path))
sourceErrors.push(...errors.map(({ error }) => error))
})
return {
files,
sourceErrors,
}
}
protected async parseTscLikeOutput(output: string) {
const errorsMap = await getRawErrsMapFromTsCompile(output)
const typesErrors = new Map<string, { error: TypeCheckError; originalError: TscErrorInfo }[]>()
errorsMap.forEach((errors, path) => {
const filepath = resolve(this.ctx.config.root, path)
const suiteErrors = errors.map((info) => {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 0
const error = new TypeCheckError(info.errMsg, [
{
file: filepath,
line: info.line,
column: info.column,
method: '', // TODO, build error based on method
sourcePos: {
line: info.line,
column: info.column,
},
},
])
Error.stackTraceLimit = limit
return {
originalError: info,
error,
}
})
typesErrors.set(filepath, suiteErrors)
})
return typesErrors
}
// TODO call before exiting process
public async clean() {
if (this.tmpConfigPath)
await rm(this.tmpConfigPath)
}
public async start() {
const { root, watch, typecheck } = this.ctx.config
const packageName = typecheck.checker === 'tsc' ? 'typescript' : 'vue-tsc'
await ensurePackageInstalled(packageName, root)
this.tmpConfigPath = await getTsconfigPath(root)
let cmd = `${typecheck.checker} --noEmit --pretty false -p ${this.tmpConfigPath}`
// use builtin watcher, because it's faster
if (watch)
cmd += ' --watch'
if (typecheck.allowJs)
cmd += ' --allowJs --checkJs'
let output = ''
const stdout = execaCommand(cmd, {
cwd: root,
stdout: 'pipe',
reject: false,
})
await this._onParseStart?.()
let rerunTriggered = false
stdout.stdout?.on('data', (chunk) => {
output += chunk
if (!watch)
return
if (output.includes('File change detected') && !rerunTriggered) {
this._onWatcherRerun?.()
this._result.sourceErrors = []
this._result.files = []
this._tests = null // test structure migh've changed
rerunTriggered = true
}
if (/Found \w+ errors*. Watching for/.test(output)) {
rerunTriggered = false
this.prepareResults(output).then((result) => {
this._result = result
this._onParseEnd?.(result)
})
output = ''
}
})
if (!watch) {
await stdout
this._result = await this.prepareResults(output)
await this._onParseEnd?.(this._result)
}
}
public getResult() {
return this._result
}
public getTestFiles() {
return Object.values(this._tests || {}).map(({ file }) => ({
...file,
result: undefined,
}))
}
}