Skip to content

Commit

Permalink
feat(linter): Implement eslint/no-object-constructor (#7345)
Browse files Browse the repository at this point in the history
A test case for `new Object()` has been commented out:

This is due to the test configuration specifying `globals: { Object:
"off" }`.
This approach follows the example set by the no_new_wrappers rule.
[Reference
Code](https://github.com/oxc-project/oxc/blob/bf839c1dfa1dcf6a48ec1fcadf49a1fe839e6f06/crates/oxc_linter/src/rules/eslint/no_new_wrappers.rs#L88)

---------

Co-authored-by: no-yan <[email protected]>
Co-authored-by: Cameron Clark <[email protected]>
  • Loading branch information
3 people authored Nov 23, 2024
1 parent 59e7e46 commit 00060ca
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 0 deletions.
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
178 changes: 178 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,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()",
"<foo />
Object()",
"<foo></foo>
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();
}
Loading

0 comments on commit 00060ca

Please sign in to comment.