oxc-project/oxc · error · OxcDiagnostic

Use `String(new Date())` instead of `Date()`

Error message

Use `String(new Date())` instead of `Date()`

What it means

Lint diagnostic from unicorn/new-for-builtins (error_date): Date was called as a function. Unlike other builtins the rule enforces with new, Date() without new returns a string of the current time, which is almost never intended when a Date instance is required.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/new_for_builtins.rs:21

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`.
    ///
    /// ### Why is this bad?
    ///
    /// Using `new` inconsistently can cause confusion. Constructors like `Array` and `RegExp` should always use `new`

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use new Date() when a Date object is needed
  2. Use String(new Date()) explicitly if a timestamp string is actually intended
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/new_for_builtins.rs:21 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/d76497cb9b8963bd. Report an issue: GitHub.