Skip to content

Commit

Permalink
fix: clean the code of no-array-constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuhiro-Mimaki committed Apr 21, 2024
1 parent 8bca04f commit 143a6b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions crates/biome_js_analyze/src/lint/nursery/no_array_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use biome_rowan::AstNode;
declare_rule! {
/// Disallow Array constructors.
///
/// The corresponding ESLint rule:
/// https://eslint.org/docs/latest/rules/no-array-constructor#rule-details
/// Use of the Array constructor to construct a new array is generally discouraged in favor of array literal notation because of the single-argument pitfall and because the Array global may be redefined.
/// The exception is when the Array constructor is used to intentionally create sparse arrays of a specified size by giving the constructor a single numeric argument.
///
/// ## Examples
///
Expand Down Expand Up @@ -71,6 +71,9 @@ impl Rule for NoArrayConstructor {
"Don't use Array constructors."
},
)
.note(markup! {
"Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument."
})
.note(markup! {
"The array literal notation [] is preferable."
}),
Expand All @@ -79,13 +82,13 @@ impl Rule for NoArrayConstructor {
}

fn validate(callee: &AnyJsExpression, arguments: &JsCallArguments) -> Option<()> {
let len = arguments.args().into_iter().count();
let mut args_iter = arguments.args().into_iter();
let first_arg = args_iter.next();
let second_arg = args_iter.next();
if callee.text() == "Array" {
if len == 1
&& !matches!(
arguments.args().into_iter().next()?.ok()?,
AnyJsCallArgument::JsSpread(_)
)
if first_arg.is_some()
&& second_arg.is_none()
&& !matches!(first_arg?.ok()?, AnyJsCallArgument::JsSpread(_))
{
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ invalid.js:1:1 lint/nursery/noArrayConstructor ━━━━━━━━━━━
2 │
3 │ Array(0, 1, 2);
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand All @@ -46,6 +48,8 @@ invalid.js:3:1 lint/nursery/noArrayConstructor ━━━━━━━━━━━
4 │
5 │ Array(...args);
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand All @@ -63,6 +67,8 @@ invalid.js:5:1 lint/nursery/noArrayConstructor ━━━━━━━━━━━
6 │
7 │ new Array();
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand All @@ -80,6 +86,8 @@ invalid.js:7:1 lint/nursery/noArrayConstructor ━━━━━━━━━━━
8 │
9 │ new Array(0, 1, 2);
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand All @@ -97,6 +105,8 @@ invalid.js:9:1 lint/nursery/noArrayConstructor ━━━━━━━━━━━
10 │
11 │ new Array(...args);
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand All @@ -113,6 +123,8 @@ invalid.js:11:1 lint/nursery/noArrayConstructor ━━━━━━━━━━
│ ^^^^^^^^^^^^^^^^^^^
12 │
i Use of the Array constructor is not allowed except creating sparse arrays of a specified size by giving a single numeric argument.
i The array literal notation [] is preferable.
Expand Down
4 changes: 2 additions & 2 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 143a6b3

Please sign in to comment.