Drupal + Technology TRACK SUPPORTED BY 17/3/2018 Code with style! - - PowerPoint PPT Presentation

drupal technology
SMART_READER_LITE
LIVE PREVIEW

Drupal + Technology TRACK SUPPORTED BY 17/3/2018 Code with style! - - PowerPoint PPT Presentation

Drupal + Technology TRACK SUPPORTED BY 17/3/2018 Code with style! Prettier + ESlint David Corbacho Romn https://www.drupaleurope.org/session/code-style-prettier-eslint We want to deliver software without flaws. How to deliver software


slide-1
SLIDE 1
slide-2
SLIDE 2

Drupal + Technology

17/3/2018

TRACK SUPPORTED BY

slide-3
SLIDE 3

David Corbacho Román

Code with style!

https://www.drupaleurope.org/session/code-style-prettier-eslint

Prettier + ESlint

slide-4
SLIDE 4

We want to deliver software without flaws.

slide-5
SLIDE 5

How to deliver software without flaws.

Ask your team members to review your code

slide-6
SLIDE 6

Code reviews

slide-7
SLIDE 7

Code reviews

slide-8
SLIDE 8

Code reviews

slide-9
SLIDE 9

Code reviews

slide-10
SLIDE 10
slide-11
SLIDE 11

Unpopular opinion: Maybe… perfect formatting is not *that* important

slide-12
SLIDE 12

Unpopular opinion: Maybe… perfect formatting is not *that* important

... and one semicolon here

slide-13
SLIDE 13

Following a convention is important

slide-14
SLIDE 14

How to deliver software without flaws.

Dynamic Code Analysis aka “testing” Static Code Analysis Tools that inspect the code without executing the software.

Linters

slide-15
SLIDE 15

How to deliver software without flaws.

Static Code Analysis Tools that inspect the code without executing the software. CODE SMELL PATTERNS BEST PRACTICES SECURITY FLAWS STYLE

Linters

slide-16
SLIDE 16

Linting in JavaScript. Evolution

2002 by Douglas Crockford, JSON creator.

slide-17
SLIDE 17

Linting evolution

2002 by Douglas Crockford, JSON creator. Too opinionated.

slide-18
SLIDE 18

Linting evolution

  • 2011. Fork of JSLint

Community driven Not opinionated More configurable 2002 by Douglas Crockford, JSON creator. Too opinionated.

slide-19
SLIDE 19

Linting evolution

2013 by Nicholas Zakas. Not opinionated Lots of configuration options Extensible with plugins + Supports NodeJS + Keeps up with ES evolution + In 2014 support for JSX 2002 by Douglas Crockford, JSON creator. Too opinionated.

  • 2011. Fork of JSLint

Community driven Not opinionated More configurable

slide-20
SLIDE 20

ESLint demo

slide-21
SLIDE 21

Too much configuration

slide-22
SLIDE 22

Too much configuration

https://github.com/dustinspecker/awesome-eslint ESLint “Shareable configurations”

slide-23
SLIDE 23

Airbnb JS Style guide

slide-24
SLIDE 24

Airbnb JS Style guide

Excellent opinionated guide to ES6 using ESLint rules.

style guide https://github.com/airbnb/javascript

slide-25
SLIDE 25

Airbnb JS Style guide.

slide-26
SLIDE 26

Too opinionated? Creates friction in the team? Escape hatch: Override Airbnb rules Example Drupal 8 core

slide-27
SLIDE 27

How Linters work? AST parser

slide-28
SLIDE 28

WHAT IF…

We use the output

  • f the AST parser

to rewrite the source code? gofmt was born. 2013 [Go formatter] Also rfmt [Reason formatter]

slide-29
SLIDE 29

WHAT IF…

We do it for JavaScript? using Wadler's algorithm from "A prettier printer" and lots of smart tweaks. Prettier was born

  • Jan. 2017

by James Long

slide-30
SLIDE 30

Prettier

prettier.io

slide-31
SLIDE 31

Prettier

https://www.youtube.com/watch?v=hkfBvpEfWdA

slide-32
SLIDE 32

Prettier

slide-33
SLIDE 33

Prettier

slide-34
SLIDE 34
  • Prettier. Not only JS.

Sept 2018 supported languages

slide-35
SLIDE 35
  • Prettier. Opinionated.

Only 9 style rules

slide-36
SLIDE 36
  • Prettier. Opinionated.

A hard pill to swallow for artisans

... and one semicolon here

slide-37
SLIDE 37

Prettier will hurt your feelings

It will not respect columns alignment or

  • ther whitespace “decorations”
slide-38
SLIDE 38
slide-39
SLIDE 39

https://www.reddit.com/r/javascript/comments/8as6ns/i_dont_like_prettier/dx14ag5/

slide-40
SLIDE 40
  • Prettier. Opinionated.

… but a quick win for juniors

slide-41
SLIDE 41

Who uses Prettier

  • Feb. 2018 https://prettier.io/blog/2018/02/26/1.11.0.html
slide-42
SLIDE 42

Benefits of Prettier

Reduces Friction. [Prettier choses formatting for you] Speed up Code Reviews Instant feedback 1 thing less to multi-task while coding Faster copy/pasting and refactoring

slide-43
SLIDE 43

Cons of Prettier

One more tool in the belt to maintain No custom formatting possible Not for everyone

slide-44
SLIDE 44

Prettier

slide-45
SLIDE 45

Prettier

slide-46
SLIDE 46

Conclusion: Before

CODE SMELL PATTERNS BEST PRACTICES SECURITY FLAWS FORMATTING

Linters

slide-47
SLIDE 47

Conclusion: Now

CODE SMELL PATTERNS BEST PRACTICES SECURITY FLAWS FORMATTING

Linters

Code reviews

slide-48
SLIDE 48

Configure Prettier + ESLint

yarn add -D eslint eslint-config-airbnb yarn add -D prettier eslint-plugin-prettier eslint-config-prettier

(you might want just eslint-config-airbnb-base, if not using React) { "extends": [ "airbnb", "plugin:prettier/recommended" ], } { "printWidth": 80, "semi": true, "singleQuote": true, "trailingComma": "all" } .prettierrc.json .eslintrc.json

it will override (disable) eslint rules not compatible with prettier Order matters

slide-49
SLIDE 49

Drupal 8.6 comes with Prettier. This is an effort to standardize the formatting of our JavaScript in addition to the rules provided by ESLint. https://www.drupal.org/node/2986680

slide-50
SLIDE 50
  • Drupal. PHP Linter

PHP Code Sniffer

Installing Coder Sniffer https://www.drupal.org/node/1419988 Helps to enforce coding standards https://www.drupal.org/docs/develop/standards/coding-standa rds Coder module contains the “sniffs” https://www.drupal.org/project/coder

slide-51
SLIDE 51

Using Prettier in your project

Option 1. Ask every developer to install Prettier on their editor and auto-save (It adds friction to the team, and could limit editor choice, personal workflow) Option 2. Run in a pre-commit hook Use lint-staged for easily setting up git commit hooks.

slide-52
SLIDE 52

Introduce Prettier to an existing project

First: Agree with the team! If only part of the team uses Prettier, it will warranty a lot of friction. Prettier by default formats the whole file, not only your changes. This multiplies the chances of git conflicts for the whole team and lengthy code reviews because of unrelated changes. Option 1. Apply Prettier to the whole project (if medium size project) It will dirt git history Option 2. Go back to the past. Rewrite git history like Prettier would have been there from the day one. https://medium.com/millennial-falcon-technology/reformatting-your-c

  • de-base-using-prettier-or-eslint-without-destroying-git-history-3505

2f3d853e

slide-53
SLIDE 53
  • Drupal. CSS linters

In the process of migrating from CSS Lint to STYLE Lint Drupal stylelint config:

slide-54
SLIDE 54

David Corbacho Román

Developer at Wunder @dcorbacho

slide-55
SLIDE 55

Become a Drupal contributor Friday from 9am

  • First timers workshop
  • Mentored contribution
  • General contribution