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/noSelfAssign): correctly handle literals #385

Merged
merged 2 commits into from
Sep 22, 2023
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 @@ -37,6 +37,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
#### Bug fixes

- Fix [#243](https://github.com/biomejs/biome/issues/243) a false positive case where the incorrect scope was defined for the `infer` type. in rule [noUndeclaredVariables](https://biomejs.dev/linter/rules/no-undeclared-variables/). Contributed by @denbezrukov
- Fix [#322](ttps://github.com/biomejs/biome/issues/322), now [noSelfAssign](https://biomejs.dev/linter/rules/no-self-assign/) correctly handles literals inside call expressions.
- Changed how [noSelfAssign](https://biomejs.dev/linter/rules/no-self-assign/) behaves. The rule is not triggered anymore on function calls. Contributed by @ematipico

#### New features

Expand Down
35 changes: 12 additions & 23 deletions crates/biome_js_analyze/src/analyzers/correctness/no_self_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use biome_js_syntax::{
inner_string_text, AnyJsArrayAssignmentPatternElement, AnyJsArrayElement, AnyJsAssignment,
AnyJsAssignmentPattern, AnyJsExpression, AnyJsLiteralExpression, AnyJsName,
AnyJsObjectAssignmentPatternMember, AnyJsObjectMember, JsAssignmentExpression,
JsAssignmentOperator, JsCallExpression, JsComputedMemberAssignment, JsComputedMemberExpression,
JsAssignmentOperator, JsComputedMemberAssignment, JsComputedMemberExpression,
JsIdentifierAssignment, JsLanguage, JsName, JsPrivateName, JsReferenceIdentifier,
JsStaticMemberAssignment, JsStaticMemberExpression, JsSyntaxToken,
};
Expand Down Expand Up @@ -334,6 +334,8 @@ impl SameIdentifiers {
.ok()?;
return Some(AnyAssignmentLike::Identifiers(source_identifier));
}
} else if identifier_like.is_literal() {
return Some(AnyAssignmentLike::Identifiers(identifier_like));
} else {
return Self::next_static_expression(left, right);
}
Expand Down Expand Up @@ -424,6 +426,7 @@ impl AnyJsAssignmentExpressionLikeIterator {
AnyJsExpression::JsIdentifierExpression(node) => {
Ok(AnyNameLike::from(node.name()?))
}
AnyJsExpression::AnyJsLiteralExpression(node) => Ok(AnyNameLike::from(node)),
_ => Err(SyntaxError::MissingRequiredChild),
})?,
source_object: source.object()?,
Expand All @@ -438,6 +441,8 @@ impl AnyJsAssignmentExpressionLikeIterator {
AnyJsExpression::JsIdentifierExpression(node) => {
Ok(AnyNameLike::from(node.name()?))
}
AnyJsExpression::AnyJsLiteralExpression(node) => Ok(AnyNameLike::from(node)),

_ => Err(SyntaxError::MissingRequiredChild),
})?,
source_object: source.object()?,
Expand Down Expand Up @@ -477,12 +482,7 @@ impl Iterator for AnyJsAssignmentExpressionLikeIterator {
self.drained = true;
Some(identifier.name().ok()?)
}
AnyJsExpression::JsCallExpression(call_expression) => {
self.current_member_expression = Some(
AnyAssignmentExpressionLike::JsCallExpression(call_expression),
);
None
}

AnyJsExpression::JsComputedMemberExpression(computed_expression) => {
self.current_member_expression = Some(
AnyAssignmentExpressionLike::JsComputedMemberExpression(computed_expression),
Expand Down Expand Up @@ -547,7 +547,7 @@ declare_node_union! {
}

declare_node_union! {
pub(crate) AnyAssignmentExpressionLike = JsStaticMemberExpression | JsComputedMemberExpression | JsCallExpression
pub(crate) AnyAssignmentExpressionLike = JsStaticMemberExpression | JsComputedMemberExpression
}

impl AnyAssignmentExpressionLike {
Expand All @@ -565,28 +565,13 @@ impl AnyAssignmentExpressionLike {
})
})
}
AnyAssignmentExpressionLike::JsCallExpression(node) => node
.callee()
.ok()
.and_then(|callee| callee.as_js_static_member_expression().cloned())
.and_then(|callee| callee.member().ok())
.and_then(|member| {
let js_name = member.as_js_name()?;
Some(AnyNameLike::from(AnyJsName::JsName(js_name.clone())))
}),
}
}

fn object(&self) -> Option<AnyJsExpression> {
match self {
AnyAssignmentExpressionLike::JsStaticMemberExpression(node) => node.object().ok(),
AnyAssignmentExpressionLike::JsComputedMemberExpression(node) => node.object().ok(),
AnyAssignmentExpressionLike::JsCallExpression(node) => node
.callee()
.ok()?
.as_js_static_member_expression()?
.object()
.ok(),
}
}
}
Expand Down Expand Up @@ -759,6 +744,10 @@ impl IdentifiersLike {
IdentifiersLike::Literal(_, right) => right.value_token().ok(),
}
}

const fn is_literal(&self) -> bool {
matches!(self, IdentifiersLike::Literal(_, _))
}
}

/// Checks if the left identifier and the right reference have the same name
Expand Down
12 changes: 10 additions & 2 deletions crates/biome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,16 @@ mod tests {
String::from_utf8(buffer).unwrap()
}

const SOURCE: &str = r#"value['optimizelyService'] = optimizelyService
const SOURCE: &str = r#"document
.querySelector(`[data-field-id="customModel-container"]`)
.querySelector('input').value = document
.querySelector(`[data-field-id="${modelField.id}-field"]`)
.querySelector('input').value;

"#;
// const SOURCE: &str = r#"document.querySelector("foo").value = document.querySelector("foo").value
//
// "#;

let parsed = parse(SOURCE, JsFileSource::tsx(), JsParserOptions::default());

Expand All @@ -251,7 +259,7 @@ mod tests {
closure_index: Some(0),
dependencies_index: Some(1),
};
let rule_filter = RuleFilter::Rule("complexity", "useLiteralKeys");
let rule_filter = RuleFilter::Rule("correctness", "noSelfAssign");
options.configuration.rules.push_rule(
RuleKey::new("nursery", "useHookAtTopLevel"),
RuleOptions::new(HooksOptions { hooks: vec![hook] }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ a = a;
a.b = a.b;
a.#b = a.#b;
a[b] = a[b];
a.b().c = a.b().c;
a.b.c = a.b.c;
({a} = {a});
a['b'].bar = a['b'].bar;
a[foobar].b = a[foobar].b;
a[10].b = a[10].b;
a[4] = a[4];
Loading
Loading