Skip to content

Commit f01408f

Browse files
Manual cleanup
1 parent 0427b47 commit f01408f

4 files changed

Lines changed: 132 additions & 159 deletions

File tree

packages/dev-playground/src/CompilerApi.res

Lines changed: 80 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
open PlaygroundConfig
1+
module Version = {
2+
type t = {
3+
id: string,
4+
label: string,
5+
}
6+
7+
let jsonStringField = (item, name) =>
8+
switch item->Dict.get(name) {
9+
| Some(JSON.String(value)) => Some(value)
10+
| _ => None
11+
}
12+
13+
let fromJson = json =>
14+
switch json {
15+
| JSON.Object(item) =>
16+
let? Some(id) = item->jsonStringField("id")
17+
let? Some(label) = item->jsonStringField("label")
18+
Some({id, label})
19+
| _ => None
20+
}
21+
}
222

323
type info = {
424
bundleId: string,
@@ -11,44 +31,26 @@ type info = {
1131
libraries: array<string>,
1232
}
1333

14-
type config = {
15-
compilerVersion: string,
16-
moduleSystem: PlaygroundConfig.moduleSystem,
17-
warnFlags: string,
18-
jsxPreserveMode: bool,
19-
experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
20-
}
21-
22-
type compilerVersion = {
23-
id: string,
24-
label: string,
25-
}
26-
27-
type successResult = {
34+
type success = {
2835
jsCode: string,
29-
parsetree: option<string>,
30-
typedtree: option<string>,
31-
@as("lambda")
32-
lambda: option<string>,
33-
lam: option<string>,
36+
parsetree: string,
37+
typedtree: string,
38+
lambda: string,
39+
lam: string,
3440
warnings: array<string>,
3541
time: float,
3642
}
3743

38-
type failureResult = {
44+
type failure = {
3945
errors: array<string>,
4046
warnings: array<string>,
4147
message: string,
4248
time: float,
4349
}
4450

45-
type result =
46-
| Success(successResult)
47-
| Failure(failureResult)
48-
4951
type formatResult =
5052
| Formatted(string)
51-
| FormatFailed(failureResult)
53+
| FormatFailed(failure)
5254

5355
type normalizedConfig = {
5456
moduleSystem: PlaygroundConfig.moduleSystem,
@@ -76,35 +78,17 @@ let pathFromBase = relativePath => {
7678
}
7779
}
7880

79-
let jsonStringField = (item, name) =>
80-
switch item->Dict.get(name) {
81-
| Some(JSON.String(value)) => Some(value)
82-
| _ => None
83-
}
84-
85-
let compilerVersionFromJson = json =>
86-
switch json {
87-
| JSON.Object(item) =>
88-
let? Some(id) = item->jsonStringField("id")
89-
let? Some(label) = item->jsonStringField("label")
90-
Some({id, label})
91-
| _ => None
92-
}
93-
9481
let parseCompilerVersions = defaultVersion => {
95-
let fallback = [{id: defaultVersion, label: defaultVersion}]
82+
let fallback = [{Version.id: defaultVersion, label: defaultVersion}]
9683
switch Env.viteCompilerVersions {
9784
| None | Some("") => fallback
9885
| Some(versionJson) =>
99-
try {
100-
switch JSON.parseOrThrow(versionJson) {
101-
| JSON.Array(items) =>
102-
let versions = items->Array.filterMap(compilerVersionFromJson)
103-
versions->Array.length === items->Array.length ? versions : fallback
104-
| _ => fallback
105-
}
106-
} catch {
86+
switch JSON.parseOrThrow(versionJson) {
87+
| JSON.Array(items) =>
88+
let versions = items->Array.filterMap(Version.fromJson)
89+
versions->Array.length === items->Array.length ? versions : fallback
10790
| _ => fallback
91+
| exception _ => fallback
10892
}
10993
}
11094
}
@@ -153,10 +137,10 @@ let versionRoot = version => `${compilerRoot}/${versionOrDefault(version)}`
153137

154138
let applyConfig = (
155139
instance,
156-
~moduleSystem: moduleSystem,
140+
~moduleSystem: PlaygroundConfig.moduleSystem,
157141
~warnFlags,
158142
~jsxPreserveMode,
159-
~experimentalFeatures: array<experimentalFeature>,
143+
~experimentalFeatures: array<PlaygroundConfig.experimentalFeature>,
160144
) => {
161145
if hasFunction(instance, "setModuleSystem") {
162146
instance->Instance.setModuleSystem((moduleSystem :> string))
@@ -224,24 +208,19 @@ let getConfigIfAvailable = (instance: compilerInstance): option<compilerConfig>
224208
}
225209

226210
let diagnosticMessage = (item, fallback) =>
227-
switch item->Diagnostic.shortMsg {
228-
| Some(message) => message
229-
| None =>
230-
switch item->Diagnostic.fullMsg {
231-
| Some(message) => message
232-
| None => fallback
233-
}
234-
}
211+
item->Diagnostic.shortMsg->Option.orElse(item->Diagnostic.fullMsg)->Option.getOr(fallback)
235212

236213
let formatLocation = item => {
237214
let row = switch item->Diagnostic.row {
238215
| Some(row) => row
239216
| None => 0
240217
}
218+
241219
let column = switch item->Diagnostic.column {
242220
| Some(column) => column
243221
| None => 0
244222
}
223+
245224
row > 0 ? `Line ${row->Int.toString}, ${column->Int.toString}` : "Compiler"
246225
}
247226

@@ -250,10 +229,12 @@ let warningToText = item => {
250229
| Some(true) => "error"
251230
| Some(false) | None => "warning"
252231
}
232+
253233
let warnNumber = switch item->Diagnostic.warnNumber {
254234
| Some(warnNumber) => ` ${warnNumber->Int.toString}`
255235
| None => ""
256236
}
237+
257238
let message = diagnosticMessage(item, "Unknown warning")
258239
`${formatLocation(item)}: ${prefix}${warnNumber}: ${message}`
259240
}
@@ -263,62 +244,52 @@ let errorToText = item => {
263244
`${formatLocation(item)}: ${message}`
264245
}
265246

266-
let failureFromCompileOutput = (compileOutput, elapsedMs): failureResult => {
247+
let failureFromCompileOutput = (compileOutput, elapsedMs): failure => {
267248
let errors = switch compileOutput->CompileResult.errors {
268249
| Some(errors) => errors->Array.map(errorToText)
269250
| None => []
270251
}
252+
271253
let warnings = switch compileOutput->CompileResult.warnings {
272254
| Some(warnings) => warnings->Array.map(warningToText)
273255
| None => []
274256
}
275-
let message = switch compileOutput->CompileResult.msg {
276-
| Some(message) => message
277-
| None =>
278-
switch compileOutput->CompileResult.shortMsg {
279-
| Some(message) => message
280-
| None =>
281-
switch compileOutput->CompileResult.fullMsg {
282-
| Some(message) => message
283-
| None =>
284-
switch errors->Array.get(0) {
285-
| Some(error) => error
286-
| None => "Compilation failed"
287-
}
288-
}
289-
}
290-
}
291257

292-
{
293-
errors,
294-
warnings,
295-
message,
296-
time: elapsedMs,
297-
}
298-
}
258+
let message =
259+
compileOutput
260+
->CompileResult.msg
261+
->Option.orElse(compileOutput->CompileResult.shortMsg)
262+
->Option.orElse(compileOutput->CompileResult.fullMsg)
263+
->Option.orElse(errors->Array.get(0))
264+
->Option.getOr("Compilation failed")
299265

300-
let normalizeFailure = (compileOutput, elapsedMs): result => Failure(
301-
failureFromCompileOutput(compileOutput, elapsedMs),
302-
)
266+
{errors, warnings, message, time: elapsedMs}
267+
}
303268

304-
let normalizeSuccess = (compileOutput, elapsedMs): result => {
305-
let warnings = switch compileOutput->CompileResult.warnings {
306-
| Some(warnings) => warnings->Array.map(warningToText)
307-
| None => []
308-
}
269+
type compileResult = result<success, failure>
270+
271+
let normalize = (compileOutput, elapsedMs): compileResult => {
272+
switch (
273+
compileOutput->CompileResult.parsetree,
274+
compileOutput->CompileResult.typedtree,
275+
compileOutput->CompileResult.lambda,
276+
compileOutput->CompileResult.lam,
277+
) {
278+
| (Some(parsetree), Some(typedtree), Some(lambda), Some(lam)) =>
279+
let warnings = switch compileOutput->CompileResult.warnings {
280+
| Some(warnings) => warnings->Array.map(warningToText)
281+
| None => []
282+
}
309283

310-
Success({
311-
jsCode: switch compileOutput->CompileResult.jsCode {
284+
let jsCode = switch compileOutput->CompileResult.jsCode {
312285
| Some(jsCode) => jsCode
313286
| None => ""
314-
},
315-
parsetree: compileOutput->CompileResult.parsetree,
316-
typedtree: compileOutput->CompileResult.typedtree,
317-
lambda: compileOutput->CompileResult.lambda,
318-
lam: compileOutput->CompileResult.lam,
319-
warnings,
320-
time: elapsedMs,
321-
})
287+
}
288+
289+
Ok({jsCode, parsetree, typedtree, lambda, lam, warnings, time: elapsedMs})
290+
291+
| _ => Error(failureFromCompileOutput(compileOutput, elapsedMs))
292+
}
322293
}
323294

324295
let loadRuntimeLibraries = async version => {
@@ -436,9 +407,10 @@ let init = async version => {
436407
}
437408
}
438409

439-
let compile = async (source, config) => {
410+
let compile = async (source, config: PlaygroundConfig.t) => {
440411
let selectedVersion = versionOrDefault(config.compilerVersion)
441412
let instance = await ensureCompiler(selectedVersion)
413+
442414
applyConfig(
443415
instance,
444416
~moduleSystem=config.moduleSystem,
@@ -449,21 +421,18 @@ let compile = async (source, config) => {
449421

450422
let start = Performance.now()
451423
let rescript = instance->Instance.rescript
424+
452425
let compileOutput = if hasFunction(rescript, "compileWithDebug") {
453426
rescript->Rescript.compileWithDebug(source)
454427
} else {
455428
rescript->Rescript.compile(source)
456429
}
457-
let elapsedMs = Performance.now() -. start
430+
let elapsedMs = Performance.now() - start
458431

459-
if compileOutput->resultIsSuccess {
460-
normalizeSuccess(compileOutput, elapsedMs)
461-
} else {
462-
normalizeFailure(compileOutput, elapsedMs)
463-
}
432+
normalize(compileOutput, elapsedMs)
464433
}
465434

466-
let format = async (source, config) => {
435+
let format = async (source, config: PlaygroundConfig.t) => {
467436
let selectedVersion = versionOrDefault(config.compilerVersion)
468437
let instance = await ensureCompiler(selectedVersion)
469438
applyConfig(
@@ -481,7 +450,7 @@ let format = async (source, config) => {
481450
} else {
482451
JsError.throwWithMessage("This compiler bundle does not expose formatting")
483452
}
484-
let elapsedMs = Performance.now() -. start
453+
let elapsedMs = Performance.now() - start
485454

486455
if formatOutput->resultIsSuccess {
487456
switch formatOutput->CompileResult.code {

0 commit comments

Comments
 (0)