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/useLiteralKeys): unescaped newline false positive #3210

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
- The [`noUnmatchableAnbSelector`](https://biomejs.dev/linter/rules/no-unmatchable-anb-selector/) rule is now able to catch unmatchable `an+b` selectors like `0n+0` or `-0n+0`. Contributed by @Sec-ant.
- The [`useHookAtTopLevel`](https://biomejs.dev/linter/rules/use-hook-at-top-level/) rule now recognizes properties named as hooks like `foo.useFoo()`. Contributed by @ksnyder9801
- Fix [#3092](https://github.com/biomejs/biome/issues/3092), prevent warning for `Custom properties (--*)`. Contributed by @chansuke
- Fix a false positive in the [`useLiteralKeys`](https://biomejs.dev/linter/rules/use-literal-keys/) rule. ([#3160](https://github.com/biomejs/biome/issues/3160))

This rule now ignores the following kind of computed member name:

```js
const a = {
[`line1
line2`]: true,
};
```

Contributed by @Sec-ant

### Parser

Expand Down
29 changes: 27 additions & 2 deletions crates/biome_js_analyze/src/lint/complexity/use_literal_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ impl Rule for UseLiteralKeys {
if matches!(node, AnyJsMember::JsComputedMemberName(_)) && value == "__proto__" {
return None;
}
// A computed property `["something"]` can always be simplified to a string literal "something".
if matches!(node, AnyJsMember::JsComputedMemberName(_)) || is_js_ident(value) {
// A computed property `["something"]` can always be simplified to a string literal "something",
// unless it is a template literal inside that contains unescaped new line characters:
//
// const a = {
// `line1
// line2`: true
// }
//
if (matches!(node, AnyJsMember::JsComputedMemberName(_)) && !has_unescaped_new_line(value))
|| is_js_ident(value)
{
return Some((inner_expression.range(), value.to_string()));
}
None
Expand Down Expand Up @@ -161,3 +170,19 @@ impl Rule for UseLiteralKeys {
declare_node_union! {
pub AnyJsMember = AnyJsComputedMember | JsComputedMemberName
}

fn has_unescaped_new_line(text: &str) -> bool {
let mut iter = text.as_bytes().iter();
while let Some(c) = iter.next() {
match c {
b'\\' => {
iter.next();
}
b'\n' => {
return true;
}
_ => {}
}
}
false
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ a = {

// optional chain
a?.["b"]?.['c']?.d?.e?.["f"]
a = {
["line1\
line2"]: true,
};
a = {
[`line1\
line2`]: true,
};
a = {
["line1\nline2"]: true,
};
a = {
[`line1\nline2`]: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ a = {

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

a = {
["line1\
line2"]: true,
};
a = {
[`line1\
line2`]: true,
};
a = {
["line1\nline2"]: true,
};
a = {
[`line1\nline2`]: true,
};
```

# Diagnostics
Expand Down Expand Up @@ -566,7 +579,8 @@ invalid.js:34:5 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
33 │ // optional chain
> 34 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
35 │
35 │ a = {
36 │ ["line1\

i Unsafe fix: Use a literal key instead.

Expand All @@ -583,7 +597,8 @@ invalid.js:34:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
33 │ // optional chain
> 34 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
35 │
35 │ a = {
36 │ ["line1\

i Unsafe fix: Use a literal key instead.

Expand All @@ -600,11 +615,109 @@ invalid.js:34:25 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
33 │ // optional chain
> 34 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ ^^^
35 │
35 │ a = {
36 │ ["line1\

i Unsafe fix: Use a literal key instead.

34 │ a?.["b"]?.['c']?.d?.e?.["f"]
│ -- --

```

```
invalid.js:36:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The computed expression can be simplified without the use of a string literal.
Sec-ant marked this conversation as resolved.
Show resolved Hide resolved

34 │ a?.["b"]?.['c']?.d?.e?.["f"]
35 │ a = {
> 36 │ ["line1\
│ ^^^^^^^
> 37 │ line2"]: true,
│ ^^^^^^
38 │ };
39 │ a = {

i Unsafe fix: Use a literal key instead.

34 34 │ a?.["b"]?.['c']?.d?.e?.["f"]
35 35 │ a = {
36 │ - ··["line1\
37 │ - ··line2"]:·true,
36 │ + ··"line1\
37 │ + ··line2":·true,
38 38 │ };
39 39 │ a = {


```

```
invalid.js:40:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The computed expression can be simplified without the use of a string literal.

38 │ };
39 │ a = {
> 40 │ [`line1\
│ ^^^^^^^
> 41 │ line2`]: true,
│ ^^^^^^
42 │ };
43 │ a = {

i Unsafe fix: Use a literal key instead.

38 38 │ };
39 39 │ a = {
40 │ - ··[`line1\
41 │ - ··line2`]:·true,
40 │ + ··"line1\
41 │ + ··line2":·true,
42 42 │ };
43 43 │ a = {


```

```
invalid.js:44:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The computed expression can be simplified without the use of a string literal.

42 │ };
43 │ a = {
> 44 │ ["line1\nline2"]: true,
│ ^^^^^^^^^^^^^^
45 │ };
46 │ a = {

i Unsafe fix: Use a literal key instead.

44 │ ··["line1\nline2"]:·true,
│ - -

```

```
invalid.js:47:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! The computed expression can be simplified without the use of a string literal.

45 │ };
46 │ a = {
> 47 │ [`line1\nline2`]: true,
│ ^^^^^^^^^^^^^^
48 │ };

i Unsafe fix: Use a literal key instead.

45 45 │ };
46 46 │ a = {
47 │ - ··[`line1\nline2`]:·true,
47 │ + ··"line1\nline2":·true,
48 48 │ };


```
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ a = {
// Exception
a = {
["__proto__"]: null,
}
}
a = {
[`line1
line2`]: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ a = {
a = {
["__proto__"]: null,
}
a = {
[`line1
line2`]: true,
};

```