ui-sdk-unslop

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unslop

Unslop

Scope is the diff (
git diff
against the base) unless you're given files. Behavior never changes: the same tests pass before and after, and nothing public is renamed.
除非指定了文件,否则处理范围是相对于基准分支的 diff(
git diff
)。行为绝不改变:修改前后相同的测试都会通过,任何公开内容都不会被重命名。

What goes

需要处理的内容

Each pattern, with what replaces it.
A comment that says what the next line does. Delete it. If the line isn't clear without it, rename something.
ts
// Close the popup when Escape is pressed.
if (event.key === "Escape") close()
A comment on every declaration. Most constants, fields and functions need none: the name and the type say what they are. Delete each one that doesn't stop a reader from getting the code wrong.
A comment that explains the design. What a value holds, where it's kept, which attribute it lands on, who reads it. The code says that. Keep the one fact it can't show, in a line, or delete the comment.
ts
// Before
// The site's color scheme, shown on <html> as `data-color-scheme`, which the
// stylesheet's colors follow. It's kept under "theme", the key the site used
// before, so readers keep what they chose.
const colorScheme = createColorScheme({
  storage: createLocalStorage("theme"),
  global: true,
})

// After
// "theme" is the key the site used before, so readers keep their choice.
const colorScheme = createColorScheme({
  storage: createLocalStorage("theme"),
  global: true,
})
A comment longer than three lines. Cut it to the reason. An explanation that needs more goes in the docs page, a test title or
contributing/
.
pnpm lint
flags it (
comment-length
).
A comment that narrates history or the task. "Now we", "Updated to", "Fixed the bug where", "As requested". Delete it. Git holds the history.
A section banner.
// ---- Helpers ----
,
// State
. Delete it. Split the file if it needs signposts.
JSDoc that repeats the name.
/** The value. */ value
. Say what the reader can't see: the default, the unit, when it's called, what happens on change.
pnpm lint
catches field docs that only repeat the name; write the rest the same way.
A hedge or a sales word in a comment or a doc. "Robust", "seamless", "comprehensive", "simply", "just", "easily", "ensure", "leverage", "utilize", "note that", "it's important to". Cut the word; if nothing is left, cut the comment.
A defensive check the types already rule out.
if (!items) return
on
items: Item[]
,
?? []
on a value that's never undefined,
try
around code that can't throw. Delete it. Narrow
unknown
at a boundary instead.
A cast or
!
with no reason.
Make the type right, or say why in a comment.
A one-caller helper, or a wrapper that only renames. Inline it, unless its name explains something the call can't.
A name that says nothing.
data
,
result
,
item2
,
handleClick2
,
temp
,
flag
,
isValid2
. Name what it is:
highlightedItem
,
isOpen
,
onTriggerPress
.
An option nobody passes, a parameter with one value, a config object for one setting. Delete it.
Logs, probes and commented-out code. Delete them.
In examples, any comment at all. Only tool directives stay (
svelte-ignore
,
eslint-disable
,
@__PURE__
).
下面列出每类模式及其替代处理。
复述下一行作用的注释。 删除它。如果删掉后代码行仍不清楚,就重命名相关内容。
ts
// Close the popup when Escape is pressed.
if (event.key === "Escape") close()
给每个声明都加注释。 大多数常量、字段和函数都不需要注释:名称和类型已经说明其用途。删除那些不能防止读者误解代码的注释。
解释设计本身的注释。 值保存什么、存放在哪里、最终落到哪个属性上、由谁读取,这些代码本身已经表达出来。只保留代码无法呈现的那一个事实,用一行写明;否则删除注释。
ts
// Before
// The site's color scheme, shown on <html> as `data-color-scheme`, which the
// stylesheet's colors follow. It's kept under "theme", the key the site used
// before, so readers keep what they chose.
const colorScheme = createColorScheme({
  storage: createLocalStorage("theme"),
  global: true,
})

// After
// "theme" is the key the site used before, so readers keep their choice.
const colorScheme = createColorScheme({
  storage: createLocalStorage("theme"),
  global: true,
})
超过三行的注释。 精简到只保留原因。需要更长解释的内容应放到文档页面、测试标题或
contributing/
中。
pnpm lint
会标记此问题(
comment-length
)。
叙述历史或任务的注释。 “现在我们”“已更新”“修复了某个 bug”“按要求处理”等。删除它们。Git 会保留历史。
区块标题注释。
// ---- Helpers ----
、
// State
。删除它们。如果文件需要路标,就拆分文件。
重复名称的 JSDoc。
/** The value. */ value
。应说明读者无法看到的信息:默认值、单位、调用时机、变化时会发生什么。
pnpm lint
会捕获仅重复名称的字段文档;其余内容也按同样原则编写。
注释或文档中的模糊措辞或宣传性词语。 “稳健”“无缝”“全面”“只需”“轻松”“确保”“借助”“利用”“注意”“很重要”等。删掉这些词;如果没有剩余内容,就删除注释。
类型已经排除不可能情况的防御性检查。 例如,在
items: Item[]
上执行
if (!items) return
,对永远不会是 undefined 的值使用
?? []
,或用
try
包裹不会抛错的代码。删除它。应在边界处收窄
unknown
。
没有理由的类型断言或
!
。
修正类型,或者用注释说明理由。
只有一个调用方的辅助函数,或只起改名作用的包装函数。 将它内联,除非它的名称能解释调用处无法表达的信息。
毫无信息量的名称。
data
、
result
、
item2
、
handleClick2
、
temp
、
flag
、
isValid2
。改为能表达实际含义的名称:
highlightedItem
、
isOpen
、
onTriggerPress
。
无人传入的选项、只有一个取值的参数、只服务于一项设置的配置对象。 删除它。
日志、探针和被注释掉的代码。 删除它们。
示例中的任何注释。 只保留工具指令(
svelte-ignore
、
eslint-disable
、
@__PURE__
)。

What stays

保留内容

  • A comment that says why, in three lines at most: a browser bug and the browser, an APG rule, a trade-off, a link to the spec or test that pins the behavior.
  • The word "primitive", and parentheses that hold code names, like "the trigger (
    MenuTrigger
    )".
  • JSDoc on every public field and export. Make it say more; don't delete it.
  • 说明原因的注释,最多三行:某个浏览器 bug 及对应浏览器、一条 APG 规则、一项取舍,或指向固定该行为的规范或测试的链接。
  • primitive
    一词,以及用于容纳代码名称的括号,例如“触发器(
    MenuTrigger
    )”。
  • 每个公开字段和导出项上的 JSDoc。让它提供更多信息,而不是删除它。

Steps

步骤

  1. List what you'll change, by file, before touching anything.
  2. Make the changes.
  3. Run
    pnpm lint
    ,
    pnpm format
    ,
    pnpm typecheck
    and the unit tests for the packages you touched. Check exit codes.
  4. Delete each comment left in the diff and reread the code. If it still reads right, leave it deleted.
  5. Read the diff once more. Revert any change that doesn't make the code easier to read.
  6. If one pattern showed up in three or more files, propose a lint rule for it in
    scripts/check-code.mjs
    or
    scripts/check-comments.mjs
    .
  1. 在修改任何内容之前,按文件列出将要进行的变更。
  2. 进行变更。
  3. 运行
    pnpm lint
    、
    pnpm format
    、
    pnpm typecheck
    以及所涉及包的单元测试。检查退出码。
  4. 删除 diff 中遗留的每一条注释并重新阅读代码。如果删掉后代码仍然读得通,就保持删除状态。
  5. 再次阅读 diff。撤销任何不能让代码更易读的变更。
  6. 如果同一模式出现在三个或更多文件中,提出在
    scripts/check-code.mjs
    或
    scripts/check-comments.mjs
    中为其添加 lint 规则的方案。