# Learning how to listen: Automatically finding bug patterns in event-driven JavaScript APIs

**Authors:** Ellen Arteca, Max Schäfer, Frank Tip  
**arXiv:** [2107.13708](https://arxiv.org/abs/2107.13708)  
**Date:** 2021-07-29  
**Summary page:** https://www.summarizepaper.com/en/arxiv-id/2107.13708v3/

## Key points

- Event-driven programming is widely used in the JavaScript community for handling UI events and AJAX requests.
- Previous work by Madsen et al. presented a static analysis for detecting issues with event-driven programming, but it was not scalable.
- This study proposes a learning-based approach to address the problem of detecting dead listeners.
- The technique involves mining a large corpus of JavaScript code using static analysis to identify code snippets that register event listeners and then applying statistical modeling to identify anomalous patterns.
- The technique was evaluated on 127,531 open-source JavaScript code bases and successfully detected 75 anomalous listener-registration patterns with a precision of 90.9% and recall of 7.5%.
- In an additional experiment on 25 open-source projects, 30 issues were reported to project maintainers, with 7 confirmed as bugs.
- A qualitative study was conducted on finding bugs in open-source projects by examining occurrences of listener-registration pairs classified as anomalous by the statistical analysis and manually analyzing the corresponding code snippets.
- The ease of determining whether a listener registration is actually a bug depends on the complexity of the access path.
- For complex access paths involving interprocedural flow, it may be challenging to determine emitted events accurately; however developers can leverage known reasons for false positives such as explicit emission checks or type checks before registering listeners.
- Very long access paths (defined as having at least five property reads, method calls, or function parameters separating them from the initial package import) were excluded from the experiment due to their time-intensive and complicated nature for manual analysis.
- Overall, this research demonstrates that a learning-based approach can effectively detect event-handling bugs in JavaScript code and provides valuable insights for improving API usage in event-driven programming scenarios.

## Summary

Event-driven programming is widely used in the JavaScript community for handling UI events and AJAX requests. However, this approach can lead to issues such as lost events or dead listeners. Previous work by Madsen et al. presented a static analysis for detecting these problems, but it was not scalable due to exponential context requirements. To address the problem of detecting dead listeners, this study proposes a learning-based approach. It involves mining a large corpus of JavaScript code using static analysis to identify code snippets that register event listeners and then applying statistical modeling to identify anomalous patterns that indicate incorrect API usage. The technique was evaluated on 127,531 open-source JavaScript code bases and successfully detected 75 anomalous listener-registration patterns with a precision of 90.9% and recall of 7.5%. In an additional experiment on 25 open-source projects, 30 issues were reported to project maintainers, with 7 confirmed as bugs. To assess the usefulness of the approach, a qualitative study was conducted on finding bugs in open-source projects. The researchers examined 100 occurrences of listener-registration pairs classified as anomalous by the statistical analysis and manually analyzed the corresponding code snippets. The ease of determining whether a listener registration is actually a bug depends on the complexity of the access path. For complex access paths involving interprocedural flow, it may be challenging to determine emitted events accurately; however developers can leverage known reasons for false positives such as explicit emission checks or type checks before registering listeners. For very long access paths (defined as having at least five property reads, method calls, or function parameters separating them from the initial package import), manual analysis would be time intensive and complicated to explain to developers; therefore these cases were excluded from the experiment. Overall, this research demonstrates that a learning-based approach can effectively detect event-handling bugs in JavaScript code and provides valuable insights for improving API usage in event-driven programming scenarios.
