From 702570d36b8a6da504052e1151bf39c0c1cacb79 Mon Sep 17 00:00:00 2001 From: Timur Gibadullin Date: Sat, 14 May 2016 18:09:43 +0300 Subject: [PATCH 1/5] Add option to the self-closing-comp --- docs/rules/self-closing-comp.md | 76 ++++++++++++++++++- lib/rules/self-closing-comp.js | 16 +++- tests/lib/rules/self-closing-comp.js | 105 +++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 3 deletions(-) diff --git a/docs/rules/self-closing-comp.md b/docs/rules/self-closing-comp.md index 3f12ea536b..9143af3823 100644 --- a/docs/rules/self-closing-comp.md +++ b/docs/rules/self-closing-comp.md @@ -17,5 +17,79 @@ var contentContainer =
; var HelloJohn = ; -var Profile = ; +var Profile = ; +``` + +## Rule Options + +It takes an option as the second parameter, which corresponding what components tags should be self-closed when this is possible. + +```js +... +"self-closing-comp": [, 'all'|'component'|'html'>] +... +``` + +### `all` + +All tags, including custom components and html components should be self-closed. + +The following patterns are considered warnings: + +```js +var HelloJohn = ; + +var contentContainer =
; +``` + +The following patterns are not considered warnings: + +```js +var HelloJohn = ; + +var Profile = ; + +var contentContainer =
; + +var contentContainer =
; +``` + +### `component` + +Only custom components tags should be self-closed. This is the default option. + +The following patterns are considered warnings: + +```js +var HelloJohn = ; +``` + +The following patterns are not considered warnings: + +```js +var contentContainer =
; + +var HelloJohn = ; + +var Profile = ; +``` + +### `html` + +Only html components tags should be self-closed. + +The following patterns are considered warnings: + +```js +var contentContainer =
; +``` + +The following patterns are not considered warnings: + +```js +var HelloJohn = ; + +var contentContainer =
; + +var contentContainer =
; ``` diff --git a/lib/rules/self-closing-comp.js b/lib/rules/self-closing-comp.js index 3971913686..f92d396fc1 100644 --- a/lib/rules/self-closing-comp.js +++ b/lib/rules/self-closing-comp.js @@ -30,6 +30,15 @@ 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 // -------------------------------------------------------------------------- @@ -37,7 +46,8 @@ module.exports = function(context) { return { JSXOpeningElement: function(node) { - if (!isComponent(node) || node.selfClosing || hasChildren(node)) { + + if (!isShouldBeSelfClosed(node)) { return; } context.report({ @@ -49,4 +59,6 @@ module.exports = function(context) { }; -module.exports.schema = []; +module.exports.schema = [{ + enum: ['all', 'component', 'html'] +}]; diff --git a/tests/lib/rules/self-closing-comp.js b/tests/lib/rules/self-closing-comp.js index df3a6828a4..2832ab54d4 100644 --- a/tests/lib/rules/self-closing-comp.js +++ b/tests/lib/rules/self-closing-comp.js @@ -50,6 +50,48 @@ ruleTester.run('self-closing-comp', rule, { }, { code: 'var HelloJohn =  ;', parserOptions: parserOptions + }, { + code: 'var HelloJohn = ;', + options: ['all'], + parserOptions: parserOptions + }, { + code: 'var Profile = ;', + options: ['all'], + parserOptions: parserOptions + }, { + code: '\ + \ + \ + ', + options: ['all'], + parserOptions: parserOptions + }, { + code: 'var contentContainer =
;', + options: ['all'], + parserOptions: parserOptions + }, { + code: 'var contentContainer =
;', + options: ['all'], + parserOptions: parserOptions + }, { + code: '\ +
\ +
\ +
', + options: ['all'], + parserOptions: parserOptions + }, { + code: 'var HelloJohn = ;', + options: ['html'], + parserOptions: parserOptions + }, { + code: 'var HelloJohn = \n;', + options: ['html'], + parserOptions: parserOptions + }, { + code: 'var HelloJohn = ;', + options: ['html'], + parserOptions: parserOptions } ], @@ -72,6 +114,69 @@ ruleTester.run('self-closing-comp', rule, { errors: [{ message: 'Empty components are self-closing' }] + }, { + code: 'var HelloJohn = ;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var HelloJohn = \n;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var HelloJohn = ;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
\n
;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
;', + options: ['all'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
;', + options: ['html'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
\n
;', + options: ['html'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var contentContainer =
;', + options: ['html'], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] } ] }); From 2978369c6a57a45597aef0990bd6fe7f2c246974 Mon Sep 17 00:00:00 2001 From: Timur Gibadullin Date: Sun, 15 May 2016 15:23:21 +0300 Subject: [PATCH 2/5] Change option type of the rule --- docs/rules/self-closing-comp.md | 37 +++---------- lib/rules/self-closing-comp.js | 18 +++++-- tests/lib/rules/self-closing-comp.js | 81 +++++----------------------- 3 files changed, 32 insertions(+), 104 deletions(-) diff --git a/docs/rules/self-closing-comp.md b/docs/rules/self-closing-comp.md index 9143af3823..ce1fdc8744 100644 --- a/docs/rules/self-closing-comp.md +++ b/docs/rules/self-closing-comp.md @@ -22,41 +22,20 @@ var Profile = ; ## Rule Options -It takes an option as the second parameter, which corresponding what components tags should be self-closed when this is possible. +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": [, 'all'|'component'|'html'>] +"self-closing-comp": [, { + "component" || true, + "html" || false +}] ... ``` -### `all` - -All tags, including custom components and html components should be self-closed. - -The following patterns are considered warnings: - -```js -var HelloJohn = ; - -var contentContainer =
; -``` - -The following patterns are not considered warnings: - -```js -var HelloJohn = ; - -var Profile = ; - -var contentContainer =
; - -var contentContainer =
; -``` - ### `component` -Only custom components tags should be self-closed. This is the default option. +When `true`, custom components tags should be self-closed. The following patterns are considered warnings: @@ -76,7 +55,7 @@ var Profile = ; ### `html` -Only html components tags should be self-closed. +When `true`, html components tags should be self-closed. The following patterns are considered warnings: @@ -87,8 +66,6 @@ var contentContainer =
; The following patterns are not considered warnings: ```js -var HelloJohn = ; - var contentContainer =
; var contentContainer =
; diff --git a/lib/rules/self-closing-comp.js b/lib/rules/self-closing-comp.js index f92d396fc1..e1d258679f 100644 --- a/lib/rules/self-closing-comp.js +++ b/lib/rules/self-closing-comp.js @@ -31,11 +31,10 @@ module.exports = function(context) { } function isShouldBeSelfClosed(node) { - var configuration = context.options[0] || 'component'; + var configuration = context.options[0] || {component: true}; return ( - configuration === 'all' || - configuration === 'component' && isComponent(node) || - configuration === 'html' && isTagName(node.name.name) + configuration.component && isComponent(node) || + configuration.html && isTagName(node.name.name) ) && !node.selfClosing && !hasChildren(node); } @@ -60,5 +59,14 @@ module.exports = function(context) { }; module.exports.schema = [{ - enum: ['all', 'component', 'html'] + type: 'object', + properties: { + component: { + type: 'boolean' + }, + html: { + type: 'boolean' + } + }, + additionalProperties: false }]; diff --git a/tests/lib/rules/self-closing-comp.js b/tests/lib/rules/self-closing-comp.js index 2832ab54d4..3d74500d5e 100644 --- a/tests/lib/rules/self-closing-comp.js +++ b/tests/lib/rules/self-closing-comp.js @@ -51,46 +51,31 @@ ruleTester.run('self-closing-comp', rule, { code: 'var HelloJohn =  ;', parserOptions: parserOptions }, { - code: 'var HelloJohn = ;', - options: ['all'], + code: 'var HelloJohn = ;', + options: [{component: false}], parserOptions: parserOptions }, { - code: 'var Profile = ;', - options: ['all'], + code: 'var HelloJohn = \n;', + options: [{component: false}], parserOptions: parserOptions }, { - code: '\ - \ - \ - ', - options: ['all'], + code: 'var HelloJohn = ;', + options: [{component: false}], parserOptions: parserOptions }, { code: 'var contentContainer =
;', - options: ['all'], + options: [{html: true}], parserOptions: parserOptions }, { code: 'var contentContainer =
;', - options: ['all'], + options: [{html: true}], parserOptions: parserOptions }, { code: '\
\
\
', - options: ['all'], - parserOptions: parserOptions - }, { - code: 'var HelloJohn = ;', - options: ['html'], - parserOptions: parserOptions - }, { - code: 'var HelloJohn = \n;', - options: ['html'], - parserOptions: parserOptions - }, { - code: 'var HelloJohn = ;', - options: ['html'], + options: [{html: true}], parserOptions: parserOptions } ], @@ -114,65 +99,23 @@ ruleTester.run('self-closing-comp', rule, { errors: [{ message: 'Empty components are self-closing' }] - }, { - code: 'var HelloJohn = ;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] - }, { - code: 'var HelloJohn = \n;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] - }, { - code: 'var HelloJohn = ;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] - }, { - code: 'var contentContainer =
;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] - }, { - code: 'var contentContainer =
\n
;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] - }, { - code: 'var contentContainer =
;', - options: ['all'], - parserOptions: parserOptions, - errors: [{ - message: 'Empty components are self-closing' - }] }, { code: 'var contentContainer =
;', - options: ['html'], + options: [{html: true}], parserOptions: parserOptions, errors: [{ message: 'Empty components are self-closing' }] }, { code: 'var contentContainer =
\n
;', - options: ['html'], + options: [{html: true}], parserOptions: parserOptions, errors: [{ message: 'Empty components are self-closing' }] }, { code: 'var contentContainer =
;', - options: ['html'], + options: [{html: true}], parserOptions: parserOptions, errors: [{ message: 'Empty components are self-closing' From d39238136dae53d866febf64183860ac00abdd8b Mon Sep 17 00:00:00 2001 From: Timur Gibadullin Date: Sun, 15 May 2016 15:27:05 +0300 Subject: [PATCH 3/5] Specify default values in the schema --- lib/rules/self-closing-comp.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/rules/self-closing-comp.js b/lib/rules/self-closing-comp.js index e1d258679f..8e4f38c82c 100644 --- a/lib/rules/self-closing-comp.js +++ b/lib/rules/self-closing-comp.js @@ -62,9 +62,11 @@ module.exports.schema = [{ type: 'object', properties: { component: { + default: true, type: 'boolean' }, html: { + default: false, type: 'boolean' } }, From 74b103df9b007f712aa36b111d59617dd0a2b7f6 Mon Sep 17 00:00:00 2001 From: Timur Gibadullin Date: Sun, 15 May 2016 20:29:54 +0300 Subject: [PATCH 4/5] Fix the rule docs --- docs/rules/self-closing-comp.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/rules/self-closing-comp.md b/docs/rules/self-closing-comp.md index ce1fdc8744..41d1e9dda0 100644 --- a/docs/rules/self-closing-comp.md +++ b/docs/rules/self-closing-comp.md @@ -26,9 +26,9 @@ The rule can take one argument to select types of tags, which should be self-clo ```js ... -"self-closing-comp": [, { - "component" || true, - "html" || false +"self-closing-comp": ["error", { + "component": true, + "html": false }] ... ``` From 756008b654f5b70a38a2be663cc136bab7f10ebe Mon Sep 17 00:00:00 2001 From: Timur Gibadullin Date: Sun, 15 May 2016 21:27:54 +0300 Subject: [PATCH 5/5] Add tests with the options object omitted --- tests/lib/rules/self-closing-comp.js | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/lib/rules/self-closing-comp.js b/tests/lib/rules/self-closing-comp.js index 3d74500d5e..b038fac856 100644 --- a/tests/lib/rules/self-closing-comp.js +++ b/tests/lib/rules/self-closing-comp.js @@ -50,6 +50,37 @@ ruleTester.run('self-closing-comp', rule, { }, { code: 'var HelloJohn =  ;', parserOptions: parserOptions + }, { + code: 'var contentContainer =
;', + options: [], + parserOptions: parserOptions + }, { + code: 'var HelloJohn = ;', + options: [], + parserOptions: parserOptions + }, { + code: 'var Profile = ;', + options: [], + parserOptions: parserOptions + }, { + code: '\ + \ + \ + ', + options: [], + parserOptions: parserOptions + }, { + code: 'var HelloJohn =
 
;', + options: [], + parserOptions: parserOptions + }, { + code: 'var HelloJohn =
{\' \'}
;', + options: [], + parserOptions: parserOptions + }, { + code: 'var HelloJohn =  ;', + options: [], + parserOptions: parserOptions }, { code: 'var HelloJohn = ;', options: [{component: false}], @@ -99,6 +130,28 @@ ruleTester.run('self-closing-comp', rule, { errors: [{ message: 'Empty components are self-closing' }] + }, + { + code: 'var HelloJohn = ;', + options: [], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var HelloJohn = \n;', + options: [], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] + }, { + code: 'var HelloJohn = ;', + options: [], + parserOptions: parserOptions, + errors: [{ + message: 'Empty components are self-closing' + }] }, { code: 'var contentContainer =
;', options: [{html: true}],