Skip to content

Commit

Permalink
fix(analyzer): useLiteralKeys removes optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Sep 9, 2023
1 parent 98caa69 commit 9e3338c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

The rule now relies on control flow analysis and thus reports more complex case fallthrough.

#### Bug fixes

- Fix [#182](https://github.com/biomejs/biome/issues/182), making [useLiteralKeys](https://biomejs.dev/linter/rules/use-literal-keys/) retains optional chain.

### Parser
### VSCode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ impl Rule for UseLiteralKeys {
AnyJsMember::AnyJsComputedMember(node) => {
let object = node.object().ok()?;
let member = js_name(ident(identifier));
let token = node.optional_chain_token().unwrap_or_else(|| token(T![.]));
let static_expression =
js_static_member_expression(object, token(T![.]), AnyJsName::JsName(member));
js_static_member_expression(object, token, AnyJsName::JsName(member));
mutation.replace_element(
node.clone().into_syntax().into(),
static_expression.into_syntax().into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ a = {
a = {
[""]: 2
}

// optional chain
a?.["b"]?.['c']?.d?.e?.["f"]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ a = {
[""]: 2
}

// optional chain
a?.["b"]?.['c']?.d?.e?.["f"]

```
# Diagnostics
Expand Down Expand Up @@ -647,4 +650,55 @@ invalid.js:37:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```

```
invalid.js:41:5 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
40 │ // optional chain
> 41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
42 │
i Suggested fix: Use a literal key instead.
41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ -- --
```

```
invalid.js:41:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
40 │ // optional chain
> 41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
42 │
i Suggested fix: Use a literal key instead.
41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ -- --
```

```
invalid.js:41:25 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
40 │ // optional chain
> 41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
42 │
i Suggested fix: Use a literal key instead.
41 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ -- --
```


0 comments on commit 9e3338c

Please sign in to comment.