單行 vs 多行、字母序 vs 邏輯序、Tab vs Space —— 沒有絕對正確,只有「全專案統一」才正確。不一致帶來的認知負荷遠超風格本身優劣。
/***** 單行:≤3 條聲明 *****/
.btn { display: inline-block; padding: .5em 1em; }
/***** 多行:>3 條聲明 *****/
.card {
position: relative;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,.1);
padding: 1.5rem;
transition: transform .2s, box-shadow .2s;
}
/***** 巢狀規則:反映 DOM *****/
.nav {
display: flex;
&-item { flex: 1; }
&-link { color: inherit; }
}
/* .stylelintrc.json 關鍵規則 */
{
"rules": {
"indentation": 4,
"string-quotes": "double",
"color-hex-case": "lower",
"color-hex-length": "short",
"selector-class-pattern": "^[a-z][a-z0-9]*(-[a-z0-9]+)*(__[a-z0-9]+(-[a-z0-9]+)*)?(--[a-z0-9]+(-[a-z0-9]+)*)?$",
"property-no-unknown": true,
"declaration-block-semicolon-newline-after": "always",
"rule-empty-line-before": "always-multi-line"
}
}
配合 Prettier 格式化、Husky + lint-staged 提交前攔截、CI 檢查 門檻,實現「無法提交不合規代碼」。