Skip to content

Commit

Permalink
feat(prefer-primordials): discourage to use array pattern assignments (
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken authored Jan 2, 2023
1 parent 2896b0f commit 05a641e
Showing 1 changed file with 116 additions and 11 deletions.
127 changes: 116 additions & 11 deletions src/rules/prefer_primordials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ enum PreferPrimordialsHint {
#[display(fmt = "Instead use the equivalent from the `primordials` object")]
GlobalIntrinsic,
#[display(fmt = "Wrap a SafeIterator from the `primordials` object")]
Iterator,
SafeIterator,
#[display(fmt = "Instead use the object pattern destructuring assignment")]
ObjectPattern,
#[display(
fmt = "Instead use `ObjectPrototypeIsPrototypeOf` from the `primordials` object"
)]
Expand Down Expand Up @@ -191,7 +193,7 @@ impl Handler for PreferPrimordialsHandler {
expr_or_spread.range(),
CODE,
PreferPrimordialsMessage::Iterator,
PreferPrimordialsHint::Iterator,
PreferPrimordialsHint::SafeIterator,
);
}
}
Expand All @@ -207,7 +209,7 @@ impl Handler for PreferPrimordialsHandler {
for_of_stmt.right.range(),
CODE,
PreferPrimordialsMessage::Iterator,
PreferPrimordialsHint::Iterator,
PreferPrimordialsHint::SafeIterator,
);
}
}
Expand Down Expand Up @@ -281,6 +283,57 @@ impl Handler for PreferPrimordialsHandler {
);
}
}

fn array_pat(&mut self, array_pat: &ast_view::ArrayPat, ctx: &mut Context) {
use ast_view::{Expr, Node, Pat};

// If array_pat.elems don't include rest pattern, should be used object pattern instead
// For example:
//
// ```js
// const [a, b] = [1, 2];
// ```
//
// should be turned into:
//
// ```js
// const { 0: a, 1: b } = [1, 2];
// ```
if !matches!(array_pat.elems.last(), Some(Some(Pat::Rest(_)))) {
ctx.add_diagnostic_with_hint(
array_pat.range(),
CODE,
PreferPrimordialsMessage::Iterator,
PreferPrimordialsHint::ObjectPattern,
);
return;
}

match array_pat.parent() {
Node::VarDeclarator(var_declarator) => {
if !matches!(var_declarator.init, Some(Expr::New(_)) | None) {
ctx.add_diagnostic_with_hint(
var_declarator.range(),
CODE,
PreferPrimordialsMessage::Iterator,
PreferPrimordialsHint::SafeIterator,
);
}
}
Node::AssignExpr(asssign_expr) => {
if !matches!(asssign_expr.right, Expr::New(_)) {
ctx.add_diagnostic_with_hint(
asssign_expr.range(),
CODE,
PreferPrimordialsMessage::Iterator,
PreferPrimordialsHint::SafeIterator,
);
}
}
// TODO(petamoriken): Support for deeply nested assignments
_ => (),
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -389,6 +442,22 @@ for (const val of new SafeArrayIterator(arr)) {}
for (const val of new SafeArrayIterator([1, 2, 3])) {}
"#,
r#"
const { 0: a, 1: b } = [1, 2];
"#,
r#"
let a, b;
({ 0: a, 1: b } = [1, 2]);
"#,
r#"
const { SafeArrayIterator } = primordials;
const [a, b, ...c] = new SafeArrayIterator([1, 2, 3]);
"#,
r#"
const { SafeArrayIterator } = primordials;
let a, b, c;
[a, b, ...c] = new SafeArrayIterator([1, 2, 3]);
"#,
r#"
const { indirectEval } = primordials;
indirectEval("console.log('This test should pass.');");
"#,
Expand Down Expand Up @@ -565,56 +634,92 @@ const noop = Function.prototype;
{
col: 7,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"foo(1, 2, ...arr);"#: [
{
col: 10,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"new Foo(1, 2, ...arr);"#: [
{
col: 14,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"[1, 2, ...[3]];"#: [
{
col: 7,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"foo(1, 2, ...[3]);"#: [
{
col: 10,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"new Foo(1, 2, ...[3]);"#: [
{
col: 14,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"for (const val of arr) {}"#: [
{
col: 18,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"for (const val of [1, 2, 3]) {}"#: [
{
col: 18,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"const [a, b] = [1, 2];"#: [
{
col: 6,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::ObjectPattern,
},
],
r#"
let a, b;
[a, b] = [1, 2];
"#: [
{
line: 3,
col: 0,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::ObjectPattern,
},
],
r#"const [a, b, ...c] = [1, 2, 3];"#: [
{
col: 6,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"
let a, b, c;
[a, b, ...c] = [1, 2, 3];
"#: [
{
line: 3,
col: 0,
message: PreferPrimordialsMessage::Iterator,
hint: PreferPrimordialsHint::SafeIterator,
},
],
r#"eval("console.log('This test should fail!');");"#: [
Expand Down

0 comments on commit 05a641e

Please sign in to comment.