mcpField
A
ast-grep-code-analysis

ast-grep-code-analysis

github.com/zenobi-usTesting & quality6860 installsUpdated today
Repository

About ast-grep-code-analysis

A code-analysis skill that uses ast-grep structural patterns to find issues across files. It guides systematic security, performance, and code-structure reviews, particularly in complex or large codebases.

Key features

  1. 01Match code using AST patterns
  2. 02Analyze security-related code patterns
  3. 03Check performance patterns such as React hooks
  4. 04Detect structural issues such as deep nesting
  5. 05Run rule sets and generate JSON reports

Use cases

  1. 01Search a large codebase for hardcoded secrets
  2. 02Review React code for hook dependency patterns
  3. 03Find deeply nested control flow across files

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/zenobi-us/dotfiles --skill ast-grep-code-analysis

After installing, type/ast-grep-code-analysisin Claude Code to confirm it loaded.

Details

From the project README

AST-Grep Code Analysis

Note

This skill requires that as-grep is installed and configured in your development environment. If it's not installed we can use mise -g --pin ast-grep

Overview

AST-Grep Code Analysis uses Abstract Syntax Tree pattern matching to systematically identify code issues, replacing manual line-by-line inspection with structural pattern recognition.

Core principle: Code structure reveals more than surface reading - AST patterns expose hidden relationships, security vulnerabilities, and architectural issues that manual inspection misses.

When to Use

digraph when_to_use {
    "Need to analyze code?" [shape=diamond];
    "Complex/nested structure?" [shape=diamond];
    "Security review needed?" [shape=diamond];
    "Performance analysis?" [shape=diamond];
    "Use ast-grep patterns" [shape=box];
    "Manual review sufficient" [shape=box];

    "Need to analyze code?" -> "Complex/nested structure?" [label="yes"];
    "Complex/nested structure?" -> "Security review needed?" [label="yes"];
    "Security review needed?" -> "Performance analysis?" [label="yes"];
    "Performance analysis?" -> "Use ast-grep patterns" [label="yes"];
    "Complex/nested structure?" -> "Manual review sufficient" [label="no"];
    "Security review needed?" -> "Manual review sufficient" [label="no"];
    "Performance analysis?" -> "Manual review sufficient" [label="no"];
}

Use when:

  • Code has nested functions, complex control flow, or multiple abstraction layers
  • Security review required (authentication, authorization, data handling)
  • Performance analysis needed (React hooks, loops, async patterns)
  • Large codebase where manual inspection is impractical
  • Need to identify patterns across multiple files
  • Time pressure requires systematic approach over ad-hoc analysis

Do NOT use when:

  • Simple, straightforward code (< 50 lines)
  • Single-file utilities with obvious structure
  • When quick glance is sufficient for the task

Core Pattern

Before (Manual Inspection):

// Agent manually reads line by line
if (data[i].admin) {
  userObj.token = generateToken(data[i].id); // "This looks insecure"
}

After (AST Pattern Matching):

# ast-grep rule: insecure-token-generation
rule:
  pattern: |
    function $FUNC($ARGS) {
      const secret = $SECRET;
      return btoa(JSON.stringify($PAYLOAD) + '.' + $SECRET);
    }
  meta:
    severity: ERROR
    message: "Hardcoded secret in token generation"

Quick Reference

Analysis TypeAST Pattern FocusCommon Issues Found
SecurityString literals in crypto functionsHardcoded secrets, weak encryption
PerformanceReact hooks dependenciesInfinite re-renders, memory leaks
StructureFunction nesting depthComplex control flow, maintainability
Data FlowVariable assignments and usageUnused variables, implicit dependencies

Implementation

Installation and Setup
# Install ast-grep
npm install -g @ast-grep/cli

# Initialize configuration
ast-grep init

# Create rules directory
mkdir -p sg-rules/security sg-rules/performance sg-rules/structure
Essential Security Patterns

Hardcoded Secrets Detection:

# sg-rules/security/hardcoded-secrets.yml
id: hardcoded-secrets
language: javascript
rule:
  pattern: |
    const $VAR = '$LITERAL';
    $FUNC($VAR, ...)
  meta:
    severity: ERROR
    message: "Potential hardcoded secret detected"

Insecure Token Generation:

# sg-rules/security/insecure-tokens.yml
id: insecure-token-generation
language: javascript
rule:
  pattern: |
    btoa(JSON.stringify($OBJ) + '.' + $SECRET)
  meta:
    severity: ERROR
    message: "Insecure token generation using base64"
Performance Pattern Detection

React Hook Dependencies:

# sg-rules/performance/react-hook-deps.yml
id: react-hook-dependency-array
language: typescript
rule:
  pattern: |
    useEffect(() => {
      $BODY
    }, [$FUNC])
  meta:
    severity: WARNING
    message: "Function dependency in useEffect may cause infinite re-renders"

Missing useCallback Optimization:

# sg-rules/performance/missing-use-callback.yml
id: missing-use-callback
language: typescript
rule:
  pattern: |
    const $FUNC = ($ARGS) => {
      $BODY
    };
  inside:
    kind: function_declaration
    has:
      kind: arrow_function
  meta:
    severity: INFO
    message: "Consider wrapping function in useCallback for optimization"
Structural Analysis Patterns

Deep Nesting Detection:

# sg-rules/structure/deep-nesting.yml
id: deep-nesting
language: javascript
rule:
  any:
    - pattern: |
        if ($COND1) {
          if ($COND2) {
            if ($COND3) {
              $BODY
            }
          }
        }
    - pattern: |
        for ($INIT) {
          for ($INIT2) {
            for ($INIT3) {
              $BODY
            }
          }
        }
  meta:
    severity: WARNING
    message: "Deep nesting detected - consider refactoring"
Running Analysis
# Run all security rules
ast-grep run -r sg-rules/security/

# Run performance analysis on React components
ast-grep run -r sg-rules/performance/ --include="*.tsx,*.jsx"

# Generate comprehensive report
ast-grep run -r sg-rules/ --format=json > analysis-report.json

# Interactive analysis
ast-grep run -r sg-rules/ --interactive

Common Mistakes

MistakeWhy It HappensFix
Too generic patternsTrying to catch everythingFocus on specific, high-impact patterns
Missing contextPatterns don't consider surrounding codeUse inside and has constraints
False positivesOverly broad matchingAdd negative constraints with not
Language-specific assumptionsJavaScript patterns applied to TypeScriptCreate separate rules per language
No severity prioritizationAll issues marked as errorUse appropriate severity levels

Real-World Impact

Before AST Analysis:

  • Manual code review: 2-3 hours for medium codebase
  • Missed security vulnerabilities: 40-60%
  • Inconsistent analysis between reviewers
  • No systematic approach to pattern detection

After AST Analysis:

  • Automated pattern detection: 5-10 minutes
  • Security vulnerability detection: 90%+
  • Consistent, repeatable analysis
  • Comprehensive coverage of known anti-patterns

Example Results:

$ ast-grep run -r sg-rules/
src/components/UserProfile.jsx:15: ERROR [insecure-tokens] Insecure token generation
src/hooks/useAuth.js:8: ERROR [hardcoded-secrets] Potential hardcoded secret
src/components/UserProfile.jsx:23: WARNING [react-hook-deps] Function dependency may cause re-renders
src/utils/processData.js:45: WARNING [deep-nesting] Deep nesting detected

Found 4 issues (2 errors, 2 warnings)

Integration Workflow

  1. Setup: Create rule sets for security, performance, structure
  2. Baseline: Run analysis on existing codebase to establish patterns
  3. Iterate: Refine rules based on false positives/negatives
  4. Automate: Integrate into CI/CD pipeline for continuous analysis
  5. Monitor: Track issue reduction over time

Required Background: Understanding of AST concepts, pattern matching, and code structure analysis. AST patterns reveal what manual inspection misses - systematic, comprehensive, and repeatable code analysis.

Comments

Markdown supported