From 408e1b1b3c5790adb6782f539380abd531736319 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 15:18:55 +0200 Subject: [PATCH] Add the "children" property of the config --- lib/rules/jsx-curly-spacing.js | 9 +- tests/lib/rules/jsx-curly-spacing.js | 719 ++++++++++++++++++++++++--- 2 files changed, 654 insertions(+), 74 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index ce8c4b1acf..589d7f3e7d 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -68,6 +68,9 @@ module.exports = { properties: { attributes: { $ref: '#/definitions/basicConfigOrBoolean' + }, + children: { + $ref: '#/definitions/basicConfigOrBoolean' } } }] @@ -97,6 +100,7 @@ module.exports = { var DEFAULT_WHEN = SPACING.never; var DEFAULT_ALLOW_MULTILINE = true; var DEFAULT_ATTRIBUTES = true; + var DEFAULT_CHILDREN = false; var sourceCode = context.getSourceCode(); var originalConfig = context.options[0] || {}; @@ -106,6 +110,8 @@ module.exports = { }); var attributes = has(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES; var attributesConfig = attributes ? normalizeConfig(attributes, defaultConfig, true) : null; + var children = has(originalConfig, 'children') ? originalConfig.children : DEFAULT_CHILDREN; + var childrenConfig = children ? normalizeConfig(children, defaultConfig, true) : null; // -------------------------------------------------------------------------- // Helpers @@ -247,7 +253,8 @@ module.exports = { break; case 'JSXElement': - return; + config = childrenConfig; + break; default: return; diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index daf01b8a4c..c0ac7b85e7 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -27,21 +27,33 @@ var parserOptions = { var ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-curly-spacing', rule, { valid: [{ - code: ';' + code: '{bar};' + }, { + code: '{ bar };' }, { code: [ ';' + 'bar', + '}>', + '{bar}', + ';' ].join('\n') + }, { + code: '{{ bar: true, baz: true }};' + }, { + code: '{ { bar: true, baz: true } };' }, { code: [ ';' + '{ bar: true, baz: true }', + '}>', + '{{ bar: true, baz: true }}', + ';' ].join('\n') }, { - code: ';' + code: '{ foo /* comment */ }' + }, { + code: '{ /* comment */ foo }' }, { code: ';', options: [{attributes: true}] @@ -55,6 +67,13 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', options: [{attributes: true}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: true}] }, { code: ';', options: [{attributes: false}] @@ -68,6 +87,13 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', options: [{attributes: false}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: false}] }, { code: ';', options: [{attributes: false}] @@ -75,15 +101,54 @@ ruleTester.run('jsx-curly-spacing', rule, { code: ';', options: [{attributes: false}] }, { - code: ';', - options: [{when: 'never'}] + code: '{bar};', + options: [{children: true}] }, { code: [ - '{', + 'bar', + '};' + ].join('\n'), + options: [{children: true}] + }, { + code: '{{ bar: true, baz: true }};', + options: [{children: true}] + }, { + code: [ + '{', '{ bar: true, baz: true }', - '} />;' + '};' ].join('\n'), - options: [{when: 'never', spacing: {objectLiterals: 'never'}}] + options: [{children: true}] + }, { + code: '{bar};', + options: [{children: false}] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + options: [{children: false}] + }, { + code: '{{ bar: true, baz: true }};', + options: [{children: false}] + }, { + code: [ + '{', + '{ bar: true, baz: true }', + '};' + ].join('\n'), + options: [{children: false}] + }, { + code: '{ bar };', + options: [{children: false}] + }, { + code: '{ { bar: true, baz: true } };', + options: [{children: false}] + }, { + code: ';', + options: [{when: 'never'}] }, { code: ';', options: [{when: 'never', allowMultiline: false}] @@ -100,6 +165,13 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', options: [{when: 'never', spacing: {objectLiterals: 'never'}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{when: 'never', spacing: {objectLiterals: 'never'}}] }, { code: ';', options: [{when: 'always'}] @@ -119,6 +191,13 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', options: [{when: 'always', spacing: {objectLiterals: 'never'}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{when: 'always', spacing: {objectLiterals: 'never'}}] }, { code: ';', options: [{attributes: {when: 'never'}}] @@ -137,7 +216,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '{ bar: true, baz: true }', '} />;' ].join('\n'), - options: [{when: 'never'}] + options: [{attributes: {when: 'never'}}] }, { code: ';', options: [{attributes: {when: 'always'}}] @@ -176,11 +255,6 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), options: [{attributes: {when: 'never', allowMultiline: true}}] - }, { - code: [ - '
{/* comment */}
;' - ].join('\n'), - options: [{attributes: {when: 'never'}}] }, { code: ';', options: [{attributes: {when: 'never'}}] @@ -193,31 +267,102 @@ ruleTester.run('jsx-curly-spacing', rule, { 'bar', '} />;' ].join('\n'), - options: [{when: 'always', spacing: {}}] + options: [{attributes: {when: 'always', spacing: {}}}] + }, { + code: ';', + options: [{attributes: {when: 'always', spacing: {objectLiterals: 'never'}}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {when: 'always', spacing: {}}}] - }, { - code: ';', options: [{attributes: {when: 'always', spacing: {objectLiterals: 'never'}}}] + }, { + code: '{bar};', + options: [{children: {when: 'never'}}] + }, { + code: '{ bar };', + options: [{children: {when: 'always'}}] + }, { + code: '{ bar };', + options: [{children: {when: 'always', allowMultiline: false}}] + }, { + code: '{{ bar:baz }};', + options: [{children: {when: 'never'}}] }, { code: [ - '{', + '{ bar: true, baz: true }', + '};' + ].join('\n'), + options: [{children: {when: 'never'}}] + }, { + code: '{ {bar:baz} };', + options: [{children: {when: 'always'}}] + }, { + code: [ + '{', + '{ bar: true, baz: true }', + '};' + ].join('\n'), + options: [{children: {when: 'always'}}] + }, { + code: [ + '{', 'bar', - '} />;' + '};' ].join('\n'), - options: [{when: 'always', allowMultiline: true}] + options: [{children: {when: 'always'}}] }, { code: [ - '{', + 'bar', + '};' + ].join('\n'), + options: [{children: {when: 'always', allowMultiline: true}}] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + options: [{children: {when: 'never'}}] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + options: [{children: {when: 'never', allowMultiline: true}}] + }, { + code: [ + '{/* comment */};' + ].join('\n'), + options: [{children: {when: 'never'}}] + }, { + code: '{bar/* comment */};', + options: [{children: {when: 'never'}}] + }, { + code: '{ bar };', + options: [{children: {when: 'always', spacing: {}}}] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + options: [{children: {when: 'always', spacing: {}}}] + }, { + code: '{{ bar: true, baz: true }};', + options: [{children: {when: 'always', spacing: {objectLiterals: 'never'}}}] + }, { + code: [ + '{', '{ bar: true, baz: true }', - '} />;' + '};' ].join('\n'), - options: [{when: 'always', spacing: {objectLiterals: 'never'}}] + options: [{children: {when: 'always', spacing: {objectLiterals: 'never'}}}] }, { code: ';' }, { @@ -283,7 +428,7 @@ ruleTester.run('jsx-curly-spacing', rule, { 'bar', '} {', '...bar', - '}/>;' + '} />;' ].join('\n'), options: [{attributes: {when: 'always'}}] }, { @@ -293,21 +438,72 @@ ruleTester.run('jsx-curly-spacing', rule, { code: '', options: [{attributes: {when: 'never', spacing: {objectLiterals: 'always'}}}] }, { - code: '
{bar}
', + code: '{bar/* comment */};', + options: [{children: {when: 'never'}}] + }, { + code: '{bar} {baz};' + }, { + code: '{bar} {baz};', + options: [{children: {when: 'never'}}] + }, { + code: '{ bar } { baz };', + options: [{children: {when: 'always'}}] + }, { + code: '{ bar } { baz };', + options: [{children: {when: 'always', allowMultiline: false}}] + }, { + code: '{{ bar:baz }} {baz};', + options: [{children: {when: 'never'}}] + }, { + code: '{ {bar:baz} } { baz };', + options: [{children: {when: 'always'}}] + }, { + code: [ + '{', + 'bar', + '} {', + 'bar', + '};' + ].join('\n'), + options: [{children: {when: 'always'}}] + }, { + code: '{bar/* comment */} {baz/* comment */};', + options: [{children: {when: 'never'}}] + }, { + code: '{3} { {a: 2} }', + options: [{children: {when: 'never', spacing: {objectLiterals: 'always'}}}] + }, { + code: '{bar}', options: [{attributes: {when: 'always'}}] }], invalid: [{ - code: ';', - output: ';', + code: '{bar};', + output: '{bar};', errors: [{ message: 'There should be no space after \'{\'' }, { message: 'There should be no space before \'}\'' }] }, { - code: ';', - output: ';', + code: '{ bar };', + output: '{ bar };', + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{{ bar: true, baz: true }};', + output: '{{ bar: true, baz: true }};', + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{ { bar: true, baz: true } };', + output: '{ { bar: true, baz: true } };', errors: [{ message: 'There should be no space after \'{\'' }, { @@ -331,6 +527,24 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'There should be no space before \'}\'' }] + }, { + code: '{ bar };', + output: '{bar};', + options: [{children: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{ { bar: true, baz: true } };', + output: '{{ bar: true, baz: true }};', + options: [{children: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] }, { code: ';', output: ';', @@ -376,30 +590,13 @@ ruleTester.run('jsx-curly-spacing', rule, { message: 'There should be no newline before \'}\'' }] }, { - code: [ - ';' - ].join('\n'), + code: ';', output: ';', - options: [{when: 'never', allowMultiline: false, spacing: {objectLiterals: 'always'}}], - errors: [{ - message: 'There should be no newline after \'{\'' - }, { - message: 'There should be no newline before \'}\'' - }] - }, { - code: [ - ';' - ].join('\n'), - output: ';', - options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'never'}}], + options: [{when: 'never', spacing: {objectLiterals: 'always'}}], errors: [{ - message: 'There should be no newline after \'{\'' + message: 'A space is required after \'{\'' }, { - message: 'There should be no newline before \'}\'' + message: 'A space is required before \'}\'' }] }, { code: [ @@ -408,21 +605,12 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'always'}}], + options: [{when: 'never', allowMultiline: false, spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { message: 'There should be no newline before \'}\'' }] - }, { - code: ';', - output: ';', - options: [{when: 'never', spacing: {objectLiterals: 'always'}}], - errors: [{ - message: 'A space is required after \'{\'' - }, { - message: 'A space is required before \'}\'' - }] }, { code: ';', output: ';', @@ -454,6 +642,19 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'There should be no space before \'}\'' }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] }, { code: ';', output: ';', @@ -463,6 +664,19 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'A space is required before \'}\'' }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] }, { code: ';', output: ';', @@ -665,6 +879,208 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'There should be no space before \'}\'' }] + }, { + code: '{ bar };', + output: '{bar};', + options: [{children: true, when: 'never'}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + output: '{bar};', + options: [{children: true, when: 'never', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: '{ { bar: true, baz: true } };', + output: '{{ bar: true, baz: true }};', + options: [{children: true, when: 'never', spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{{ bar: true, baz: true }};', + output: '{ { bar: true, baz: true } };', + options: [{children: true, when: 'never', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar};', + output: '{ bar };', + options: [{children: true, when: 'always'}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + output: '{ bar };', + options: [{children: true, when: 'always', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: '{ { bar: true, baz: true } };', + output: '{{ bar: true, baz: true }};', + options: [{children: true, when: 'always', spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{{ bar: true, baz: true }};', + output: '{ { bar: true, baz: true } };', + options: [{children: true, when: 'always', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{ bar };', + output: '{bar};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{ bar };', + output: '{bar};', + options: [{children: {when: 'never', allowMultiline: false}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{bar};', + output: '{ bar };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar};', + output: '{ bar };', + options: [{children: {when: 'always', allowMultiline: false}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{ bar};', + output: '{ bar };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar };', + output: '{ bar };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }] + }, { + code: '{ bar};', + output: '{bar};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }] + }, { + code: '{bar };', + output: '{bar};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + output: '{bar};', + options: [{children: {when: 'never', allowMultiline: false}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '};' + ].join('\n'), + output: '{ bar };', + options: [{children: {when: 'always', allowMultiline: false}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: '{bar};', + output: '{ bar };', + options: [{children: {when: 'always', spacing: {}}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{ bar};', + output: '{ bar };', + options: [{children: {when: 'always', spacing: {}}}], + errors: [{ + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar };', + output: '{ bar };', + options: [{children: {when: 'always', spacing: {}}}], + errors: [{ + message: 'A space is required after \'{\'' + }] + }, { + code: '{ {bar: true, baz: true} };', + output: '{{bar: true, baz: true}};', + options: [{children: {when: 'always', spacing: {objectLiterals: 'never'}}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] }, { code: ';', output: ';', @@ -895,16 +1311,16 @@ ruleTester.run('jsx-curly-spacing', rule, { message: 'A space is required before \'}\'' }] }, { - code: '
', - output: '
', + code: '', + output: '', errors: [{ message: 'There should be no space after \'{\'' }, { message: 'There should be no space before \'}\'' }] }, { - code: '
', - output: '
', + code: '', + output: '', options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' @@ -912,21 +1328,178 @@ ruleTester.run('jsx-curly-spacing', rule, { message: 'A space is required before \'}\'' }] }, { - code: '
', - output: '
', + code: '', + output: '', errors: [{ message: 'There should be no space after \'{\'' }, { message: 'There should be no space before \'}\'' }] }, { - code: '
', - output: '
', + code: '', + output: '', options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { message: 'A space is required before \'}\'' }] + }, { + code: '{ bar } { baz };', + output: '{bar} {baz};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }, { + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{ bar } { baz };', + output: '{bar} {baz};', + options: [{children: {when: 'never', allowMultiline: false}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }, { + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: '{bar} {baz};', + output: '{ bar } { baz };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }, { + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar} {baz};', + output: '{ bar } { baz };', + options: [{children: {when: 'always', allowMultiline: false}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }, { + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{ bar} { baz};', + output: '{ bar } { baz };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required before \'}\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{bar } {baz };', + output: '{ bar } { baz };', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required after \'{\'' + }] + }, { + code: '{ bar} { baz};', + output: '{bar} {baz};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space after \'{\'' + }] + }, { + code: '{bar } {baz };', + output: '{bar} {baz};', + options: [{children: {when: 'never'}}], + errors: [{ + message: 'There should be no space before \'}\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '} {', + 'baz', + '};' + ].join('\n'), + output: '{bar} {baz};', + options: [{children: {when: 'never', allowMultiline: false}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }, { + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + '{', + 'bar', + '} {', + 'baz', + '};' + ].join('\n'), + output: '{ bar } { baz };', + options: [{children: {when: 'always', allowMultiline: false}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }, { + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: '{ 3 } bar={{a: 2}}', + output: '{3} bar={ {a: 2} }', + options: [{children: {when: 'never', spacing: {objectLiterals: 'always'}}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }, { + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{foo /* comment */}', + output: '{ foo /* comment */ }', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '{/* comment */ foo}', + output: '{ /* comment */ foo }', + options: [{children: {when: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }] });