lint

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lint provides a small analysis framework, modeled on golang.org/x/tools/go/analysis, for reporting style and correctness warnings on SQL that already analyzes without errors.

Index

Constants

This section is empty.

Variables

Analyzers is every rule bqls ships; Config decides which of them run.

View Source
var InNull = &Analyzer{
	Name:           "in-null",
	Doc:            "reports `IN (NULL)` / `NOT IN (NULL)` lists of only NULLs, which always yield NULL",
	DefaultEnabled: true,
	Run:            runInNull,
}
View Source
var NullComparison = &Analyzer{
	Name:           "null-comparison",
	Doc:            "reports `= NULL` / `!= NULL` comparisons, which always yield NULL",
	DefaultEnabled: true,
	Run:            runNullComparison,
}
View Source
var PartitionFilter = &Analyzer{
	Name:           "partition-filter",
	Doc:            "reports queries on partitioned tables that never filter on the partition column",
	DefaultEnabled: true,
	Run:            runPartitionFilter,
}
View Source
var UnusedCTE = &Analyzer{
	Name:           "unused-cte",
	Doc:            "reports WITH entries that are never referenced",
	DefaultEnabled: true,
	Run:            runUnusedCTE,
}

Functions

func HasComment

func HasComment(src string) bool

HasComment reports whether src contains a comment. Comment markers inside string literals and quoted identifiers are not comments.

func NullReplacement

func NullReplacement(op string) (string, bool)

NullReplacement returns the IS [NOT] NULL form for a written comparison operator.

Types

type Analyzer

type Analyzer struct {
	// Name is the rule identifier used in settings, suppression comments,
	// and as the LSP diagnostic code.
	Name           string
	Doc            string
	DefaultEnabled bool
	Run            func(*Pass)
}

Analyzer is a single lint rule.

type Config

type Config struct {
	Rules map[string]bool `json:"rules,omitempty"`
}

Config is the user-facing lint settings, e.g. {"rules": {"null-comparison": false}}. The Diagnose cache in package source keys on these fields, so update it when adding one.

func (Config) Enabled

func (c Config) Enabled(a *Analyzer) bool

Enabled reports whether a is enabled, letting user settings override the default.

type Diagnostic

type Diagnostic struct {
	Rule           string
	Range          lsp.Range
	Message        string
	Severity       Severity
	SuggestedFixes []SuggestedFix
}

func Run

func Run(ctx context.Context, f file.ParsedFile, cfg Config, env Env, analyzers ...*Analyzer) []Diagnostic

Run executes the enabled analyzers over f and returns unsuppressed diagnostics. Files that failed analysis are skipped because rules rely on a complete resolved AST and would otherwise produce noise on top of real errors.

type Env

type Env struct {
	LookupTable TableMetadataLookup
	DryRun      *bq.JobStatus
}

Env carries optional external inputs to analyzers. Every field may be nil.

type IgnoreLine

type IgnoreLine struct {
	// Rules is empty when every rule is ignored.
	Rules []string
	// RulesEnd is the byte offset in the line where another rule name can be
	// appended.
	RulesEnd int
}

IgnoreLine is a line consisting only of a `bqls:ignore` comment.

func ParseIgnoreLine

func ParseIgnoreLine(line string) (IgnoreLine, bool)

ParseIgnoreLine reports whether line is a standalone `bqls:ignore` directive, which ParseSuppressions applies to the following line.

type LineState

type LineState struct {
	// StartsInCode is false when the line begins inside a string literal or
	// block comment opened on an earlier line.
	StartsInCode bool
	// EndsInCode is false when the line ends inside a string literal or a
	// comment.
	EndsInCode bool
}

LineState tells whether a line's edges are outside string literals and comments, i.e. whether text inserted there would be read as code.

func LineStates

func LineStates(src string) []LineState

LineStates returns the LineState of every line in src.

type NullComparisonParts

type NullComparisonParts struct {
	Other string
	Op    string
}

NullComparisonParts is a `<other> <op> <null>` comparison split from its source text.

func SplitNullComparison

func SplitNullComparison(cmpSrc, nullSrc string, nullOnRight bool) (NullComparisonParts, bool)

SplitNullComparison slices the non-NULL operand and the operator out of the comparison's own source text rather than using the operand's location, because locations exclude parentheses the user wrote around either side.

type Pass

type Pass struct {
	Context  context.Context
	Analyzer *Analyzer
	File     file.ParsedFile
	// LookupTable is nil-safe to call; it returns (nil, nil) when no lookup is configured.
	LookupTable TableMetadataLookup
	DryRun      *bq.JobStatus
	// contains filtered or unexported fields
}

Pass is the input to a single Analyzer run over one file.

func (*Pass) Report

func (p *Pass) Report(d Diagnostic)

Report records a diagnostic. Rule defaults to the analyzer's name, and any severity other than Information is reported as Warning.

type Severity

type Severity = lsp.DiagnosticSeverity

Severity is restricted to non-error levels so that lint findings never look like analysis failures.

const (
	SeverityWarning     Severity = lsp.Warning
	SeverityInformation Severity = lsp.Information
)

type SuggestedFix

type SuggestedFix struct {
	Title string
	Edits []lsp.TextEdit
}

func LiteralNullComparisonFix

func LiteralNullComparisonFix(f file.ParsedFile, e file.Error) (SuggestedFix, bool)

LiteralNullComparisonFix builds an IS [NOT] NULL rewrite for GoogleSQL's "Operands of = cannot be literal NULL" analysis error. Lint never runs on files with errors, so this fix hangs off the error diagnostic instead.

type Suppressions

type Suppressions map[int]suppression

Suppressions maps a 0-based line number to the rules suppressed on it.

func ParseSuppressions

func ParseSuppressions(src string) Suppressions

ParseSuppressions scans src for `bqls:ignore` comment directives.

A directive in a comment that follows code on the same line suppresses that line; a directive in a comment alone on its line suppresses the next line. Without rule names, every rule is suppressed.

func (Suppressions) Suppressed

func (s Suppressions) Suppressed(rule string, line int) bool

Suppressed reports whether diagnostics of rule starting on line are suppressed.

type TableMetadataLookup

type TableMetadataLookup func(ctx context.Context, path string) (*bq.TableMetadata, error)

TableMetadataLookup fetches metadata for a `project.dataset.table` path.

Jump to

Keyboard shortcuts

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