-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
feat(biome_js_analyze): implement useConsistentObjectDefinition
rule
#5042
base: main
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #5042 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall the code looks good, I left some suggestion. I think we settled on a different name
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_literals.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_literals.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_literals.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_literals.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_literals.rs
Outdated
Show resolved
Hide resolved
useConsistentObjectLiterals
useConsistentObjectDefinition
useConsistentObjectDefinition
useConsistentObjectDefinition
rule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of questions and considerations:
- The rule doesn't have any severity, is it intended? FYI rules without severity will have a
Information
severity - There many cases that aren't covered by the tests. Do you intend to implement them later or now in this PR?
/// ### syntax | ||
/// | ||
/// The syntax to use: | ||
/// - `explicit`: enforces the use of explicit object property syntax in every case | ||
/// - `shorthand`: enforces the use of shorthand object property syntax when possible | ||
/// | ||
/// **Default:** `explicit` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would appreciate if you could provide an example with syntax: "shorthand"
. Our infrastructure allows you to do that, here the contribution guide that explains how to set it up: https://github.com/biomejs/biome/blob/main/crates/biome_analyze/CONTRIBUTING.md#using-rule-options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added a ### Valid
and ### Invalid
section for each syntax
option, let me know if it looks good like that so I can resolve this one!
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
pub struct UseConsistentObjectDefinitionOptions { | ||
syntax: ObjectPropertySyntax, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Options must be documented. These are exposed to our users via JSON Schema, so we need to provide a good description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry but what exactly am I supposed to add in this block you commented? I couldn't find other rules writing documentation there.
crates/biome_js_analyze/src/lint/nursery/use_consistent_object_definition.rs
Outdated
Show resolved
Hide resolved
const obj = { | ||
foo: foo, | ||
bar: function () { return "bar"; }, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ESLint rule has an extensive set of tests, we would appreciate we could port some of them.
For example, these cases aren't covered:
const a = {
"foo": foo, // literal
"foo": "foo", // right literal,
['foo']: foo, // computed properties
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the link, I took a bunch of their tests and extracted some to cover pretty much all definitions, they have a lot but most are for options or extra features not yet implemented here.
I also updated the actual rule code because there were indeed some cases my rule was missing, that I only saw because of the new tests.
While in the process of that I got to thinking, currently I consider the following as non-shorthandable:
const obj = {
"stringLiteral": "stringLiteral",
"quotedProperty": quotedProperty,
'singleQuoted': singleQuoted,
}
Which is in fact true, because this is NOT valid:
const obj = {
"stringLiteral",
"quotedProperty",
'singleQuoted',
}
However, the ESLint object-shorthand
has the option avoidQuotes
, which makes it so instead of ignoring the above syntax as non-shorthandable it first recommends using just stringLiteral
instead of "stringLiteral"
, where possible, and then says it can be shorthanded.
I was considering adding this as an option here too but I felt it went a bit against the Biome way since the rule would not be able to be described succinctly anymore. I think another rule like noUnnecessaryPropertyQuotes
would be best and would integrate nicely with this one, or maybe we already have one and I just don't know. What do you think @ematipico?
Summary
Related: #4816
This PR adds a rule inspired by object-shorthand from eslint. It is not a complete port.
I would also like to add a code action in the future, shouldn't be too hard. I may open a new PR for that but at the moment it would be too time consuming to look into.
Test Plan
Snapshot tests for both the options supported by the rule can be found in
crates\biome_js_analyze\tests\specs\nursery\useConsistentObjectLiterals
.