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

feat(biome_js_analyze): implement useConsistentObjectDefinition rule #5042

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

dy0gu
Copy link
Contributor

@dy0gu dy0gu commented Feb 6, 2025

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.

@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels Feb 6, 2025
Copy link

codspeed-hq bot commented Feb 6, 2025

CodSpeed Performance Report

Merging #5042 will not alter performance

Comparing dy0gu:use-consistent-object-literals (624c4e3) with next (13869c0)

Summary

✅ 94 untouched benchmarks

Copy link
Member

@ematipico ematipico left a 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

@dy0gu dy0gu changed the title feat(biome_js_analyze): implement useConsistentObjectLiterals feat(biome_js_analyze): implement useConsistentObjectDefinition Feb 7, 2025
@dy0gu dy0gu changed the title feat(biome_js_analyze): implement useConsistentObjectDefinition feat(biome_js_analyze): implement useConsistentObjectDefinition rule Feb 7, 2025
@dy0gu dy0gu requested a review from ematipico February 11, 2025 09:28
Copy link
Member

@ematipico ematipico left a 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?

Comment on lines +57 to +63
/// ### 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`
Copy link
Member

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

Copy link
Contributor Author

@dy0gu dy0gu Feb 12, 2025

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,
Copy link
Member

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.

Copy link
Contributor Author

@dy0gu dy0gu Feb 12, 2025

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.

Comment on lines 1 to 4
const obj = {
foo: foo,
bar: function () { return "bar"; },
};
Copy link
Member

@ematipico ematipico Feb 11, 2025

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
}

Copy link
Contributor Author

@dy0gu dy0gu Feb 12, 2025

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?

@ematipico ematipico deleted the branch biomejs:main February 12, 2025 11:41
@ematipico ematipico closed this Feb 12, 2025
@ematipico ematipico reopened this Feb 12, 2025
@ematipico ematipico changed the base branch from next to main February 12, 2025 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CLI Area: CLI A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants