A lightweight Code Property Graph Generator
Note
This is the Node.js port of CodeSCI, providing essentially the same functionality.
Program analysis commonly relies on tools such as Joern and CodeQL to generate Code Property Graphs (CPGs). However, these tools are often heavyweight and require complex configurations, making them less user-friendly. In particular, they are difficult to scale for processing large-scale datasets in software engineering and machine learning research.
To address this limitation, we propose CodeSCI, a lightweight code property graph generator based on Tree-Sitter as science research infrastructure.
This project is under the development.
WELCOME TO PR!
Warning
The main difference lies in the underlying graph representation. The original Python version of CodeSCI relies on NetworkX for graph data structures, while the Node.js ecosystem currently lacks a unified and widely adopted graph analysis library comparable to NetworkX.
Therefore, we use Graphology as the graph representation layer, as it provides the closest alternative to NetworkX in the Node.js ecosystem.
As a result, the APIs for serialization, deserialization, and visualization differ slightly from those in the original Python implementation of CodeSCI. Please refer to the documentation for further details.
This npm package is distributed exclusively as an ESM module. It is written in JavaScript and provides TypeScript type definitions.
Install your package:
npm install codesci
pnpm add codesci # Or other package manager.Import the codesci library.
import { toAST, toCFG, toDFG } from 'codesci';
const sourceCode = `
#include <stdio.h>
int main() {
int a[] = {5,2,8,1,9}, n = 5;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-1-i; j++)
if (a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
for (int i = 0; i < n; i++) printf("%d ", [i]);
return 0;
}
`;
// Generate AST, CFG, and DFG - these are graphology objects
const ast = toAST({ code: sourceCode, language: "c" });
const cfg = toCFG({ code: sourceCode, language: "c" });
const dfg = toDFG({ code: sourceCode, language: "c" });
// You can now use ast, cfg, dfg
console.log('AST generated:', ast);
console.log('CFG generated:', cfg);
console.log('DFG generated:', dfg);Contributions are always welcome! We appreciate constructive contributions of all kinds, such as adding support for new programming languages, implementing additional graph types, or improving existing features.
This project is under the MIT license.
