Skip to content

Commit

Permalink
chore improve diagnostic message
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Jun 14, 2024
1 parent 350cbc1 commit 3e284b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
37 changes: 26 additions & 11 deletions crates/biome_js_analyze/src/lint/complexity/use_literal_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ declare_rule! {

impl Rule for UseLiteralKeys {
type Query = Ast<AnyJsMember>;
type State = (TextRange, String);
type State = (TextRange, String, bool);
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let mut is_computed_member_name = false;
let inner_expression = match node {
AnyJsMember::AnyJsComputedMember(computed_member) => computed_member.member().ok()?,
AnyJsMember::JsComputedMemberName(member) => member.expression().ok()?,
AnyJsMember::JsComputedMemberName(member) => {
is_computed_member_name = true;
member.expression().ok()?
}
};
let value = inner_expression.as_static_value()?;
let value = value.as_string_constant()?;
Expand All @@ -79,7 +83,7 @@ impl Rule for UseLiteralKeys {
// The first is a regular property.
// The second is a special property that changes the object prototype.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
if matches!(node, AnyJsMember::JsComputedMemberName(_)) && value == "__proto__" {
if is_computed_member_name && value == "__proto__" {
return None;
}
// A computed property `["something"]` can always be simplified to a string literal "something",
Expand All @@ -90,25 +94,36 @@ impl Rule for UseLiteralKeys {
// line2`: true
// }
//
if (matches!(node, AnyJsMember::JsComputedMemberName(_)) && !has_unescaped_new_line(value))
|| is_js_ident(value)
{
return Some((inner_expression.range(), value.to_string()));
if (is_computed_member_name && !has_unescaped_new_line(value)) || is_js_ident(value) {
return Some((
inner_expression.range(),
value.to_string(),
is_computed_member_name,
));
}
None
}

fn diagnostic(_ctx: &RuleContext<Self>, (range, _): &Self::State) -> Option<RuleDiagnostic> {
fn diagnostic(
_ctx: &RuleContext<Self>,
(range, _, is_computed_member_name): &Self::State,
) -> Option<RuleDiagnostic> {
Some(RuleDiagnostic::new(
rule_category!(),
range,
markup! {
"The computed expression can be simplified without the use of a string literal."
if *is_computed_member_name {
markup! {
"The computed expression can be simplified to a string literal."
}
} else {
markup! {
"The computed expression can be simplified without the use of a string literal."
}
},
))
}

fn action(ctx: &RuleContext<Self>, (_, identifier): &Self::State) -> Option<JsRuleAction> {
fn action(ctx: &RuleContext<Self>, (_, identifier, _): &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let mut mutation = ctx.root().begin();
match node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ invalid.js:10:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:12:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
10 │ a.b[c["d"]] = "something";
11 │ a = {
Expand All @@ -364,7 +364,7 @@ invalid.js:12:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:15:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
13 │ };
14 │ a = {
Expand Down Expand Up @@ -436,7 +436,7 @@ invalid.js:18:5 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:19:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
17 │ a.b[`$c`];
18 │ a.b["_d"];
Expand All @@ -455,7 +455,7 @@ invalid.js:19:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:20:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
18 │ a.b["_d"];
19 │ class C { ["a"] = 0 }
Expand All @@ -474,7 +474,7 @@ invalid.js:20:12 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:21:16 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
19 │ class C { ["a"] = 0 }
20 │ class C { ["a"](){} }
Expand All @@ -493,7 +493,7 @@ invalid.js:21:16 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:22:16 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
20 │ class C { ["a"](){} }
21 │ class C { get ["a"](){} }
Expand All @@ -512,7 +512,7 @@ invalid.js:22:16 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:24:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
22 │ class C { set ["a"](x){} }
23 │ a = {
Expand All @@ -531,7 +531,7 @@ invalid.js:24:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:27:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
25 │ }
26 │ a = {
Expand All @@ -555,7 +555,7 @@ invalid.js:27:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:30:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
28 │ }
29 │ a = {
Expand Down Expand Up @@ -628,7 +628,7 @@ invalid.js:34:25 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:36:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
34 │ a?.["b"]?.['c']?.d?.e?.["f"]
35 │ a = {
Expand Down Expand Up @@ -656,7 +656,7 @@ invalid.js:36:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:40:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
38 │ };
39 │ a = {
Expand Down Expand Up @@ -684,7 +684,7 @@ invalid.js:40:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:44:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
42 │ };
43 │ a = {
Expand All @@ -703,7 +703,7 @@ invalid.js:44:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.js:47:4 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
45 │ };
46 │ a = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type T = {
```
invalid.ts:2:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
1 │ export interface I {
> 2 │ ["p1"]: number
Expand All @@ -52,7 +52,7 @@ invalid.ts:2:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━
```
invalid.ts:4:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
2 │ ["p1"]: number
3 │
Expand All @@ -71,7 +71,7 @@ invalid.ts:4:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━
```
invalid.ts:6:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
4 │ get ["p2"](): number
5 │
Expand All @@ -90,7 +90,7 @@ invalid.ts:6:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━
```
invalid.ts:8:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
6 │ set ["p2"](x: number)
7 │
Expand All @@ -109,7 +109,7 @@ invalid.ts:8:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━
```
invalid.ts:10:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
8 │ ["m1"](): void
9 │
Expand All @@ -128,7 +128,7 @@ invalid.ts:10:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.ts:14:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
13 │ export type T = {
> 14 │ ["p1"]: number
Expand All @@ -146,7 +146,7 @@ invalid.ts:14:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.ts:16:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
14 │ ["p1"]: number
15 │
Expand All @@ -165,7 +165,7 @@ invalid.ts:16:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.ts:18:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
16 │ get ["p2"](): number
17 │
Expand All @@ -184,7 +184,7 @@ invalid.ts:18:7 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.ts:20:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
18 │ set ["p2"](x: number)
19 │
Expand All @@ -203,7 +203,7 @@ invalid.ts:20:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━
```
invalid.ts:22:3 lint/complexity/useLiteralKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The computed expression can be simplified without the use of a string literal.
! The computed expression can be simplified to a string literal.
20 │ ["m1"](): void
21 │
Expand Down

0 comments on commit 3e284b5

Please sign in to comment.