-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathenhance-logs.ts
More file actions
141 lines (117 loc) · 3.64 KB
/
enhance-logs.ts
File metadata and controls
141 lines (117 loc) · 3.64 KB
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
import chalk from 'chalk'
import { normalizePath } from 'vite'
import { Visitor, parseSync } from 'oxc-parser'
import type { CallExpression, MemberExpression } from 'oxc-parser'
type Insertion = {
at: number
text: string
}
const buildLineStarts = (source: string) => {
const starts = [0]
for (let i = 0; i < source.length; i++) {
if (source[i] === '\n') {
starts.push(i + 1)
}
}
return starts
}
const offsetToLineColumn = (offset: number, lineStarts: Array<number>) => {
// Binary search to find the nearest line start <= offset.
let low = 0
let high = lineStarts.length - 1
while (low <= high) {
const mid = (low + high) >> 1
const lineStart = lineStarts[mid]
if (lineStart === undefined) {
break
}
if (lineStart <= offset) {
low = mid + 1
} else {
high = mid - 1
}
}
const lineIndex = Math.max(0, high)
const lineStart = lineStarts[lineIndex] ?? 0
return {
line: lineIndex + 1,
column: offset - lineStart + 1,
}
}
const isConsoleMemberExpression = (
callee: CallExpression['callee'],
): callee is MemberExpression => {
return (
callee.type === 'MemberExpression' &&
callee.computed === false &&
callee.object.type === 'Identifier' &&
callee.object.name === 'console' &&
callee.property.type === 'Identifier' &&
(callee.property.name === 'log' || callee.property.name === 'error')
)
}
const applyInsertions = (source: string, insertions: Array<Insertion>) => {
const ordered = [...insertions].sort((a, b) => b.at - a.at)
let next = source
for (const insertion of ordered) {
next = next.slice(0, insertion.at) + insertion.text + next.slice(insertion.at)
}
return next
}
export function enhanceConsoleLog(code: string, id: string, port: number) {
const [filePath] = id.split('?')
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
const location = filePath?.replace(normalizePath(process.cwd()), '')!
try {
const result = parseSync(filePath ?? id, code, {
sourceType: 'module',
lang: 'tsx',
range: true,
})
if (result.errors.length > 0) {
return
}
const insertions: Array<Insertion> = []
const lineStarts = buildLineStarts(code)
new Visitor({
CallExpression(node) {
if (!isConsoleMemberExpression(node.callee)) {
return
}
const { line, column } = offsetToLineColumn(node.start, lineStarts)
const finalPath = `${location}:${line}:${column}`
const serverLogMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(finalPath)}\n → `
const browserLogMessage = `%cLOG%c %cGo to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(
finalPath,
)}%c \n → `
const argsArray =
`[${JSON.stringify(serverLogMessage)}]` +
` : [${JSON.stringify(browserLogMessage)},` +
`${JSON.stringify('color:#A0A')},` +
`${JSON.stringify('color:#FFF')},` +
`${JSON.stringify('color:#55F')},` +
`${JSON.stringify('color:#FFF')}]`
const injectedPrefix =
`...(typeof window === 'undefined' ? ${argsArray})` +
`${node.arguments.length > 0 ? ', ' : ''}`
const insertionPoint =
node.arguments[0]?.start !== undefined
? node.arguments[0].start
: node.end - 1
insertions.push({
at: insertionPoint,
text: injectedPrefix,
})
},
}).visit(result.program)
if (insertions.length === 0) {
return
}
return {
code: applyInsertions(code, insertions),
map: null,
}
} catch {
return
}
}