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/noNonNullAssertion): dont create an incorrect unsafe fix for x[y.z!] #1757

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Changes from 1 commit
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
Next Next commit
fix: omit the case where the non-null assertion is inside []
ah-yu committed Feb 6, 2024
commit 4d7554754bb8f7f6abe3d9377ffa36f1f6e77178
Original file line number Diff line number Diff line change
@@ -102,7 +102,13 @@ impl Rule for NoNonNullAssertion {
let old_node = AnyJsExpression::TsNonNullAssertionExpression(node.clone());

match node.parent::<AnyJsExpression>()? {
AnyJsExpression::JsComputedMemberExpression(parent) => {
AnyJsExpression::JsComputedMemberExpression(parent)
if parent.object().is_ok_and(|object| {
object
.as_ts_non_null_assertion_expression()
.is_some_and(|object| object == node)
}) =>
{
if parent.is_optional() {
// object!?["prop"] --> object?.["prop"]
mutation.replace_node(old_node, assertion_less_expr);
Original file line number Diff line number Diff line change
@@ -18,3 +18,4 @@ x.y.z!?.();
x.y.z!!!?.();
(b! as number) = "test";
(b!! as number) = "test";
x[y.z!];
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ x.y.z!?.();
x.y.z!!!?.();
(b! as number) = "test";
(b!! as number) = "test";
x[y.z!];
```

@@ -405,7 +406,7 @@ invalid.ts:19:2 lint/style/noNonNullAssertion ━━━━━━━━━━━
> 19 │ (b! as number) = "test";
│ ^^
20 │ (b!! as number) = "test";
21 │
21 │ x[y.z!];
```
@@ -419,7 +420,22 @@ invalid.ts:20:2 lint/style/noNonNullAssertion ━━━━━━━━━━━
19 │ (b! as number) = "test";
> 20 │ (b!! as number) = "test";
│ ^^^
21 │
21 │ x[y.z!];
22 │
```

```
invalid.ts:21:3 lint/style/noNonNullAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Forbidden non-null assertion.
19 │ (b! as number) = "test";
20 │ (b!! as number) = "test";
> 21 │ x[y.z!];
│ ^^^^
22 │
```