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

ヤバいESLint/TSLintルール作っちゃったかもしれない

F425aff2e1f934fc1e5fa95b1f933068?s=47 boiyama
April 24, 2018

 ヤバいESLint/TSLintルール作っちゃったかもしれない

Roppongi.js #2 での発表資料です。
https://roppongi-js.connpass.com/event/82998/

F425aff2e1f934fc1e5fa95b1f933068?s=128

boiyama

April 24, 2018
Tweet

Transcript

  1. Ϡό ͍ E S L i n t / T

    S L i n t ϧ ʔϧ ࡞ ͬ ͪ Ό ͬ ͨ ͔ ΋ ͠ Ε ͳ ͍ 2 0 1 8 . 4 . 2 4 R o p p o n g i . j s # 2
  2. P ro f i l e • ϑϩϯτΤϯυΤϯδχΞ at PERSOL

    • GitHub: boiyaa
 Twitter: boiyaaaaaa
  3. Ͳ Μ ͳ ϧ ʔϧ ͔ return จΛېࢭͯ͠Έͨ eslint-plugin-no-return tslint-no-return

    ݩʑ͸ଞͷؔ਺ܕݴޠνοΫͳจ๏Λਅࣅ͔ͨͬͨ
 ˍؔ਺ͷॻ͖ํΛἧ͔͑ͨͬͨతͳಈػɻ
  4. // Error function foo(bar) { const baz = bar **

    2; return baz; } // Error const foo = function (bar) { const baz = bar ** 2; return baz; }; // Error const foo = bar => { const baz = bar ** 2; return baz; }; // OK const foo = bar => bar ** 2;
  5. ஋Λฦؔ͢਺Λॻ࣌͘ʹ͸ɺΞϩʔؔ਺ͷ؆ܿจମ͔͠ ࢖͑ͳ͘ͳΔ͚͔ͩͱࢥ͚ͬͨͲ จ͕ॻ͚ͳ͍؆ܿจମʹΑͬͯɺ৭ʑͳॻ͖ํΛ౷ҰͰ ͖ͨΓɺ Lint ϧʔϧΛγϯϓϧʹͰ͖ͨΓ͢Δ͜ͱʹؾ ͍ͮͨɻ

  6. ৚݅ͷॻ͖ํΛ౷ҰͰ͖ͯɺ
 ϧʔϧΛݮΒͤͨɻ

  7. const foo = bar => { if (bar == "baz")

    { return "qux"; } return "quux"; }; const foo = bar => { switch (bar) { case "baz": { return "qux"; } default: { return "quux"; } } }; // ! const foo = bar => (bar == "baz" ? "qux" : "quux");
  8. if ΍ switch ͸ ESLint ϧʔϧଟ͍ͷͰ࢖͑ͳ͍ͳΒ͖ͬ͢ Γ͍͍ͯ͠ͱࢥ͏ɻ Ͱ΋ࡾ߲ԋࢉࢠ͸൱ఆ೿͕݁ߏ͍Δ͠ೖΕࢠ͸Ϡό͍ɻ Ramda Λ࢖ͬͯΈΔͱ͔

  9. const foo = bar => (bar == "baz" ? "qux"

    : "quux"); // ! const foo = ifElse(equals("baz"), always("qux"), always("quux"));
  10. const foo = (bar, baz) => { if (bar <

    baz) { return "qux"; } else if (bar > baz) { return "quux"; } return "corge"; }; // ! const foo = cond([ [R.lt(R.__, R.__), R.always("qux")], [R.gt(R.__, R.__), R.always("quux")], [T, always("corge")] ]);
  11. const foo = bar => { switch (bar) { case

    "baz": { return "qux"; } case "quux": { return "corge"; } default: { return "grault"; } } }; // ! const foo = cond([ [equals("baz"), always("qux")], [equals("quux"), always("corge")], [T, always("grault")] ]);
  12. ϧʔϓͷॻ͖ํΛ౷ҰͰ͖ͨɻ

  13. const foo = bar => { const baz = [];

    for (let i = 0; i < bar.length; i++) { baz.push(bar[i] ** 2); } return baz; }; // ! const foo = bar => bar.map(baz => baz ** 2);
  14. const foo = count => { const baz = [];

    for (let i = 0; i < count; i++) { baz.push(i ** 2); } return baz; }; // ! const foo = (count, baz, i) => i < count ? foo(count, [].concat(baz, [i ** 2]), i + 1) : baz;
  15. for ͱ͔ while ͱ͔ॻ͚ͳ͍ͱɺ഑ྻ͸͍͍ͱͯ͠ɺ
 ؆ܿจମʴࡾ߲ԋࢉࢠͷ࠶ؼؔ਺͸ಡΈʹ͍͘ɻ ͜Ε΋ Ramda Ͱॻ͍ͯΈΔɻ

  16. const foo = bar => { const baz = [];

    for (let i = 0; i < bar.length; i++) { baz.push(bar[i] ** 2); } return baz; }; // ! const foo = bar => bar.map(baz => baz ** 2); // ! const foo = bar => map(baz => baz ** 2, bar);
  17. const foo = count => { const baz = [];

    for (let i = 0; i < count; i++) { baz.push(i ** 2); } return baz; }; // ! const foo = (count, baz, i) => i < count ? foo(count, [].concat(baz, [i ** 2]), i + 1) : baz; // ! const foo = count => times(i => i ** 2, count);
  18. ม਺ɾఆ਺ΛఆٛͰ͖ͳ͘ͳͬͨ

  19. const foo = bar => { const baz = bar

    ** 2; return baz + baz; }; // ! const foo = bar => bar ** 2 + bar ** 2; // ! let ࣜ෩Ξϓϩʔν const foo = bar => (baz => baz + baz)(bar ** 2);
  20. const foo = bar => { const baz = bar

    ** 2; const qux = baz ** 2; const quux = qux ** 2; return baz + qux + quux; }; // ! let ࣜ෩Ξϓϩʔν͸Ϡό͍ const foo = bar => (baz => ((baz, qux) => ((baz, qux, quux) => baz + qux + quux)(baz, qux, qux ** 2))( baz, baz ** 2 ))(bar ** 2);
  21. // ͜Μͳؔ਺Λ࡞Δ const letin = (initialValue, funcs) => funcs .reduce(

    (previousValue, func) => [ ...previousValue, func.apply(null, previousValue) ], initialValue ) .pop();
  22. const foo = bar => { const baz = bar

    ** 2; const qux = baz ** 2; const quux = qux ** 2; return baz + qux + quux; }; // ! const foo = bar => letin( [bar], [ bar => bar * bar, (bar, baz) => baz * baz, (bar, baz, qux) => qux * qux, (bar, baz, qux, quux) => baz + qux + quux ] );
  23. return Ҏ֎ͷจ͸͔͚ΔͷͰɺ ஋Λฦ͞ͳ͍ؔ਺ͳΒͳΜͰ΋͋Γ

  24. function foo(bar) { bar.baz = "qux"; bar.quux.push("corge"); } const foo

    = function (callback) { callback(); }; const foo = () => { throw new Error("bar"); }; function* foo() { yield "bar"; }
  25. · ͱ Ί • ESLint/TSLint ͷϧʔϧઃఆ͕γϯϓϧʹͳΔ • ͪΐͬͱίʔυॻ͖ʹ͍͘ • গͳ͍ϧʔϧͰ৭ʑͳॻ͖ํ੍͕ݶ͞ΕΔ͜ͱࣗମ͸

    ͍͍͔ͳͱࢥ͍ͬͯΔ
  26. Έ͍ͨͳ