Tern http://ternjs.net 1 // Demo variable 2 3 var - - PowerPoint PPT Presentation

tern
SMART_READER_LITE
LIVE PREVIEW

Tern http://ternjs.net 1 // Demo variable 2 3 var - - PowerPoint PPT Presentation

Tern http://ternjs.net 1 // Demo variable 2 3 var audience = "GOTO Berlin"; 4 5 var greeting = "hello " + a 6 7 console.log(greeting); 8 Static scope 1 var aGlobal = 10; 2 3


slide-1
SLIDE 1

Tern

http://ternjs.net

slide-2
SLIDE 2

// Demo variable ​ var audience = "GOTO Berlin"; ​ var greeting = "hello " + a ​ console.log(greeting); ​ 1 2 3 4 5 6 7 8

slide-3
SLIDE 3

Static scope

var aGlobal = 10; ​ function foo(arg1, arg2) { a } ​ 1 2 3 4 5 6

slide-4
SLIDE 4

Dynamic types

var player = {name: "Heinrich", score: 9999}; ​ function foo(x) { x. } ​ foo(player); ​ 1 2 3 4 5 6 7 8

slide-5
SLIDE 5

Observation

var x = "foo"; // x is a string var y = x; // y is also a string

slide-6
SLIDE 6

Observation

var x = "foo"; // x is a string OR bool if (quux()) x = false; var y = x; // y is also a string OR bool

slide-7
SLIDE 7

Abstract Interpretation

slide-8
SLIDE 8
slide-9
SLIDE 9

Syntax tree

{ "type": "Program", "body": [ {↔}, {↔}, {↔} ] } 1 2 3 4 20 44 68 69

slide-10
SLIDE 10

The user is typing

var array = []; if (code.isBeing("edited")) { must.know.type.of(array.in

  • rderTo = complete("a", "property");

​ 1 2 3 4 5

slide-11
SLIDE 11

var x = "foo"; var y = x;

string x

string

y

string

slide-12
SLIDE 12

var x = "foo"; if (quux()) x = false; var y = x;

string x

string bool

y

string bool

bool

slide-13
SLIDE 13

function f(a, b) { return g(b, a); } function g(x, y) { return x + y; } var r = f("foo", "bar");

slide-14
SLIDE 14

function g(x, y) { return x + y; }

[+] fn( x y )→ g

fn

slide-15
SLIDE 15

function f(a, b) { return g(b, a); } function g(x, y) { return x + y; }

[+] fn( x y )→ g

fn

fn( a b )→ f

fn'

slide-16
SLIDE 16

function f(a, b) { return g(b, a); } function g(x, y) { return x + y; }

[+] fn( x y )→ g

fn

fn( a b )→ f

fn'

call( )→

slide-17
SLIDE 17

function f(a, b) { return g(b, a); } function g(x, y) { return x + y; }

[+] fn( x y )→ g

fn

fn( a b )→ f

fn'

call( )→

slide-18
SLIDE 18

[+] fn( x y )→ g

fn

fn( a b )→ f

fn'

call( )→ call( )→ r

function f(a, b) { return g(b, a); } function g(x, y) { return x + y; } var r = f("foo", "bar");

slide-19
SLIDE 19

[+] fn( x

str

y

str )→ str

g

fn

fn( a

str

b

str )→ str

f

fn'

call(

str str )→ str

call(

str str )→ str

r

str

string

slide-20
SLIDE 20

var o = {x: 10, y: 0};

  • bj

.y

number

.x

number

  • bj
slide-21
SLIDE 21

var o = {x: 10, y: 0}; var h = o.y;

  • bj

.y

number

.x

number

  • bj

get .y h

slide-22
SLIDE 22

var o = {x: 10, y: 0}; var h = o.y;

  • bj

.y

number

.x

number

  • bj

get .y

number

h

number

slide-23
SLIDE 23

Parametric polymorphism

function lastOf(array) { return array[array.length - 1]; } var num = lastOf([1, 2, 3]); var str = lastOf(["a", "b", "c"]);

slide-24
SLIDE 24

Parametric polymorphism

function lastOf(array) { return array[array.length - 1]; }

get <i> fn( arr )→

slide-25
SLIDE 25

var __extends = function(child, parent) { for (var key in parent) child[key] = parent[key]; function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); };

Now what?

slide-26
SLIDE 26

Missing Connections

slide-27
SLIDE 27

Practical Use

slide-28
SLIDE 28
slide-29
SLIDE 29

// File 1 ​ var myVar = {x: 1, y: 2}; ​ (function(exports) { exports.capitalize = function(word) { return word.charAt(0).toUpperCase() word.slice(1); }; ​ exports.garble = function(word) { function garbleCh(ch) { return String.fromCharCode( ch.charCodeAt(0) + 1); } return word.replace(/./g, garbleCh }; }(this.myMod = {})); ​ // File 2 ​ my ​ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4

slide-30
SLIDE 30

For Emacs, Vim, & ST

➤ install editor plug-in ➤ add .tern-project file ➤ go

slide-31
SLIDE 31
slide-32
SLIDE 32

Related Work

slide-33
SLIDE 33
slide-34
SLIDE 34

Fin.

ternjs.net github.com/marijnh/tern marijnhaverbeke.nl twitter.com/marijnjh

slide-35
SLIDE 35
slide-36
SLIDE 36

eloquentjavascript.net

slide-37
SLIDE 37