Skip to content

Commit

Permalink
fix(useLiteralKeys): don't report quoted member names (#3111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Jun 7, 2024
1 parent 9590d57 commit 15dc839
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 513 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,23 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

#### Bug fixes

- The `no-empty-block` css lint rule now treats empty blocks containing comments as valid ones. Contributed by @Sec-ant
- The `noEmptyBlock` css lint rule now treats empty blocks containing comments as valid ones. Contributed by @Sec-ant

- [useLiteralKeys](https://biomejs.dev/linter/rules/use-literal-keys/) no longer reports quoted member names ([#3085](https://github.com/biomejs/biome/issues/3085)).

Previously [useLiteralKeys](https://biomejs.dev/linter/rules/use-literal-keys/) reported quoted member names that can be unquoted.
For example, the rule suggested the following fix:

```diff
- const x = { "prop": 0 };
+ const x = { prop: 0 };
```

This conflicted with the option [quoteProperties](https://biomejs.dev/reference/configuration/#javascriptformatterquoteproperties) of our formatter.

The rule now ignores quoted member names.

Contributed by @Conaclos

### Parser

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ where
/// # fn main() -> FormatResult<()> {
/// let context = SimpleFormatContext::new(SimpleFormatOptions {
/// indent_style: IndentStyle::Space,
/// indent_width: 4.into(),
/// indent_width: 4.try_into().unwrap(),
/// ..SimpleFormatOptions::default()
/// });
///
Expand Down
22 changes: 4 additions & 18 deletions crates/biome_js_analyze/src/lint/complexity/use_literal_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use biome_js_factory::make::{
};
use biome_js_syntax::{
AnyJsAssignment, AnyJsComputedMember, AnyJsMemberExpression, AnyJsName, AnyJsObjectMemberName,
JsComputedMemberName, JsLiteralMemberName, JsSyntaxKind, T,
JsComputedMemberName, T,
};
use biome_rowan::{declare_node_union, AstNode, BatchMutationExt, TextRange};
use biome_unicode_table::is_js_ident;
Expand Down Expand Up @@ -70,23 +70,14 @@ impl Rule for UseLiteralKeys {
let node = ctx.query();
let inner_expression = match node {
AnyJsMember::AnyJsComputedMember(computed_member) => computed_member.member().ok()?,
AnyJsMember::JsLiteralMemberName(member) => {
if member.value().ok()?.kind() == JsSyntaxKind::JS_STRING_LITERAL {
let name = member.name().ok()?;
if is_js_ident(&name) {
return Some((member.range(), name.to_string()));
}
}
return None;
}
AnyJsMember::JsComputedMemberName(member) => member.expression().ok()?,
};
let value = inner_expression.as_static_value()?;
let value = value.as_string_constant()?;
// `{["__proto__"]: null }` and `{"__proto__": null}`/`{"__proto__": null}`
// have different semantic.
// The first is a regular property.
// The second is a specical property that changes the object prototype.
// 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__" {
return None;
Expand Down Expand Up @@ -142,13 +133,8 @@ impl Rule for UseLiteralKeys {
}
}
}
AnyJsMember::JsLiteralMemberName(node) => {
mutation.replace_token(node.value().ok()?, make::ident(identifier));
}
AnyJsMember::JsComputedMemberName(member) => {
let name_token = if is_js_ident(identifier) {
make::ident(identifier)
} else if ctx.as_preferred_quote().is_double() {
let name_token = if ctx.as_preferred_quote().is_double() {
make::js_string_literal(identifier)
} else {
make::js_string_literal_single_quotes(identifier)
Expand All @@ -173,5 +159,5 @@ impl Rule for UseLiteralKeys {
}

declare_node_union! {
pub AnyJsMember = AnyJsComputedMember | JsLiteralMemberName | JsComputedMemberName
pub AnyJsMember = AnyJsComputedMember | JsComputedMemberName
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@ a = {
a = {
[`b`]: d
};
a = {
"b": d
};
a.b[`$c`];
a.b["_d"];
class C { ["a"] = 0 }
class C { "a" = 0 }
class C { ["a"](){} }
class C { "a"(){} }
class C { get ["a"](){} }
class C { get "a"(){} }
class C { set ["a"](x){} }
class C { set "a"(x){} }
a = {
["1+1"]: 2
}
Expand All @@ -36,9 +29,6 @@ a = {
a = {
[""]: 2
}
a = {
"__proto__": null,
}

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

0 comments on commit 15dc839

Please sign in to comment.