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 5 commits into
base: next
Choose a base branch
from

Conversation

dy0gu
Copy link

@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

Comment on lines 184 to 186
RuleDiagnostic::new(rule_category!(), node.range(), markup! {{title}}).note(
markup! { "Using a consistent object literal syntax makes it easier to anticipate the structure of an object." },
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our diagnostics must match our rule pillars. In this case, we don't have a message for the error, which should be the first one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but that part is the note, I used the title for the error, or does this not count as saying what the error is?

let title = match options.syntax {
  ObjectLiteralSyntax::Shorthand => {
      "Use shorthand object property syntax whenever possible."
  }
  ObjectLiteralSyntax::Explicit => {
      "Always use explicit object property assignment syntax."
  }
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I read "Use shorthand object property, it tells the user how to solve the error, which is the third pillar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, just updated it. I believe the title now covers the first pillar and the note does a good job at covering both the second and third, while a code action is not implemented.

That should be everything, let me know what you think!

@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

#[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.

Comment on lines +86 to +88
#[default]
Explicit,
Shorthand,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's document the variants.

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
}

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