KScript docs
EN

The language

Control flow

<if $n < 10>
  <!-- … -->
</if>
<elif $n < 100>
  <!-- … -->
</elif>
<else>
  <!-- … -->
</else>

<for each="p" in="$people">
  <!-- … -->
</for>
<for each="i" at="n" in="0..10 step 2">
  <!-- … -->
</for>
<for each="key, value" in="$map">
  <!-- … -->
</for>
<while $queue->count() > 0>
  <!-- … -->
  <break />
  <!-- … -->
  <continue />
  <!-- … -->
</while>

<match $choice>
  <case is="1">
    <echo>One</echo>
  </case>
  <case is="2, 3">
    <!-- … -->
  </case>
  <case when="$it > 9">
    <echo>Out of range</echo>
  </case>
  <default>
    <!-- … -->
  </default>
</match>
if $n < 10 {
  // …
} elif $n < 100 {
  // …
} else {
  // …
}

for p in $people {
  // …
}
for i at n in 0..10 step 2 {
  // …
}
for key, value in $map {
  // …
}
while $queue->count() > 0 {
  // …
  break
  // …
  continue
  // …
}

match $choice {
  1 => echo 'One'
  2, 3 => {
    // …
  }
  _ when $it > 9 => echo 'Out of range'
  _ => {
    // …
  }
}

Each arm of a match is pattern => result. The pattern is one value or several (2, 3 =>), or _ for anything, and a when test can follow it (_ when $it > 9 =>). The result is one statement on the same line, or a { } block of them. A match used as a value writes its arms the same way, with a value as each result: $label = match $choice { 1 => 'one', 2, 3 => 'a few', _ => 'many' }. Arms are separated by a new line or a comma, and the first arm that fits is taken. In XML the arms are <case is="2, 3">, <case when="…"> and <default>.

One-line if

When an if does a single thing, write it on one line: the condition, =>, then the statement. It reads like the arms of a match and like fn(name) => ..., and it keeps guard checks and early exits to one line each.

<let name="basket" value="['apple', 'plum', 'pear']" />
<let name="budget" value="3" />

<!-- The guard is one line, so the main path stays at the left edge. -->
<if $basket->is_empty()>
  <exit />
</if>

<!-- 3 items and a budget of 3: the test is true, so the line prints. -->
<if $basket->count() >= $budget>
  <echo>The basket is full</echo>
</if>

<for each="fruit" in="$basket">
  <!-- 'plum' is skipped, because continue moves on to the next fruit. -->
  <if $fruit == 'plum'>
    <continue />
  </if>
  <echo value="$fruit" />
</for>
<!--
  apple
  pear
-->
$basket = ['apple', 'plum', 'pear']
$budget = 3

// The guard is one line, so the main path stays at the left edge.
if $basket->is_empty() => exit

// 3 items and a budget of 3: the test is true, so the line prints.
if $basket->count() >= $budget => echo 'The basket is full'

for fruit in $basket {
  // 'plum' is skipped, because continue moves on to the next fruit.
  if $fruit == 'plum' => continue
  echo $fruit
}
// apple
// pear

elif and else take the same form, one branch per line:

<let name="temperature" value="18" />

<!-- 18 is not below 5, but it is below 20, so 'Mild' is printed. -->
<if $temperature < 5>
  <echo>Cold</echo>
</if>
<elif $temperature < 20>
  <echo>Mild</echo>
</elif>
<else>
  <echo>Warm</echo>
</else>
$temperature = 18

// 18 is not below 5, but it is below 20, so 'Mild' is printed.
if $temperature < 5 => echo 'Cold'
elif $temperature < 20 => echo 'Mild'
else => echo 'Warm'

The statement after => can be anything that fits on a line: a call (if $continue => @method_call()), an assignment (if $count > 2 => $label = 'many'), echo, return, break, continue, throw or exit. It ends where the line ends, so if $ready => echo 1; echo 2 prints 2 either way. For more than one statement, or for a loop, a match or another if, use a { } block. The first => outside brackets ends the condition, so fn(fruit) => ... inside a call and @load() catch => false stay part of it. if $ready => { } is an error: a block follows the condition without an arrow. XML has no one-line form, so kscript convert writes <if> with the statement inside.

In XML, <if>, <elif>, <while> and <match> take their expression straight in the start tag: <if $user.is_logged_in> is short for <if test="$user.is_logged_in">, and <match $choice> for <match value="$choice">. Both forms work in scripts and pages; use the long one when the tag has other attributes. Inside the tag, <, > and quotes need no escaping, so <if $n > 3> and <if $name == "Joan"> work. A > ends the tag unless it has a space on both sides with more of the condition after it, so write $n > 3, not $n>3. kscript convert and the editor's <if> snippet use the short form.

Shorthand tags make a .ks.xml file KScript markup rather than strict XML: KScript's own parser, formatter and editor support read it, but generic XML tools such as xmllint will reject it. Stick to test="…" if a file has to stay valid XML.

Early returns instead of else

Inside a function or a loop, end a branch early instead of writing else: return in a function, continue or break in a loop (they act on a loop in the same function, never on the loop that called it). The main path then stays at the left edge and each special case is dealt with first.

<fn name="grade" params="score: number">
  <if $score >= 90>
    <return value="'A'" />
  </if>
  <if $score >= 75>
    <return value="'B'" />
  </if>
  <return value="'C'" />
</fn>

<echo>{ @grade(82) }</echo>
fn grade(score: number) {
  if $score >= 90 => return 'A'
  if $score >= 75 => return 'B'
  return 'C'
}

echo "{ @grade(82) }"

kscript check and the editor warn about else and elif with KS6001 there, and at the top of a script when every branch before already ends with return, break, continue, throw or exit. The quick fix removes the else (or turns elif into a separate if) and, when the chain is the last statement of the function or loop, adds the return or continue for you. Pages, <live> blocks and for…else (for an empty list) are not flagged, since there is nothing to return from. KS6001 comes from the default standard, kscript:recommended. To turn it off for a project, set "lint": { "rules": { "KS6001": "off" } } in kscript.json (or in a standard your projects share).

Unused code

kscript check and the editor also point out code that does nothing. In the editor it is drawn faded, with quick fixes to delete it or to rename it with a leading _, which marks it as unused on purpose (_ names are never reported).

Code What it flags
KS6002 A function nothing calls. Calls from the other files of the project count: a library function another script imports and calls, a controller a route file names (to="people.show", or <Resource to="people"> for its index, store, show, update and destroy), and middleware named in kscript.json. A function that only calls itself is still unused.
KS6003 A variable, constant or loop variable whose value is never read, including one that is only ever set. A script's top-level constants are exported like its functions, so they count as used when an importing file uses them. In pages, only a function's own variables are checked, because a layout or component may read the rest.
KS6004 A function parameter the body never reads. For a lambda, only the parameters after the last one it reads, since the caller decides what it is passed: write fn(_y) => ... to keep the position.
KS6005 An import nothing uses: an as alias, a name in names="...", a whole script, or a package none of whose elements, commands, methods or variables appear.
KS6006 Not unused code, but a style hint: a list of names written as one string in a .kscript file (Middleware(use: "log, cors")). Write it as an array, use: [log, cors]; the quick fix does it for you. XML attributes are text, so use="log, cors" is fine there.
KS6008 Not unused code either: a script in a project that declares a second type, model, enum or error type. Each goes in its own file, named after it, and is imported where it is used; the warning says where to move it (types/order-status.kscript). The type the file is named after stays. Scripts outside a project aren't checked.

The project is the folder with the nearest kscript.json and everything under it (hidden folders, bin, obj and node_modules are skipped). A script outside a project is checked against the other files in its own folder. A function that is part of an API used from outside the project can say so with @public in its doc comment:

<!---
  Kept for scripts that still call it.
  @public
-->
<fn name="titlecase" params="text: string" value="$text->upper()" />
/// Kept for scripts that still call it.
/// @public
fn titlecase(text: string) => $text->upper()

Like KS6001, these come from the default standard, kscript:recommended, as warnings (kscript:strict makes them errors). A project can change or turn off each one in kscript.json, such as "lint": { "rules": { "KS6004": "off" } }.