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

Add no-common-typos rule #981

Closed
wants to merge 8 commits into from
Closed

Conversation

koenpunt
Copy link

@koenpunt koenpunt commented Nov 28, 2016

I had it quite some time that I incorrectly used dangerouslySetInnerHtml instead of dangerouslySetInnerHTML.
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:

  • Documentation

Copy link
Member

@ljharb ljharb left a 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.

* @return {RegExp}
*/
function parsePattern(pattern) {
if (pattern instanceof RegExp) {
Copy link
Member

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?

Copy link
Author

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

Copy link
Member

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.

Copy link
Member

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);
Copy link
Member

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.

Copy link
Author

@koenpunt koenpunt Nov 28, 2016

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?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, exactly that.

Copy link
Author

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

Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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.?

Copy link
Author

@koenpunt koenpunt Nov 29, 2016

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"
  }
}]

Copy link
Member

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?

'});'
].join('\n'),
options: options,
parser: 'babel-eslint',
Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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?

Copy link
Member

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.

@koenpunt
Copy link
Author

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
}

@lencioni
Copy link
Collaborator

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.

@ljharb
Copy link
Member

ljharb commented Nov 29, 2016

It's also worth noting that React itself does this for all DOM elements in React v15.3 and later.

@jseminck
Copy link
Contributor

This can probably be closed now that we have the no-typos rule? Any specific cases that we want to add/support can go to that rule instead.

@ljharb
Copy link
Member

ljharb commented Jul 17, 2017

Indeed, this is a bit duplicated by #1209.

Happy to reopen if it can be adapted.

@ljharb ljharb closed this Jul 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

4 participants