UNPKG

@swissquote/eslint-plugin-swissquote

Version:

- Provide a style guide for your JavaScript - Warns on common mistakes in JavaScript - Works with EcmaScript 2015+

1,415 lines 741 kB
import "node:module"; import node_module from "node:module"; import node_path from "node:path"; import { ASTUtils, ESLintUtils } from "../typescript-eslint/utils.js"; import { AST_NODE_TYPES as types_js_AST_NODE_TYPES } from "../typescript-eslint/types.js"; import { randomBytes } from "node:crypto"; import { DefinitionType, ScopeType } from "../typescript-eslint/scope-manager.js"; import { findVariable, getStaticValue } from "../typescript-eslint/ast-utils.js"; import typescript from "typescript"; import { getConstrainedTypeAtLocation } from "../typescript-eslint/type-utils.js"; import { unionConstituents } from "../ts-api-utils/index.js"; import { simpleTraverse } from "../typescript-eslint/typescript-estree.js"; const dist_t = Symbol.for("@ts-pattern/matcher"), dist_e = Symbol.for("@ts-pattern/isVariadic"), dist_n = "@ts-pattern/anonymous-select-key", dist_r = (t)=>Boolean(t && "object" == typeof t), dist_i = (e)=>e && !!e[dist_t], dist_o = (n, s, c)=>{ if (dist_i(n)) { const e = n[dist_t](), { matched: r, selections: i } = e.match(s); return r && i && Object.keys(i).forEach((t)=>c(t, i[t])), r; } if (dist_r(n)) { if (!dist_r(s)) return !1; if (Array.isArray(n)) { if (!Array.isArray(s)) return !1; let t = [], r = [], u = []; for (const o of n.keys()){ const s = n[o]; dist_i(s) && s[dist_e] ? u.push(s) : u.length ? r.push(s) : t.push(s); } if (u.length) { if (u.length > 1) throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed."); if (s.length < t.length + r.length) return !1; const e = s.slice(0, t.length), n = 0 === r.length ? [] : s.slice(-r.length), i = s.slice(t.length, 0 === r.length ? 1 / 0 : -r.length); return t.every((t, n)=>dist_o(t, e[n], c)) && r.every((t, e)=>dist_o(t, n[e], c)) && (0 === u.length || dist_o(u[0], i, c)); } return n.length === s.length && n.every((t, e)=>dist_o(t, s[e], c)); } return Reflect.ownKeys(n).every((e)=>{ var u; const r = n[e]; return (e in s || dist_i(u = r) && "optional" === u[dist_t]().matcherType) && dist_o(r, s[e], c); }); } return Object.is(s, n); }, dist_s = (e)=>{ var n, o, u; return dist_r(e) ? dist_i(e) ? null != (n = null == (o = (u = e[dist_t]()).getSelectionKeys) ? void 0 : o.call(u)) ? n : [] : Array.isArray(e) ? dist_c(e, dist_s) : dist_c(Object.values(e), dist_s) : []; }, dist_c = (t, e)=>t.reduce((t, n)=>t.concat(e(n)), []); function dist_u(...t) { if (1 === t.length) { const [e] = t; return (t)=>dist_o(e, t, ()=>{}); } if (2 === t.length) { const [e, n] = t; return dist_o(e, n, ()=>{}); } throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`); } function dist_a(t) { return Object.assign(t, { optional: ()=>dist_h(t), and: (e)=>dist_d(t, e), or: (e)=>y(t, e), select: (e)=>void 0 === e ? dist_v(t) : dist_v(e, t) }); } function l(t) { return Object.assign(((t)=>Object.assign(t, { [Symbol.iterator] () { let n = 0; const r = [ { value: Object.assign(t, { [dist_e]: !0 }), done: !1 }, { done: !0, value: void 0 } ]; return { next: ()=>{ var t; return null != (t = r[n++]) ? t : r.at(-1); } }; } }))(t), { optional: ()=>l(dist_h(t)), select: (e)=>l(void 0 === e ? dist_v(t) : dist_v(e, t)) }); } function dist_h(e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ let n = {}; const r = (t, e)=>{ n[t] = e; }; return void 0 === t ? (dist_s(e).forEach((t)=>r(t, void 0)), { matched: !0, selections: n }) : { matched: dist_o(e, t, r), selections: n }; }, getSelectionKeys: ()=>dist_s(e), matcherType: "optional" }) }); } const dist_f = (t, e)=>{ for (const n of t)if (!e(n)) return !1; return !0; }, g = (t, e)=>{ for (const [n, r] of t.entries())if (!e(r, n)) return !1; return !0; }, dist_m = (t, e)=>{ const n = Reflect.ownKeys(t); for (const r of n)if (!e(r, t[r])) return !1; return !0; }; function dist_d(...e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ let n = {}; const r = (t, e)=>{ n[t] = e; }; return { matched: e.every((e)=>dist_o(e, t, r)), selections: n }; }, getSelectionKeys: ()=>dist_c(e, dist_s), matcherType: "and" }) }); } function y(...e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ let n = {}; const r = (t, e)=>{ n[t] = e; }; return dist_c(e, dist_s).forEach((t)=>r(t, void 0)), { matched: e.some((e)=>dist_o(e, t, r)), selections: n }; }, getSelectionKeys: ()=>dist_c(e, dist_s), matcherType: "or" }) }); } function dist_p(e) { return { [dist_t]: ()=>({ match: (t)=>({ matched: Boolean(e(t)) }) }) }; } function dist_v(...e) { const r = "string" == typeof e[0] ? e[0] : void 0, i = 2 === e.length ? e[1] : "string" == typeof e[0] ? void 0 : e[0]; return dist_a({ [dist_t]: ()=>({ match: (t)=>{ let e = { [null != r ? r : dist_n]: t }; return { matched: void 0 === i || dist_o(i, t, (t, n)=>{ e[t] = n; }), selections: e }; }, getSelectionKeys: ()=>[ null != r ? r : dist_n ].concat(void 0 === i ? [] : dist_s(i)) }) }); } function dist_b(t) { return !0; } function w(t) { return "number" == typeof t; } function S(t) { return "string" == typeof t; } function dist_j(t) { return "bigint" == typeof t; } const K = dist_a(dist_p(dist_b)), O = dist_a(dist_p(dist_b)), E = K, dist_x = (t)=>Object.assign(dist_a(t), { startsWith: (e)=>{ var n; return dist_x(dist_d(t, (n = e, dist_p((t)=>S(t) && t.startsWith(n))))); }, endsWith: (e)=>{ var n; return dist_x(dist_d(t, (n = e, dist_p((t)=>S(t) && t.endsWith(n))))); }, minLength: (e)=>dist_x(dist_d(t, ((t)=>dist_p((e)=>S(e) && e.length >= t))(e))), length: (e)=>dist_x(dist_d(t, ((t)=>dist_p((e)=>S(e) && e.length === t))(e))), maxLength: (e)=>dist_x(dist_d(t, ((t)=>dist_p((e)=>S(e) && e.length <= t))(e))), includes: (e)=>{ var n; return dist_x(dist_d(t, (n = e, dist_p((t)=>S(t) && t.includes(n))))); }, regex: (e)=>{ var n; return dist_x(dist_d(t, (n = e, dist_p((t)=>S(t) && Boolean(t.match(n)))))); } }), A = dist_x(dist_p(S)), N = (t)=>Object.assign(dist_a(t), { between: (e, n)=>N(dist_d(t, ((t, e)=>dist_p((n)=>w(n) && t <= n && e >= n))(e, n))), lt: (e)=>N(dist_d(t, ((t)=>dist_p((e)=>w(e) && e < t))(e))), gt: (e)=>N(dist_d(t, ((t)=>dist_p((e)=>w(e) && e > t))(e))), lte: (e)=>N(dist_d(t, ((t)=>dist_p((e)=>w(e) && e <= t))(e))), gte: (e)=>N(dist_d(t, ((t)=>dist_p((e)=>w(e) && e >= t))(e))), int: ()=>N(dist_d(t, dist_p((t)=>w(t) && Number.isInteger(t)))), finite: ()=>N(dist_d(t, dist_p((t)=>w(t) && Number.isFinite(t)))), positive: ()=>N(dist_d(t, dist_p((t)=>w(t) && t > 0))), negative: ()=>N(dist_d(t, dist_p((t)=>w(t) && t < 0))) }), P = N(dist_p(w)), dist_k = (t)=>Object.assign(dist_a(t), { between: (e, n)=>dist_k(dist_d(t, ((t, e)=>dist_p((n)=>dist_j(n) && t <= n && e >= n))(e, n))), lt: (e)=>dist_k(dist_d(t, ((t)=>dist_p((e)=>dist_j(e) && e < t))(e))), gt: (e)=>dist_k(dist_d(t, ((t)=>dist_p((e)=>dist_j(e) && e > t))(e))), lte: (e)=>dist_k(dist_d(t, ((t)=>dist_p((e)=>dist_j(e) && e <= t))(e))), gte: (e)=>dist_k(dist_d(t, ((t)=>dist_p((e)=>dist_j(e) && e >= t))(e))), positive: ()=>dist_k(dist_d(t, dist_p((t)=>dist_j(t) && t > 0))), negative: ()=>dist_k(dist_d(t, dist_p((t)=>dist_j(t) && t < 0))) }), T = dist_k(dist_p(dist_j)), B = dist_a(dist_p(function(t) { return "boolean" == typeof t; })), dist_ = dist_a(dist_p(function(t) { return "symbol" == typeof t; })), W = dist_a(dist_p(function(t) { return null == t; })), $ = dist_a(dist_p(function(t) { return null != t; })); var z = { __proto__: null, matcher: dist_t, optional: dist_h, array: function(...e) { return l({ [dist_t]: ()=>({ match: (t)=>{ if (!Array.isArray(t)) return { matched: !1 }; if (0 === e.length) return { matched: !0 }; const n = e[0]; let r = {}; if (0 === t.length) return dist_s(n).forEach((t)=>{ r[t] = []; }), { matched: !0, selections: r }; const i = (t, e)=>{ r[t] = (r[t] || []).concat([ e ]); }; return { matched: t.every((t)=>dist_o(n, t, i)), selections: r }; }, getSelectionKeys: ()=>0 === e.length ? [] : dist_s(e[0]) }) }); }, set: function(...e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ if (!(t instanceof Set)) return { matched: !1 }; let n = {}; if (0 === t.size) return { matched: !0, selections: n }; if (0 === e.length) return { matched: !0 }; const r = (t, e)=>{ n[t] = (n[t] || []).concat([ e ]); }, i = e[0]; return { matched: dist_f(t, (t)=>dist_o(i, t, r)), selections: n }; }, getSelectionKeys: ()=>0 === e.length ? [] : dist_s(e[0]) }) }); }, map: function(...e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ if (!(t instanceof Map)) return { matched: !1 }; let n = {}; if (0 === t.size) return { matched: !0, selections: n }; const r = (t, e)=>{ n[t] = (n[t] || []).concat([ e ]); }; if (0 === e.length) return { matched: !0 }; var i; if (1 === e.length) throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null == (i = e[0]) ? void 0 : i.toString()}`); const [s, c] = e; return { matched: g(t, (t, e)=>{ const n = dist_o(s, e, r), i = dist_o(c, t, r); return n && i; }), selections: n }; }, getSelectionKeys: ()=>0 === e.length ? [] : [ ...dist_s(e[0]), ...dist_s(e[1]) ] }) }); }, record: function(...e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>{ if (null === t || "object" != typeof t || Array.isArray(t)) return { matched: !1 }; var n; if (0 === e.length) throw new Error(`\`P.record\` wasn't given enough arguments. Expected (value) or (key, value), received ${null == (n = e[0]) ? void 0 : n.toString()}`); let r = {}; const i = (t, e)=>{ r[t] = (r[t] || []).concat([ e ]); }, [s, c] = 1 === e.length ? [ A, e[0] ] : e; return { matched: dist_m(t, (t, e)=>{ const n = "string" != typeof t || Number.isNaN(Number(t)) ? null : Number(t), r = null !== n && dist_o(s, n, i), u = dist_o(s, t, i), a = dist_o(c, e, i); return (u || r) && a; }), selections: r }; }, getSelectionKeys: ()=>0 === e.length ? [] : [ ...dist_s(e[0]), ...dist_s(e[1]) ] }) }); }, intersection: dist_d, union: y, not: function(e) { return dist_a({ [dist_t]: ()=>({ match: (t)=>({ matched: !dist_o(e, t, ()=>{}) }), getSelectionKeys: ()=>[], matcherType: "not" }) }); }, when: dist_p, select: dist_v, any: K, unknown: O, _: E, string: A, number: P, bigint: T, boolean: B, symbol: dist_, nullish: W, nonNullable: $, instanceOf: function(t) { return dist_a(dist_p(function(t) { return (e)=>e instanceof t; }(t))); }, shape: function(t) { return dist_a(dist_p(dist_u(t))); } }; class I extends Error { constructor(t){ let e; try { e = JSON.stringify(t); } catch (n) { e = t; } super(`Pattern matching error: no pattern matches value ${e}`), this.input = void 0, this.input = t; } } const L = { matched: !1, value: void 0 }; function M(t) { return new R(t, L); } class R { constructor(t, e){ this.input = void 0, this.state = void 0, this.input = t, this.state = e; } with(...t) { if (this.state.matched) return this; const e = t[t.length - 1], r = [ t[0] ]; let i; 3 === t.length && "function" == typeof t[1] ? i = t[1] : t.length > 2 && r.push(...t.slice(1, t.length - 1)); let s = !1, c = {}; const u = (t, e)=>{ s = !0, c[t] = e; }, a = r.some((t)=>dist_o(t, this.input, u)) && (!i || Boolean(i(this.input))) ? { matched: !0, value: e(s ? dist_n in c ? c[dist_n] : c : this.input, this.input) } : L; return new R(this.input, a); } when(t, e) { if (this.state.matched) return this; const n = Boolean(t(this.input)); return new R(this.input, n ? { matched: !0, value: e(this.input, this.input) } : L); } otherwise(t) { return this.state.matched ? this.state.value : t(this.input); } exhaustive(t = dist_F) { return this.state.matched ? this.state.value : t(this.input); } run() { return this.exhaustive(); } returnType() { return this; } narrow() { return this; } } function dist_F(t) { throw new I(t); } var core_a; function $constructor(name, initializer, params) { function init(inst, def) { if (!inst._zod) Object.defineProperty(inst, "_zod", { value: { def, constr: _, traits: new Set() }, enumerable: false }); if (inst._zod.traits.has(name)) return; inst._zod.traits.add(name); initializer(inst, def); const proto = _.prototype; const keys = Object.keys(proto); for(let i = 0; i < keys.length; i++){ const k = keys[i]; if (!(k in inst)) inst[k] = proto[k].bind(inst); } } const Parent = params?.Parent ?? Object; class Definition extends Parent { } Object.defineProperty(Definition, "name", { value: name }); function _(def) { var _a; const inst = params?.Parent ? new Definition() : this; init(inst, def); (_a = inst._zod).deferred ?? (_a.deferred = []); for (const fn of inst._zod.deferred)fn(); return inst; } Object.defineProperty(_, "init", { value: init }); Object.defineProperty(_, Symbol.hasInstance, { value: (inst)=>{ if (params?.Parent && inst instanceof params.Parent) return true; return inst?._zod?.traits?.has(name); } }); Object.defineProperty(_, "name", { value: name }); return _; } Symbol("zod_brand"); class $ZodAsyncError extends Error { constructor(){ super("Encountered Promise during synchronous parse. Use .parseAsync() instead."); } } class $ZodEncodeError extends Error { constructor(name){ super(`Encountered unidirectional transform during encode: ${name}`); this.name = "ZodEncodeError"; } } (core_a = globalThis).__zod_globalConfig ?? (core_a.__zod_globalConfig = {}); const globalConfig = globalThis.__zod_globalConfig; function core_config(newConfig) { if (newConfig) Object.assign(globalConfig, newConfig); return globalConfig; } const cuid = /^[cC][0-9a-z]{6,}$/; const cuid2 = /^[0-9a-z]+$/; const ulid = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/; const xid = /^[0-9a-vA-V]{20}$/; const ksuid = /^[A-Za-z0-9]{27}$/; const nanoid = /^[a-zA-Z0-9_-]{21}$/; const duration = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/; const guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/; const uuid = (version)=>{ if (!version) return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/; return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`); }; const email = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/; const _emoji = "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$"; function emoji() { return new RegExp(_emoji, "u"); } const ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/; const ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/; const cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/; const cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/; const regexes_base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/; const regexes_base64url = /^[A-Za-z0-9_-]*$/; const httpProtocol = /^https?$/; const e164 = /^\+[1-9]\d{6,14}$/; const dateSource = "(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))"; const date = /*@__PURE__*/ new RegExp(`^${dateSource}$`); function timeSource(args) { const hhmm = "(?:[01]\\d|2[0-3]):[0-5]\\d"; const regex = "number" == typeof args.precision ? -1 === args.precision ? `${hhmm}` : 0 === args.precision ? `${hhmm}:[0-5]\\d` : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}` : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`; return regex; } function regexes_time(args) { return new RegExp(`^${timeSource(args)}$`); } function datetime(args) { const time = timeSource({ precision: args.precision }); const opts = [ "Z" ]; if (args.local) opts.push(""); if (args.offset) opts.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)"); const timeRegex = `${time}(?:${opts.join("|")})`; return new RegExp(`^${dateSource}T(?:${timeRegex})$`); } const regexes_string = (params)=>{ const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : "[\\s\\S]*"; return new RegExp(`^${regex}$`); }; const lowercase = /^[^A-Z]*$/; const uppercase = /^[^a-z]*$/; function getEnumValues(entries) { const numericValues = Object.values(entries).filter((v)=>"number" == typeof v); const values = Object.entries(entries).filter(([k, _])=>-1 === numericValues.indexOf(+k)).map(([_, v])=>v); return values; } function jsonStringifyReplacer(_, value) { if ("bigint" == typeof value) return value.toString(); return value; } function util_cached(getter) { const set = false; return { get value () { if (!set) { const value = getter(); Object.defineProperty(this, "value", { value }); return value; } throw new Error("cached value already set"); } }; } function nullish(input) { return null == input; } function cleanRegex(source) { const start = source.startsWith("^") ? 1 : 0; const end = source.endsWith("$") ? source.length - 1 : source.length; return source.slice(start, end); } const EVALUATING = /* @__PURE__*/ Symbol("evaluating"); function defineLazy(object, key, getter) { let value; Object.defineProperty(object, key, { get () { if (value === EVALUATING) return; if (void 0 === value) { value = EVALUATING; value = getter(); } return value; }, set (v) { Object.defineProperty(object, key, { value: v }); }, configurable: true }); } function assignProp(target, prop, value) { Object.defineProperty(target, prop, { value, writable: true, enumerable: true, configurable: true }); } function mergeDefs(...defs) { const mergedDescriptors = {}; for (const def of defs){ const descriptors = Object.getOwnPropertyDescriptors(def); Object.assign(mergedDescriptors, descriptors); } return Object.defineProperties({}, mergedDescriptors); } function esc(str) { return JSON.stringify(str); } function slugify(input) { return input.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, ""); } const captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args)=>{}; function util_isObject(data) { return "object" == typeof data && null !== data && !Array.isArray(data); } const util_allowsEval = /* @__PURE__*/ util_cached(()=>{ if (globalConfig.jitless) return false; if ("u" > typeof navigator && navigator?.userAgent?.includes("Cloudflare")) return false; try { const F = Function; new F(""); return true; } catch (_) { return false; } }); function isPlainObject(o) { if (false === util_isObject(o)) return false; const ctor = o.constructor; if (void 0 === ctor) return true; if ("function" != typeof ctor) return true; const prot = ctor.prototype; if (false === util_isObject(prot)) return false; if (false === Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf")) return false; return true; } function shallowClone(o) { if (isPlainObject(o)) return { ...o }; if (Array.isArray(o)) return [ ...o ]; if (o instanceof Map) return new Map(o); if (o instanceof Set) return new Set(o); return o; } const propertyKeyTypes = /* @__PURE__*/ new Set([ "string", "number", "symbol" ]); function escapeRegex(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function clone(inst, def, params) { const cl = new inst._zod.constr(def ?? inst._zod.def); if (!def || params?.parent) cl._zod.parent = inst; return cl; } function normalizeParams(_params) { const params = _params; if (!params) return {}; if ("string" == typeof params) return { error: ()=>params }; if (params?.message !== void 0) { if (params?.error !== void 0) throw new Error("Cannot specify both `message` and `error` params"); params.error = params.message; } delete params.message; if ("string" == typeof params.error) return { ...params, error: ()=>params.error }; return params; } function optionalKeys(shape) { return Object.keys(shape).filter((k)=>"optional" === shape[k]._zod.optin && "optional" === shape[k]._zod.optout); } function pick(schema, mask) { const currDef = schema._zod.def; const checks = currDef.checks; const hasChecks = checks && checks.length > 0; if (hasChecks) throw new Error(".pick() cannot be used on object schemas containing refinements"); const def = mergeDefs(schema._zod.def, { get shape () { const newShape = {}; for(const key in mask){ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`); if (mask[key]) newShape[key] = currDef.shape[key]; } assignProp(this, "shape", newShape); return newShape; }, checks: [] }); return clone(schema, def); } function omit(schema, mask) { const currDef = schema._zod.def; const checks = currDef.checks; const hasChecks = checks && checks.length > 0; if (hasChecks) throw new Error(".omit() cannot be used on object schemas containing refinements"); const def = mergeDefs(schema._zod.def, { get shape () { const newShape = { ...schema._zod.def.shape }; for(const key in mask){ if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`); if (mask[key]) delete newShape[key]; } assignProp(this, "shape", newShape); return newShape; }, checks: [] }); return clone(schema, def); } function extend(schema, shape) { if (!isPlainObject(shape)) throw new Error("Invalid input to extend: expected a plain object"); const checks = schema._zod.def.checks; const hasChecks = checks && checks.length > 0; if (hasChecks) { const existingShape = schema._zod.def.shape; for(const key in shape)if (void 0 !== Object.getOwnPropertyDescriptor(existingShape, key)) throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead."); } const def = mergeDefs(schema._zod.def, { get shape () { const _shape = { ...schema._zod.def.shape, ...shape }; assignProp(this, "shape", _shape); return _shape; } }); return clone(schema, def); } function safeExtend(schema, shape) { if (!isPlainObject(shape)) throw new Error("Invalid input to safeExtend: expected a plain object"); const def = mergeDefs(schema._zod.def, { get shape () { const _shape = { ...schema._zod.def.shape, ...shape }; assignProp(this, "shape", _shape); return _shape; } }); return clone(schema, def); } function merge(a, b) { if (a._zod.def.checks?.length) throw new Error(".merge() cannot be used on object schemas containing refinements. Use .safeExtend() instead."); const def = mergeDefs(a._zod.def, { get shape () { const _shape = { ...a._zod.def.shape, ...b._zod.def.shape }; assignProp(this, "shape", _shape); return _shape; }, get catchall () { return b._zod.def.catchall; }, checks: b._zod.def.checks ?? [] }); return clone(a, def); } function partial(Class, schema, mask) { const currDef = schema._zod.def; const checks = currDef.checks; const hasChecks = checks && checks.length > 0; if (hasChecks) throw new Error(".partial() cannot be used on object schemas containing refinements"); const def = mergeDefs(schema._zod.def, { get shape () { const oldShape = schema._zod.def.shape; const shape = { ...oldShape }; if (mask) for(const key in mask){ if (!(key in oldShape)) throw new Error(`Unrecognized key: "${key}"`); if (mask[key]) shape[key] = Class ? new Class({ type: "optional", innerType: oldShape[key] }) : oldShape[key]; } else for(const key in oldShape)shape[key] = Class ? new Class({ type: "optional", innerType: oldShape[key] }) : oldShape[key]; assignProp(this, "shape", shape); return shape; }, checks: [] }); return clone(schema, def); } function required(Class, schema, mask) { const def = mergeDefs(schema._zod.def, { get shape () { const oldShape = schema._zod.def.shape; const shape = { ...oldShape }; if (mask) for(const key in mask){ if (!(key in shape)) throw new Error(`Unrecognized key: "${key}"`); if (mask[key]) shape[key] = new Class({ type: "nonoptional", innerType: oldShape[key] }); } else for(const key in oldShape)shape[key] = new Class({ type: "nonoptional", innerType: oldShape[key] }); assignProp(this, "shape", shape); return shape; } }); return clone(schema, def); } function aborted(x, startIndex = 0) { if (true === x.aborted) return true; for(let i = startIndex; i < x.issues.length; i++)if (x.issues[i]?.continue !== true) return true; return false; } function explicitlyAborted(x, startIndex = 0) { if (true === x.aborted) return true; for(let i = startIndex; i < x.issues.length; i++)if (x.issues[i]?.continue === false) return true; return false; } function prefixIssues(path, issues) { return issues.map((iss)=>{ var _a; (_a = iss).path ?? (_a.path = []); iss.path.unshift(path); return iss; }); } function unwrapMessage(message) { return "string" == typeof message ? message : message?.message; } function finalizeIssue(iss, ctx, config) { const message = iss.message ? iss.message : unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config.customError?.(iss)) ?? unwrapMessage(config.localeError?.(iss)) ?? "Invalid input"; const { inst: _inst, continue: _continue, input: _input, ...rest } = iss; rest.path ?? (rest.path = []); rest.message = message; if (ctx?.reportInput) rest.input = _input; return rest; } function getLengthableOrigin(input) { if (Array.isArray(input)) return "array"; if ("string" == typeof input) return "string"; return "unknown"; } function util_issue(...args) { const [iss, input, inst] = args; if ("string" == typeof iss) return { message: iss, code: "custom", input, inst }; return { ...iss }; } const $ZodCheck = /*@__PURE__*/ $constructor("$ZodCheck", (inst, def)=>{ var _a; inst._zod ?? (inst._zod = {}); inst._zod.def = def; (_a = inst._zod).onattach ?? (_a.onattach = []); }); const $ZodCheckMaxLength = /*@__PURE__*/ $constructor("$ZodCheckMaxLength", (inst, def)=>{ var _a; $ZodCheck.init(inst, def); (_a = inst._zod.def).when ?? (_a.when = (payload)=>{ const val = payload.value; return !nullish(val) && void 0 !== val.length; }); inst._zod.onattach.push((inst)=>{ const curr = inst._zod.bag.maximum ?? 1 / 0; if (def.maximum < curr) inst._zod.bag.maximum = def.maximum; }); inst._zod.check = (payload)=>{ const input = payload.value; const length = input.length; if (length <= def.maximum) return; const origin = getLengthableOrigin(input); payload.issues.push({ origin, code: "too_big", maximum: def.maximum, inclusive: true, input, inst, continue: !def.abort }); }; }); const $ZodCheckMinLength = /*@__PURE__*/ $constructor("$ZodCheckMinLength", (inst, def)=>{ var _a; $ZodCheck.init(inst, def); (_a = inst._zod.def).when ?? (_a.when = (payload)=>{ const val = payload.value; return !nullish(val) && void 0 !== val.length; }); inst._zod.onattach.push((inst)=>{ const curr = inst._zod.bag.minimum ?? -1 / 0; if (def.minimum > curr) inst._zod.bag.minimum = def.minimum; }); inst._zod.check = (payload)=>{ const input = payload.value; const length = input.length; if (length >= def.minimum) return; const origin = getLengthableOrigin(input); payload.issues.push({ origin, code: "too_small", minimum: def.minimum, inclusive: true, input, inst, continue: !def.abort }); }; }); const $ZodCheckLengthEquals = /*@__PURE__*/ $constructor("$ZodCheckLengthEquals", (inst, def)=>{ var _a; $ZodCheck.init(inst, def); (_a = inst._zod.def).when ?? (_a.when = (payload)=>{ const val = payload.value; return !nullish(val) && void 0 !== val.length; }); inst._zod.onattach.push((inst)=>{ const bag = inst._zod.bag; bag.minimum = def.length; bag.maximum = def.length; bag.length = def.length; }); inst._zod.check = (payload)=>{ const input = payload.value; const length = input.length; if (length === def.length) return; const origin = getLengthableOrigin(input); const tooBig = length > def.length; payload.issues.push({ origin, ...tooBig ? { code: "too_big", maximum: def.length } : { code: "too_small", minimum: def.length }, inclusive: true, exact: true, input: payload.value, inst, continue: !def.abort }); }; }); const $ZodCheckStringFormat = /*@__PURE__*/ $constructor("$ZodCheckStringFormat", (inst, def)=>{ var _a, _b; $ZodCheck.init(inst, def); inst._zod.onattach.push((inst)=>{ const bag = inst._zod.bag; bag.format = def.format; if (def.pattern) { bag.patterns ?? (bag.patterns = new Set()); bag.patterns.add(def.pattern); } }); if (def.pattern) (_a = inst._zod).check ?? (_a.check = (payload)=>{ def.pattern.lastIndex = 0; if (def.pattern.test(payload.value)) return; payload.issues.push({ origin: "string", code: "invalid_format", format: def.format, input: payload.value, ...def.pattern ? { pattern: def.pattern.toString() } : {}, inst, continue: !def.abort }); }); else (_b = inst._zod).check ?? (_b.check = ()=>{}); }); const $ZodCheckRegex = /*@__PURE__*/ $constructor("$ZodCheckRegex", (inst, def)=>{ $ZodCheckStringFormat.init(inst, def); inst._zod.check = (payload)=>{ def.pattern.lastIndex = 0; if (def.pattern.test(payload.value)) return; payload.issues.push({ origin: "string", code: "invalid_format", format: "regex", input: payload.value, pattern: def.pattern.toString(), inst, continue: !def.abort }); }; }); const $ZodCheckLowerCase = /*@__PURE__*/ $constructor("$ZodCheckLowerCase", (inst, def)=>{ def.pattern ?? (def.pattern = lowercase); $ZodCheckStringFormat.init(inst, def); }); const $ZodCheckUpperCase = /*@__PURE__*/ $constructor("$ZodCheckUpperCase", (inst, def)=>{ def.pattern ?? (def.pattern = uppercase); $ZodCheckStringFormat.init(inst, def); }); const $ZodCheckIncludes = /*@__PURE__*/ $constructor("$ZodCheckIncludes", (inst, def)=>{ $ZodCheck.init(inst, def); const escapedRegex = escapeRegex(def.includes); const pattern = new RegExp("number" == typeof def.position ? `^.{${def.position}}${escapedRegex}` : escapedRegex); def.pattern = pattern; inst._zod.onattach.push((inst)=>{ const bag = inst._zod.bag; bag.patterns ?? (bag.patterns = new Set()); bag.patterns.add(pattern); }); inst._zod.check = (payload)=>{ if (payload.value.includes(def.includes, def.position)) return; payload.issues.push({ origin: "string", code: "invalid_format", format: "includes", includes: def.includes, input: payload.value, inst, continue: !def.abort }); }; }); const $ZodCheckStartsWith = /*@__PURE__*/ $constructor("$ZodCheckStartsWith", (inst, def)=>{ $ZodCheck.init(inst, def); const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`); def.pattern ?? (def.pattern = pattern); inst._zod.onattach.push((inst)=>{ const bag = inst._zod.bag; bag.patterns ?? (bag.patterns = new Set()); bag.patterns.add(pattern); }); inst._zod.check = (payload)=>{ if (payload.value.startsWith(def.prefix)) return; payload.issues.push({ origin: "string", code: "invalid_format", format: "starts_with", prefix: def.prefix, input: payload.value, inst, continue: !def.abort }); }; }); const $ZodCheckEndsWith = /*@__PURE__*/ $constructor("$ZodCheckEndsWith", (inst, def)=>{ $ZodCheck.init(inst, def); const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`); def.pattern ?? (def.pattern = pattern); inst._zod.onattach.push((inst)=>{ const bag = inst._zod.bag; bag.patterns ?? (bag.patterns = new Set()); bag.patterns.add(pattern); }); inst._zod.check = (payload)=>{ if (payload.value.endsWith(def.suffix)) return; payload.issues.push({ origin: "string", code: "invalid_format", format: "ends_with", suffix: def.suffix, input: payload.value, inst, continue: !def.abort }); }; }); const $ZodCheckOverwrite = /*@__PURE__*/ $constructor("$ZodCheckOverwrite", (inst, def)=>{ $ZodCheck.init(inst, def); inst._zod.check = (payload)=>{ payload.value = def.tx(payload.value); }; }); class Doc { constructor(args = []){ this.content = []; this.indent = 0; if (this) this.args = args; } indented(fn) { this.indent += 1; fn(this); this.indent -= 1; } write(arg) { if ("function" == typeof arg) { arg(this, { execution: "sync" }); arg(this, { execution: "async" }); return; } const content = arg; const lines = content.split("\n").filter((x)=>x); const minIndent = Math.min(...lines.map((x)=>x.length - x.trimStart().length)); const dedented = lines.map((x)=>x.slice(minIndent)).map((x)=>" ".repeat(2 * this.indent) + x); for (const line of dedented)this.content.push(line); } compile() { const F = Function; const args = this?.args; const content = this?.content ?? [ "" ]; const lines = [ ...content.map((x)=>` ${x}`) ]; return new F(...args, lines.join("\n")); } } const errors_initializer = (inst, def)=>{ inst.name = "$ZodError"; Object.defineProperty(inst, "_zod", { value: inst._zod, enumerable: false }); Object.defineProperty(inst, "issues", { value: def, enumerable: false }); inst.message = JSON.stringify(def, jsonStringifyReplacer, 2); Object.defineProperty(inst, "toString", { value: ()=>inst.message, enumerable: false }); }; const $ZodError = $constructor("$ZodError", errors_initializer); const $ZodRealError = $constructor("$ZodError", errors_initializer, { Parent: Error }); function flattenError(error, mapper = (issue)=>issue.message) { const fieldErrors = {}; const formErrors = []; for (const sub of error.issues)if (sub.path.length > 0) { fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || []; fieldErrors[sub.path[0]].push(mapper(sub)); } else formErrors.push(mapper(sub)); return { formErrors, fieldErrors }; } function formatError(error, mapper = (issue)=>issue.message) { const fieldErrors = { _errors: [] }; const processError = (error, path = [])=>{ for (const issue of error.issues)if ("invalid_union" === issue.code && issue.errors.length) issue.errors.map((issues)=>processError({ issues }, [ ...path, ...issue.path ])); else if ("invalid_key" === issue.code) processError({ issues: issue.issues }, [ ...path, ...issue.path ]); else if ("invalid_element" === issue.code) processError({ issues: issue.issues }, [ ...path, ...issue.path ]); else { const fullpath = [ ...path, ...issue.path ]; if (0 === fullpath.length) fieldErrors._errors.push(mapper(issue)); else { let curr = fieldErrors; let i = 0; while(i < fullpath.length){ const el = fullpath[i]; const terminal = i === fullpath.length - 1; if (terminal) { curr[el] = curr[el] || { _errors: [] }; curr[el]._errors.push(mapper(issue)); } else curr[el] = curr[el] || { _errors: [] }; curr = curr[el]; i++; } } } }; processError(error); return fieldErrors; } const _parse = (_Err)=>(schema, value, _ctx, _params)=>{ const ctx = _ctx ? { ..._ctx, async: false } : { async: false }; const result = schema._zod.run({ value, issues: [] }, ctx); if (result instanceof Promise) throw new $ZodAsyncError(); if (result.issues.length) { const e = new (_params?.Err ?? _Err)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))); captureStackTrace(e, _params?.callee); throw e; } return result.value; }; const _parseAsync = (_Err)=>async (schema, value, _ctx, params)=>{ const ctx = _ctx ? { ..._ctx, async: true } : { async: true }; let result = schema._zod.run({ value, issues: [] }, ctx); if (result instanceof Promise) result = await result; if (result.issues.length) { const e = new (params?.Err ?? _Err)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))); captureStackTrace(e, params?.callee); throw e; } return result.value; }; const _safeParse = (_Err)=>(schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, async: false } : { async: false }; const result = schema._zod.run({ value, issues: [] }, ctx); if (result instanceof Promise) throw new $ZodAsyncError(); return result.issues.length ? { success: false, error: new (_Err ?? $ZodError)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))) } : { success: true, data: result.value }; }; const safeParse = /* @__PURE__*/ _safeParse($ZodRealError); const _safeParseAsync = (_Err)=>async (schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, async: true } : { async: true }; let result = schema._zod.run({ value, issues: [] }, ctx); if (result instanceof Promise) result = await result; return result.issues.length ? { success: false, error: new _Err(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))) } : { success: true, data: result.value }; }; const safeParseAsync = /* @__PURE__*/ _safeParseAsync($ZodRealError); const _encode = (_Err)=>(schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, direction: "backward" } : { direction: "backward" }; return _parse(_Err)(schema, value, ctx); }; const _decode = (_Err)=>(schema, value, _ctx)=>_parse(_Err)(schema, value, _ctx); const _encodeAsync = (_Err)=>async (schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, direction: "backward" } : { direction: "backward" }; return _parseAsync(_Err)(schema, value, ctx); }; const _decodeAsync = (_Err)=>async (schema, value, _ctx)=>_parseAsync(_Err)(schema, value, _ctx); const _safeEncode = (_Err)=>(schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, direction: "backward" } : { direction: "backward" }; return _safeParse(_Err)(schema, value, ctx); }; const _safeDecode = (_Err)=>(schema, value, _ctx)=>_safeParse(_Err)(schema, value, _ctx); const _safeEncodeAsync = (_Err)=>async (schema, value, _ctx)=>{ const ctx = _ctx ? { ..._ctx, direction: "backward" } : { direction: "backward" }; return _safeParseAsync(_Err)(schema, value, ctx); }; const _safeDecodeAsync = (_Err)=>async (schema, value, _ctx)=>_safeParseAsync(_Err)(schema, value, _ctx); const versions_version = { major: 4, minor: 4, patch: 3 }; const $ZodType = /*@__PURE__*/ $constructor("$ZodType", (inst, def)=>{ var _a; inst ?? (inst = {}); inst._zod.def = def; inst._zod.bag = inst._zod.bag || {}; inst._zod.version = versions_version; const checks = [ ...inst._zod