chore: 設定 stylelint(支援 libsass)並導入 husky 提交前自動 lint#76
Conversation
- add husky and lint-staged dev dependencies - run stylelint --fix on staged .css/.scss files via pre-commit hook - include .scss in lint:styles and lint:fix:styles globs
- adjust .stylelintrc.json for libsass compatibility (legacy color notation, prefix media-feature range, allow global sass functions) - relax selector-class-pattern to allow BEM naming; disable no-descending-specificity - fix remaining lint errors: duplicate selectors/properties, property order, variable kebab-case, extend placeholder, number precision, comment spacing - apply stylelint --fix property ordering across css/scss files
There was a problem hiding this comment.
Code Review
This pull request configures Husky and lint-staged for pre-commit linting, updates Stylelint rules, and formats various CSS and SCSS files. The review feedback highlights several compatibility and code quality improvements, such as enforcing numeric alpha values in Stylelint to avoid compilation errors in older environments, reverting Selector Level 4 :not() lists to chained selectors, renaming variables to proper kebab-case, restoring -webkit-clip-path for older Safari support, and correcting a multi-declaration line formatting issue.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| "media-feature-range-notation": "prefix", | ||
| "color-function-notation": "legacy", |
There was a problem hiding this comment.
在相容舊版編譯器(如 libsass)與舊版瀏覽器的情境下,rgba() 中的百分比不透明度(例如 rgba(0, 0, 0, 18%))可能會導致編譯錯誤或解析失敗。\n\n建議在 .stylelintrc.json 中加入 \"alpha-value-notation\": \"number\" 規則,強制使用小數格式(例如 0.18),並還原各樣式檔案中被自動修正為百分比的 alpha 值。
"media-feature-range-notation": "prefix",
"color-function-notation": "legacy",
"alpha-value-notation": "number",| padding: 0.6rem 1.2rem; | ||
|
|
||
| &:hover:not(.button-disabled):not(:disabled), &:focus { | ||
| &:hover:not(.button-disabled, :disabled), &:focus { |
There was a problem hiding this comment.
:not() 內使用選擇器列表(例如 :not(.button-disabled, :disabled))是 CSS Selectors Level 4 的新特性,在 Internet Explorer 以及舊版 Safari/Chrome/Firefox 中完全不支援。\n\n由於此專案需要相容舊版環境(libsass),建議在 .stylelintrc.json 中設定 \"selector-not-notation\": \"double\",並將此處還原為多個 :not() 連接的寫法(:not(.button-disabled):not(:disabled)),以確保相容性。
| &:hover:not(.button-disabled, :disabled), &:focus { | |
| &:hover:not(.button-disabled):not(:disabled), &:focus { |
There was a problem hiding this comment.
應為 "selector-not-notation": "simple"
selector-not-notation 在這個 Stylelint 版本只有兩種值:simple 和 complex。沒有 double。
| $accordion-header-fontcolor: var(--textColor); | ||
| $accordion-header-active-fontcolor: var(--backgroundColor); |
There was a problem hiding this comment.
PR 說明中提到「SCSS 變數改為 kebab-case」,但此處將 fontColor 改為 fontcolor 僅是全小寫,並非標準的 kebab-case。\n\n為了與同檔案中的其他變數(如 $accordion-header-active-bg)保持一致,建議命名為 $accordion-header-font-color 與 $accordion-header-active-font-color。\n\n>
$accordion-header-font-color: var(--textColor);
$accordion-header-active-font-color: var(--backgroundColor); | border: 0; | ||
| clip: rect(0,0,0,0); | ||
| clip-path: inset(50%); |
There was a problem hiding this comment.
在 Safari 15.3 以前以及 iOS Safari 15.3 以前的版本中,HTML 元素上的 clip-path 仍需要 -webkit- 前綴。\n\n為了確保舊版 Apple 裝置上的相容性,避免 .sr-only 的內容意外顯露,建議保留 -webkit-clip-path: inset(50%);。
| border: 0; | |
| clip: rect(0,0,0,0); | |
| clip-path: inset(50%); | |
| border: 0; | |
| clip: rect(0,0,0,0); | |
| -webkit-clip-path: inset(50%); | |
| clip-path: inset(50%); |
| left: 0; | ||
| position: absolute; right: 0; | ||
| top: 0; |
| --gov-font-sans-serif: system-ui, -apple-system, "Segoe UI", roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; | ||
| --gov-font-monospace: sfmono-regular, menlo, monaco, consolas, "Liberation Mono", "Courier New", monospace; | ||
| --gov-gradient: linear-gradient(180deg, rgba(255, 255, 255, 15%), rgba(255, 255, 255, 0%)); |
There was a problem hiding this comment.
stylelint --fix 目前會把 custom property 裡未加引號的字體名稱轉成小寫,例如 Roboto → roboto、SFMono-Regular → sfmono-regular。雖然多數瀏覽器比對 font family 時通常不區分大小寫,但 Linux fontconfig 在某些設定下可能是 case-sensitive,因此這類大小寫變更仍可能影響字體匹配結果。
建議把非 generic 的字體名稱加上引號,並保留 system-ui、-apple-system、sans-serif、monospace 這類 generic/system keyword 不加引號,例如:
--gov-font-sans-serif: system-ui, -apple-system, "Segoe UI", "Roboto", "Helvetica Neue", "Noto Sans", "Liberation Sans", "Arial", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
--gov-font-monospace: "SFMono-Regular", "Menlo", "Monaco", "Consolas", "Liberation Mono", "Courier New", monospace;| "lint:fix:styles": "npx stylelint '**/*.css' --ignore-path .gitignore --fix" | ||
| "lint:styles": "npx stylelint '**/*.{css,scss}' --ignore-path .gitignore", | ||
| "lint:fix:styles": "npx stylelint '**/*.{css,scss}' --ignore-path .gitignore --fix", | ||
| "prepare": "husky" |
There was a problem hiding this comment.
建議在 readme 補充開發前要先 npm install
開發
安裝前端開發工具與 Git hooks:
npm install在本機啟動網站:
hugo server
概述
導入 stylelint 對 CSS/SCSS 的 lint 與自動修正,並透過 husky + lint-staged 在 commit 前自動處理。針對專案使用的 libsass(Hugo 內建轉譯器)調整規則,確保 lint 規範與實際編譯環境一致。
Lint 設定變更(.stylelintrc.json)
既有規則
extendsstylelint-config-standard-scsspluginsstylelint-orderorder/ordercustom-properties,declarationsorder/properties-alphabetical-ordertrue本次新增規則
selector-class-patternblock__element--modifier,避免誤判既有命名no-descending-specificitynullscss/no-global-function-namesnullunquote/unitless等全域函式(libsass 不支援 Dart Sass module)media-feature-range-notation"prefix"@media (max-width: …)(libsass 不支援(width <= …)range 語法)color-function-notation"legacy"rgba(…)(libsass 不支援rgb(0 0 0 / .5)現代語法)npm scripts 變更(package.json)
lint:styles/lint:fix:styles的 glob 由**/*.css擴充為**/*.{css,scss},使 SCSS 一併納入檢查。提交前自動化(husky + lint-staged)
husky與lint-stageddevDependencies。.css/.scss執行stylelint --fix並重新 stage。樣式修正
stylelint --fix的屬性排序等安全變更(多檔案)。table.scss:合併重複選擇器(no-duplicate-selectors)guide.scss:移除重複的clip-pathwarning-text.scss:拆開同行宣告並修正屬性排序accordion.scss:SCSS 變數改為 kebab-case(含使用處)form.scss:@extend .list加上 inline disable 註解breadcrumb.scss:數字精度修正main.scss://註解後補空白驗證
npm run lint:styles零錯誤hugo --gc --minify成功編譯全部 91 頁變更範圍
涵蓋 31 個檔案(
.stylelintrc.json、package.json、package-lock.json、.husky/,以及 CSS/SCSS 樣式檔)。