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 option to the self-closing-comp #600

Merged
merged 5 commits into from
Jun 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion docs/rules/self-closing-comp.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,79 @@ var contentContainer = <div className="content"></div>;

var HelloJohn = <Hello name="John" />;

var Profile = <Hello name="John"><img src="picture.png" /></Hello>;
var Profile = <Hello name="John"><img src="picture.png" /></Hello>;
```

## Rule Options

It takes an option as the second parameter, which corresponding what components tags should be self-closed when this is possible.
Copy link
Member

Choose a reason for hiding this comment

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

We should indicate which is the default here - "component" i think.


```js
...
"self-closing-comp": [<enabled>, 'all'|'component'|'html'>]
Copy link
Member

Choose a reason for hiding this comment

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

i wonder if rather than "all", this should have the format { component: true, html: true } - that allows for other categorizations, and works just as well for the default of { component: true, html: false }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, your solution is better, thanks, I'll fix this

...
```

### `all`

All tags, including custom components and html components should be self-closed.

The following patterns are considered warnings:

```js
var HelloJohn = <Hello name="John"></Hello>;

var contentContainer = <div className="content"></div>;
```

The following patterns are not considered warnings:

```js
var HelloJohn = <Hello name="John" />;

var Profile = <Hello name="John"><img src="picture.png" /></Hello>;

var contentContainer = <div className="content" />;

var contentContainer = <div className="content"><div /></div>;
```

### `component`

Only custom components tags should be self-closed. This is the default option.

The following patterns are considered warnings:

```js
var HelloJohn = <Hello name="John"></Hello>;
```

The following patterns are not considered warnings:

```js
var contentContainer = <div className="content"></div>;

var HelloJohn = <Hello name="John" />;

var Profile = <Hello name="John"><img src="picture.png" /></Hello>;
```

### `html`

Only html components tags should be self-closed.

The following patterns are considered warnings:

```js
var contentContainer = <div className="content"></div>;
```

The following patterns are not considered warnings:

```js
var HelloJohn = <Hello name="John"></Hello>;

var contentContainer = <div className="content" />;

var contentContainer = <div className="content"><div /></div>;
```
16 changes: 14 additions & 2 deletions lib/rules/self-closing-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ module.exports = function(context) {
return true;
}

function isShouldBeSelfClosed(node) {
var configuration = context.options[0] || 'component';
return (
configuration === 'all' ||
configuration === 'component' && isComponent(node) ||
configuration === 'html' && isTagName(node.name.name)
) && !node.selfClosing && !hasChildren(node);
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {

JSXOpeningElement: function(node) {
if (!isComponent(node) || node.selfClosing || hasChildren(node)) {

if (!isShouldBeSelfClosed(node)) {
return;
}
context.report({
Expand All @@ -49,4 +59,6 @@ module.exports = function(context) {

};

module.exports.schema = [];
module.exports.schema = [{
enum: ['all', 'component', 'html']
}];
105 changes: 105 additions & 0 deletions tests/lib/rules/self-closing-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,48 @@ ruleTester.run('self-closing-comp', rule, {
}, {
code: 'var HelloJohn = <Hello name="John">&nbsp;</Hello>;',
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John" />;',
options: ['all'],
parserOptions: parserOptions
}, {
code: 'var Profile = <Hello name="John"><img src="picture.png" /></Hello>;',
options: ['all'],
parserOptions: parserOptions
}, {
code: '\
<Hello>\
<Hello name="John" />\
</Hello>',
options: ['all'],
parserOptions: parserOptions
}, {
code: 'var contentContainer = <div className="content" />;',
options: ['all'],
parserOptions: parserOptions
}, {
code: 'var contentContainer = <div className="content"><img src="picture.png" /></div>;',
options: ['all'],
parserOptions: parserOptions
}, {
code: '\
<div>\
<div className="content" />\
</div>',
options: ['all'],
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John"></Hello>;',
options: ['html'],
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
options: ['html'],
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
options: ['html'],
parserOptions: parserOptions
}
],

Expand All @@ -72,6 +114,69 @@ ruleTester.run('self-closing-comp', rule, {
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var HelloJohn = <Hello name="John"></Hello>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"></div>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content">\n</div>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"> </div>;',
options: ['all'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"></div>;',
options: ['html'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content">\n</div>;',
options: ['html'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"> </div>;',
options: ['html'],
parserOptions: parserOptions,
errors: [{
message: 'Empty components are self-closing'
}]
}
]
});