diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 9639ded6cce92..bf0a1f8a312ab 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -96,6 +96,7 @@ mod eslint { pub mod no_new_wrappers; pub mod no_nonoctal_decimal_escape; pub mod no_obj_calls; + pub mod no_object_constructor; pub mod no_plusplus; pub mod no_proto; pub mod no_prototype_builtins; @@ -525,6 +526,7 @@ oxc_macros::declare_all_lint_rules! { eslint::max_classes_per_file, eslint::max_lines, eslint::max_params, + eslint::no_object_constructor, eslint::no_alert, eslint::no_array_constructor, eslint::no_async_promise_executor, diff --git a/crates/oxc_linter/src/rules/eslint/no_object_constructor.rs b/crates/oxc_linter/src/rules/eslint/no_object_constructor.rs new file mode 100644 index 0000000000000..2592b9fddc5e7 --- /dev/null +++ b/crates/oxc_linter/src/rules/eslint/no_object_constructor.rs @@ -0,0 +1,178 @@ +use oxc_ast::ast::Expression; +use oxc_ast::AstKind; +use oxc_diagnostics::OxcDiagnostic; +use oxc_macros::declare_oxc_lint; +use oxc_semantic::IsGlobalReference; +use oxc_span::Span; + +use crate::{context::LintContext, rule::Rule, AstNode}; + +fn no_object_constructor_diagnostic(span: Span) -> OxcDiagnostic { + OxcDiagnostic::warn("Disallow calls to the `Object` constructor without an argument") + .with_help("Use object literal notation {} instead") + .with_label(span) +} + +#[derive(Debug, Default, Clone)] +pub struct NoObjectConstructor; + +declare_oxc_lint!( + /// ### What it does + /// + /// Disallow calls to the Object constructor without an argument + /// + /// ### Why is this bad? + /// + /// Use of the Object constructor to construct a new empty object is generally discouraged in favor of object literal notation because of conciseness and because the Object global may be redefined. The exception is when the Object constructor is used to intentionally wrap a specified value which is passed as an argument. + /// + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: + /// ```js + /// Object(); + /// new Object(); + /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```js + /// Object("foo"); + /// const obj = { a: 1, b: 2 }; + /// const isObject = value => value === Object(value); + /// const createObject = Object => new Object(); + /// ``` + NoObjectConstructor, + pedantic, + pending +); + +impl Rule for NoObjectConstructor { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + let (span, callee, arguments, type_parameters) = match node.kind() { + AstKind::CallExpression(call_expr) => ( + call_expr.span, + &call_expr.callee, + &call_expr.arguments, + &call_expr.type_parameters, + ), + AstKind::NewExpression(new_expr) => { + (new_expr.span, &new_expr.callee, &new_expr.arguments, &new_expr.type_parameters) + } + _ => { + return; + } + }; + + let Expression::Identifier(ident) = &callee else { + return; + }; + + if ident.is_global_reference_name("Object", ctx.symbols()) + && arguments.len() == 0 + && type_parameters.is_none() + { + ctx.diagnostic(no_object_constructor_diagnostic(span)); + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec![ + "new Object(x)", + "Object(x)", + "new globalThis.Object", + "const createObject = Object => new Object()", + "var Object; new Object;", + // Disabled because the eslint-test uses languageOptions: { globals: { Object: "off" } } + // "new Object()", + ]; + + let fail = vec![ + "new Object", + "Object()", + "const fn = () => Object();", + "Object() instanceof Object;", + "const obj = Object?.();", + "(new Object() instanceof Object);", + // Semicolon required before `({})` to compensate for ASI + "Object()", + "foo() + Object()", + "var yield = bar.yield + Object()", + "var foo = { bar: baz } + Object()", + " + Object()", + " + Object()", + // No semicolon required before `({})` because ASI does not occur + "Object()", + "{} + Object()", + "function foo() {} + Object()", + "class Foo {} + Object()", + "foo: Object();", + "foo();Object();", + "{ Object(); }", + "if (a) Object();", + "if (a); else Object();", + "while (a) Object();", + "do Object(); while (a);", + "for (let i = 0; i < 10; i++) Object();", + "for (const prop in obj) Object();", + "for (const element of iterable) Object();", + "with (obj) Object();", + // No semicolon required before `({})` because ASI still occurs + "const foo = () => {} + Object()", + "a++ + Object()", + "a-- + Object()", + "function foo() { + return + Object(); + }", + "function * foo() { + yield + Object(); + }", + "do {} while (a) Object()", + "debugger + Object()", + "for (;;) { + break + Object() + }", + r"for (;;) { + continue + Object() + }", + "foo: break foo + Object()", + "foo: while (true) continue foo + Object()", + "const foo = bar + export { foo } + Object()", + "export { foo } from 'bar' + Object()", + r"export * as foo from 'bar' + Object()", + "import foo from 'bar + Object()", + "var yield = 5; + yield: while (foo) { + if (bar) + break yield + new Object(); + }", + ]; + + Tester::new(NoObjectConstructor::NAME, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/no_object_constructor.snap b/crates/oxc_linter/src/snapshots/no_object_constructor.snap new file mode 100644 index 0000000000000..f6d313176e5ca --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_object_constructor.snap @@ -0,0 +1,330 @@ +--- +source: crates/oxc_linter/src/tester.rs +--- + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:1] + 1 │ new Object + · ────────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:1] + 1 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:18] + 1 │ const fn = () => Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:1] + 1 │ Object() instanceof Object; + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:13] + 1 │ const obj = Object?.(); + · ────────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:2] + 1 │ (new Object() instanceof Object); + · ──────────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:1] + 1 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ foo() + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ var yield = bar.yield + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ var foo = { bar: baz } + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:1] + 1 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ {} + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ function foo() {} + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ class Foo {} + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:6] + 1 │ foo: Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:7] + 1 │ foo();Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:3] + 1 │ { Object(); } + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:8] + 1 │ if (a) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:14] + 1 │ if (a); else Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:11] + 1 │ while (a) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:4] + 1 │ do Object(); while (a); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:30] + 1 │ for (let i = 0; i < 10; i++) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:25] + 1 │ for (const prop in obj) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:33] + 1 │ for (const element of iterable) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:12] + 1 │ with (obj) Object(); + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ const foo = () => {} + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ a++ + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ a-- + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:3:13] + 2 │ return + 3 │ Object(); + · ──────── + 4 │ } + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:3:13] + 2 │ yield + 3 │ Object(); + · ──────── + 4 │ } + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:1:17] + 1 │ do {} while (a) Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ debugger + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:3:13] + 2 │ break + 3 │ Object() + · ──────── + 4 │ } + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:3:13] + 2 │ continue + 3 │ Object() + · ──────── + 4 │ } + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ foo: break foo + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ foo: while (true) continue foo + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:3:9] + 2 │ export { foo } + 3 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ export { foo } from 'bar' + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:2:9] + 1 │ export * as foo from 'bar' + 2 │ Object() + · ──────── + ╰──── + help: Use object literal notation {} instead + + × Unterminated string + ╭─[no_object_constructor.tsx:1:17] + 1 │ import foo from 'bar + · ───── + 2 │ Object() + ╰──── + + ⚠ eslint(no-object-constructor): Disallow calls to the `Object` constructor without an argument + ╭─[no_object_constructor.tsx:5:13] + 4 │ break yield + 5 │ new Object(); + · ──────────── + 6 │ } + ╰──── + help: Use object literal notation {} instead