scope

package
v0.7.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package scope provides context scope detection for functions.

Overview

The scope package identifies functions that have context.Context parameters and tracks the parameter names for use in error messages.

Scope Detection

A function has "context scope" if it declares a context.Context parameter or a configured carrier type parameter:

// Has context scope - ctx is tracked
func worker(ctx context.Context) {
    // ctx available: ["ctx"]
}

// Has context scope - multiple names tracked
func handler(reqCtx, bgCtx context.Context) {
    // ctx available: ["reqCtx", "bgCtx"]
}

// No context scope - no context parameter
func helper() {
    // Not analyzed for context propagation
}

Building Scope Map

Use Build to create a scope map for all functions in a package:

funcScopes := scope.Build(pass, inspector, carriers)

The resulting Map maps AST nodes (FuncDecl, FuncLit) to their Scope:

type Map map[ast.Node]*Scope

type Scope struct {
    CtxNames []string  // Context parameter names
}

Finding Enclosing Scope

During analysis, use FindEnclosing to find the context scope for a node:

inspector.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) bool {
    scope := scope.FindEnclosing(funcScopes, stack)
    if scope == nil {
        return true  // No context in scope, skip analysis
    }
    // Analyze node with context available
    ctxName := scope.CtxName()  // Returns first name or "ctx"
    ...
})

Nested Functions

Nested functions inherit context scope from their enclosing function:

func outer(ctx context.Context) {
    // outer has scope with ["ctx"]

    inner := func() {
        // inner does NOT have its own scope
        // FindEnclosing returns outer's scope
    }
}

However, if a nested function has its own context parameter, it has its own scope:

func outer(outerCtx context.Context) {
    inner := func(innerCtx context.Context) {
        // inner has its own scope with ["innerCtx"]
    }
}

Package scope provides context scope detection for functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map map[ast.Node]*Scope

Map maps AST nodes to their scopes.

func Build

func Build(pass *analysis.Pass, insp *inspector.Inspector, carriers []carrier.Carrier) Map

Build identifies functions with context parameters.

type Scope

type Scope struct {
	CtxNames []string
}

Scope holds context information for a function scope.

func FindEnclosing

func FindEnclosing(scopes Map, stack []ast.Node) *Scope

FindEnclosing finds the closest enclosing function with a context parameter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL