Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ESLint & Prettier Auto Check & Fix

ESLint & Prettier Auto Check & Fix

NodeSchool Osaka #36で発表した資料です。コーディングスタイルのチェックと修正を自動化するためにESLintとPrettierを導入した話をしました。

Masashi Hirano

July 08, 2017
Tweet

More Decks by Masashi Hirano

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ \ lBCPVUz\ lOBNFzl.BTBTIJ)JSBOPz  lXPSLTzl8FCMJP *ODz  lUXJUUFSzl!TIJTBNB@z 

    lHJUIVCzlTIJTBNBz  l/PEF4DIPPMzlDPOUSJCVUFUPbMFWFMNFVQ`z ^ ^ ࡢ೔ͷϦΞॆͬ͜͝ͷ༷ࢠ
  2. Ͳ͏΍ͬͯESLintઃఆ͢Δʁ ? How would you like to configure ESLint? (Use

    arrow keys) Answer questions about your style › Use a popular style guide Inspect your JavaScript file(s) // ͍͔ͭ͘ͷ࣭໰ʹ౴͑Δ // ਓؾͷελΠϧΨΠυΛ࢖༻͢Δ // طଘͷJavascriptΛݕࠪ͢Δ
  3. ? How would you like to configure ESLint? Use a

    popular style guide ? Which style guide do you want to follow? (Use arrow keys) › Google Airbnb Standard ͲͷελΠϧʹै͏ʁ
  4. ? How would you like to configure ESLint? Use a

    popular style guide ? Which style guide do you want to follow? Google ? What format do you want your config file to be in? JavaScript › YAML JSON ※ΤσΟλʹΑͬͯ͸ಛఆͷܗࣜͰ͔͠ิ׬ػೳ͕࢖͑ͳ͔ͬͨΓ͠·͢ɻ Intellij΍WebStorm͸yamlܗࣜͰ͸ิ׬ͯ͘͠Ε·ͤΜɻ ઃఆϑΝΠϧͷܗࣜ͸ʁ
  5. ྫ͑͹͜ͷίʔυʹର࣮ͯ͠ߦ var printTime = function() { var date = new

    Date(); var time = date.getTime(); var unixtime = Math.floor(date.getTime() / 1000) console.log('time: ' + time); console.log("unixtime: " + unixtime) } printTime(); Πϯσϯτἧ͍ͬͯͳ͍ ηϛίϩϯ͕ແ͍ ΫΥςʔγϣϯ͕ἧ͍ͬͯͳ͍
  6. ࣮ߦ݁Ռ 1:1 error Unexpected var, use let or const instead

    no-var 2:3 error Unexpected var, use let or const instead no-var 3:1 error Unexpected var, use let or const instead no-var 4:5 error Unexpected var, use let or const instead no-var 4:53 error Missing semicolon semi 7:17 error Strings must use singlequote quotes 7:41 error Missing semicolon semi 8:2 error Missing semicolon semi ✖ 8 problems (8 errors, 0 warnings) 8 errors, 0 warnings potentially fixable with the `--fix` option. ΠϯσϯτͰΤϥʔ͕ग़͍ͯͳ͍ʂʂ ࣍ʹઆ໌͢ΔϧʔϧͷઃఆͰΧόʔ͠·͢
  7. rulesΛઃఆ $ vi .eslintrc.yml extends: google rules: indent: - error

    - 2 quotes: - error - double no-var: off w Πϯσϯτ͕εϖʔε̎ͭͰͳ͚Ε͹Τϥʔ w μϒϧΫΥςʔγϣϯͰͳ͚Ε͹Τϥʔ w WBSΛ࢖༻ͯ͠΋ΤϥʔʹͳΒͳ͍
  8. ࣮ߦ݁Ռ 3:1 error Expected indentation of 2 spaces but found

    0 indent 4:1 error Expected indentation of 2 spaces but found 4 indent 4:53 error Missing semicolon semi 6:1 error Expected indentation of 2 spaces but found 4 indent 6:17 error Strings must use doublequote quotes 7:1 error Expected indentation of 2 spaces but found 4 indent 7:41 error Missing semicolon semi 8:2 error Missing semicolon semi ✖ 8 problems (8 errors, 0 warnings) 8 errors, 0 warnings potentially fixable with the `--fix` option. w Πϯσϯτ͕εϖʔε̎Ͱͳ͍͔ΒΤϥʔ w μϒϧΫΥʔςʔγϣϯ͡Όͳ͍͔ΒΤϥʔ w WBSېࢭͷΤϥʔ͕ඇදࣔʹͳͬͨ
  9. .eslintrc ࠶ܝ extends: google rules: indent: - error - 2

    quotes: - error - double no-var: off ઃఆͨ͠ϧʔϧ௨Γʹࣗಈमਖ਼͔͚͍ͨ
  10. ର৅ͷίʔυʢ࣮ߦલʣ var printTime = function() { var date = new

    Date(); var time = date.getTime(); var unixtime = Math.floor(date.getTime() / 1000) console.log('time: ' + time); console.log("unixtime: " + unixtime) } printTime(); Πϯσϯτ ηϛίϩϯ ΫΥςʔγϣϯ
  11. ର৅ͷίʔυʢ࣮ߦޙʣ var printTime = function() { var date = new

    Date(); var time = date.getTime(); var unixtime = Math.floor(date.getTime() / 1000); console.log("time: " + time); console.log("unixtime: " + unixtime); }; printTime(); ࣗಈमਖ਼͞Εͨʂ
  12. Πϯετʔϧͱ࣮ߦ $ npm install -D prettier $ prettier —write index.js

    // σΟϨΫτϦࢦఆͷ৔߹ $ prettier —write src/**/*.js ŠXSJUFΦϓγϣϯΛ෇͚ͳ͍ͱ ϑΝΠϧʹ্ॻ͖͞Ε·ͤΜɻ