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
- Variables
- func FindConfigFile(dir string) (string, bool)
- func HasComment(src string) bool
- func NullReplacement(op string) (string, bool)
- type Analyzer
- type Config
- type Diagnostic
- type Env
- type IgnoreLine
- type LineState
- type NullComparisonParts
- type Pass
- type Severity
- type SuggestedFix
- type Suppressions
- type TableMetadataLookup
Constants ¶
const ConfigFileName = ".bqls.yaml"
ConfigFileName is the settings file shared by the CLI and the language server.
Variables ¶
var Analyzers = []*Analyzer{ NullComparison, InNull, UnusedCTE, UnusedDeclare, PartitionFilter, OrderByInSubquery, WildcardTableSuffix, }
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 OrderByInSubquery = &Analyzer{ Name: "order-by-in-subquery", Doc: "reports ORDER BY in a subquery, CTE or derived table whose order cannot reach the result", DefaultEnabled: true, Run: runOrderByInSubquery, }
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, }
var UnusedDeclare = &Analyzer{ Name: "unused-declare", Doc: "reports DECLARE variables that are never referenced", DefaultEnabled: true, Run: runUnusedDeclare, }
var WildcardTableSuffix = &Analyzer{ Name: "wildcard-table-suffix", Doc: "reports wildcard table queries that never filter on _TABLE_SUFFIX", DefaultEnabled: true, Run: runWildcardTableSuffix, }
Functions ¶
func FindConfigFile ¶ added in v0.8.1
FindConfigFile looks for ConfigFileName in dir and its parents, stopping at the repository root (a directory containing .git, which is a file in worktrees) so that a checkout never picks up settings from outside it.
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
// NewOptions returns a pointer to the rule's default options, which the
// configured options are decoded onto as JSON (unknown fields are
// rejected) and handed to Run as Pass.Options. Nil means the rule takes
// no options.
NewOptions func() any
Run func(*Pass)
}
Analyzer is a single lint rule.
type Config ¶
type Config struct {
Rules map[string]bool `json:"rules,omitempty"`
// Options holds each rule's options object without "enabled". It is
// decoded into the rule's NewOptions value only when the rule runs, so
// a config can be parsed without knowing the rules.
Options map[string]json.RawMessage `json:"-"`
}
Config is the user-facing lint settings. Each entry under "rules" is either a bool or an object with an optional "enabled" and the rule's options:
{"rules": {"null-comparison": false, "some-rule": {"enabled": true, "max": 3}}}
The Diagnose cache in package source compares configs with Equal, so update it when adding a field.
func LoadConfigFile ¶ added in v0.8.1
func ParseConfigFile ¶ added in v0.8.1
ParseConfigFile reads the "lint" section of a config file. Other top-level keys are left for other features.
func (Config) Clone ¶ added in v0.8.1
Clone returns a deep copy, so a caller can keep c while others mutate theirs.
func (Config) Enabled ¶
Enabled reports whether a is enabled, letting user settings override the default.
func (Config) Override ¶ added in v0.8.1
Override returns c with the settings in o applied on top. Options are merged key by key, so o can change one option without restating the rest. Neither c nor o is modified.
func (*Config) UnmarshalJSON ¶ added in v0.8.1
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
// Options is the value from Analyzer.NewOptions with the configured
// options applied, or nil when the analyzer takes none.
Options any
// 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.