Skip to content

no-foreign-prototype-patch ​

Changing the prototype of a class the module doesn't own: an imported class (Batcher.prototype.begin = ..., PIXI.Batcher.prototype), or a global one other than the ECMAScript built-ins (HTMLElement.prototype, WebSocket.prototype). Assignments, compound assignments and delete of its members, Object.defineProperty, Object.defineProperties, Object.assign, Object.setPrototypeOf, Reflect.defineProperty, Reflect.set and Reflect.deleteProperty on it, also through a binding that holds it (const proto = Batcher.prototype as Patched; proto.begin = ...), and handing it to a function that patches its parameter (patchMethod(Batcher.prototype, "begin", ...)), of the same file or imported from another. At module level too.

Why: the class outlives the module that patches it. A package is loaded once, but the module patching it can run again while the package stays loaded: each boot of the app in a test file, a hot reload, a page booted again in a test DOM. Each run patches on top of the last patch, so the method grows a wrapper per run, and each wrapper keeps the module instance that made it alive, with everything that module held. Guards like let installed = false don't help: the guard is reset with the module, the prototype isn't. An adopter's client tests grew by about 150 MB per boot this way, until they ran out of heap.

ts
const original = WebSocket.prototype.send;
const proto = WebSocket.prototype;

proto.send = function (this: WebSocket, data: Parameters<WebSocket["send"]>[0]) {
  console.debug("send");
  original.call(this, data);
};

✅ Accepted: wrap the call, not the class

✓ Accepted0 diagnostics
ts
export function sendTraced(socket: WebSocket, data: string): void {
  console.debug("send", data.length);
  socket.send(data);
}

A helper doesn't change that: the call is reported where the prototype is handed over.

ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

function patchClose(proto: Mut<WebSocket>, make: (original: WebSocket["close"]) => WebSocket["close"]): void {
  proto.close = make(proto.close);
}

patchClose(mut(WebSocket.prototype), (close) => function (this: WebSocket, ...args: Parameters<WebSocket["close"]>) {
  console.debug("close");
  close.apply(this, args);
});
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";

function patchClose(proto: Mut<WebSocket>, make: (original: WebSocket["close"]) => WebSocket["close"]): void {
  proto.close = make(proto.close);
}

patchClose(WebSocket.prototype, (close) => function (this: WebSocket, ...args: Parameters<WebSocket["close"]>) {
  console.debug("close");
  close.apply(this, args);
});

A subclass (class TracedBatcher extends Batcher { override begin() { ... } }) changes nothing shared either. Where the library leaves no other way in (a protected method it calls itself), keep the original once, on the prototype or in a WeakMap keyed by it, and always build the patch from that original, so a second run replaces the first patch instead of wrapping it. Mark that line // safe-ts-ignore no-foreign-prototype-patch: <why>, so the escape hatch is counted.

What counts ​

  • Foreign: an imported binding, a member of one (ns.Batcher), or a global (HTMLElement, window.EventTarget).
  • Your own classes (declared in the module) aren't reported. The class is made again each time its module runs, so a patch made next to it doesn't pile up. Still, a method belongs in the class body, where its shape is fixed from the start.
  • The ECMAScript built-ins written out at the patch (Array.prototype.last = ..., Object.defineProperty(String.prototype, ...)) are oxlint's no-extend-native, which the preset turns on. That rule covers only those built-ins written out; this one covers imported classes, the platform's other classes (DOM, WebSocket, ...) and a prototype held in a binding (const proto = Array.prototype), the built-ins included there.
  • A helper whose parameter it patches (proto[name] = ..., Object.defineProperty(proto, ...), Reflect.set(proto, ...), delete proto.x, or handing it on to another such helper): the call that hands it a foreign prototype is reported, the built-ins included, since no-extend-native doesn't follow the call. The helper can be of the same file or imported from another (by name, as a namespace's member patchers.patch, the default export, through re-exports, or a const copy of one), and it can hand the parameter on to helpers of other files in turn. A helper is a function declared at the top level of its file (function, or a const holding an arrow or function expression); one declared in a .d.ts file isn't followed.
  • Reading a prototype (const begin = Batcher.prototype.begin) or passing it to a function that only reads it isn't a patch.

This rule alone reports a foreign prototype patch. no-mutable-global used to report the ones made in a function too, advising to patch once at module load; that isn't enough when the module can run again, so it now leaves them to this rule, and a line gets one finding with advice that holds.

What the rule can't see: a helper that is a method, a class's static member or a value handed around (a parameter, a field), a prototype reached through Object.getPrototypeOf(instance), and a class re-exported under a local binding (const B = vendor.Batcher; B.prototype.x = ...).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra