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 3 commits
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
53 changes: 52 additions & 1 deletion docs/rules/self-closing-comp.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,56 @@ 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

The rule can take one argument to select types of tags, which should be self-closed when this is possible. By default only custom components tags should be self-closed.

```js
...
"self-closing-comp": [<enabled>, {
"component" <boolean> || true,
Copy link
Member

Choose a reason for hiding this comment

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

|| true means it will never be false, so i don't think this is good notation. I'd just provide the default object:

"self-closing-comp": ["error", {
  "component": true,
  "html": false
}]

I think it's pretty self-apparent that you can override them with your own booleans

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, fixed

"html" <boolean> || false
}]
...
```

### `component`

When `true`, custom components tags should be self-closed.

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`

When `true`, 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 contentContainer = <div className="content" />;

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

function isShouldBeSelfClosed(node) {
var configuration = context.options[0] || {component: true};
return (
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 +58,17 @@ module.exports = function(context) {

};

module.exports.schema = [];
module.exports.schema = [{
type: 'object',
properties: {
component: {
default: true,
type: 'boolean'
},
html: {
default: false,
type: 'boolean'
}
},
additionalProperties: false
}];
48 changes: 48 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,33 @@ ruleTester.run('self-closing-comp', rule, {
}, {
code: 'var HelloJohn = <Hello name="John">&nbsp;</Hello>;',
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John"></Hello>;',
options: [{component: false}],
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
options: [{component: false}],
parserOptions: parserOptions
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
options: [{component: false}],
parserOptions: parserOptions
}, {
code: 'var contentContainer = <div className="content" />;',
options: [{html: true}],
parserOptions: parserOptions
}, {
code: 'var contentContainer = <div className="content"><img src="picture.png" /></div>;',
options: [{html: true}],
parserOptions: parserOptions
}, {
code: '\
<div>\
<div className="content" />\
</div>',
options: [{html: true}],
parserOptions: parserOptions
}
],

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