-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathutils.ts
242 lines (219 loc) · 6.16 KB
/
utils.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
235
236
237
238
239
240
241
242
import { Range } from 'vscode-languageserver-textdocument';
import * as c from './constants';
import * as childProcess from 'child_process';
import * as p from "vscode-languageserver-protocol";
import * as path from 'path';
import * as t from "vscode-languageserver-types";
import * as tmp from 'tmp';
import fs from 'fs';
import { report } from 'process';
// TODO: races here
// TODO: this doesn't handle file:/// scheme
export let findProjectRootOfFile = (source: p.DocumentUri): null | p.DocumentUri => {
let dir = path.dirname(source)
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
return dir
} else {
if (dir === source) {
// reached top
return null
} else {
return findProjectRootOfFile(dir)
}
}
}
type execResult = {
kind: 'success',
result: string
} | {
kind: 'error'
error: string,
};
export let formatUsingValidBscPath = (code: string, bscPath: p.DocumentUri, isInterface: boolean): execResult => {
// library cleans up after itself. No need to manually remove temp file
let tmpobj = tmp.fileSync();
let extension = isInterface ? c.resiExt : c.resExt;
let fileToFormat = tmpobj.name + extension;
fs.writeFileSync(fileToFormat, code, { encoding: 'utf-8' });
try {
let result = childProcess.execFileSync(bscPath, ['-color', 'never', '-format', fileToFormat], { stdio: 'pipe' })
return {
kind: 'success',
result: result.toString(),
}
} catch (e) {
return {
kind: 'error',
error: e.message,
}
}
}
export let runBsbWatcherUsingValidBsbPath = (bsbPath: p.DocumentUri, projectRootPath: p.DocumentUri) => {
let process = childProcess.execFile(bsbPath, ['-w'], { cwd: projectRootPath })
return process
// try {
// let result = childProcess.execFileSync(bsbPath, [], { stdio: 'pipe', cwd: projectRootPath })
// return {
// kind: 'success',
// result: result.toString(),
// }
// } catch (e) {
// return {
// kind: 'error',
// error: e.message,
// }
// }
}
export let parseDiagnosticLocation = (location: string): Range => {
// example output location:
// 3:9
// 3:5-8
// 3:9-6:1
// language-server position is 0-based. Ours is 1-based. Don't forget to convert
// also, our end character is inclusive. Language-server's is exclusive
let isRange = location.indexOf('-') >= 0
if (isRange) {
let [from, to] = location.split('-')
let [fromLine, fromChar] = from.split(':')
let isSingleLine = to.indexOf(':') >= 0
let [toLine, toChar] = isSingleLine ? to.split(':') : [fromLine, to]
return {
start: { line: parseInt(fromLine) - 1, character: parseInt(fromChar) - 1 },
end: { line: parseInt(toLine) - 1, character: parseInt(toChar) },
}
} else {
let [line, char] = location.split(':')
let start = { line: parseInt(line) - 1, character: parseInt(char) }
return {
start: start,
end: start,
}
}
}
type filesDiagnostics = {
[key: string]: p.Diagnostic[];
}
type parsedCompilerLogResult = {
done: boolean,
result: filesDiagnostics,
}
export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult => {
/* example .compiler.log file content that we're gonna parse:
#Start(1600519680823)
Syntax error!
/Users/chenglou/github/reason-react/src/test.res:1:8-2:3
1 │ let a =
2 │ let b =
3 │
This let-binding misses an expression
Warning number 8
/Users/chenglou/github/reason-react/src/test.res:3:5-8
1 │ let a = j`😀`
2 │ let b = `😀`
3 │ let None = None
4 │ let bla: int = "
5 │ hi
You forgot to handle a possible case here, for example:
Some _
We've found a bug for you!
/Users/chenglou/github/reason-react/src/test.res:3:9
1 │ let a = 1
2 │ let b = "hi"
3 │ let a = b + 1
This has type: string
Somewhere wanted: int
#Done(1600519680836)
*/
type parsedDiagnostic = {
code: number | undefined,
severity: t.DiagnosticSeverity,
tag: t.DiagnosticTag | undefined,
content: string[]
}
let parsedDiagnostics: parsedDiagnostic[] = [];
let lines = content.split('\n');
let done = false;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (line.startsWith(' We\'ve found a bug for you!')) {
parsedDiagnostics.push({
code: undefined,
severity: t.DiagnosticSeverity.Error,
tag: undefined,
content: []
})
} else if (line.startsWith(' Warning number ')) {
let warningNumber = parseInt(line.slice(' Warning number '.length))
let tag: t.DiagnosticTag | undefined = undefined
switch (warningNumber) {
case 11:
case 20:
case 26:
case 27:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 60:
case 66:
case 67:
case 101:
tag = t.DiagnosticTag.Unnecessary
break;
case 3:
tag = t.DiagnosticTag.Deprecated
break;
}
parsedDiagnostics.push({
code: Number.isNaN(warningNumber) ? undefined : warningNumber,
severity: t.DiagnosticSeverity.Warning,
tag: tag,
content: []
})
} else if (line.startsWith(' Syntax error!')) {
parsedDiagnostics.push({
code: undefined,
severity: t.DiagnosticSeverity.Error,
tag: undefined,
content: []
})
} else if (line.startsWith('#Done(')) {
done = true
} else if (/^ +[0-9]+ /.test(line)) {
// code display. Swallow
} else if (line.startsWith(' ')) {
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(line)
}
}
let result: filesDiagnostics = {}
parsedDiagnostics.forEach(parsedDiagnostic => {
let [fileAndLocation, ...diagnosticMessage] = parsedDiagnostic.content
let locationSeparator = fileAndLocation.indexOf(':')
let file = fileAndLocation.substring(2, locationSeparator)
let location = fileAndLocation.substring(locationSeparator + 1)
if (result[file] == null) {
result[file] = []
}
let cleanedUpDiagnostic = diagnosticMessage
.map(line => {
// remove the spaces in front
return line.slice(2)
})
.join('\n')
// remove start and end whitespaces/newlines
.trim() + '\n';
result[file].push({
severity: parsedDiagnostic.severity,
tags: parsedDiagnostic.tag === undefined ? [] : [parsedDiagnostic.tag],
code: parsedDiagnostic.code,
range: parseDiagnosticLocation(location),
source: "ReScript",
message: cleanedUpDiagnostic,
})
})
return { done, result }
}