skip to main content
research-article
Open Access
Artifacts Evaluated & Functional

Fast and precise type checking for JavaScript

Published:12 October 2017Publication History
Skip Abstract Section

Abstract

In this paper we present the design and implementation of Flow, a fast and precise type checker for JavaScript that is used by thousands of developers on millions of lines of code at Facebook every day. Flow uses sophisticated type inference to understand common JavaScript idioms precisely. This helps it find non-trivial bugs in code and provide code intelligence to editors without requiring significant rewriting or annotations from the developer. We formalize an important fragment of Flow's analysis and prove its soundness. Furthermore, Flow uses aggressive parallelization and incrementalization to deliver near-instantaneous response times. This helps it avoid introducing any latency in the usual edit-refresh cycle of rapid JavaScript development. We describe the algorithms and systems infrastructure that we built to scale Flow's analysis.

References

  1. Alexander Aiken and Edward L. Wimmers. 1992. Solving systems of set constraints. Proceedings of the Seventh Annual IEEE Symposium on Logic in Computer Science (1992), 329–340. http://ieeexplore.ieee.org/lpdocs/epic03/wrapper.htm? arnumber=185545Google ScholarGoogle Scholar
  2. Alexander Aiken, Edward L. Wimmers, and T. K. Lakshman. 1994. Soft Typing with Conditional Types. In Proceedings of the 21st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’94). ACM, New York, NY, USA, 163–173. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Davide Ancona and Andrea Corradi. 2016. Semantic subtyping for imperative object-oriented languages. Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications -OOPSLA 2016, 568–587. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Christopher Anderson, Paola Giannini, and Sophia Drossopoulou. 2005. Towards Type Inference for Javascript. In Proceedings of the 19th European Conference on Object-Oriented Programming (ECOOP’05). Springer-Verlag, Berlin, Heidelberg, 428– 452. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Esben Andreasen and Anders Møller. 2014. Determinacy in Static Analysis for jQuery. In Proceedings of the 2014 ACM International Conference on Object Oriented Programming Systems Languages & Applications (OOPSLA ’14). ACM, New York, NY, USA, 17–31. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Gavin Bierman, Martín Abadi, and Mads Torgersen. 2014. Understanding TypeScript. In ECOOP 2014 – Object-Oriented Programming: 28th European Conference, Uppsala, Sweden, July 28 – August 1, 2014. Proceedings, Richard Jones (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg, 257–281. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Giuseppe Castagna and Victor Lanvin. 2017. Gradual Typing with Union and Intersection Types. In Proceedings of ICFP. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Giuseppe Castagna, Tommaso Petrucciani, and Kim Nguyen. 2016. Set-theoretic Types for Polymorphic Variants. In Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming (ICFP 2016). ACM, New York, NY, USA, 378–391. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Satish Chandra, Colin S. Gordon, Jean-Baptiste Jeannin, Cole Schlesinger, Manu Sridharan, Frank Tip, and Youngil Choi. 2016. Type Inference for Static Compilation of JavaScript. In Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA 2016). ACM, New York, NY, USA, 410–429. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Avik Chaudhuri, Panagiotis Vekris, Sam Goldman, Marshall Roch, and Gabriel Levi. 2017. Fast and Precise Type Checking for JavaScript (Extended version). https://arxiv.org/abs/1708.08021 . (2017).Google ScholarGoogle Scholar
  11. Wontae Choi, Satish Chandra, George C. Necula, and Koushik Sen. 2015. SJS: A Type System for JavaScript with Fixed Object Layout. In SAS (Lecture Notes in Computer Science), Vol. 9291. Springer, 181–198. Google ScholarGoogle ScholarCross RefCross Ref
  12. Ravi Chugh, David Herman, and Ranjit Jhala. 2012. Dependent Types for JavaScript. In Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA ’12). ACM, New York, NY, USA, 587–606. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Closure. 2009. Closure Compiler. https://developers.google.com/closure/compiler/ . (2009). Accessed: 2016-11-15.Google ScholarGoogle Scholar
  14. Yann Collet. 2011. LZ4-Extremely fast compression. (2011). https://github.com/lz4/lz4 .Google ScholarGoogle Scholar
  15. Dart. 2011. Dart Language Specification. https://www.dartlang.org/guides/language/spec . (2011). Accessed: 2016-11-15.Google ScholarGoogle Scholar
  16. Stephen Dolan and Alan Mycroft. 2017. Polymorphism, Subtyping, and Type Inference in MLsub. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages - POPL 2017. ACM Press, New York, New York, USA, 60–72.Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Jonathan Eifrig, Scott Smith, and Valery Trifonov. 1995. Sound Polymorphic Type Inference for Objects. SIGPLAN Not. 30, 10 (Oct. 1995), 169–184. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Manuel Fähndrich and Alex Aiken. 1996. Making Set-Constraint Program Analyses Scale. Technical Report. Berkeley, CA, USA.Google ScholarGoogle Scholar
  19. Manuel Fähndrich, Jeffrey S. Foster, Zhendong Su, and Alexander Aiken. 1998. Partial Online Cycle Elimination in Inclusion Constraint Graphs. In Proceedings of the ACM SIGPLAN 1998 Conference on Programming Language Design and Implementation (PLDI ’98). ACM, New York, NY, USA, 85–96. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Cormac Flanagan. 1997. Effective Static Debugging via Componential Set-Based Analysis. Ph.D. Dissertation. Rice University.Google ScholarGoogle Scholar
  21. Cormac Flanagan and Matthias Felleisen. 1999. Componential Set-based Analysis. ACM Trans. Program. Lang. Syst. 21, 2 (March 1999), 370–416. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Alain Frisch, Giuseppe Castagna, and Véronique Benzaken. 2008. Semantic Subtyping: Dealing Set-theoretically with Function, Union, Intersection, and Negation Types. J. ACM 55, 4, Article 19 (Sept. 2008), 64 pages.Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Arjun Guha, Claudiu Saftoiu, and Shriram Krishnamurthi. 2011. Typing Local Control and State Using Flow Analysis. In Proceedings of the 20th European Conference on Programming Languages and Systems: Part of the Joint European Conferences on Theory and Practice of Software (ESOP’11/ETAPS’11). Springer-Verlag, Berlin, Heidelberg, 256–275. Google ScholarGoogle ScholarCross RefCross Ref
  24. Hack. 2014. Hack Language Specification. https://github.com/hhvm/hack-langspec . (2014). Accessed: 2016-11-15.Google ScholarGoogle Scholar
  25. Thomas S. Heinze, Anders Møller, and Fabio Strocco. 2016. Type Safety Analysis for Dart. In Proceedings of the 12th Symposium on Dynamic Languages (DLS 2016). ACM, New York, NY, USA, 1–12. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Simon Holm Jensen, Magnus Madsen, and Anders Møller. 2011. Modeling the HTML DOM and Browser API in Static Analysis of JavaScript Web Applications. In Proceedings of the 19th ACM SIGSOFT Symposium and the 13th European Conference on Foundations of Software Engineering (ESEC/FSE ’11). ACM, New York, NY, USA, 59–69. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Simon Holm Jensen, Anders Møller, and Peter Thiemann. 2009. Type analysis for JavaScript. Lecture Notes in Computer Science (including subseries Lecture Notes in Artificial Intelligence and Lecture Notes in Bioinformatics) 5673 LNCS (2009), 238–255.Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Simon Holm Jensen, Anders Møller, and Peter Thiemann. 2010. Interprocedural Analysis with Lazy Propagation. In Proceedings of the 17th International Conference on Static Analysis (SAS’10). Springer-Verlag, Berlin, Heidelberg, 320–339. Google ScholarGoogle ScholarCross RefCross Ref
  29. Vineeth Kashyap, Kyle Dewey, Ethan A. Kuefner, John Wagner, Kevin Gibbons, John Sarracino, Ben Wiedermann, and Ben Hardekopf. 2014. JSAI: A Static Analysis Platform for JavaScript. In Proceedings of the 22Nd ACM SIGSOFT International Symposium on Foundations of Software Engineering (FSE 2014). ACM, New York, NY, USA, 121–132.Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Hongki Lee, Sooncheol Won, Joonho Jin, Junhee Cho, and Sukyoung Ryu. 2012. SAFE: Formal specification and implementation of a scalable analysis framework for ECMAScript. In International Workshop on Foundations of Object-Oriented Languages (FOOL), Vol. 10.Google ScholarGoogle Scholar
  31. Benjamin S. Lerner, Joe Gibbs Politz, Arjun Guha, and Shriram Krishnamurthi. 2013. TeJaS: Retrofitting Type Systems for JavaScript. Proceedings of the 9th symposium on Dynamic languages - DLS ’13, 1–16.Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Changhee Park and Sukyoung Ryu. 2015. Scalable and Precise Static Analysis of JavaScript Applications via LoopSensitivity. In 29th European Conference on Object-Oriented Programming (ECOOP 2015) (Leibniz International Proceedings in Informatics (LIPIcs)), John Tang Boyland (Ed.), Vol. 37. Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany, 735–756.Google ScholarGoogle Scholar
  33. François Pottier. 1998. Type Inference in the Presence of Subtyping: from Theory to Practice. Research Report 3483. INRIA. http://hal.inria.fr/docs/00/07/32/05/PDF/RR-3483.pdfGoogle ScholarGoogle Scholar
  34. François Pottier. 2001. Simplifying Subtyping Constraints: a Theory. Information & Computation 170, 2 (Nov. 2001), 153–183.Google ScholarGoogle Scholar
  35. Aseem Rastogi, Nikhil Swamy, Cédric Fournet, Gavin Bierman, and Panagiotis Vekris. 2015. Safe & Efficient Gradual Typing for TypeScript. In Proceedings of the 42Nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’15). ACM, New York, NY, USA, 167–180.Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. Patrick M. Rondon, Ming Kawaguci, and Ranjit Jhala. 2008. Liquid Types. In Proceedings of the 29th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’08). ACM, New York, NY, USA, 159–169. Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Frederick Smith, David Walker, and J. Gregory Morrisett. 2000. Alias Types. In Proceedings of the 9th European Symposium on Programming Languages and Systems (ESOP ’00). Springer-Verlag, London, UK, UK, 366–381. Google ScholarGoogle ScholarCross RefCross Ref
  38. Zhendong Su, Alexander Aiken, Joachim Niehren, Tim Priesnitz, and Ralf Treinen. 2002. The First-order Theory of Subtyping Constraints. In Proceedings of the 29th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’02). ACM, New York, NY, USA, 203–216. Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Zhendong Su, Manuel Fähndrich, and Alexander Aiken. 2000. Projection Merging: Reducing Redundancies in Inclusion Constraint Graphs. In Proceedings of the 27th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’00). ACM, New York, NY, USA, 81–95. Google ScholarGoogle ScholarDigital LibraryDigital Library
  40. Peter Thiemann. 2005. Towards a Type System for Analyzing Javascript Programs. In Proceedings of the 14th European Conference on Programming Languages and Systems (ESOP’05). Springer-Verlag, Berlin, Heidelberg, 408–422. Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Sam Tobin-Hochstadt and Matthias Felleisen. 2008. The Design and Implementation of Typed Scheme. In Proceedings of the 35th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’08). ACM, New York, NY, USA, 395–406. Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. Valery Trifonov and Scott Smith. 1996. Subtyping Constrained Types. SAS ’96: Proceedings of the 3rd International Symposium on Static Analysis (1996), 349–365.Google ScholarGoogle Scholar
  43. TypeScript. 2012. TypeScript Design Goals. https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals . (2012). Accessed: 2016-11-15.Google ScholarGoogle Scholar
  44. Dimitris Vardoulakis. 2012. CFA2: Pushdown Flow Analysis for Higher-Order Languages. Ph.D. Dissertation. Northeastern University.Google ScholarGoogle Scholar
  45. Panagiotis Vekris, Benjamin Cosman, and Ranjit Jhala. 2016. Refinement Types for TypeScript. In Proceedings of the 37th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’16). ACM, New York, NY, USA, 310–325. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. A.K. Wright and M. Felleisen. 1994. A Syntactic Approach to Type Soundness. Inf. Comput. 115, 1 (Nov. 1994), 38–94. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Fast and precise type checking for JavaScript

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!