Scaling
Best practices With Syntax Trees

Will Klein

Best Practices

 

Four Years Ago

 

Two Years Ago

 

Today

Testing

User

Experience

 

Developer

Experience

 

Documentation

Code review

Convention Tests

Convention Tests, AKA

  • Linters
  • Code analyzers
  • Static analysis

Language

Conventions

What about     our project Conventions?

What If We could write our own convention tests?

Convention Test "Frameworks"

  • Common tests included
  • Pluggable testing API
  • ESLint, for example
// Life, Universe, and Everything
var answer = 6 * 7;
// Life, Universe, and Everything
var answer = 6 * 7;
10111010001011011101000101

...

How does this work?

// Life, Universe, and Everything
var answer = 6 * 7;
[var] [answer] [=] [6] [*] [7][;]

...

Tokenize

// Life, Universe, and Everything
var answer = 6 * 7;
[var] [answer] [=] [6] [*] [7][;]

...

...

?

Parse

 

A Syntax Tree

Syntax Tree

  • Abstract syntax tree
  • AST
  • Syntax tree is easier to say

An example

This Code...

should really be

no-unused-vars

semi

ESLint Rules

???

ESLint Rules

IfStatement

Nested IfStatement

Nested IfStatement

ESLint

  • Builds our syntax tree for us
  • Iterates over nodes in the tree
  • For each node, do we have a test?

ESLint: Rule boilerplate

ESLint: Rule boilerplate

ESLint: Rule boilerplate

ESLint: Rule boilerplate

Nested IfStatement


ESLint: context

Directly nested CASE

Directly nested CASE

Directly nested CASE

Directly nested CASE

block Nested CASE

block Nested CASE

block Nested CASE

Siblings Case

Else If Case

Else If Case

Else If Case

no-nested-if

Demo

Docs & Code

What else?

We Just Learned

  • Convention tests
  • Syntax trees
  • How to write a convention test using syntax trees

Go Forth and Test

  • Implement existing tools
  • Consider your project's conventions
  • Write your own convention tests

Go Forth and Test

  • Implement existing tools
  • Consider your project's conventions
  • Write your own convention tests
  • Improve your developer experience

programs must be written for people to read, and only incidentally for machines to execute

Donald Knuth

Structure and Interpretation of Computer Programs

http://willkle.in

will.klein@gmail.com

Thanks

@willslab

Made with Slides.com