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

chore(linter): improve the documentation of eslint/no-sparse-arrays #9180

Merged
Merged
Changes from all 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
36 changes: 33 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_sparse_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,43 @@ declare_oxc_lint!(
///
/// ### Why is this bad?
///
/// The confusion around sparse arrays is enough that it’s recommended to avoid using them unless you are certain that they are useful in your code.
/// Take the following example:
///
/// ### Example
/// ```javascript
/// const items = [,,];
/// ```
///
/// While the items array in this example has a length of 2, there are actually
/// no values in items[0] or items[1]. The fact that the array literal is
/// valid with only commas inside, coupled with the length being set and
/// actual item values not being set, make sparse arrays confusing for many
/// developers.
///
/// The confusion around sparse arrays is enough that it’s recommended to
/// avoid using them unless you are certain that they are useful in your
/// code.
///
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```javascript
/// var items = [,,];
/// ```
///
/// ```javascript
/// var colors = [ "red",, "blue" ];
/// ```
///
/// Examples of **correct** code for this rule:
/// ```javascript
/// var items = [];
/// ```
///
/// // trailing comma (after the last element) is not a problem
/// ```javascript
/// var colors = [ "red", "blue", ];
/// ```
NoSparseArrays,
eslint,
correctness
Expand Down Expand Up @@ -79,7 +109,7 @@ impl Rule for NoSparseArrays {
fn test() {
use crate::tester::Tester;

let pass = vec!["var a = [ 1, 2, ]"];
let pass = vec!["var a = [ 1, 2, ]", "var a = [];"];

let fail = vec![
"var a = [,];",
Expand Down