Skip to content

Rule for requiring HTML entities to be escaped #681

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

Merged
merged 1 commit into from
Sep 16, 2016

Conversation

pfhayes
Copy link
Contributor

@pfhayes pfhayes commented Jul 15, 2016

I will also add in some documentation around this, but wanted to get this out there to make sure I was on the right track

I ran this on our codebase and it caught a bunch of errors so it appears to work

code: [
'class Comp1 extends Component {',
' render() {',
' return <div>Multiple errors: \'>></div>;',
Copy link
Member

Choose a reason for hiding this comment

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

can we also ensure that {'>'} is allowed, by adding a test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the test on line 70 should cover that case

Copy link
Member

Choose a reason for hiding this comment

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

whoops, missed that. thanks, that works.

'}'
].join('\n'),
args: [1],
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.

wait, why is babel-eslint required? the default parser should be able to handle all of these examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay - I can remove this. I copied this from another test and didn't know it could be removed

Copy link
Member

Choose a reason for hiding this comment

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

if it works when removed, then i'd say please do :-) if not, then obv you should keep it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I tried removing it and now all the tests fail with "Parsing error: Unexpected token <"

Is that correct? how is the JSX compiled if babel-eslint is not used?

Copy link
Member

Choose a reason for hiding this comment

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

ah - we may need to enable the jsx parser option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that something I can do? I tried setting parser: 'jsx' but that didn't work

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great thanks! the latest version should use the correct parser

@pfhayes
Copy link
Contributor Author

pfhayes commented Jul 19, 2016

Any thoughts on this rule? Happy to make any recommended updates.

We've been running this in development and have already gotten a lot of value out of it. It has caught several errors around mistyped closing tags

cc @yannickcr

// Rule Definition
// ------------------------------------------------------------------------------

var DEFAULTS = ['>', '<', '"', '\'', '}'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see the value in linting for >, since you have provided such a good example of what that can be problematic. However, I'm not so sure about the other characters listed in the defaults here. Can you provide similar reasoning for each of these characters?

Copy link
Member

Choose a reason for hiding this comment

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

I think <<div> is just as problematic as </div>>, and {<Foo />}} and {{<Foo />} so those seem obvious to me (let's add {?)

I'm not sure I see the purpose in having double or single quotes in here, though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I also found the omission of { curious. In any case, it would be really great if the documentation included an example of why each of the defaults can be problematic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I omitted { because it's not actually possible to include this character inside a tag - this character begins a curly-brace section and is a syntax error. I could include it here for completeness but AFAICT would have no effect.

This does raise a good point that < is also extraneous, I should remove that.

Here's an example where quotes can bite you:

<Foo
  a="b"
  c="d">
  e="f"

Perhaps you get into this state by sorting props alphabetically (this is where it has happened to me). I'm happy to update the documentation to include this example as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added examples for all the cases to the documentation.

@ljharb
Copy link
Member

ljharb commented Jul 25, 2016

@pfhayes please rebase so as to remove merge commits from the PR :-)

@pfhayes
Copy link
Contributor Author

pfhayes commented Jul 25, 2016

I've squashed this into a single commit

// included accidentally.
var DEFAULTS = ['>', '"', '\'', '}'];

module.exports = function(context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the new eslint rule format

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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.

LGTM

@lencioni
Copy link
Collaborator

This looks good. Thanks for your contribution!

@lencioni lencioni merged commit 21a3339 into jsx-eslint:master Sep 16, 2016
@pfhayes pfhayes deleted the unescape branch September 16, 2016 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

5 participants