Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

POC pattern for converting switches to functions #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .grit/patterns/SwitchToFunction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Prefer early returns

```
language js

[
`let $var`,
SwitchStatement(cases=$cases, discriminant=$disc)
] => `const $var = $func($disc)` where {
// note how the name for this is not present in the original, but the LLM guesses it effectively
$input = guess(hint="Input name", fallback="input")
$tests = []
// TODO: we need `all` not `some` (use case for map?)
$cases <: some bubble($tests) SwitchCase(test=$test, consequent=$dec) where {
$return = `null`
$exp = []
$dec <: some bubble($exp, $return, $var) $x where {
$x <: not BreakStatement()
if ($x <: `$var = $val`) {
$return = $val
} else {
$exp = [...$exp, $x]
}
}
$tests = [...$tests, `if ($input === $test) { $exp; return $return; }`]
}
$func = `($input) => {
$tests
return nice;
}`
}
```