DataDog / datadog-static-analyzer

Datadog Static Analyzer
https://docs.datadoghq.com/static_analysis/
Apache License 2.0
100 stars 12 forks source link

[STAL-1960] Implement compatibility layer with Stella for QueryMatch. #400

Closed jasonforal closed 3 months ago

jasonforal commented 4 months ago

What problem are you trying to solve?

The stella library attaches "captures", "capturesList", and "context" to the query match object. As a TypeScript interface, this would be

interface QueryMatch {
    captures: Record<string, TreeSitterNode>,
    capturesList: Record<string, TreeSitterNode[]>,
    context: {
        arguments: Record<string, string>,
        code: string,
        filename: string,
    }
}

And thus, current rules have syntax like:

function visit(query, filename, code) {
    const capture = query.captures.cap_name;
    const fileContents = query.context.code;
    const filename = query.context.filename;
    // ...etc
}

This is a different JavaScript API than what we are using for ddsa, and we want to provide a compatibility layer to ease the transition/refactoring of rules to the new ddsa API.

What is your solution?

Earlier, we partially addressed the object-style access of captures with with this commit in #393.

This PR furthers that effort by fully implementing the Stella API listed in the "Problem" section by using JavaScript Proxies.

Benefits of using Proxy:

It's worth noting that this compatibility layer necessarily introduces a performance overhead. To avoid this, rules should be refactored use the native ddsa API.

Alternatives considered

What the reviewer should know

jasonforal commented 4 months ago

Going to force push this to pro-actively handle merge conflicts, assuming https://github.com/DataDog/datadog-static-analyzer/pull/399 lands.

No significant changes with the push (rebase + some nits)