一句话:提出 Scotty,将 DefinitelyTyped 的类型声明转为契约并绑定到库与测试套件边界,通过运行测试检测类型与 JavaScript 代码的不匹配。
DefinitelyTyped 为数千个 JS 库提供类型声明,但类型与代码无形式化关联,且随时间容易失配。本文提出 Scotty:将每个包的类型转为契约,装在库与测试套件边界上,运行测试即可暴露类型与 JavaScript 代码的不匹配。该工具强调自动化与通用性,主动牺牲完备性以避免假阳性。Scotty 能处理 DefinitelyTyped 上 8006 个包中的约 26%(代码可用且测试通过的包中占 61%),揭示大量错误并促成 100 个被接受的修复 PR,证明该思路对长期维护的价值。作者还总结了 JavaScript 生态中语言语义之外因素的重要性,并发现程序员有时偏好错误类型,为 TypeScript 改进提供了方向。
The DefinitelyTyped repository hosts type declarations for thousands of JavaScript libraries. Given the lack of formal connection between the types and the corresponding code, a natural question is are the types right? An equally important question, as DefinitelyTyped and the libraries it supports change over time, is how can we keep the types from becoming wrong? In this paper we offer Scotty, a tool that detects mismatches between the types and code in the Definitely-Typed repository. More specifically, Scotty checks each package by converting its types into contracts and installing the contracts on the boundary between the library and its test suite. Running the test suite in this environment can reveal mismatches between the types and the JavaScript code. As automation and generality are both essential if such a tool is going to remain useful in the long term, we focus on techniques that sacrifice completeness, instead preferring to avoid false positives. Scotty currently handles about 26% of the 8006 packages on DefinitelyTyped (61% of the packages whose code is available and whose test suite passes). Perhaps unsurprisingly, running the tests with these contracts in place revealed many errors in Definitely-Typed. More surprisingly, despite the inherent limitations of the techniques we use, this exercise led to one hundred accepted pull requests that fix errors in DefinitelyTyped, demonstrating the value of this approach for the long-term maintenance of DefinitelyTyped. It also revealed a number of lessons about working in the JavaScript ecosystem and how details beyond the semantics of the language can be surprisingly important. Best of all, it also revealed a few places where programmers preferred incorrect types, suggesting some avenues of research to improve TypeScript.
@article{paperbot1421,
title = {Highly illogical, Kirk: spotting type mismatches in the large despite broken contracts, unsound types, and too many linters},
author = {Joshua Hoeflich and Robert Bruce Findler and Manuel Serrano},
journal = {Proceedings of the ACM on Programming Languages},
volume = {6},
number = {OOPSLA2},
year = {2022},
doi = {10.1145/3563305}
}