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

ESLint The Iron Yard Charlotte

ESLint The Iron Yard Charlotte

Guest presentation for front-end engineers on Friday 2016-09-30

Mark Pedrotti

September 30, 2016
Tweet

More Decks by Mark Pedrotti

Other Decks in Programming

Transcript

  1. ESLint is a tool for code quality Mark Pedrotti twitter.com/ittordepam

    github.com/pedrottimark speakerdeck.com/pedrottimark/eslint-the-iron-yard-charlotte
  2. Learning objectives • Find resources • Prioritize criteria and feedback

    • Start linting an existing project • Integrate linting in your editor • Configure rules for a project
  3. Find resources on eslint.org • Getting Started with ESLint •

    Configuring ESLint • Command Line Interface • Rules • Integrations
  4. Prioritize criteria for code and errors 1. Correct incorrect 2.

    Clear unclear 3. Complete incomplete 4. Changeable unchangeable 5. Consistent inconsistent
  5. Prioritize feedback Discussing Design ! reaction expresses personal feelings, or

    what people think they are expected to say ! direction tells how to solve problems " critique says what does or doesn’t work, and analyzes choices compared to objectives
  6. Prioritize feedback Tom Greever ! We don’t want our stakeholders

    
 to fall in love with our designs. " We want them to fall in love 
 with the logic and reason 
 behind those designs.
  7. Correct • array-callback-return
 enforce return statements
 in callbacks of array

    methods For example, filter, map, and reduce. Luke taught me how to use them :)
  8. /*eslint array-callback-return: "error"*/ function cardQuestionObject(cards, card, sideQuestion, nOther) { const

    sideAnswer = sideOpposite(sideQuestion); const answerCorrect = card[sideAnswer]; const cardsOther = sample(cards.filter(function (cardOther) { cardOther.id !== card.id; }), nOther); const answersOther = cardsOther.map(function (cardOther) { cardOther[sideAnswer]; }); return { /* card question object */ }; }
  9. /*eslint array-callback-return: "error"*/ function cardQuestionObject(cards, card, sideQuestion, nOther) { const

    sideAnswer = sideOpposite(sideQuestion); const answerCorrect = card[sideAnswer]; const cardsOther = sample(cards.filter(function (cardOther) { return cardOther.id !== card.id; }), nOther); const answersOther = cardsOther.map(function (cardOther) { return cardOther[sideAnswer]; }); return { /* card question object */ }; }
  10. /*eslint array-callback-return: "error"*/ function cardQuestionObject(cards, card, sideQuestion, nOther) { const

    sideAnswer = sideOpposite(sideQuestion); const answerCorrect = card[sideAnswer]; const cardsOther = sample(cards.filter( (cardOther) => cardOther.id !== card.id // compare arrow function ), nOther); const answersOther = cardsOther.map( (cardOther) => cardOther[sideAnswer] // compare arrow function ); return { /* card question object */ }; }
  11. /*eslint curly: "error"*/ let statePrev = store.getState(); function writeChanges() {

    const state = store.getState(); if (statePrev.cards !== state.cards) writeCards(state.cards); // asynchronous if (statePrev.decks !== state.decks) writeDecks(state.decks); // asynchronous statePrev = state; }
  12. /*eslint curly: "error"*/ let statePrev = store.getState(); function writeChanges() {

    const state = store.getState(); if (statePrev.cards !== state.cards) { writeCards(state.cards); // asynchronous } if (statePrev.decks !== state.decks) { writeDecks(state.decks); // asynchronous } statePrev = state; }
  13. /*eslint curly: "error"*/ let { cards: cardsPrev, decks: decksPrev }

    = store.getState(); function writeChanges() { const { cards, decks } = store.getState(); if (cardsPrev !== cards) { writeCards(cards); // asynchronous cardsPrev = cards; } if (decksPrev !== decks) { writeDecks(decks); // asynchronous decksPrev = decks; } }
  14. Complete and correct • no-unused-vars disallow unused variables
 Often from

    incomplete refactoring • no-undef disallow undeclared variables.
 Static analysis while you write code
 instead of errors when you run code.
  15. /*eslint no-undef: "error"*/ /*eslint no-unused-vars: "error"*/ function cardQuestionObject(cards, card, sideQuestion,

    nOther) { const sideAnswer = sideOpposite(sideQuestion); const correctAnswer = card[sideAnswer]; // no-unused-vars const cardsOther = sample(cards.filter(/* callback */, nOther); const answersOther = cardsOther.map(/* callback */); return { cardID: card.id, question: card[sideQuestion], answerCorrect, // no-undef answers: shuffle([answerCorrect, ...answersOther]), // no-undef };
  16. /*eslint comma-dangle: ["error", "always-multiline"]*/ /*eslint object-property-newline: "error"*/ function cardQuestionObject(cards, card,

    sideQuestion, nOther) { // declare variables return { cardID: card.id, question: card[sideQuestion], answerCorrect, answers: shuffle([answerCorrect, ...answersOther]) // , }; }
  17. /*eslint comma-dangle: ["error", "always-multiline"]*/ /*eslint object-property-newline: "error"*/ function cardQuestionObject(cards, card,

    sideQuestion, nOther) { // declare variables return { cardID: card.id, question: card[sideQuestion], answerCorrect, answers: shuffle([answerCorrect, ...answersOther]), sideQuestion, // could determine lang, dir, font, and so on }; }
  18. Consistent • indent enforce consistent indentation • semi require or

    disallow semicolons Critique about style: If you can’t configure a rule 
 to report it as a problem, consider letting it go.
  19. /*eslint indent: ["error", 2]*/ /*eslint semi: ["error", "always"]*/ function writeChanges()

    { const { cards, decks } = store.getState() if (cardsPrev !== cards) { writeCards(cards) // asynchronous cardsPrev = cards } if (decksPrev !== decks) { writeDecks(decks) // asynchronous decksPrev = decks } }
  20. /*eslint indent: ["error", 2]*/ /*eslint semi: ["error", "always"]*/ function writeChanges()

    { const { cards, decks } = store.getState(); if (cardsPrev !== cards) { writeCards(cards); // asynchronous cardsPrev = cards; } if (decksPrev !== decks) { writeDecks(decks); // asynchronous decksPrev = decks; } }
  21. Start linting an existing project 1. git clone https://github.com/ bonniee/learning-react-native

    2. cd learning-react-native 3. cd Zebreto subdirectory! 4. npm install
  22. Getting Started 1. Open the project in your editor. 2.

    Open the package.json file.
 Does it have devDependencies? 3. npm install --save-dev eslint
 See eslint in devDependencies.
  23. ./node_modules/.bin/eslint --init • How to configure ESLint?
 Inspect your JavaScript

    file(s) • Which files? src/**/*.js • What format config file? JavaScript • ECMAScript 6 yes
  24. • ES6 Modules yes • Where will your code run?

    Node • JSX yes • React yes
  25. Configuring ESLint 1. Create an empty .eslintrc.js file 
 in

    the Zebreto folder. 2. Paste an example configuration. 3. If the example is JSON, insert module.exports = at the beginning.
  26. 4. Click the Babel-ESLint link 
 under Specifying Parser. Read.

    5. npm install --save-dev babel-eslint 6. Paste "parser": "babel-eslint" Imitate the example for Esprima.
  27. 1. Add "lint": "eslint src" 
 to "scripts" in package.json

    2. npm run lint 
 8 missing semicolon errors 
 followed by npm stuff 3. npm run lint -s 
 omits npm stuff
  28. Integrate linting in your editor 
 Receive modeless feedback •

    to fix problems while the context is still fresh in your mind, • but without disrupting the flow 
 of your productive concentration.
  29. 1. Click Extensions in the View Bar 
 of VS

    Code. 2. Scroll to ESLint, 
 and then click Install. 3. Click Explorer in the View Bar. 4. On the View menu, 
 click Problems.
  30. 1. In your editor, 
 open a file that has

    an error: 
 src/components/HeadingText.js 2. In the Filter by type or text box, 
 type eslint 3. Click the error in Problems 
 to see it in the Editor.
  31. 1. Open a file that has errors: 
 src/components/Input.js 2.

    Click an error in Problems 
 to see it in the Editor. 3. Click the light bulb. 4. Click Fix this semi problem
  32. 1. Add "lint:fix": "eslint --fix src" 
 to "scripts" in

    package.json 2. npm run lint:fix -s 3. In your editor, see that an error was fixed in Input.js
  33. Configure rules for a project • When ESLint reports problems,

    
 fix the code; or if it is correct, find options or turn off rules. • When you find problems in code, 
 find rules that will report them.
  34. Configuring ESLint 1. From Extending Configuration Files, paste into .eslintrc.js

    
 "extends": "eslint:recommended" 2. npm run lint -s
 70 errors
  35. 1. Open src/styles/fonts.js 2. Click the error in Problems:
 'module'

    is not defined
 Here is an idiom from Node.js:
 module.exports = …
  36. Rules 1. Read the no-undef rule page. 2. Read about

    /*global */ comments
 which are not the fluent solution. 3. Read about node environment 
 which are the fluent solution.
  37. Configuring ESLint 1. From Specifying Environments, paste into .eslintrc.js under

    "env" 
 "node": true 2. npm run lint -s
 55 errors
  38. 1. Open src/stores/DeckMetaStore.js 2. Read the no-console rule page. 3.

    Paste in .eslintrc.js under "rules"
 no-console: ["error", { allow: ["error"] }] 4. npm run lint -s
 50 errors
  39. 1. Click the error in Problems. 2. Edit to allow

    console.info too
 no-console: ["error", { allow: ["error", "info"] }] 3. npm run lint -s
 48 errors
  40. 1. Open src/components/Zebreto.js 2. In a browser, search for 


    ESLint no-unused-vars React JSX 3. Click link about #2156 4. Read, and then go to issue #1911 5. Click link about Prevent variables used in JSX to be incorrectly marked as unused
  41. Configuration of eslint-plugin-react 1. Paste into .eslintrc.js 
 plugins: [

    "react" ] 2. Paste under "rules" 
 "react/jsx-uses-react": "error", "react/jsx-uses-vars": "error",
  42. 1. npm i -D eslint-plugin-react 2. npm run lint -s


    8 errors 3. Click the error in Problems: 
 'Component' is defined by never used 4. Delete , { Component }
 7 errors
  43. Find other resources • ESLint Demo on eslint.org • Search

    the docs… on eslint.org • @geteslint on twitter.com • Search eslint-plugin on npmjs.com • .editorconfig file on editorconfig.org