-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[New] jsx-no-literals
: add elementOverrides
option
#3812
[New] jsx-no-literals
: add elementOverrides
option
#3812
Conversation
… to ignore this rule on specific elements
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3812 +/- ##
==========================================
+ Coverage 97.49% 97.79% +0.29%
==========================================
Files 132 135 +3
Lines 9707 9825 +118
Branches 3554 3609 +55
==========================================
+ Hits 9464 9608 +144
+ Misses 243 217 -26 ☔ View full report in Codecov by Sentry. |
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.
Initial review.
d676a54
to
3c98eb2
Compare
2608777
to
8fa7280
Compare
8fa7280
to
b8217ed
Compare
Not sure what to do about the failing |
Don’t worry about it :-) |
Resolves #3808
Preface - I didn't intend to rewrite the whole rule, but I ended up needing to re-write the way the rule fetches and applies the config which resulted in the majority of the rule being reworked.
What is the purpose of this PR?
Adds a new option,
elementOverrides
which adds the ability to override the rule options for any named component. Specifying options withinelementOverrides
creates a new context for the rule, so the rule will only apply the new options to the specified element and its children (ifapplyToNestedElements
istrue
- see below). This means that the root rule options will not apply to the specified element.elementOverrides
is an object where the keys are the element names and the values are objects with the same options that this rule previously supported (noStrings
,allowedStrings
,noAttributeStrings
andignoreProps
).elementOverrides
also supports 2 new options:allowElement
andapplyToNestedElements
.allowElement
(default:false
) - Whentrue
the rule will allow the specified element to have string literals as children, wrapped or unwrapped without warning. If thenoStrings
option is also specified, then only unwrapped literals will be allowed through. This option satisfies [Feature Request]: AddallowedElements
option tojsx-no-literals
#3808.applyToNestedElements
(default:true
) - Whenfalse
the rule will not apply the current options set to nested elements. This is useful when you want to apply the rule to a specific element, but not to its children.The element name must match the following regular expression. It does not support intrinsic JSX elements (ie.
div
orspan
) and all elements that do not match the pattern will be filtered out when the config is normalized. The element name can also be a compound/namespaced/sub component (JSXMemberExpression
) likeModal.Button
orReact.Fragment
. If an element name likeFragment
is specified, the override config will match on either of the following components:The rule will also construct a simple import (or require) map specifically for renamed imports (ie
import { A as B } from 'c';
. The rule will match on the original (imported
) specifier instead of the renamed (local
) specifier.What should reviewers focus on?
Most of the actual rule/option logic has remained unchanged. However, I re-ordered some of the node visitors for performance with regards to when
getOverrideConfig
is called since it is a relatively expensive function. Also, a lot of the visitor functions needed the current config passed in which required some changes within those too.How is this PR tested?
This PR adds roughly 50 tests. Each option (new and old) is individually tested for both valid and invalid cases. For each option, there are roughly 3 states that are tested:
All extraneous features are also tested such as imports/requires renames, the element name pattern and compound components
Based on a manual scan of the coverage report, I think all branches that need to be covered have been covered by tests. There are a few extra that exist to validate type check.