Delphi-RAG-Lint

drag-lint help

Spring4D AST rules: resolve in loop, service locator, swallowed errors

Since: 1.64.0-alpha

Three dependency-injection rules on one parse tree: Resolve in a loop (ON), service locator (OFF), registration error swallowed (ON).

Read this to see what di-resolve-in-loop, di-service-locator and di-registration-error-swallowed report and which defaults they ship with.

How to run it

drag-lint lint <path> [--profile <name>] [--target-compiler <ver|auto>] [--rule <id>|help] [--disable id1,id2] [--rules-dir <dir>] [--json] [--labels] [--no-preprocess] [--library-db <lib.sqlite>] [--build-config <name>] [--no-budget]

Example:

drag-lint lint <file.pas> --rule di-resolve-in-loop

Full flag list: lint in the CLI reference.

drag-lint lint <path> [--profile <name>] [--target-compiler <ver|auto>] [--rule <id>|help] [--disable id1,id2] [--rules-dir <dir>] [--json] [--labels] [--no-preprocess] [--library-db <lib.sqlite>] [--build-config <name>] [--no-budget]

Example:

drag-lint lint <file.pas> --rule di-service-locator

Full flag list: lint in the CLI reference.

drag-lint lint <path> [--profile <name>] [--target-compiler <ver|auto>] [--rule <id>|help] [--disable id1,id2] [--rules-dir <dir>] [--json] [--labels] [--no-preprocess] [--library-db <lib.sqlite>] [--build-config <name>] [--no-budget]

Example:

drag-lint lint <file.pas> --rule di-registration-error-swallowed

Full flag list: lint in the CLI reference.

Where to click

No IDE surface: this feature runs from the CLI (see How to run it).

Details

Four lint rules over the Spring4D container facts of a project index (1.64, lane D1, PLAN-RULES-165 rows B1-B4). They read what index already stores -- di_bindings (fluent RegisterType<T>.Implements<I> chains), the di-resolve / di-unresolved references, the call graph and the type heritage -- so they need no re-parse. All four are project-wide (lint-all), category dependency-injection, ON by default.

Running the rules

drag-lint lint-all --db <project.sqlite>
drag-lint lint-all --db <project.sqlite> --rule di-registration-unreachable

Switch one off with "disabled": ["di-duplicate-registration"] in drag-lint-lint.json, or accept one site with a dl:ok <rule> review on the reported line.

Reachability

Three of the rules ask whether a routine ever runs. A routine is reached when a resolved call or a reference to its name leads, through any chain of callers, to code outside every routine (the program body, an initialization section, a DFM event binding, a declaration), or to a routine the framework calls by itself: virtual, override, dynamic, abstract, message, published, or named like a method of an interface. A by-name reference counts even when it is not a call (P := RegisterAll;), so a method pointer keeps its target reached. A walk that visits more than 400 routines answers "reached".

Without a program (.dpr) in the index the closure is not whole -- the caller and the registration may live in the application that uses the units -- so di-resolve-unregistered and di-registration-unreachable stay silent and every routine counts as reached.

di-resolve-unregistered (warning)

A Resolve<I> that the program reaches, for an I that no registration in the project names: no RegisterType<T>.Implements<I> / .As<I>, no RegisterInstance<I> / RegisterFactory<I> (any registration the index stores as di-unresolved counts for every type it names), and no self-registered class (RegisterType<TFoo> without Implements) implementing I. Spring4D raises when the line runs.

Not reported: TryResolve (it answers False by design); a resolve of a generic parameter of the enclosing routine or class (Resolve<I>, Resolve<IDataService<I>> inside TObjectFactory<I> -- a wildcard); a resolve in a routine nothing reaches (the ORM3 TPlanFactory overloads); a project that holds no registration at all (the container is configured outside it). Type texts compare without whitespace, case or unit qualifiers (DIIntf.IAlpha = IAlpha).

di-duplicate-registration (warning)

The same interface (and the same registration name -- Implements<I>('a') and Implements<I>('b') are different) registered twice by code that runs. ONE finding per pair of registering routines, however many interfaces they share; it sits on the later registration of the pair's first shared interface and names the earlier one and the count. A routine that registers the same interface twice is a pair with itself.

Not reported: a registration marked .AsDefault (the deliberate override); a registration in a routine nothing reaches -- that whole routine is di-registration-unreachable instead (the ORM3 SERVER shape: 265 interfaces registered in two units, one of them dead, gives 0 duplicates and 1 unreachable).

di-registration-unreachable (warning)

A routine that holds container registrations (fluent or not, including ones inside an anonymous method passed to a helper such as SafeRegister) and that nothing reaches: a dead container configuration whose registrations never run. Reported once per routine, on its implementation header, with the number of registrations. A routine called only from other unreached routines is unreached too.

Not reported: registrations in an initialization section or the program body; a routine reached through a method pointer, an override or a published method.

di-impl-not-implementing (error)

RegisterType<TFoo>.Implements<IBar> where neither TFoo nor any ancestor implements IBar; Spring4D raises ERegistrationException when the container is built. Interface inheritance counts (TFoo implementing IChild = interface(IBar) is accepted), and a generic interface matches by its base name.

Not reported: IBar not declared in the index (a library interface), TFoo not in the index or generic, or a heritage that passes through a class the index does not hold (other than the RTL roots TObject, TInterfacedObject, TComponent and friends, which cannot implement a project interface). This rule does not ask whether the registration runs.

Fixtures

tests\fixtures\v164\di-project\ (main, noreg, nodpr), runner tests\autotest\run_v164_di_project.ps1: every // FIRE <rule> line must report and every other line stay silent.

Spring4D AST rules (1.64)

Three more dependency-injection rules that read one parse tree (no index fact):

  • di-resolve-in-loop (hint, ON): a Resolve call inside a loop body.
  • di-service-locator (hint, OFF): a Resolve call outside the composition root (service-locator use).
  • di-registration-error-swallowed (warning, ON): a registration inside a try ... except whose handler swallows the exception. One finding per swallowing helper routine, not one per call site. Same-unit helpers only.
drag-lint lint <file.pas> --rule di-resolve-in-loop

di-captive-dependency (warning, 1.65)

A singleton registration (RegisterType<TFoo>.Implements<IFoo>.AsSingleton) whose implementation is injected with an interface that is registered only transient (the default) or AsSingletonPerThread. The container builds that dependency once, and the singleton keeps it for the life of the program; a per-thread one is then used from every thread that reaches the singleton. One finding per singleton registration, at the registration line, naming every captured member.

What counts as injected (what Spring4D would inject):

  • the constructor marked [Inject]; without one, the greediest constructor (most parameters of a registered type) of the nearest class in the heritage chain that declares a constructor;
  • every [Inject] field or property along the indexed heritage chain.

Not reported: a TFunc<IFoo>, Lazy<IFoo> or ILazy<IFoo> parameter (resolved on demand, the fix this rule suggests); a dependency of the same or a longer lifetime; an interface that is also registered as a singleton anywhere (a named one included); pooled and custom lifetimes (AsPooled, AsCustom); a transient or per-thread captor; a field without [Inject]; an implementation class not in the index, generic, or declared twice. Like di-impl-not-implementing, the rule does not ask whether the registration runs.

drag-lint lint-all --db <project.sqlite> --rule di-captive-dependency

Fixture tests\fixtures\v165\di-captive\main, runner tests\autotest\run_v165_di_captive.ps1.

di-resolve-in-hot-handler (hint, 1.65)

A container Resolve<I> / TryResolve<I> inside a handler that runs for every row or every paint, so the lookup repeats each time. Hot handlers are procedures that

  • are named after the event: the name ends in CalcFields, GetText, DrawCell (so CustomDrawCell too), Timer or Paint (Query1CalcFields, ViewCustomDrawCell, Timer1Timer, an overridden Paint), and are a method whose header is a procedure; or
  • are bound in the form's .dfm to OnCalcFields, OnGetText, OnDrawCell, OnCustomDrawCell, OnTimer or OnPaint, whatever their name (the binding is matched through the .dfm / .pas pair of the same base name).

One finding per Resolve, at the Resolve. Fix: resolve once in the constructor or FormCreate and keep the instance.

Not reported: a handler bound only to another event (OnClick), a Resolve in FormCreate, a function named GetText (a getter), a plain (non-method) procedure named DrawCell, a method that merely contains Timer in the middle of its name. An index without dfm_properties finds hot handlers by name only.

di-register-after-build (warning, 1.65)

A Register...<T> call on the container R that follows an R.Build in the same routine, in source order. The container is already built, so the registration is lost or raises. The receiver text is compared without case (GlobalContainer, a local C); a registration on another container, in another routine, or in the other branch of one if ... else is not reported. One finding per registration. Per-file, from the parse tree (lint <file.pas> works); the .dpr is not linted by per-file rules.

di-build-missing (warning, 1.65)

The project registers types with a Spring4D container (a fluent RegisterType<T>.Implements<I> chain in the index) and no call named Build exists anywhere in it. One finding for the project, on the first registration. A Build call counts whatever its receiver (the index does not type the container), except one the index resolved to a class that is no container (TReportBuilder.Build). Silent without a program in the index or without a registration. A Build inside a package the index does not hold is not seen: the rule can then be wrong, which is why it is a warning to review, not an error.

drag-lint lint-all --db <project.sqlite> --rule di-resolve-in-hot-handler,di-build-missing
drag-lint lint <file.pas> --rule di-register-after-build

Fixtures tests\fixtures\v165\di-ext\ (hot, build, nobuild, buildunit, noreg), runner tests\autotest\run_v165_D3b_di_ext.ps1.


All features | CLI reference | IDE menus