oxc-project/oxc · error · OxcDiagnostic
Use `{fn_name}()` instead of `new {fn_name}()`
Error message
Use `{fn_name}()` instead of `new {fn_name}()` What it means
Raised by unicorn/new-for-builtins when configured to disallow `new`, for builtins (e.g. Object, Function, Array without args) where using the constructor form is discouraged. The faulting input is the `new FnName()` expression at the labeled span.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/new_for_builtins.rs:17
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::BinaryOperator;
use crate::{
AstNode, context::LintContext, globals::GLOBAL_OBJECT_NAMES, rule::Rule,
utils::call_uses_optional_chain,
};
fn enforce(span: Span, fn_name: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Use `new {fn_name}()` instead of `{fn_name}()`")).with_label(span)
}
fn disallow(span: Span, fn_name: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Use `{fn_name}()` instead of `new {fn_name}()`")).with_label(span)
}
fn error_date(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Use `String(new Date())` instead of `Date()`").with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NewForBuiltins;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces the use of `new` for the following builtins: `Object`, `Array`, `ArrayBuffer`, `BigInt64Array`,
/// `BigUint64Array`, `DataView`, `Date`, `Error`, `Float16Array`, `Float32Array`, `Float64Array`, `Function`, `Int8Array`,
/// `Int16Array`, `Int32Array`, `Map`, `WeakMap`, `Set`, `WeakSet`, `Promise`, `RegExp`, `Uint8Array`,
/// `Uint16Array`, `Uint32Array`, `Uint8ClampedArray`, `SharedArrayBuffer`, `Proxy`, `WeakRef`, `FinalizationRegistry`.
///
/// Disallows the use of `new` for the following builtins: `String`, `Number`, `Boolean`, `Symbol`, `BigInt`.View on GitHub (pinned to e1e7af627c)
Solutions
- Drop `new` and call the builtin directly, e.g. `Object(x)` instead of `new Object(x)`
- Prefer literal forms like `{}`, `[]`, `() => {}` over the equivalent constructors
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/new_for_builtins.rs:17 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/bceaec110d9abdcd.
Report an issue: GitHub.