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

fix(lint/useForOf): handle shorthand property #1666

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#1640](https://github.com/biomejs/biome/issues/1640). [useEnumInitializers](https://biomejs.dev/linter/rules/use-enum-initializers) code action now generates valid code when last member has a comment but no comma. Contributed by @kalleep

- Fix [#1653](https://github.com/biomejs/biome/issues/1653). Handle a shorthand value in `useForOf` to avoid the false-positive case. Contributed by @togami2864

### Parser

## 1.5.3 (2024-01-22)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic, Ru
use biome_console::markup;
use biome_js_semantic::{ReferencesExtensions, SemanticModel};
use biome_js_syntax::{
AnyJsExpression, AnyJsForInitializer, AnyJsStatement, JsAssignmentExpression,
JsAssignmentOperator, JsBinaryExpression, JsBinaryOperator, JsForStatement,
JsIdentifierBinding, JsIdentifierExpression, JsPostUpdateExpression, JsPostUpdateOperator,
JsPreUpdateExpression, JsPreUpdateOperator, JsSyntaxKind, JsSyntaxToken, JsUnaryOperator,
AnyJsExpression, AnyJsForInitializer, AnyJsObjectMember, AnyJsStatement,
JsAssignmentExpression, JsAssignmentOperator, JsBinaryExpression, JsBinaryOperator,
JsForStatement, JsIdentifierBinding, JsIdentifierExpression, JsPostUpdateExpression,
JsPostUpdateOperator, JsPreUpdateExpression, JsPreUpdateOperator,
JsShorthandPropertyObjectMember, JsSyntaxKind, JsSyntaxToken, JsUnaryOperator,
JsVariableDeclarator,
};
use biome_rowan::{declare_node_union, AstNode, AstSeparatedList, TextRange};
Expand Down Expand Up @@ -52,7 +53,7 @@ declare_node_union! {
}

declare_node_union! {
pub(crate) AnyBindingExpression = JsPostUpdateExpression | JsPreUpdateExpression | JsIdentifierExpression
pub(crate) AnyBindingExpression = JsPostUpdateExpression | JsPreUpdateExpression | JsIdentifierExpression | JsShorthandPropertyObjectMember
}

impl Rule for UseForOf {
Expand Down Expand Up @@ -99,15 +100,13 @@ impl Rule for UseForOf {
.as_js_static_member_expression()?
.object()
.ok()?;

let index_only_used_with_array = |reference| {
let array_in_use = reference_being_used_by_array(reference, &array_used_in_for)
.is_some_and(|array_in_use| array_in_use);
let is_delete = is_delete(reference).is_some_and(|is_delete| is_delete);

array_in_use && !is_delete
};

if references.iter().all(index_only_used_with_array) {
Some(())
} else {
Expand Down Expand Up @@ -147,6 +146,12 @@ fn list_initializer_references(
return None;
}

if let Some(AnyJsObjectMember::JsShorthandPropertyObjectMember(expr)) =
AnyJsObjectMember::cast(reference.syntax().parent()?)
{
return Some(AnyBindingExpression::from(expr));
}

AnyBindingExpression::try_from(AnyJsExpression::cast(reference.syntax().parent()?)?)
.ok()
})
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/useForOf/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ for (let i = 0; i < arr.length; i++) {
arr[i] = 0;
}

for (let i = 0; i < array.length; i++) {
console.log({ i });
}

for (var c = 0; c < arr.length; c++) {
doMath(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ for (let i = 0; i < arr.length; i++) {
arr[i] = 0;
}

for (let i = 0; i < array.length; i++) {
console.log({ i });
}

for (var c = 0; c < arr.length; c++) {
doMath(c);
}
Expand Down
2 changes: 2 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#1640](https://github.com/biomejs/biome/issues/1640). [useEnumInitializers](https://biomejs.dev/linter/rules/use-enum-initializers) code action now generates valid code when last member has a comment but no comma. Contributed by @kalleep

- Fix [#1653](https://github.com/biomejs/biome/issues/1653). Handle a shorthand value in `useForOf` to avoid the false-positive case. Contributed by @togami2864

### Parser

## 1.5.3 (2024-01-22)
Expand Down
Loading