Slide 1

Slide 1 text

twitter.com/mgechev github.com/mgechev blog.mgechev.com Linting Angular

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

twitter.com/mgechev github.com/mgechev

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Agenda History Codelyzer How codelyzer works? Compilers 101 Future plans

Slide 7

Slide 7 text

History

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Applying The Style Guide In Your Project

Slide 12

Slide 12 text

Enforcing Style

Slide 13

Slide 13 text

Enforcing Style • Fork the official style guide • Modify the styles according to your needs Introduce the style guide to your team Verify that each individual code change follows it

Slide 14

Slide 14 text

Enforcing Style • Fork the official style guide • Modify the styles according to your needs • Introduce the style guide to your team Verify that each individual code change follows it

Slide 15

Slide 15 text

Enforcing Style • Fork the official style guide • Modify the styles according to your needs • Introduce the style guide to your team • Verify that each individual code change follows it

Slide 16

Slide 16 text

Core Review Process

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

‣ Manual ‣ Boring ‣ Error Prone

Slide 19

Slide 19 text

codelyzer

Slide 20

Slide 20 text

Codelyzer is a project which aims to enforce common style using static code analysis

Slide 21

Slide 21 text

Codelyzer Rules • Components / directives name suffix • Components / directives selectors • Implement life cycle hook interfaces • Bind to public members • No dead CSS styles • …

Slide 22

Slide 22 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 23

Slide 23 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 24

Slide 24 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 25

Slide 25 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 26

Slide 26 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 27

Slide 27 text

Codelyzer Rules Components / directives name suffix Components / directives selectors Implement life cycle hook interfaces Bind to public members No dead CSS styles …

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

How Codelyzer Works?

Slide 31

Slide 31 text

Compiler COMPILER Input Output

Slide 32

Slide 32 text

Compiler COMPILER
 
 
 
 Input Output FRONT-END BACK-END

Slide 33

Slide 33 text

FRONT-END Compiler Input Output

Slide 34

Slide 34 text

FRONT-END
 
 
 
 Compiler Input Output LEXICAL ANALYSIS SYNTAX ANALYSIS … SEMANTIC ANALYSIS

Slide 35

Slide 35 text

Lexical Analysis tokenize('const product = a * b;')

Slide 36

Slide 36 text

Lexical Analysis tokenize('const product = a * b;')

Slide 37

Slide 37 text

Lexical Analysis tokenize('const product = a * b;') [ { lexeme: 'const', type: 'keyword' }, { lexeme: 'product', type: 'identifier' }, { lexeme: '=', type: 'operator' }, ... ]

Slide 38

Slide 38 text

Syntax Analysis parse(tokenize('...'))

Slide 39

Slide 39 text

Syntax Analysis parse(tokenize('...'))

Slide 40

Slide 40 text

Syntax Analysis parse(tokenize('...')) CONST PRODUCT * A B

Slide 41

Slide 41 text

Syntax Analysis parse(tokenize('...')) CONST PRODUCT * A B AST

Slide 42

Slide 42 text

Semantic Analysis typeCheck(parse(tokenize('...')))

Slide 43

Slide 43 text

checkConst(node) { return checkExpression(node.initializer) } checkExpression(node) { if (node instanceof BinOp) { return checkBinOperation(node); } ... } checkBinOperation(node) { if (isMultiplication(node)) { if (!isNumber(node.left) || !isNumber(node.right)) { throw new Error("Node must be 'number'"); } ... } }

Slide 44

Slide 44 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 45

Slide 45 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 46

Slide 46 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 47

Slide 47 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 48

Slide 48 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 49

Slide 49 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 50

Slide 50 text

checkConst(node) checkExpression(node) checkBinOperation(node) isNumber(node) CONST PRODUCT * A B

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Static program analysis is the analysis of computer software that is performed without actually executing programs. Wikipedia

Slide 53

Slide 53 text

codelyzer

Slide 54

Slide 54 text

Codelyzer Goals • Analyze the source code statically • Show warning when a style is violated • Extensible • Reuse as much as possible

Slide 55

Slide 55 text

tsc

Slide 56

Slide 56 text

tslint

Slide 57

Slide 57 text

Scott Wu Alex Eagle Martin Probst

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

tsc.parse( tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent { ... } `) );

Slide 60

Slide 60 text

ast = tsc.parse( tsc.tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent { ... } `) );

Slide 61

Slide 61 text

ast = tsc.parse( tsc.tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent { ... } `) );

Slide 62

Slide 62 text

tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent { ... } `) );

Slide 63

Slide 63 text

tokenize(` @Component({ selector: '[tooltip]', template: '...' }) class TooltipComponent { ... } `) ); CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 64

Slide 64 text

Validate Selector visitClass(node) { node.decorators.forEach(visitDecorator); } visitDecorator(node) { if (node.name === 'Component') { visitComponentMetadata(readMetadata(node)) } } visitComponentMetadata(metadata) { if (!parseSelector(metadata.selector).element) { tslint.report('Components should have element selectors'); } }

Slide 65

Slide 65 text

Validate Selector visitClass(node) visitDecorator(node) visitComponentMetadata(metadata) CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 66

Slide 66 text

Validate Selector visitClass(node) visitDecorator(node) visitComponentMetadata(metadata) CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 67

Slide 67 text

Validate Selector visitClass(node) visitDecorator(node) visitComponentMetadata(metadata) CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 68

Slide 68 text

Validate Selector visitClass(node) visitDecorator(node) visitComponentMetadata(metadata) CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 69

Slide 69 text

Validate Selector visitClass(node) visitDecorator(node) visitComponentMetadata(metadata) CLASS DECORATORS[0] OBJ LITERAL SELECTOR [TOOLTIP] TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1]

Slide 70

Slide 70 text

No Dead CSS

Slide 71

Slide 71 text

@Component({ selector: 'header-cmp', template: `

{{ title }}

Hello {{ name }}!
`, styles: [` h1 { font-size: 25px; } .greeting spam { color: red; } `] }) class HeaderComponent { ... }

Slide 72

Slide 72 text

@Component({ selector: 'header-cmp', template: `

{{ title }}

Hello {{ name }}!
`, styles: [` h1 { font-size: 25px; } .greeting spam { color: red; } `] }) class HeaderComponent { ... }

Slide 73

Slide 73 text

CLASS DECORATORS[0] OBJ LITERAL SELECTOR HEADER-CMP TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1] PROPERTY[2] [‘…’] STYLES

Slide 74

Slide 74 text

CLASS DECORATORS[0] OBJ LITERAL SELECTOR HEADER-CMP TEMPLATE ‘…’ PROPERTY[0] PROPERTY[1] PROPERTY[2] [‘…’] STYLES

Slide 75

Slide 75 text

Strings Are Hard To Analyze

Slide 76

Slide 76 text

CLASS DECORATORS[0] OBJ LITERAL SELECTOR HEADER-CMP TEMPLATE TEMPLATE AST PROPERTY[0] PROPERTY[1] PROPERTY[2] STYLE ASTS STYLES ROOTS CSS RULES

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Current Limitations • Linting per file - not entire program • Doesn’t perform type checking • Will fail when selector is variable

Slide 80

Slide 80 text

import { Component as NgComponent } from '@angular/core'; const selector = '[tool' + 'tip]'; @NgComponent({ selector, template: '...' }) class TooltipComponent { ... }

Slide 81

Slide 81 text

import { Component as NgComponent } from '@angular/core'; const selector = '[tool' + 'tip]'; @NgComponent({ selector, template: '...' }) class TooltipComponent { ... }

Slide 82

Slide 82 text

import { Component as NgComponent } from '@angular/core'; const selector = '[tool' + 'tip]'; @NgComponent({ selector, template: '...' }) class TooltipComponent { ... }

Slide 83

Slide 83 text

ngast github.com/mgechev/ngast

Slide 84

Slide 84 text

ngast • Provides high-level API to the compiler • Allows: • Deep metadata collection • Static evaluation of foldable expressions

Slide 85

Slide 85 text

import { Component as NgComponent } from '@angular/core'; const selector = '[tool' + 'tip]'; @NgComponent({ selector, template: '...' }) class TooltipComponent { ... }

Slide 86

Slide 86 text

import { Component as NgComponent } from '@angular/core'; const selector = '[tool' + 'tip]'; @NgComponent({ selector, template: '...' }) class TooltipComponent { ... } { "isHost": false, "isComponent": true, "selector": "[tooltip]", "changeDetection": 1, "inputs": {}, "outputs": {}, "template": { "template": "...", ... }, ... }

Slide 87

Slide 87 text

Codelyzer 3 • Deep metadata collection • Autofixes • Much more!

Slide 88

Slide 88 text

Thank you! twitter.com/mgechev github.com/mgechev blog.mgechev.com