{"record":{"id":"59b70edfc1acd10b","repo":"jestjs/jest","slug":"pkg-name-value-arg-is-a-negative-integer","errorCode":null,"errorMessage":"${pkg}: ${name} value ${arg} is a negative integer","messagePattern":"(.+?): (.+?) value (.+?) is a negative integer","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/diff-sequences/src/index.ts","lineNumber":767,"sourceCode":"      bEnd,\n      transposed,\n      callbacks,\n      aIndexesF,\n      aIndexesR,\n      division,\n    );\n  }\n};\n\nconst validateLength = (name: string, arg: unknown) => {\n  if (typeof arg !== 'number') {\n    throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);\n  }\n  if (!Number.isSafeInteger(arg)) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);\n  }\n  if (arg < 0) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);\n  }\n};\n\nconst validateCallback = (name: string, arg: unknown) => {\n  const type = typeof arg;\n  if (type !== 'function') {\n    throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);\n  }\n};\n\n// Compare items in two sequences to find a longest common subsequence.\n// Given lengths of sequences and input function to compare items at indexes,\n// return by output function the number of adjacent items and starting indexes\n// of each common subsequence.\nexport default function diffSequence(\n  aLength: number,\n  bLength: number,\n  isCommon: IsCommon,","sourceCodeStart":749,"sourceCodeEnd":785,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/diff-sequences/src/index.ts#L749-L785","documentation":"validateLength's final check rejects negative safe integers with a RangeError, because negative lengths have no meaning for a sequence index range. The algorithm iterates from 0 up to the length, so a negative bound would skip all items and indicate a caller bug.","triggerScenarios":"Calling diffSequence with a length argument that is a negative integer (e.g. -1), typically from an arithmetic mistake such as `a.length - b.length` when b is longer, or from `Math.max`/`Math.min` applied in the wrong order.","commonSituations":"Subtraction that can go negative (e.g. `a.length - offset` with offset > length); slicing bounds computed incorrectly; defaulting a missing value to -1 as a sentinel.","solutions":["Clamp the length to a minimum of 0 with `Math.max(0, value)`.","Fix the upstream arithmetic so it cannot underflow (re-check offsets/slices).","If -1 is used as a sentinel for 'no sequence', convert it to 0 before calling diffSequence."],"exampleFix":"// before\nconst len = a.length - removed; // can be -3\ndiffSequence(len, b.length, isCommon, found);\n\n// after\nconst len = Math.max(0, a.length - removed);\ndiffSequence(len, b.length, isCommon, found);","handlingStrategy":"validation","validationCode":"const safeLen = (v) => Math.max(0, Math.floor(v));\n// ensure non-negative before calling diffSequence\ndiffSequence(safeLen(aLen), safeLen(bLen), isCommon, found);","typeGuard":"function isNonNegativeInteger(v: unknown): v is number {\n  return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp computed lengths with Math.max(0, ...) to prevent underflow.","Avoid -1 sentinels for 'empty' — use 0 when interfacing with diff-sequences.","Unit-test boundary cases (empty sequences, equal sequences) in your diff wrapper."],"tags":["diff-sequences","input-validation","rangeerror","numeric"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}