Absence and null: no-null-check-for-undefined, a JSON null kept, no-null at outside types #63
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/fb3-absence"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Round 3 of the 0.2.0 adoption feedback, area absence and
null(top-ten #1 and #9).Per feedback item
1. A rule mirroring
no-undefined-check-for-null: done. New ruleno-null-check-for-undefined, in the default set as an error (it reports only where the type is known to have nonull). It reports:=== null,!== null,== null,!= null;nullexpected by an assertion:node:assert/node:assert/strictand chaiequal,strictEqual,deepEqual, ... (on the module, imported by name,assert.strict.*), also at one element of an array or object literal (assert.deepEqual([a, b], ["paid", null]));isNull/isNotNull, and vitest/jesttoBe(null),toEqual(null),toStrictEqual(null),toBeNull(),.not.A value counts as "no
null" when its declared type has none: variables, parameters, destructuring, fields (inherited too, and class fields and parameter properties), return types of the project's functions and methods. It follows imports and re-exports (export *too), array elements,for...of, element callbacks (forEach((x) => ...)),Map#get,Array#find/at/pop,awaitof aPromise<Option<T>>,??,?:, and the packs' Option safe versions. The per-file part (null_check.rs) turns declarations into small shapes; the project pass evaluates them across files.Left alone: anything that may hold
nullor whose type isn't known. That covers DOM lookups (they keeptoBeNull()),unknown/any, packages' types, generics (first<T>(): Option<T>),ReturnType<...>, and conditional and mapped types.Decision on #61's check: the safe-version
=== nullreport moved fromno-nullinto the new rule (same message wording, Compare with undefined), so each message has one rule. Where the new rule reports a loose== null,no-null's report of thatnullis dropped, so there are no two reports for one mistake.Quick fixes: Compare with undefined, Use toBeUndefined(), Use toBeDefined() (for
.not.toBeNull()), chai Use isUndefined() / Use isDefined().2.
parseJson/validatekeeping a JSONnull: done, with types rather than an option.JsonNull: in a type, only anullfits it and the result keeps it.category: string | JsonNullrefuses a missing key;category?: string | JsonNullleaves it out;Option<T> | JsonNullgivesundefinedfor missing andnullfornull. In a dictionary, anullvalue keeps its key. Works infromFieldsandarbitrarytoo.RawJson: any JSON asJSON.parsegives it, everynullkept, so it comes back byte for byte. It is checked likeJsonValue(no NaN, class instances or cycles).nullbecomesundefined).jsonNullis now typedJsonNulland documented as the output-format and raw-JSON escape, not only for tests.JSON.stringifyof data holding it, or typed withJsonNullfields, isn't reported.{ nulls: "keep" }option:parseJson<JsonValue>(text, { nulls: "keep" })would hand backnulls that its type says can't be there. The type says it instead."category": null= every category, missing = refused), a canonical hash of settings withnulls, byte-identical round trips, and arbitrary round trips.3.
no-nullacceptsnull/?? nullwhere a package's or the platform's type is expected: done, except one case it can't see. This covers the return of a function or arrow declared with such a type (bound(p): SQLInputValue,params.map((p): SQLInputValue => p ?? null), asyncPromise<T>too) andpush/unshiftinto an array declared with such elements. It also covers the elements of such an array literal, a variable declared with such a type, and assignment to a field of a package's object, also for eachchild of group.children. It covers methods of values declared with the platform's type (gl.texImage2D(..., null)withgl: WebGL2RenderingContext, orthis.#gl), using #57's global types and excluding containers likeMap,ArrayandRecord. Last, thenullin ael: HTMLElement | nullparameter is allowed when the parameter only goes to outside code or is tested (compared, read from,?.,if (el)), and is never returned, stored or passed to the program's own functions. It reuses #46'spackage_type.Not covered: TwitchRTS's actual
c.parent = null, whereccomes from aSet<Container>in aMapvalue. Without a type checker, nothing declares it there (documented in "What the rule can't see").4. The
null-to-undefinedcodemod writesx?: Option<T>: done. It does this when every parameter after the one with= nullmay be left out too. Otherwise the default stays (x: Option<T> = undefined, the same thing).5. Docs: done.
Upgrading notes:
exactOptionalPropertyTypesandkind: c ? undefined : kind: a paragraph in the absence guide (write the two objects, orOption<T>).Docs pages changed
docs/rules/no-null-check-for-undefined.md.docs/rules/no-null.md,docs/guide/absence.md("nullfrom outside", comparisons both ways,JsonNull,exactOptionalPropertyTypes),docs/guide/json.md("Keeping a JSONnull", replacing "A JSONnullin test data"),docs/guide/libraries.md,docs/guide/adoption.md,docs/guide/testing.md.docs/README.mdandREADME.mdrule tables and std listing, the docs sidebar,CHANGELOG.md(Unreleased: Added and Changed; #61's line now names the new rule).TwitchRTS check
The branch's own code already carries the hand workarounds, so it has 0
no-nulland 0 new-rule reports before and after.To measure #1, I took the 199 comparison and assertion lines the adoption commit changed by hand from
nulltoundefined(=== null,toBeNull,assert.equal(x, null), ...) and put thenullforms back in a scratch copy. Results:defined<T>(x)),ReturnType<typeof setTimeout>timers, admin-api types reached through mapped types, or arenullarguments rather than tests. All are left alone by design.Checker time on the 209 test entries: no measurable change (587 ms vs 607 ms on main,
SAFE_TS_CACHE=0, 5 runs).Verification
All pass:
npm run build,cargo nextest run --workspace(212),cargo clippy -q --workspace --all-targets(nothing new in this code),npm run test:std,npm run typecheck,npm run typecheck:emit,node scripts/docs-typecheck.mjs(388 examples),npm run test:docs,npm run test:vite,npm run bench:check,node scripts/verify-packages.mjs.New tests:
null_check(cross-file, barrel re-export, classes, assertions, left-alone cases) andnull_outside.JSON.stringifydoesn't reportjsonNull/JsonNull.JsonNull/RawJson.🤖 Generated with Claude Code