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 ignoreProps
option to ignore props validation
#2146
Conversation
…lidation Co-authored-by: Bharat Soni <i.bharat.soni@gmail.com> Co-authored-by: Serg Hospodarets <shospodarets@gmail.com>
@ljharb added new option to validate props. I've set it to true by default so that behaviour stays the same, user can set the value to false in order to remove props validation. One more thing, rule passes if prop is a strings literal( |
@ljharb the build is breaking from starting, it works fine in my local, is it because I am using ES6 syntax? Could you please help me if you are aware of the error.. |
docs/rules/jsx-no-literals.md
Outdated
|
||
* `noStrings` - Enforces no string literals used as children, wrapped or unwrapped. | ||
* `validateProps` - Enforces no template literals used as props. |
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.
why only template? i'd expect any literals to be detected by this option
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.
I was trying to mimic the same behaviour as before, will detect string literals too.
@ljharb I see you removed your comment to detect and disallow string literals in props, shall I add that feature or not.. |
@iiison i marked it resolved because you indicated you'd incorporate it. There's no reason for a rule called "literals" to differentiate between string and template. |
@ljharb my laptop was broken for some time, can you please provide the context of the last comment that you resolved.. I will fix it as soon as I get a little context of it. |
@iiison see #2146 (comment), i've unmarked it as resolved. basically, we want template and string literals checked. |
@ljharb if we don't allow strings in props, then you need to declare variables for all props like |
@iiison sure, that's why you might not want to enable the option. There's no difference between |
@ljharb updated the PR. Added requested changes. But build is failing. The code is failing in few versions of Node(not sure why), can you please help me with that. |
lib/rules/jsx-no-literals.js
Outdated
const sourceCode = context.getSourceCode(); | ||
create: function({ | ||
report, | ||
options: [configs = {}], |
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.
does this syntax work in node 4?
lib/rules/jsx-no-literals.js
Outdated
create: function(context) { | ||
const isNoStrings = context.options[0] ? context.options[0].noStrings : false; | ||
const sourceCode = context.getSourceCode(); | ||
create: function({ |
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.
let's try going back to the non-destructuring form.
lib/rules/jsx-no-literals.js
Outdated
} = configs; | ||
const isNoStrings = noStrings; | ||
const shouldValidateProps = validateProps; | ||
const sourceCode = getSourceCode(); |
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.
in particular, it seems that eslint 3 relies on the receiver (the this
value) of getSourceCode
being context
.
fixed all errors! |
@ljharb Let me know if you want to add more changes to the PR. |
tests/lib/rules/jsx-no-literals.js
Outdated
{message: stringsMessage('\'bar\'')} | ||
] | ||
}, { | ||
code: '<Foo event={() => {}} />', |
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.
this rule is about string literals. why would a function value ever trigger an error?
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.
I suppose I went a little too far on this. Will remove the logic.
docs/rules/jsx-no-literals.md
Outdated
``` | ||
|
||
```jsx | ||
var Hello = <div event={()=>{}} />; |
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.
should this be removed from the readme?
@ljharb I'm not sure why, but AppVeyor is failing out of nowhere. I just deleted 2 lines from the readme file, not sure why it is breaking. |
I'm not sure either, looks like a jest error - either way, could you try rebasing on latest master instead of merging? |
The build was failing when I didn't merge the master in the branch. but will try the rebase as well. |
5ac178c
to
29d70e6
Compare
rebased with the master, still, the build is failing with the same error. |
@ljharb build is also failing for the master |
e76c282
to
8a1eb1e
Compare
Blocked by #2253. |
@iiison any interest in moving this forward? Happy to take a look if you're no longer vested in this fix. |
@mauricecruz Please go ahead. I won't be able to work on this for a while. |
@mauricecruz @ljharb @iiison Any progress on this or is it still blocked? If nobody else has the time I can probably find some time to finish this up soon. |
jsx-no-literals
: add validateProps
option to ignore props validation
448f86c
to
cf2210c
Compare
tests/lib/rules/jsx-no-literals.js
Outdated
errors: [{message: stringsMessage('`Test`')}] | ||
}, { | ||
code: '<Foo bar={`${baz}`} />', | ||
options: [{noStrings: true}], | ||
options: [{noStrings: true, validateProps: true}], |
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.
These test changes show that the option (that doesn't exist yet) seems to already be true
by default prior to this PR. In that case, this option should probably be more like ignoreProps: true
which defaults to false.
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.
@ljharb just checking... Is this the only pending requested change?
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.
@Gasparila at the moment, yes - i'll have to give it a rereview after it's next updated
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 found this great react eslint rule to use in a big product and faced the same problem that it goes through the props, which was a bit surprising, but ok.
In short, the current rule behavior is to check props, facilitating it to be disabled by default may break some existing users functionalities, who may rely on this.
+1 , is should be either changed to ````validateProps(
true` default)``` in the current naming, or switched to `ignoreProps`
cf2210c
to
6dd14b9
Compare
ping @iiison, are you still interested in completing this PR? |
@SalimBensiali if you want to post a link to your branch with the needed changes (do NOT post a new PR, please) then i'm happy to pull it in. |
Hey @ljharb @SalimBensiali @iiison |
122721f
to
13e649e
Compare
jsx-no-literals
: add validateProps
option to ignore props validationjsx-no-literals
: add ignoreProps
option to ignore props validation
13e649e
to
fa863ce
Compare
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 @iiison and @malyw!
Fixes #2142