noExtendNative
Summary
Section titled “Summary”- Rule available since:
v2.5.7 - Diagnostic Category:
lint/nursery/noExtendNative - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-extend-native
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noExtendNative": "error" } } }}Description
Section titled “Description”Disallow extending the prototype of built-in objects.
Adding properties to the prototype of a built-in such as Object,
Array, or Error leaks into every value of that type. The new
property shows up in every for...in, collides with other libraries
that patch the same prototype, and breaks assumptions across the whole
program. Extend a subclass or use a standalone helper instead.
This rule flags a direct prototype assignment
(Builtin.prototype.x = ...), computed prototype access
(Builtin["prototype"].x = ...), and
Object.defineProperty/Object.defineProperties targeting a
built-in prototype.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”Object.prototype.extra = "a";code-block.js:1:1 lint/nursery/noExtendNative ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid extending the prototype of ‘Object’.
> 1 │ Object.prototype.extra = “a”;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Patching a native prototype leaks the property into every value of that type and can collide with other code.
ℹ Use a subclass or a standalone helper instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Array.prototype.times = function () {};code-block.js:1:1 lint/nursery/noExtendNative ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid extending the prototype of ‘Array’.
> 1 │ Array.prototype.times = function () {};
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Patching a native prototype leaks the property into every value of that type and can collide with other code.
ℹ Use a subclass or a standalone helper instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Object.defineProperty(Array.prototype, "times", { value: 999 });code-block.js:1:1 lint/nursery/noExtendNative ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid extending the prototype of ‘Array’.
> 1 │ Object.defineProperty(Array.prototype, “times”, { value: 999 });
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Patching a native prototype leaks the property into every value of that type and can collide with other code.
ℹ Use a subclass or a standalone helper instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class CustomArray extends Array {}const obj = {};obj.extra = "a";Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.