Testi ting an and Evol
- lving
g TypeScr Script De Declaration
- n Files
with P Progr
- gram An
Analysi sis s
Erik Krogh Kristensen
joint work with Anders Møller Π CENTER FOR ADVANCED SOFTWARE ANALYSIS
http://casa.au.dk/
Testi ting an and Evol olving g TypeScr Script De Declaration - - PowerPoint PPT Presentation
Testi ting an and Evol olving g TypeScr Script De Declaration on Files with P Progr ogram An Analysi sis s Erik Krogh Kristensen joint work with Anders Mller C ENTER FOR A DVANCED S OFTWARE A NALYSIS http://casa.au.dk/
Erik Krogh Kristensen
joint work with Anders Møller Π CENTER FOR ADVANCED SOFTWARE ANALYSIS
http://casa.au.dk/
2
⇒ mislead type checking and code completion!
3
4
JavaScript library implementation TypeScript type declaration
4
function Constraint(...) { /** * Equations to be solved in * this constraint * * @property equations * @type {Array} */ this.equations = []; ... } ... class Constraint { constructor(...); equeations: Equation[]; ... }
JavaScript library implementation TypeScript type declaration
5
6
7
Based on automated testing
(Randoop: Feedback-directed random test generation, Pacheco, Lahiri, Ernst, and Ball, ICSE'07)
How to adapt Randoop-style testing from Java to TypeScript?
(structural typing, higher-order functions, generics, …)
8
[Type Test Scripts for TypeScript Testing, Kristensen & Møller, OOPSLA 2017]
JavaScript library implementation TypeScript type declaration
var Store = { makeItem: function(n) { return { print: function() { return n; } } } } declare var Store: { makeItem(n: number): Item } interface Item { print(): string }
JavaScript implementation TypeScript declaration Type Test Script JavaScript interpreter Mismatches
2804 found mismatches (many with same root cause)
previous work (TSCheck)
10
error 63 strict nulls 47 benign 14
e xp e xpor t
f unc nc t i o t i on n bi ndAc t i onCr e a t or s <A e xt e nd e xt e nds s Ac t i onCr e a t or <a ny a ny>> ( a c t i onCr e a t or : A) : A; f un f unc t i c t i on
va r va r r e s ul t = {}; f or f or ( va r va r ke y i n i n c r e a t or s ) { va r va r c r e a t or = c r e a t or s [ ke y] ; i f i f ( t ype o t ype of c r e a t or === ' f unc ' f unc t i o t i on' n' ) { r e s ul t [ ke y] = bi ndAc t i onCr e a t or ( c r e a t or , di s pa t c h) ; } } r e t ur n r e t ur n r e s ul t ; }
redux.d.ts redux.js
13
Infer the same declaration that an expert human would write ?
f unc t i on f unc t i on ge t ( obj , k) { r e t ur n r e t ur n obj [ k] ; }
f unc t c t i on ge t <T>( obj : T[ ] , k: num be r ) : T f unc t c t i on ge t <T>( obj : {[ i : s t r i n i ng] : T}, k: s t r i ng) : T f unc t c t i on ge t <T, K e xt e nds ke yof T>( obj : T, k: K) : T[ K] ; f unc t c t i on ge t ( obj : num be r [ ] , k: num be r ) : num be r
14
Snapshot of heap after loading
15
Also for methods that are never called within the library
A framework for type inference with subtyping, François Pottier, ICFP’98
To get information about how the library uses itself
16
Upper-bound and lower-bound dataflow analysis à la Pottier
“what values may flow in?”
“how may the values be used?”
17
x = ne w C( ) ; . . . f oo( x) ; f unc t i on f oo( a ) { . . . b = a . ne xt ; . . . }
Both kinds of information give useful hints to types, when analyzing libraries without the applications!
e xpor
a s s Spr i t e e xt e n e nds PI XI . Cont a i ne r { c ons t r uc t or ( t e xt ur e : PI XI . Te xt ur e ) ; s t a t i c f r om Fr a m e : ( f r a m e I d: s t r i n i ng | num be be r ) => PI XI . Spr i t e ; s t a t i c f r om I m a ge : ( i m a ge I d: s t r i n i ng, c r os s or i gi n: a ny, s c a l e M
_he i ght : num be be r ; _wi dt h: num be be r ; a nc hor : PI XI . Poi nt ; bl e ndM
be r ;
s e t Te xt ur e : ( t e xt ur e : PI XI . Te xt ur e ) => voi d; s ha de r : a ny; t e xt ur e : PI XI . Te xt ur e ; t i nt : num be r ; }
Example output from running TSInfer on PixiJS 2.2
Our goal is to get close to what a human would write
?: boolean ?: number
private private PIXI.Shader private
18
19
new.js TSInfer new.gen.d.ts Compare List of changes TSInfer
Filter Filtered list of changes
First naive approach Our approach
20
Too many false positives, due to
21
Π CENTER FOR ADVANCED SOFTWARE ANALYSIS
http://casa.au.dk/
22