Slide 1

Slide 1 text

Next-Generation JavaScript Language Tooling @ariyahidayat March 19, 2014

Slide 2

Slide 2 text

http://todomvc.com/architecture-examples/backbone/

Slide 3

Slide 3 text

MyView = Backbone.View.extend({ events: { 'click .toggle': 'toggleCompletd', 'click .destroy': 'clear', 'blur .edit': 'close', }, toggleCompleted: function () { this.model.toggle() }, // and so on }); Unexpected comma Missing semicolon

Slide 4

Slide 4 text

Tools separate us

Slide 5

Slide 5 text

Static analysis Dynamic analysis Transformation

Slide 6

Slide 6 text

Every tool is open-source Tweak/customize/run with it! There are links (everywhere) to detailed articles/blog posts speakerdeck.com/ariya

Slide 7

Slide 7 text

Composeable Tools Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

Slide 8

Slide 8 text

{ type: "Program", body: [ { type: "VariableDeclaration", declarations: [ { type: "VariableDeclarator", id: { type: "Identifier", name: "answer" }, init: { type: "Literal", value: 42, raw: "42" } } ], kind: "var" } ] } Syntax Tree var answer = 42;

Slide 9

Slide 9 text

Building Blocks → Tools Parser Code Generator Obvious examples: minifier, obfuscator, ...

Slide 10

Slide 10 text

Execution Visualization http://int3.github.io/metajs/

Slide 11

Slide 11 text

Static Analysis

Slide 12

Slide 12 text

Stray Logging function detect_console(code) { function check(node) { if (node.type === 'CallExpression') { if (node.callee.type === 'MemberExpression') { if (node.callee.object.name === 'console') { alert('console call at line', node.loc.start.line); } } } } var tree = esprima.parse(code, { loc: true }); estraverse.traverse(tree, { enter:check }); } http://ariya.ofilabs.com/2013/04/automagic-removal-of-javascript-logging.html

Slide 13

Slide 13 text

Boolean Traps reload(x, y, false) function checkLastArgument(node) { var args = node['arguments']; if (args.length < 2) { return; } if (typeof args[args.length - 1].value === 'boolean') { report(node, 'Dangerous Boolean literal'); } } http://ariya.ofilabs.com/2012/06/detecting-boolean-traps-with-esprima.html

Slide 14

Slide 14 text

Nested Ternary Conditionals http://ariya.ofilabs.com/2012/10/detecting-nested-ternary-conditionals.html var str = (age < 1) ? "baby" : (age < 5) ? "toddler" : (age < 18) ? "child": "adult";

Slide 15

Slide 15 text

Cyclomatic Complexity http://ariya.ofilabs.com/2012/12/complexity-analysis-of-javascript-code.html if (true) "foo"; else "bar"; Control Flow Graph 6 edges 6 nodes 1 exit Cyclomatic Complexity = 2

Slide 16

Slide 16 text

Monitoring of Complexity http://ariya.ofilabs.com/2013/05/continuous-monitoring-of-javascript-code-complexity.html

Slide 17

Slide 17 text

“YOU SHALL NOT PASS!” — Darth Vader

Slide 18

Slide 18 text

Complexity Visualization http://ariya.ofilabs.com/2013/01/javascript-code-complexity-visualization.html

Slide 19

Slide 19 text

Transformation

Slide 20

Slide 20 text

Source Transformation Parser Code Generator In-place Modification Non-Destructive Regenerative http://ariya.ofilabs.com/2013/06/javascript-source-transformation-non-destructive-vs-regenerative.html

Slide 21

Slide 21 text

String Literal Quotes http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html console.log('Hello') console.log("Hello")

Slide 22

Slide 22 text

ES6 Lexical Block Scope http://ariya.ofilabs.com/2013/05/es6-and-block-scope.html function f() { let j = data.length; console.log(j, 'items'); for (let i = 0; i < j; ++i) { let j = data[i] * data[i]; console.log(j); // squares } } function f() { var j = data.length; console.log(j, 'items'); for (var i = 0; i < j; ++i) { var j$0 = data[i] * data[i]; console.log(j$0); // squares } }

Slide 23

Slide 23 text

ES6 “Class” at class Derived extends Base { constructor(value) { super(value + 1); } getValue() { return super.getValue() - 1; } static getName() { return "Derived"; } } http://benjamn.github.io/fluent2014-talk/#/26 function Derived(value) { Base.call(this, value + 1); } Derived.prototype = Object.create(Base. prototype); Derived.prototype.constructor = Derived; Derived.prototype.getValue = function() { return Base.prototype.getValue.call (this) - 1; }; Derived.getName = function() { return "Derived"; };

Slide 24

Slide 24 text

Dynamic Analysis

Slide 25

Slide 25 text

Fast = Enough? Alice Bob Chuck Dan ... Bob Alice Dan Chuck ... Sort How’s the speed? 2 ms to sort 10 contacts

Slide 26

Slide 26 text

Array.prototype.swap = function (i, j) { var k = this[i]; this[i] = this[j]; this[j] = k; } function sort(list) { var items = list.slice(0), swapped = false, p, q; for (p = 1; p < items.length; ++p) { for (q = 0; q < items.length - p; ++q) { if (items[q + 1] < items[q]) { items.swap(q, q + 1); swapped =true; } } if (!swapped) break; } return items; } Bubble Sort ???

Slide 27

Slide 27 text

Run-time Complexity http://ariya.ofilabs.com/2012/01/scalable-web-apps-the-complexity-issue.html Array.prototype.swap = function (i, j) { var k = this[i]; this[i] = this[j]; this[j] = k; } Array.prototype.swap = function (i, j) { Log({ name: 'Array.prototype.swap', lineNumber: 1, range: [23, 94] }); var k = this[i]; this[i] = this[j]; this[j] = k; } http://esprima.org/demo/functiontrace.html

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

T D D B D D P D D

Slide 30

Slide 30 text

Code Coverage with Istanbul function inc(p, q) { if (q == undefined) q = 1; return p + q/q; } assert("inc(4) must give 5", inc(4) == 5); E = Else is not taken http://ariya.ofilabs.com/2012/12/javascript-code-coverage-with-istanbul.html

Slide 31

Slide 31 text

http://ariya.ofilabs.com/2012/09/the-hidden-trap-of-code-coverage.html Does not catch the missing code sequence

Slide 32

Slide 32 text

Coverage Thresholds http://ariya.ofilabs.com/2013/05/hard-thresholds-on-javascript-code-coverage.html istanbul check-coverage --statement -5 --branch -3 --function 100

Slide 33

Slide 33 text

“If you think JSLint hurts your feelings, wait until you use Istanbul.”

Slide 34

Slide 34 text

...I gave you my heart But the very next day you gave it away...

Slide 35

Slide 35 text

Final Words

Slide 36

Slide 36 text

http://ariya.ofilabs.com/2012/12/quality-code-via-multiple-layers-of-defense.html Being Defensive

Slide 37

Slide 37 text

Being Objective Mr. Reviewer Your code sucks! Your code sucks!

Slide 38

Slide 38 text

Tools: The Final Frontier To boldly analyze what no man has analyzed before...

Slide 39

Slide 39 text

Thank You shapesecurity.com @ariyahidayat

Slide 40

Slide 40 text

Evaluate This Session Sign-in: www.eclipsecon.org Select session from schedule Evaluate: 1 2 3