Skip to content

Commit a8f2692

Browse files
committed
Small refactor
1 parent 18406f7 commit a8f2692

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

server/src/utils.ts

+33-36
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export let runBsbWatcherUsingValidBsbPath = (
112112
} else {
113113
return childProcess.execFile(bsbPath, ["-w"], {
114114
cwd: projectRootPath,
115-
})
115+
});
116116
}
117117
// try {
118118
// let result = childProcess.execFileSync(bsbPath, [], { stdio: 'pipe', cwd: projectRootPath })
@@ -159,38 +159,8 @@ export let parseDiagnosticLocation = (location: string): Range => {
159159
}
160160
};
161161

162-
let findLocationSeparator = (fileAndLocation: string) => {
163-
// Exclude the two first letters in windows paths to avoid the first colon in eg "c:\\.."
164-
if (process.platform === "win32") {
165-
return fileAndLocation.indexOf(":", 2);
166-
} else {
167-
return fileAndLocation.indexOf(":");
168-
}
169-
};
170-
171-
let separateFileAndLocation = (fileAndLocation: string): [string, string] => {
172-
let locationSeparator = findLocationSeparator(fileAndLocation);
173-
let file = fileAndLocation.slice(0, locationSeparator);
174-
let location = fileAndLocation.slice(locationSeparator + 1);
175-
176-
if (process.platform === "win32") {
177-
return [`file:\\\\\\${file}`, location];
178-
} else {
179-
return [file, location]
180-
}
181-
};
182-
183-
type filesDiagnostics = {
184-
[key: string]: p.Diagnostic[];
185-
};
186-
type parsedCompilerLogResult = {
187-
done: boolean;
188-
result: filesDiagnostics;
189-
};
190-
export let parseCompilerLogOutput = (
191-
content: string
192-
): parsedCompilerLogResult => {
193-
/* example .compiler.log file content that we're gonna parse:
162+
// Logic for parsing .compiler.log
163+
/* example .compiler.log content:
194164
195165
#Start(1600519680823)
196166
@@ -228,8 +198,36 @@ export let parseCompilerLogOutput = (
228198
Somewhere wanted: int
229199
230200
#Done(1600519680836)
231-
*/
201+
*/
202+
203+
// parser helper
204+
let splitFileAndLocation = (fileAndLocation: string) => {
205+
let isWindows = process.platform === "win32";
206+
// Exclude the two first letters in windows paths to avoid the first colon in eg "c:\\.."
207+
let locationSeparator = isWindows
208+
? fileAndLocation.indexOf(":", 2)
209+
: fileAndLocation.indexOf(":");
210+
211+
let file = fileAndLocation.slice(0, locationSeparator);
212+
let location = fileAndLocation.slice(locationSeparator + 1);
213+
214+
return {
215+
file: isWindows ? `file:\\\\\\${file}` : file,
216+
location,
217+
};
218+
};
232219

220+
// main parsing logic
221+
type filesDiagnostics = {
222+
[key: string]: p.Diagnostic[];
223+
};
224+
type parsedCompilerLogResult = {
225+
done: boolean;
226+
result: filesDiagnostics;
227+
};
228+
export let parseCompilerLogOutput = (
229+
content: string
230+
): parsedCompilerLogResult => {
233231
type parsedDiagnostic = {
234232
code: number | undefined;
235233
severity: t.DiagnosticSeverity;
@@ -300,8 +298,7 @@ export let parseCompilerLogOutput = (
300298
let result: filesDiagnostics = {};
301299
parsedDiagnostics.forEach((parsedDiagnostic) => {
302300
let [fileAndLocationLine, ...diagnosticMessage] = parsedDiagnostic.content;
303-
304-
let [file, location] = separateFileAndLocation(fileAndLocationLine.slice(2));
301+
let { file, location } = splitFileAndLocation(fileAndLocationLine.slice(2));
305302

306303
if (result[file] == null) {
307304
result[file] = [];

0 commit comments

Comments
 (0)