|
| 1 | +use oxc_ast::ast::Expression; |
| 2 | +use oxc_ast::AstKind; |
| 3 | +use oxc_diagnostics::OxcDiagnostic; |
| 4 | +use oxc_macros::declare_oxc_lint; |
| 5 | +use oxc_semantic::IsGlobalReference; |
| 6 | +use oxc_span::Span; |
| 7 | + |
| 8 | +use crate::{context::LintContext, rule::Rule, AstNode}; |
| 9 | + |
| 10 | +fn no_object_constructor_diagnostic(span: Span) -> OxcDiagnostic { |
| 11 | + OxcDiagnostic::warn("Disallow calls to the `Object` constructor without an argument") |
| 12 | + .with_help("Use object literal notation {} instead") |
| 13 | + .with_label(span) |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Default, Clone)] |
| 17 | +pub struct NoObjectConstructor; |
| 18 | + |
| 19 | +declare_oxc_lint!( |
| 20 | + /// ### What it does |
| 21 | + /// |
| 22 | + /// Disallow calls to the Object constructor without an argument |
| 23 | + /// |
| 24 | + /// ### Why is this bad? |
| 25 | + /// |
| 26 | + /// 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. |
| 27 | + /// |
| 28 | + /// ### Examples |
| 29 | + /// |
| 30 | + /// Examples of **incorrect** code for this rule: |
| 31 | + /// ```js |
| 32 | + /// Object(); |
| 33 | + /// new Object(); |
| 34 | + /// ``` |
| 35 | + /// |
| 36 | + /// Examples of **correct** code for this rule: |
| 37 | + /// ```js |
| 38 | + /// Object("foo"); |
| 39 | + /// const obj = { a: 1, b: 2 }; |
| 40 | + /// const isObject = value => value === Object(value); |
| 41 | + /// const createObject = Object => new Object(); |
| 42 | + /// ``` |
| 43 | + NoObjectConstructor, |
| 44 | + pedantic, |
| 45 | + pending |
| 46 | +); |
| 47 | + |
| 48 | +impl Rule for NoObjectConstructor { |
| 49 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 50 | + let (span, callee, arguments, type_parameters) = match node.kind() { |
| 51 | + AstKind::CallExpression(call_expr) => ( |
| 52 | + call_expr.span, |
| 53 | + &call_expr.callee, |
| 54 | + &call_expr.arguments, |
| 55 | + &call_expr.type_parameters, |
| 56 | + ), |
| 57 | + AstKind::NewExpression(new_expr) => { |
| 58 | + (new_expr.span, &new_expr.callee, &new_expr.arguments, &new_expr.type_parameters) |
| 59 | + } |
| 60 | + _ => { |
| 61 | + return; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + let Expression::Identifier(ident) = &callee else { |
| 66 | + return; |
| 67 | + }; |
| 68 | + |
| 69 | + if ident.is_global_reference_name("Object", ctx.symbols()) |
| 70 | + && arguments.len() == 0 |
| 71 | + && type_parameters.is_none() |
| 72 | + { |
| 73 | + ctx.diagnostic(no_object_constructor_diagnostic(span)); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn test() { |
| 80 | + use crate::tester::Tester; |
| 81 | + |
| 82 | + let pass = vec![ |
| 83 | + "new Object(x)", |
| 84 | + "Object(x)", |
| 85 | + "new globalThis.Object", |
| 86 | + "const createObject = Object => new Object()", |
| 87 | + "var Object; new Object;", |
| 88 | + // Disabled because the eslint-test uses languageOptions: { globals: { Object: "off" } } |
| 89 | + // "new Object()", |
| 90 | + ]; |
| 91 | + |
| 92 | + let fail = vec![ |
| 93 | + "new Object", |
| 94 | + "Object()", |
| 95 | + "const fn = () => Object();", |
| 96 | + "Object() instanceof Object;", |
| 97 | + "const obj = Object?.();", |
| 98 | + "(new Object() instanceof Object);", |
| 99 | + // Semicolon required before `({})` to compensate for ASI |
| 100 | + "Object()", |
| 101 | + "foo() |
| 102 | + Object()", |
| 103 | + "var yield = bar.yield |
| 104 | + Object()", |
| 105 | + "var foo = { bar: baz } |
| 106 | + Object()", |
| 107 | + "<foo /> |
| 108 | + Object()", |
| 109 | + "<foo></foo> |
| 110 | + Object()", |
| 111 | + // No semicolon required before `({})` because ASI does not occur |
| 112 | + "Object()", |
| 113 | + "{} |
| 114 | + Object()", |
| 115 | + "function foo() {} |
| 116 | + Object()", |
| 117 | + "class Foo {} |
| 118 | + Object()", |
| 119 | + "foo: Object();", |
| 120 | + "foo();Object();", |
| 121 | + "{ Object(); }", |
| 122 | + "if (a) Object();", |
| 123 | + "if (a); else Object();", |
| 124 | + "while (a) Object();", |
| 125 | + "do Object(); while (a);", |
| 126 | + "for (let i = 0; i < 10; i++) Object();", |
| 127 | + "for (const prop in obj) Object();", |
| 128 | + "for (const element of iterable) Object();", |
| 129 | + "with (obj) Object();", |
| 130 | + // No semicolon required before `({})` because ASI still occurs |
| 131 | + "const foo = () => {} |
| 132 | + Object()", |
| 133 | + "a++ |
| 134 | + Object()", |
| 135 | + "a-- |
| 136 | + Object()", |
| 137 | + "function foo() { |
| 138 | + return |
| 139 | + Object(); |
| 140 | + }", |
| 141 | + "function * foo() { |
| 142 | + yield |
| 143 | + Object(); |
| 144 | + }", |
| 145 | + "do {} while (a) Object()", |
| 146 | + "debugger |
| 147 | + Object()", |
| 148 | + "for (;;) { |
| 149 | + break |
| 150 | + Object() |
| 151 | + }", |
| 152 | + r"for (;;) { |
| 153 | + continue |
| 154 | + Object() |
| 155 | + }", |
| 156 | + "foo: break foo |
| 157 | + Object()", |
| 158 | + "foo: while (true) continue foo |
| 159 | + Object()", |
| 160 | + "const foo = bar |
| 161 | + export { foo } |
| 162 | + Object()", |
| 163 | + "export { foo } from 'bar' |
| 164 | + Object()", |
| 165 | + r"export * as foo from 'bar' |
| 166 | + Object()", |
| 167 | + "import foo from 'bar |
| 168 | + Object()", |
| 169 | + "var yield = 5; |
| 170 | + yield: while (foo) { |
| 171 | + if (bar) |
| 172 | + break yield |
| 173 | + new Object(); |
| 174 | + }", |
| 175 | + ]; |
| 176 | + |
| 177 | + Tester::new(NoObjectConstructor::NAME, pass, fail).test_and_snapshot(); |
| 178 | +} |
0 commit comments