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(linter): implement react/style-prop-object #6342

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

Conversation

nekosaur
Copy link

@nekosaur nekosaur commented Oct 7, 2024

Implements not recommended rule react/style-prop-object (#1022)

Copy link

graphite-app bot commented Oct 7, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added the A-linter Area - Linter label Oct 7, 2024
return false;
};

let AstKind::VariableDeclarator(var_decl) = node.kind() else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider checking FormalParameters as well

Copy link
Author

Choose a reason for hiding this comment

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

I don't have a great grasp of the AST yet, but can FormalParameters also be something other than function parameters?

Copy link
Collaborator

Choose a reason for hiding this comment

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

They're used in type parameters, but I don't think that's relevant here.

return false;
};

return is_invalid_expression(var_decl.init.as_ref(), ctx);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You may be able to check type annotations here for simple cases. See no-throw-literal for an example.

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean as a cheaper way instead of checking .init?

Copy link
Collaborator

Choose a reason for hiding this comment

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

function SomeComponent(props) {
  let foo: string | undefined
  if (bar) foo = 'foo'
  return <div style={foo} />
}


fn style_prop_object_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Style prop value must be an object")
.with_help("Make sure the 'style' prop value is an object")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This help message is redundant and adds no extra information that the diagnostic message does not already provide. Consider removing it or providing more information (e.g. what type we think style is, if not an object)

declare_oxc_lint!(
/// ### What it does
/// Require that the value of the prop style be an object or a variable that is an object.
///
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing "Why is this bad?" section.

---
⚠ eslint-plugin-react(style-prop-object): Style prop value must be an object
╭─[style_prop_object.tsx:1:6]
1 │ <div style="color: 'red'" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shrink the span to only cover the attribute's value

.map(|v| {
v.iter()
.filter_map(serde_json::Value::as_str)
.map(ToString::to_string)
Copy link
Collaborator

Choose a reason for hiding this comment

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

use CompactStr instead of Strings

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, if you sort this, you can use a binary search for O(log(n)) containment checks (instead of .contains, which is O(n).

@DonIsaac
Copy link
Collaborator

DonIsaac commented Oct 7, 2024

Thanks for your interest in Oxc! This PR will be a great contribution. Just a few small fixes and we should be able to merge this.

Copy link

codspeed-hq bot commented Oct 12, 2024

CodSpeed Performance Report

Merging #6342 will not alter performance

Comparing nekosaur:feat/style-prop-object-rule (c66e40f) with main (9f555d7)

Summary

✅ 30 untouched benchmarks

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants