Most online code snippets do not run. This means that developers looking to reuse code from online sources must manually find and fix errors. We present an approach for automatically evaluating and correcting errors in Node.js code snippets: Node Code Correction (NCC). NCC leverages the ability of the TypeScript compiler to generate errors and inform code corrections through the combination of TypeScript's built-in codefixes, our own targeted fixes, and deletion of erroneous lines. Compared to existing approaches using linters, our findings suggest that NCC is capable of detecting a larger number of errors per snippet and more error types, and it is more efficient at fixing snippets. We find that 73.7% of the code snippets in NPM documentation have errors; with the use of NCC's corrections, this number was reduced to 25.1%. Our evaluation confirms that the use of the TypeScript compiler to inform code corrections is a promising strategy to aid in the reuse of code snippets from online sources.
翻译:大多数在线代码片段无法运行。这意味着希望重用在线代码的开发者必须手动发现并修复错误。我们提出了一种自动评估和纠正Node.js代码片段错误的方法:Node代码纠正(NCC)。NCC利用TypeScript编译器生成错误的能力,通过结合TypeScript内置的代码修复、我们自己的定向修复以及删除错误行来提供代码纠正。与现有使用linter的方法相比,我们的研究表明NCC能够检测每个代码片段中更多的错误和更多的错误类型,并且在修复代码片段方面更高效。我们发现NPM文档中73.7%的代码片段存在错误;使用NCC的纠正后,这一比例降至25.1%。我们的评估证实,利用TypeScript编译器辅助代码纠正是促进在线代码片段重用的一种有前途的策略。