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 ¶
var Analyzers = []*Analyzer{ NullComparison, InNull, UnusedCTE, PartitionFilter, }
Analyzers is every rule bqls ships; Config decides which of them run.
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, }
var NullComparison = &Analyzer{ Name: "null-comparison", Doc: "reports `= NULL` / `!= NULL` comparisons, which always yield NULL", DefaultEnabled: true, Run: runNullComparison, }
var PartitionFilter = &Analyzer{ Name: "partition-filter", Doc: "reports queries on partitioned tables that never filter on the partition column", DefaultEnabled: true, Run: runPartitionFilter, }
var UnusedCTE = &Analyzer{ Name: "unused-cte", Doc: "reports WITH entries that are never referenced", DefaultEnabled: true, Run: runUnusedCTE, }
Functions ¶
func HasComment ¶
HasComment reports whether src contains a comment. Comment markers inside string literals and quoted identifiers are not comments.
func NullReplacement ¶
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 ¶
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.
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 ¶
LineStates returns the LineState of every line in src.
type NullComparisonParts ¶
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.
type SuggestedFix ¶
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 ¶
TableMetadataLookup fetches metadata for a `project.dataset.table` path.