Skip to content

Latest commit

 

History

History
132 lines (111 loc) · 10.5 KB

File metadata and controls

132 lines (111 loc) · 10.5 KB

new-for-builtins

📝 Enforce correct use of new for builtin constructors.

💼 This rule is enabled in the following configs: ✅ recommended, ☑️ unopinionated.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Many builtin constructors can be called with or without new, but new should be preferred for consistency with constructors that require it. This rule also reports selected builtin namespace objects that are not callable or constructible.

For non-callable top-level namespace objects covered by ESLint core, like Math, JSON, Reflect, Atomics, Intl, and Temporal, use no-obj-calls.

Enforces the use of new for the following builtins:

Disallows the use of new for the following builtins:

These should not use new as that would create object wrappers for the primitive values, which is not what you want. However, without new they can be useful for coercing a value to that type.

This rule is fixable, except new String(), new Number(), and new Boolean(), which return wrapped objects. Optional chains that would require adding new are ignored, as new cannot be optional. Reports for builtin namespace objects are not fixable.

Disallows calling or constructing the following builtin namespace objects:

Examples

// ❌
const list = Array(10);

// ✅
const list = new Array(10);
// ❌
const now = Date();

// ✅
const now = String(new Date());
// ❌
const map = Map([
	['foo', 'bar']
]);

// ✅
const map = new Map([
	['foo', 'bar']
]);