-
-
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
Add no-common-typos rule #981
Conversation
9a18b31
to
b9596ad
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.
Please add test cases with SFCs, as well as class-based components.
lib/rules/no-common-typos.js
Outdated
* @return {RegExp} | ||
*/ | ||
function parsePattern(pattern) { | ||
if (pattern instanceof RegExp) { |
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.
since configs come from JSON and YAML, when would it be an instanceof RegExp?
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.
Because config can be JavaScript too:
Configuration Files - use a JavaScript, JSON or YAML
http://eslint.org/docs/user-guide/configuring
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.
Then instanceof
is not reliable (due to cross-realm issues) and so we should be converting it to a string and parsing it.
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.
Either way, the schema should require it be a string, since rule schemas don't permit JS-only values, afaik.
return { | ||
JSXAttribute: function(node) { | ||
rules.forEach(function(rule) { | ||
var regex = parsePattern(rule.pattern); |
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'm very concerned about using full user-provided regex patterns without sanitizing them.
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's that? Because of user's stupidity and potential rogue expressions?
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.
Yes, exactly that.
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.
With fixing not enabled this shouldn't be an issue IMO
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.
users using a shared config that then run with --fix
makes it an issue.
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 changed it already so that regular expressions are always treated as string. Updated my previous comment to reflect that.
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'm saying that the user of eslint should be protected against a malicious or accidental config, that might come from a plugin or a shared config.
Which means that the person who writes the pattern can't be trusted to mark it as safe - and neither can the user - which means the rule itself has to ensure that nothing unsafe can possibly happen.
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.
So a predefined set of common typos, or better, common matchers like exact, case-sensitive, etc.?
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.
For example:
"no-common-typos": [{
"case-sensitive": {
"somepattern": "SomePattern",
"dangerouslySetInnerHTML": "dangerouslySetInnerHTML"
},
"exact": {
"colour": "color"
}
}]
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.
That's certainly one way to do it - or alternatively, [{ pattern: "SomePattern": caseInsensitive: true }]
perhaps?
tests/lib/rules/no-common-typos.js
Outdated
'});' | ||
].join('\n'), | ||
options: options, | ||
parser: 'babel-eslint', |
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 does this test need to use babel-eslint? all of these examples should use the default parser.
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.
No idea, just used a different rule as boilerplate.
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 ran the tests without parser
specified, and then it raises errors, so I assume it is needed. Or why should it use the default parser?
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.
The default parser, but when ecmaVersion
set to 6 or higher, and jsx
set to true.
It should use the default parser because that's the important one it works with - if you also want to test that it works with babel-eslint that's fine, but that's secondary.
Also, I'm not sure about the schema. Currently it requires patterns like so: "no-common-typos": [{
pattern: /somepattern/i,
correct: "SomePattern"
}] But maybe a format like this is better: "no-common-typos": {
"SomePattern": /somepattern/i,
"OtherPattern": /otherpattern/i
} |
Would it be more useful to have a rule that knows what all of the DOM element props can be, and if you use something that isn't one of those it warns? That would have the advantages of working for everyone out of the box, and being specific for each individual node type. |
It's also worth noting that React itself does this for all DOM elements in React v15.3 and later. |
This can probably be closed now that we have the |
Indeed, this is a bit duplicated by #1209. Happy to reopen if it can be adapted. |
I had it quite some time that I incorrectly used
dangerouslySetInnerHtml
instead ofdangerouslySetInnerHTML
.To prevent certain typos I introduce a rule for which you can set a pattern and the corrected string.
I can imagine this might not only apply to attributes, in which case this rule could be extended to also validate props and other variables.
Todo: