KScript 文档
ZH

代码检查标准

编写标准

标准文件包含 name、description、extends、lint 和 format。允许注释和末尾逗号:

JSON
// ../shared/acme/kscript-standard.json
{
  "$schema": "https://raw.githubusercontent.com/kylepantall/kscript/master/editors/vscode/schemas/kscript-standard.schema.json",
  "name": "acme",
  "description": "Acme house style.",
  "extends": "kscript:recommended",
  "lint": {
    "rules": { "KS6*": "error" },
    "warnings_as_errors": true
  },
  "format": { "indent": 4, "max_blank_lines": 2 }
}

lint 接受 rules(为每个代码或前缀指定严重程度:off、hint、info、warning 或 error)和 warnings_as_errors。format 接受所有格式设置:indent、tab_width、print_width、max_blank_lines、end_of_line、preserve、rules 和 enabled(见 格式化)。

检查和格式化哪些文件(include 和 exclude)取决于每个项目的文件夹,因此这些内容保留在 kscript.json 中。在标准中设置它们会收到警告,并且会被忽略。serve、test、app 和 packages 也同样被忽略。

将标准文件命名为 kscript-standard.json,以便项目可以指定其文件夹,或 something.kscript-standard.json。VS Code 扩展会验证并补全这两者,其他名称同样可用。

设置使用 snake_case 书写(warnings_as_errors、print_width)。旧的 camelCase 名称(warningsAsErrors、printWidth)是错误,并会提示新名称。未知设置会产生警告。

标准可以存放的位置

"standard" 在 kscript.json 中,"extends" 在标准中接受内置名称、路径或它们的列表:

JSON
"standard": "kscript:recommended"                           // built in
"standard": "./standards/team.kscript-standard.json"        // a file, relative to the file that names it
"standard": "../shared/acme"                                // a folder holding kscript-standard.json
"standard": "~/kscript/standard.json"                       // a file in your home folder
"standard": ["kscript:recommended", "./team.json", "./this-repo.json"]   // several, applied in order

extends 中的路径相对于标准文件,因此标准可以一起移动。文件夹形式便于以 Git 子模块、共享文件夹或团队与项目并列检出的包的方式共享标准。

找不到、不是有效 JSON,或(直接或通过其他标准)扩展自身的标准是错误,kscript check 会以退出代码 2 停止。

Edit this page on GitHub