Files
Sebastian Poxhofer 16ef1715e9 refactor(linters): add rules for @sindresorhus/is helpers (#45229)
* feat(lint): add renovate/prefer-is-helpers and renovate/prefer-is-object rules

Flag ad-hoc type checks that have @sindresorhus/is equivalents:
- .filter(Boolean) -> .filter(isTruthy)
- typeof x === 'string' -> isString(x)
- x === null || x === undefined -> isNullOrUndefined(x) (and negation)

The typeof x === 'object' shape lives in a separate suggestion-only rule
(renovate/prefer-is-object, kept at warn) because isObject()/isPlainObject()
are not drop-in equivalents: null, arrays and functions behave differently.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: fix renovate/prefer-is-helpers violations

Replace ad-hoc type checks with @sindresorhus/is helpers across lib/:
- 13x .filter(Boolean) -> .filter(isTruthy)
- 36x typeof x ==/!== 'string' -> isString(x) / !isString(x)
- 6x x === null || x === undefined -> isNullOrUndefined(x) (and negation)

Promote renovate/prefer-is-helpers to error now that the codebase is clean.
renovate/prefer-is-object stays at warn: its 15 remaining sites guard
parsed YAML/JSON or unknown values where isObject()/isPlainObject() are
not provably equivalent (null/array/function semantics).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: enforce renovate/prefer-is-object at error level

Resolve the 15 remaining warnings and raise the rule from warn to error:

- Migrate 10 sites to isObject() where the guarded value provably cannot
  be a function (parsed YAML/JSON, typed exec command arguments, or
  test-controlled fixtures): helm-values, woodpecker, bun, pre-commit
  managers, exec utils, file package cache, and the github http spec.
  In the bun manager this also fixes a latent crash where a null
  `workspaces` value would throw on the `in` operator.
- Keep 5 sites as reasoned oxlint-disable comments where excluding
  functions is load-bearing: the byte-exact fingerprint canonicalizer
  (2), the circularity walker, the zod result guard, and the generic
  hasKey helper.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 14:22:03 +00:00

104 lines
2.6 KiB
TypeScript

import { isString } from '@sindresorhus/is';
import { DateTime } from 'luxon';
import { z } from 'zod/v4';
export type Timestamp = string & { __timestamp: never };
const timezoneOffset = new Date().getTimezoneOffset() * 60000;
const millenium = 946684800000; // 2000-01-01T00:00:00.000Z
const tomorrowOffset = 86400000; // 24 * 60 * 60 * 1000;
function isValid(date: DateTime): boolean {
if (!date.isValid) {
return false;
}
const tomorrow = DateTime.now().toMillis() + tomorrowOffset; // 24 * 60 * 60 * 1000;
const ts = date.toMillis();
return ts > millenium && ts < tomorrow;
}
export function asTimestamp(input: unknown): Timestamp | null {
if (input instanceof Date) {
const date = DateTime.fromJSDate(input, { zone: 'UTC' });
if (isValid(date)) {
return date.toISO() as Timestamp;
}
return null;
}
if (typeof input === 'number') {
const millisDate = DateTime.fromMillis(input, { zone: 'UTC' });
if (isValid(millisDate)) {
return millisDate.toISO() as Timestamp;
}
const secondsDate = DateTime.fromSeconds(input, { zone: 'UTC' });
if (isValid(secondsDate)) {
return secondsDate.toISO() as Timestamp;
}
return null;
}
if (isString(input)) {
const isoDate = DateTime.fromISO(input, { zone: 'UTC' });
if (isValid(isoDate)) {
return isoDate.toISO() as Timestamp;
}
const httpDate = DateTime.fromHTTP(input, { zone: 'UTC' });
if (isValid(httpDate)) {
return httpDate.toISO() as Timestamp;
}
const sqlDate = DateTime.fromSQL(input, { zone: 'UTC' });
if (isValid(sqlDate)) {
return sqlDate.toISO() as Timestamp;
}
const numberLikeDate = DateTime.fromFormat(input, 'yyyyMMddHHmmss', {
zone: 'UTC',
});
if (isValid(numberLikeDate)) {
return numberLikeDate.toISO() as Timestamp;
}
const numberLikeOffsetDate = DateTime.fromFormat(
input,
'yyyyMMddHHmmssZZZ',
{ zone: 'UTC' },
);
if (isValid(numberLikeOffsetDate)) {
return numberLikeOffsetDate.toISO() as Timestamp;
}
const fallbackDate = DateTime.fromMillis(
Date.parse(input) - timezoneOffset,
{ zone: 'UTC' },
);
if (isValid(fallbackDate)) {
return fallbackDate.toISO() as Timestamp;
}
return null;
}
return null;
}
export const Timestamp = z.unknown().transform((input, ctx) => {
const timestamp = asTimestamp(input);
if (!timestamp) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Invalid timestamp',
});
return z.NEVER;
}
return timestamp;
});
export const MaybeTimestamp = Timestamp.nullable().catch(null);