Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): Implement eslint/no-object-constructor #7345

Merged
merged 8 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
105 changes: 105 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_object_constructor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 {
// See <https://oxc.rs/docs/contribute/linter/adding-rules.html#diagnostics> for details
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(
crate::rules::eslint::no_object_constructor::no_object_constructor_diagnostic(span),
);
azihsoyn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[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);",
];
camc314 marked this conversation as resolved.
Show resolved Hide resolved

Tester::new(NoObjectConstructor::NAME, pass, fail).test_and_snapshot();
}
45 changes: 45 additions & 0 deletions crates/oxc_linter/src/snapshots/no_object_constructor.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
⚠ 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
Loading