Project rules
The built-in KScript style sets the defaults for the CLI and editor. Put kscript.rules.kscript beside kscript.json. The command line and editor both read it when checking or formatting project files.
<Style indent_size="4" print_width="100" />
<Lint code="KS9001" element="img" missing_attribute="alt" message="Give this image alternative text." />
<Format element="img" attributes="one_per_line" />
<Format model="Attachments" options="one_per_line" />Style(indent_size: 4, print_width: 100)
Lint(
code: 'KS9001',
element: 'img',
missing_attribute: 'alt',
message: 'Give this image alternative text.',
)
Format(element: 'img', attributes: 'one_per_line')
Format(model: 'Attachments', options: 'one_per_line')Styleoverrides document-wide formatting defaults. It acceptsindent_size,use_tabs,print_width,max_blank_lines,end_of_lineand the switches listed under Turning rules off. Formatting starts with the built-in style, then applies editor indentation, projectStylesettings and explicitformatsettings inkscript.json, in that order.Lintchecks an element for eithermissing_attributeorforbidden_attribute. Codes must be unique and useKS9xxx. The default severity iswarning. Setseverity:toerror,warning,infoorhint, or override it inkscript.jsonwith"lint": { "rules": { "KS9001": "error" } }.Formatsets the attribute layout of an HTML or XML tag toone_per_lineorinline. It can also split a script model's options one per line and add a trailing comma. Use'*'for all tags or models. A named layout takes precedence over'*'.- Use numbers or booleans for literal layout settings such as
print_widthoruse_tabs. Quote literal text settings.
Function rules
Use fn(value) => ... when the rule depends on the document. This rule trims a literal alt attribute. Interpolated attributes and event or binding expressions keep their original text.
<Format element="img" attribute="alt" value="fn(text) => $text->trim()" />Format(
element: 'img',
attribute: 'alt',
value: fn(text) => $text->trim(),
)A layout function returns 'one_per_line', 'inline' or null to use the normal layout. For example, this rule condenses an empty block to Something(val1, val2) {} and expands the arguments with trailing commas when the block has content.
<Format statement="Something" arguments="fn(node) => $node.has_body_content ? 'one_per_line' : 'inline'" />Format(
statement: 'Something',
arguments: fn(node) => $node.has_body_content ? 'one_per_line' : 'inline',
)Use attributes: with an HTML or XML element: to choose its layout the same way. A node passed to a function has name, kind, attributes and has_body_content. A lint check: function can return its own message, true for the declaration's message, or false or null to skip the diagnostic.
<Lint code="KS9002" element="img" check="fn(node) => $node.attributes->has('alt') ? null : 'An image needs alternative text.'" />Lint(
code: 'KS9002',
element: 'img',
check: fn(node) => $node.attributes->has('alt') ? null : 'An image needs alternative text.',
)Functions run in a restricted expression context with access to their argument, comparisons, conditionals and text operations such as trim(), upper() and starts_with(). Project rule files use declarations and cannot import packages or call commands.