tag:crieit.net,2005:https://crieit.net/tags/StyleLint/feed 「StyleLint」の記事 - Crieit Crieitでタグ「StyleLint」に投稿された最近の記事 2022-03-05T23:05:27+09:00 https://crieit.net/tags/StyleLint/feed tag:crieit.net,2005:PublicArticle/18135 2022-03-05T23:05:27+09:00 2022-03-05T23:05:27+09:00 https://crieit.net/posts/stylelint-adjust-rgb-and-scss-20220305 stylelint 周りのアップデート (Scss対応、 rgb 指定) <p>久々にパッケージガーデニングしたら stylelint から Scss 関連や <code>rgb</code> の指定で怒られるようになってしまったので対処。</p> <h2 id="現象"><a href="#%E7%8F%BE%E8%B1%A1">現象</a></h2> <pre><code class="bash">$ stylelint ./src/scss/**/*.scss src/scss/global/_scssVariables.scss 19:19 ✖ Unexpected unknown function "color.scale" function-no-unknown 21:19 ✖ Unexpected unknown function "color.adjust" function-no-unknown 22:17 ✖ Unexpected unknown function "color.adjust" function-no-unknown 23:19 ✖ Unexpected unknown function "color.scale" function-no-unknown 24:20 ✖ Unexpected unknown function "color.scale" function-no-unknown src/scss/object/component/_c-button.scss 19:31 ✖ Expected modern color-function notation color-function-notation src/scss/_plugins/articlesns/_articlesns.scss 40:16 ✖ Unexpected unknown function "color.scale" function-no-unknown 53:31 ✖ Unexpected unknown function "map.get" function-no-unknown 57:35 ✖ Unexpected unknown function "color.scale" function-no-unknown 57:47 ✖ Unexpected unknown function "map.get" function-no-unknown 57:81 ✖ Unexpected unknown function "map.get" function-no-unknown </code></pre> <p>npmパッケージをアップデートした際に stylelint やそれに関連するパッケージ等をアップデートしたところ、いくつかの項目で怒られるようになってしまいました。</p> <p>この中で特に気になったのが <code>Expected modern color-function notation color-function-notation</code> のエラー。</p> <p><a target="_blank" rel="nofollow noopener" href="https://twitter.com/kaji2/status/1451433868863553536">かじさんはTwitterを使っています 「stylelint-config-standard の 23.0.0 アップデートで、 color-function-notation が 'modern' で追加された。 従来のコンマ区切りだったrgb()、rgba()をスペース区切りに揃えるもの。ついでにrgbaをrgbに揃えている。 https:\/\/t.co\/yHFngFOS8J」 \/ Twitter</a></p> <p>検索したところ、このツイートがヒット。</p> <p><code>stylelint-config-standard</code> のアップデートで色指定が引っかかるようになったようです。これは css level 4 の構文に対応したもののようです。</p> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://triple-underscore.github.io/css-color-ja.html#rgb-functions">CSS Color Module Level 4 (日本語訳)</a></li> </ul> <p>構文としては次のように変更になります。</p> <pre><code class="css">/* 旧来 */ .hoge { background-color: rgba(48, 48, 48, 0.5); } /* 新 */ .hoge { background-color: rgb(48 48 48 / 50%); } </code></pre> <ul> <li><code>rgb()</code> と <code>rgba()</code> から <code>rgb()</code> へ統一</li> <li>RGB値はカンマ区切りからスペース区切りへ</li> <li>RGB値とアルファ値の区切りはカンマからスラッシュへ</li> </ul> <p>一方、 Scss でも独自に <code>rgb()</code> が存在しています。このおかげで、 Scss で次のように色の指定でRGB値ではなく16進数や変数を使用しても問題が発生しないわけですね。</p> <pre><code class="scss">$black: #333; .hoge { background-color: rgba($black, 0.5); } </code></pre> <p>ただ、 Scss 側ではまだカンマ区切りしか対応していなさそうです。そのため、仮に css level 4 の構文を使用して Scss のトランスパイルを走らせると、次のようにエラーになってしまいます。</p> <pre><code class="bash">[hh:ii:ss] gulp-notify: [sass] Error: src\scss\object\component\_c-newscards.scss Error: Missing element $blue. ╷ 128 │ box-shadow: inset 2px 2px 5px 0 rgb(g.$us-main-color 0.5); │ ^^^^^^^^^^^^^^^^^^^^^^^^^ ╵ src\scss\object\component\_c-newscards.scss 128:37 @use src\scss\contents.scss 36:1 root stylesheet 19:31 ✖ Expected modern color-function notation color-function-notation </code></pre> <p>Scss の構文にすると stylelint でコケて、 stylelint のデオフォルトに従って css level 4 の構文にすると Scss のトランスパイルが通らない、と。</p> <h2 id="対処"><a href="#%E5%AF%BE%E5%87%A6">対処</a></h2> <p>仕方ないので、上述引用のツイートと同様に <code>color-function-notation</code> のパラメータを <code>legacy</code> にすることで回避することにしました。</p> <h3 id=".stylelint.json"><a href="#.stylelint.json">.stylelint.json</a></h3> <pre><code class="json"> "rules": { // 略 "color-function-notation": "legacy", // 略 } </code></pre> <h2 id="余談"><a href="#%E4%BD%99%E8%AB%87">余談</a></h2> <p>上述対処の前に他の回避方法がないか試す中で、一通りパッケージをアップデートしたりしましたがかえって Scss の関数等のエラーが出てしまったのは冒頭の通り。</p> <p>そこでそれらへの対応として次のようにしました。</p> <h3 id="package.json"><a href="#package.json">package.json</a></h3> <pre><code class="json"> "devDependencies": { // 略 "stylelint-config-standard-scss": "3.0.0", // 追加 // 略 } </code></pre> <h3 id=".stylelint.json"><a href="#.stylelint.json">.stylelint.json</a></h3> <pre><code class="json"> "extends": [ "stylelint-config-standard", "stylelint-config-standard-scss", // 追加 "stylelint-config-recommended-scss", "stylelint-config-prettier" ], </code></pre> <p>まず <code>stylelint-config-standard-scss</code> を追加。</p> <h3 id=".stylelint.json"><a href="#.stylelint.json">.stylelint.json</a></h3> <pre><code class="json"> "rules": { // 略 "at-rule-no-unknown": null, // Scss関数は stylelint-config-standard-scss に任せるようにしたため独自で追加していた設定は削除 "function-no-unknown": [ true, // map.get や color.scale 等 Dart Sass の関数は引っかかってしまうのでルールを追加 { "ignoreFunctions": [ "/^map\\..+/", "/^color\\..+/" ] } ], "scss/at-rule-no-unknown": true, // Scssのデフォルト関数以外は引っかかるようにする "scss/dollar-variable-pattern": "^(([a-z][a-zA-Z0-9_]+)|([a-z][a-z0-9]*)(-[a-zA-Z0-9_]+)*)$", // 互換性のためケバブケース強制は除去 "scss/at-mixin-pattern": "^(([a-z][a-zA-Z0-9_]+)|([a-z][a-z0-9]*)(-[a-zA-Z0-9_]+)*)$", // 互換性のためケバブケース強制は除去 "scss/at-mixin-argumentless-call-parentheses": "always" // 互換性のため引数なしのmixin呼び出しの際は括弧強制 "stylelint-config-standard-scss": "3.0.0", // 追加 // 略 } </code></pre> <p>その上で <code>.stylelint.json</code> のルールをいくつか変更しました。</p> <p>これで stylelint を走らせてエラーが出なくなったことを確認。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <h3 id="color-function-notation"><a href="#color-function-notation">color-function-notation</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://stylelint.io/user-guide/rules/list/color-function-notation/">color-function-notation | Stylelint</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://twitter.com/kaji2/status/1451433868863553536">かじさんはTwitterを使っています 「stylelint-config-standard の 23.0.0 アップデートで、 color-function-notation が 'modern' で追加された。 従来のコンマ区切りだったrgb()、rgba()をスペース区切りに揃えるもの。ついでにrgbaをrgbに揃えている。 https:\/\/t.co\/yHFngFOS8J」 \/ Twitter</a></li> </ul> <h3 id="css level 4"><a href="#css+level+4">css level 4</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://triple-underscore.github.io/css-color-ja.html#rgb-functions">CSS Color Module Level 4 (日本語訳)</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://stackoverflow.com/questions/66825515/getting-error-in-css-with-rgb0-0-0-15">Getting error in CSS with <code>rgb\(0 0 0 \/ 15%\)</code> - Stack Overflow</a> <ul> <li>似たようなケース</li> </ul></li> </ul> <h3 id="Scss 周りへの対応"><a href="#Scss+%E5%91%A8%E3%82%8A%E3%81%B8%E3%81%AE%E5%AF%BE%E5%BF%9C">Scss 周りへの対応</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/taqumo/items/2ed2cd7e35301d4aed8e">Stylelint 14.0.0 で Sass ファイルを lint させる方法 - Qiita</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://github.com/stylelint-scss/stylelint-config-standard-scss">GitHub - stylelint-scss\/stylelint-config-standard-scss: The standard shareable SCSS config for Stylelint.</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://stylelint.io/user-guide/rules/list/at-rule-no-unknown/">at-rule-no-unknown | Stylelint</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://stylelint.io/user-guide/rules/list/function-no-unknown/">function-no-unknown | Stylelint</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://github.com/stylelint-scss/stylelint-scss/blob/master/src/rules/at-mixin-pattern/README.md">stylelint-scss\/README.md at master · stylelint-scss\/stylelint-scss · GitHub</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/shigemaruu/items/2e64fe0a425946a2e4a1">stylelint-config-sass-guidelinesの内容をメモする - Qiita</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://github.com/stylelint-scss/stylelint-scss/blob/master/src/rules/at-mixin-argumentless-call-parentheses/README.md">stylelint-scss\/README.md at master · stylelint-scss\/stylelint-scss · GitHub</a></li> </ul> <h3 id="marked"><a href="#marked">marked</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://marked.js.org/">Marked Documentation</a></li> </ul> <p>パースする関数が <code>marked</code> から <code>marked.parse</code> メソッドに変更になっていた。</p> arm-band tag:crieit.net,2005:PublicArticle/16051 2020-09-07T14:22:14+09:00 2020-09-07T23:02:38+09:00 https://crieit.net/posts/Nuxt-js-lint Nuxt.jsにlintを導入してみた <h2 id="動機"><a href="#%E5%8B%95%E6%A9%9F">動機</a></h2> <p>以前からなんとなくeslintは導入してあーだこーだしてたのですが、ポートフォリオサイトやブログサイトをNetlifyからFirebaseに移植する際に、github actions(CI)を使うようになり、せっかくだからもう少し本格的にlintやtestを導入してみようということで、色々調べて導入してみました。</p> <p>その手順を初心者なりに備忘録としてまとめてみました。<br /> (だいぶ我流な部分もありますが多めに見ていただければ幸いです)</p> <p>アドバイスなどは大歓迎です。</p> <p>知識が増えたら随時更新します。</p> <p><a target="_blank" rel="nofollow noopener" href="https://blog.taka1156.site">https://blog.taka1156.site</a></p> <p><a target="_blank" rel="nofollow noopener" href="https://github.com/taka1156/nuxt-blog">https://github.com/taka1156/nuxt-blog</a> - アトミックデザインの形にプロジェクトを見直し中</p> <p>↑ブログのプロジェクトで導入してみました。(Nuxt.js + Firebase + microCMS 構成)</p> <h2 id="環境"><a href="#%E7%92%B0%E5%A2%83">環境</a></h2> <ul> <li>mac OS Catalina</li> <li>Node.js v12.16.1</li> <li>npm v6.14.3</li> <li>yarn v1.22.0</li> </ul> <h2 id="手順"><a href="#%E6%89%8B%E9%A0%86">手順</a></h2> <ol> <li><p>これらをインストール</p> <ul> <li><p>eslint関連<br /> <code>yarn add eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue babel-eslint</code></p></li> <li><p>stylelint関連<br /> <code>yarn add stylelint stylelint-config-standard stylelint-config-prettier stylelint-prettier</code></p></li> <li><p>prettier<br /> <code>yarn add prettier</code></p></li> </ul> <p>※ なんでprettierを入れたか<br /> eslintだけだとエラーは出すが修正はしない項目(コード整形関連)もあるので併用する。<br /> (逆にprettierは細かい命名規則や処理に関するエラーは出さない?)</p></li> <li><p>設定ファイルを書く</p> <ul> <li>.eslintrc.json</li> </ul> <pre><code class="json">{ "parserOptions": { "parser": "babel-eslint", "sourceType": "module" }, "extends": [ "eslint:recommended", "plugin:vue/recommended", "plugin:prettier/recommended", "prettier/vue" ], "plugins": [ "vue" ], "env": { "jest": true, "browser": true, "es6": true, "node": true }, "globals": {}, "rules": { "camelcase": [2,{"properties": "always"}], "quotes": [2,"single", { "avoidEscape": true } ], "eqeqeq": [2, "always", {"null": "ignore"}], "prefer-const": 2, "vue/component-name-in-template-casing": "off", "vue/no-v-html": "off" } } </code></pre> <ul> <li>.stylelintrc.json</li> </ul> <pre><code class="json">{ "extends": ["stylelint-config-standard", "stylelint-prettier/recommended"], "rules": { "color-no-invalid-hex": true, "color-hex-case": "lower", "selector-class-pattern": "^(?:(?:o|c|u|t|s|is|has|_|js|qa)-)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*(?:__[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:--[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?(?:\\[.+\\])?$" } } </code></pre> <ul> <li>.eslintignore, .stylelintignore<br /> ※ storybook-static、coverage は storybookやjestを導入していると生成されるファイル</li> </ul> <pre><code class="txt"># ビルドファイル dist storybook-static coverage # 設定、モジュール .nuxt .storybook node_modules </code></pre> <ul> <li>.prtteirrc.json</li> </ul> <pre><code class="json">{ "printWidth": 85, "tabWidth": 2, "singleQuote": true, "semi": true, "bracketSpacing": true, "arrowParens": "avoid" } </code></pre></li> <li><p>scriptsを追加</p> <pre><code class="json">"scripts": { "lint-js": "eslint --ext .js,.vue .", "lint-style": "stylelint \"**/*.vue\" \"**/*.css\"", "lint-js:fix": "eslint --ext .js,.vue . --fix", "lint-style:fix": "stylelint \"**/*.vue\" \"**/*.css\" --fix", "lint:check": "yarn lint-js & yarn lint-style", "lint:fix": "yarn lint-js && yarn lint-style", ... } </code></pre></li> <li><p>お好みでvscode設定を追加<br /> 保存時に勝手にlint処理が走るようになるので便利<br /> (vscode/setting.jsonに記述)</p> <pre><code class="json">{ "comments": "保存時にeslint、stylelintを自動実行", "eslint.alwaysShowStatus": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true, "source.fixAll.stylelint": true }, } </code></pre></li> </ol> <h2 id="まとめ"><a href="#%E3%81%BE%E3%81%A8%E3%82%81">まとめ</a></h2> <p>eslint、stylelintについてざっくりと使い方やルールの指定方法についてわかってきたので、<br /> 積極的に活用して読みやすいコードを意識していきたいですね。</p> <p>また、eslintルールについても、気になるものがあればまとめてみようと思います。</p> taka1156