Abstract
The recent use of 'Big Code' with state-of-the-art deep learning methods offers promising avenues to ease program source code writing and correction. As a first step towards automatic code repair, we implemented a graph neural network model that predicts token types for Javascript programs. The predictions achieve an accuracy above $90\%$, which improves on previous similar work.
Introduction
JavaScript is a dynamically typed language, offering developers significant flexibility but lacking the static type-checking safety found in languages like Java or C#. This often results in runtime errors that are difficult to catch during the development phase. While the "Big Code" movement—applying machine learning to massive source code repositories—has shown promise in code completion and defect detection, inferring types in JavaScript remains a significant challenge. Previous attempts often relied on manual feature engineering or focused on narrow subtasks. This research aims to create a high-accuracy, agnostic model that predicts a wide range of JavaScript types by leveraging the structural and semantic context of the code.
Methodology
The core innovation of this approach lies in representing source code as a complex graph rather than a simple sequence of text tokens.
Graph Representation
Nodes in the graph are derived from the Abstract Syntax Tree (AST). The model utilizes multi-modal edges, incorporating both syntactic edges (parent-child relationships in the AST) and semantic edges (such as "defined-by" or "next-use" relationships). This allows the network to understand how data flows through the program.
Neural Architectures
Each node is assigned a feature vector based on its AST type, properties, and string value. String values are embedded using a Gated Recurrent Unit (GRU) to capture character-level patterns. The study compared two Graph Neural Network (GNN) architectures:
- Graph Convolutional Networks (GCN): These aggregate messages from a node's immediate neighbors.
- Gated Graph Neural Networks (GGNN): these use a GRU cell to update node states over several iterative message-passing steps.
To facilitate global context sharing, a "master node" was introduced, connected to every other node in the graph.
Key Insights: Structure Over Names
A striking finding of this research is that the model remains effective even when variable names are removed or obfuscated. By analyzing the structural context within the graph, the GNN can identify types based on usage patterns rather than relying on identifier strings.
By including semantic edges like "next-use," the GNN can track a variable across different parts of a function, mimicking the way a human developer traces a variable's lifecycle to understand its purpose.
Results
The models were evaluated using a large-scale dataset of JavaScript files gathered from GitHub, with ground truth established through runtime analysis of test suites.
The results represent a significant improvement over previous baselines. The GGNN model achieved a 90.79% F1-score on the test set, while the GCN achieved 87.25%. Both models outperformed the previous state-of-the-art baseline of 81% reported in earlier literature. The GGNN's iterative message-passing scheme proved particularly well-suited for the interconnected nature of source code.
Conclusion and Future Work
While the current model achieves high accuracy across eight general JavaScript types, it does not yet distinguish between specific user-defined classes or complex object shapes. Future work will focus on expanding the label set to include more specific types (e.g., RegExp or Date) and investigating more granular semantic relationships. The ultimate goal is to integrate these predictions into tools that automatically notify programmers of type mismatches during the coding process.
Summary updated