From 0f0ab2199f69cda81b788793ff9049bdf73608b6 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sat, 6 May 2017 23:01:12 +0200 Subject: [PATCH 1/9] Move the first option into the config object Use the "spaces" name. --- lib/rules/jsx-curly-spacing.js | 12 +-- tests/lib/rules/jsx-curly-spacing.js | 148 +++++++++++++-------------- 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 90563798ef..25c921cfee 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -32,10 +32,11 @@ module.exports = { fixable: 'code', schema: [{ - enum: SPACING_VALUES - }, { type: 'object', properties: { + spaces: { + enum: SPACING_VALUES + }, allowMultiline: { type: 'boolean' }, @@ -47,8 +48,7 @@ module.exports = { } } } - }, - additionalProperties: false + } }] }, @@ -58,8 +58,8 @@ module.exports = { var DEFAULT_ALLOW_MULTILINE = true; var sourceCode = context.getSourceCode(); - var baseSpacing = context.options[0] || DEFAULT_SPACING; - var config = context.options[1] || {}; + var config = context.options[0] || {}; + var baseSpacing = config.spaces || DEFAULT_SPACING; var multiline = has(config, 'allowMultiline') ? config.allowMultiline : DEFAULT_ALLOW_MULTILINE; var spacingConfig = config.spacing || {}; var objectLiteralSpacing = spacingConfig.objectLiterals || baseSpacing; diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index cebe4eee6f..000b22c786 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -36,141 +36,141 @@ ruleTester.run('jsx-curly-spacing', rule, { ].join('\n') }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: [ ';' ].join('\n'), - options: ['never', {spacing: {objectLiterals: 'never'}}] + options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}] }, { code: ';', - options: ['always'] + options: [{spaces: 'always'}] }, { code: ';', - options: ['always', {allowMultiline: false}] + options: [{spaces: 'always', allowMultiline: false}] }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: [ ';' ].join('\n'), - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['never'] + options: [{spaces: 'never'}] }, { code: [ '
{/* comment */}
;' ].join('\n'), - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['always', {spacing: {}}] + options: [{spaces: 'always', spacing: {}}] }, { code: [ ';' ].join('\n'), - options: ['always', {spacing: {}}] + options: [{spaces: 'always', spacing: {}}] }, { code: ';', - options: ['always', {spacing: {objectLiterals: 'never'}}] + options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] }, { code: [ ';' ].join('\n'), - options: ['always', {allowMultiline: true}] + options: [{spaces: 'always', allowMultiline: true}] }, { code: [ ';' ].join('\n'), - options: ['always', {spacing: {objectLiterals: 'never'}}] + options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] }, { code: ';' }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['always'] + options: [{spaces: 'always'}] }, { code: ';', - options: ['always', {allowMultiline: false}] + options: [{spaces: 'always', allowMultiline: false}] }, { code: [ ';' ].join('\n'), - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';' }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['always'] + options: [{spaces: 'always'}] }, { code: ';', - options: ['always', {allowMultiline: false}] + options: [{spaces: 'always', allowMultiline: false}] }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: ';', - options: ['always'] + options: [{spaces: 'always'}] }, { code: [ ';' ].join('\n'), - options: ['always'] + options: [{spaces: 'always'}] }, { code: ';', - options: ['never'] + options: [{spaces: 'never'}] }, { code: '', - options: ['never', {spacing: {objectLiterals: 'always'}}] + options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}] }, { code: '
{bar}
', - options: ['always'] + options: [{spaces: 'always'}] }], invalid: [{ code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -203,7 +203,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -216,7 +216,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['never', {allowMultiline: false, spacing: {objectLiterals: 'never'}}], + options: [{spaces: 'never', allowMultiline: false, spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -229,7 +229,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['never', {allowMultiline: false, spacing: {objectLiterals: 'always'}}], + options: [{spaces: 'never', allowMultiline: false, spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -242,7 +242,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['always', {allowMultiline: false, spacing: {objectLiterals: 'never'}}], + options: [{spaces: 'always', allowMultiline: false, spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -255,7 +255,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['always', {allowMultiline: false, spacing: {objectLiterals: 'always'}}], + options: [{spaces: 'always', allowMultiline: false, spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -264,7 +264,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -273,7 +273,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -282,28 +282,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -314,7 +314,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -327,7 +327,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -336,7 +336,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always', {spacing: {}}], + options: [{spaces: 'always', spacing: {}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -345,21 +345,21 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always', {spacing: {}}], + options: [{spaces: 'always', spacing: {}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: ['always', {spacing: {}}], + options: [{spaces: 'always', spacing: {}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: ['always', {spacing: {objectLiterals: 'never'}}], + options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -368,7 +368,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -377,7 +377,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -386,7 +386,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -395,7 +395,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -404,28 +404,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -436,7 +436,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -449,7 +449,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -458,7 +458,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -471,7 +471,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -484,7 +484,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -497,7 +497,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -510,7 +510,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required before \'}\'' }, { @@ -519,7 +519,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -528,7 +528,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -537,7 +537,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: ['never'], + options: [{spaces: 'never'}], errors: [{ message: 'There should be no space before \'}\'' }, { @@ -552,7 +552,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['never', {allowMultiline: false}], + options: [{spaces: 'never', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -571,7 +571,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: ['always', {allowMultiline: false}], + options: [{spaces: 'always', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -584,7 +584,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '', output: '', - options: ['never', {spacing: {objectLiterals: 'always'}}], + options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -605,7 +605,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -622,7 +622,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: ['always'], + options: [{spaces: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { From 9a8f80083088daa92987dfd739e695402f00559d Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sat, 6 May 2017 23:35:31 +0200 Subject: [PATCH 2/9] Add a test case for a fixed bug Previously when "spacing" was set and "allowMultiline" was missing, it was set to `undefined` and not `true` (as it should). --- tests/lib/rules/jsx-curly-spacing.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index 000b22c786..84ef968d85 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -102,6 +102,14 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), options: [{spaces: 'always', spacing: {}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{spaces: 'always', spacing: {}}], + parserOptions: parserOptions }, { code: ';', options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] From c9e9ded88580dedff8e5cd5c309266d1ca714051 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 12:41:23 +0200 Subject: [PATCH 3/9] Allow attributes to have their own config This change does not make sense by itself, but it will with the added children config. --- lib/rules/jsx-curly-spacing.js | 86 ++++++-- tests/lib/rules/jsx-curly-spacing.js | 286 +++++++++++++++++++++------ 2 files changed, 289 insertions(+), 83 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 25c921cfee..7f324824d3 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -32,37 +32,80 @@ module.exports = { fixable: 'code', schema: [{ - type: 'object', - properties: { - spaces: { - enum: SPACING_VALUES - }, - allowMultiline: { - type: 'boolean' - }, - spacing: { + definitions: { + basicConfig: { type: 'object', properties: { - objectLiterals: { + spaces: { enum: SPACING_VALUES + }, + allowMultiline: { + type: 'boolean' + }, + spacing: { + type: 'object', + properties: { + objectLiterals: { + enum: SPACING_VALUES + } + } } } + }, + basicConfigOrBoolean: { + oneOf: [{ + $ref: '#/definitions/basicConfig' + }, { + type: 'boolean' + }] } - } + }, + + allOf: [{ + $ref: '#/definitions/basicConfig' + }, { + type: 'object', + properties: { + attributes: { + $ref: '#/definitions/basicConfigOrBoolean' + } + } + }] }] }, create: function(context) { + function normalizeConfig(configOrTrue, defaults, lastPass) { + var config = configOrTrue === true ? {} : configOrTrue; + var spaces = config.spaces || defaults.spaces; + var allowMultiline = has(config, 'allowMultiline') ? config.allowMultiline : defaults.allowMultiline; + var spacing = config.spacing || {}; + var objectLiteralSpaces = spacing.objectLiterals || defaults.objectLiteralSpaces; + if (lastPass) { + // On the final pass assign the values that should be derived from others if they are still undefined + objectLiteralSpaces = objectLiteralSpaces || spaces; + } + + return { + spaces, + allowMultiline, + objectLiteralSpaces + }; + } + var DEFAULT_SPACING = SPACING.never; var DEFAULT_ALLOW_MULTILINE = true; + var DEFAULT_ATTRIBUTES = true; var sourceCode = context.getSourceCode(); - var config = context.options[0] || {}; - var baseSpacing = config.spaces || DEFAULT_SPACING; - var multiline = has(config, 'allowMultiline') ? config.allowMultiline : DEFAULT_ALLOW_MULTILINE; - var spacingConfig = config.spacing || {}; - var objectLiteralSpacing = spacingConfig.objectLiterals || baseSpacing; + var originalConfig = context.options[0] || {}; + var defaultConfig = normalizeConfig(originalConfig, { + spaces: DEFAULT_SPACING, + allowMultiline: DEFAULT_ALLOW_MULTILINE + }); + var attributes = has(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES; + var attributesConfig = attributes ? normalizeConfig(attributes, defaultConfig, true) : null; // -------------------------------------------------------------------------- // Helpers @@ -200,6 +243,7 @@ module.exports = { if (node.parent.type === 'JSXElement') { return; } + var config = attributesConfig; var first = context.getFirstToken(node); var last = sourceCode.getLastToken(node); var second = context.getTokenAfter(first, {includeComments: true}); @@ -217,28 +261,28 @@ module.exports = { } var isObjectLiteral = first.value === second.value; - var spacing = isObjectLiteral ? objectLiteralSpacing : baseSpacing; + var spacing = isObjectLiteral ? config.objectLiteralSpaces : config.spaces; if (spacing === SPACING.always) { if (!sourceCode.isSpaceBetweenTokens(first, second)) { reportRequiredBeginningSpace(node, first); - } else if (!multiline && isMultiline(first, second)) { + } else if (!config.allowMultiline && isMultiline(first, second)) { reportNoBeginningNewline(node, first, spacing); } if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) { reportRequiredEndingSpace(node, last); - } else if (!multiline && isMultiline(penultimate, last)) { + } else if (!config.allowMultiline && isMultiline(penultimate, last)) { reportNoEndingNewline(node, last, spacing); } } else if (spacing === SPACING.never) { if (isMultiline(first, second)) { - if (!multiline) { + if (!config.allowMultiline) { reportNoBeginningNewline(node, first, spacing); } } else if (sourceCode.isSpaceBetweenTokens(first, second)) { reportNoBeginningSpace(node, first); } if (isMultiline(penultimate, last)) { - if (!multiline) { + if (!config.allowMultiline) { reportNoEndingNewline(node, last, spacing); } } else if (sourceCode.isSpaceBetweenTokens(penultimate, last)) { diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index 84ef968d85..c26af8e3be 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -34,6 +34,14 @@ ruleTester.run('jsx-curly-spacing', rule, { '{ bar: true, baz: true }', '} />;' ].join('\n') + }, { + code: [ + ';' + ].join('\n') + }, { + code: ';' }, { code: ';', options: [{spaces: 'never'}] @@ -44,15 +52,53 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}] + }, { + code: ';', + options: [{spaces: 'never', allowMultiline: false}] + }, { + code: ';', + options: [{spaces: 'never', allowMultiline: true}] + }, { + code: [ + ';' + ].join('\n'), + options: [{spaces: 'never', allowMultiline: true}] + }, { + code: ';', + options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}] }, { code: ';', options: [{spaces: 'always'}] }, { code: ';', options: [{spaces: 'always', allowMultiline: false}] + }, { + code: ';', + options: [{spaces: 'always', allowMultiline: true}] + }, { + code: [ + ';' + ].join('\n'), + options: [{spaces: 'always', allowMultiline: true}] + }, { + code: ';', + options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] + }, { + code: ';', + options: [{attributes: {spaces: 'never'}}] + }, { + code: ';', + options: [{attributes: {spaces: 'always'}}] + }, { + code: ';', + options: [{attributes: {spaces: 'always', allowMultiline: false}}] }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: [ ';', - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'always', allowMultiline: true}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: {spaces: 'never'}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: {spaces: 'never', allowMultiline: true}}] }, { code: [ '
{/* comment */}
;' ].join('\n'), - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';', - options: [{spaces: 'always', spacing: {}}] + options: [{attributes: {spaces: 'always', spacing: {}}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always', spacing: {}}], - parserOptions: parserOptions + options: [{attributes: {spaces: 'always', spacing: {}}}] }, { code: ';', - options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] + options: [{attributes: {spaces: 'always', spacing: {objectLiterals: 'never'}}}] }, { code: [ ';' }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';', - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: ';', - options: [{spaces: 'always', allowMultiline: false}] + options: [{attributes: {spaces: 'always', allowMultiline: false}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always', allowMultiline: true}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: {spaces: 'never', allowMultiline: true}}] }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';' }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';', - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: ';', - options: [{spaces: 'always', allowMultiline: false}] + options: [{attributes: {spaces: 'always', allowMultiline: false}}] }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: ';', - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }, { code: ';', - options: [{spaces: 'never'}] + options: [{attributes: {spaces: 'never'}}] }, { code: '', - options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}] + options: [{attributes: {spaces: 'never', spacing: {objectLiterals: 'always'}}}] }, { code: '
{bar}
', - options: [{spaces: 'always'}] + options: [{attributes: {spaces: 'always'}}] }], invalid: [{ code: ';', output: ';', - options: [{spaces: 'never'}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', errors: [{ message: 'There should be no space after \'{\'' }, { @@ -211,7 +284,29 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', + options: [{spaces: 'never'}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', options: [{spaces: 'never', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -269,6 +364,15 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'There should be no newline before \'}\'' }] + }, { + code: ';', + output: ';', + options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }, { code: ';', output: ';', @@ -279,9 +383,67 @@ ruleTester.run('jsx-curly-spacing', rule, { message: 'A space is required before \'}\'' }] }, { - code: ';', + code: [ + ';' + ].join('\n'), output: ';', options: [{spaces: 'always', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{spaces: 'always', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: {spaces: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: {spaces: 'never', allowMultiline: false}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: {spaces: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -290,28 +452,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -322,7 +484,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{attributes: {spaces: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -335,7 +497,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -344,7 +506,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {}}], + options: [{attributes: {spaces: 'always', spacing: {}}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -353,21 +515,21 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {}}], + options: [{attributes: {spaces: 'always', spacing: {}}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {}}], + options: [{attributes: {spaces: 'always', spacing: {}}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}], + options: [{attributes: {spaces: 'always', spacing: {objectLiterals: 'never'}}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -376,7 +538,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -385,7 +547,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{attributes: {spaces: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -394,7 +556,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -403,7 +565,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -412,28 +574,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -444,7 +606,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{attributes: {spaces: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -457,7 +619,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -466,7 +628,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -479,7 +641,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{attributes: {spaces: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -492,7 +654,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -505,7 +667,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -518,7 +680,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }, { @@ -527,7 +689,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -536,7 +698,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -545,7 +707,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{attributes: {spaces: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }, { @@ -560,7 +722,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{attributes: {spaces: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -579,7 +741,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{attributes: {spaces: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -592,7 +754,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '', output: '', - options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}], + options: [{attributes: {spaces: 'never', spacing: {objectLiterals: 'always'}}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -613,7 +775,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -630,7 +792,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: [{spaces: 'always'}], + options: [{attributes: {spaces: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { From 7b9d9adf803a7eacda87b84294c0f8aca93805c9 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 12:45:52 +0200 Subject: [PATCH 4/9] Change "spaces" to "when" --- lib/rules/jsx-curly-spacing.js | 14 +- tests/lib/rules/jsx-curly-spacing.js | 192 +++++++++++++-------------- 2 files changed, 103 insertions(+), 103 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 7f324824d3..bdf397450c 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -36,7 +36,7 @@ module.exports = { basicConfig: { type: 'object', properties: { - spaces: { + when: { enum: SPACING_VALUES }, allowMultiline: { @@ -78,30 +78,30 @@ module.exports = { function normalizeConfig(configOrTrue, defaults, lastPass) { var config = configOrTrue === true ? {} : configOrTrue; - var spaces = config.spaces || defaults.spaces; + var when = config.when || defaults.when; var allowMultiline = has(config, 'allowMultiline') ? config.allowMultiline : defaults.allowMultiline; var spacing = config.spacing || {}; var objectLiteralSpaces = spacing.objectLiterals || defaults.objectLiteralSpaces; if (lastPass) { // On the final pass assign the values that should be derived from others if they are still undefined - objectLiteralSpaces = objectLiteralSpaces || spaces; + objectLiteralSpaces = objectLiteralSpaces || when; } return { - spaces, + when, allowMultiline, objectLiteralSpaces }; } - var DEFAULT_SPACING = SPACING.never; + var DEFAULT_WHEN = SPACING.never; var DEFAULT_ALLOW_MULTILINE = true; var DEFAULT_ATTRIBUTES = true; var sourceCode = context.getSourceCode(); var originalConfig = context.options[0] || {}; var defaultConfig = normalizeConfig(originalConfig, { - spaces: DEFAULT_SPACING, + when: DEFAULT_WHEN, allowMultiline: DEFAULT_ALLOW_MULTILINE }); var attributes = has(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES; @@ -261,7 +261,7 @@ module.exports = { } var isObjectLiteral = first.value === second.value; - var spacing = isObjectLiteral ? config.objectLiteralSpaces : config.spaces; + var spacing = isObjectLiteral ? config.objectLiteralSpaces : config.when; if (spacing === SPACING.always) { if (!sourceCode.isSpaceBetweenTokens(first, second)) { reportRequiredBeginningSpace(node, first); diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index c26af8e3be..904ef625d0 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -44,207 +44,207 @@ ruleTester.run('jsx-curly-spacing', rule, { code: ';' }, { code: ';', - options: [{spaces: 'never'}] + options: [{when: 'never'}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}] + options: [{when: 'never', spacing: {objectLiterals: 'never'}}] }, { code: ';', - options: [{spaces: 'never', allowMultiline: false}] + options: [{when: 'never', allowMultiline: false}] }, { code: ';', - options: [{spaces: 'never', allowMultiline: true}] + options: [{when: 'never', allowMultiline: true}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'never', allowMultiline: true}] + options: [{when: 'never', allowMultiline: true}] }, { code: ';', - options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}] + options: [{when: 'never', spacing: {objectLiterals: 'never'}}] }, { code: ';', - options: [{spaces: 'always'}] + options: [{when: 'always'}] }, { code: ';', - options: [{spaces: 'always', allowMultiline: false}] + options: [{when: 'always', allowMultiline: false}] }, { code: ';', - options: [{spaces: 'always', allowMultiline: true}] + options: [{when: 'always', allowMultiline: true}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always', allowMultiline: true}] + options: [{when: 'always', allowMultiline: true}] }, { code: ';', - options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] + options: [{when: 'always', spacing: {objectLiterals: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}] + options: [{attributes: {when: 'always', allowMultiline: false}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'never'}] + options: [{when: 'never'}] }, { code: ';', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always', allowMultiline: true}}] + options: [{attributes: {when: 'always', allowMultiline: true}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'never', allowMultiline: true}}] + options: [{attributes: {when: 'never', allowMultiline: true}}] }, { code: [ '
{/* comment */}
;' ].join('\n'), - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'always', spacing: {}}}] + options: [{attributes: {when: 'always', spacing: {}}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always', spacing: {}}] + options: [{when: 'always', spacing: {}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always', spacing: {}}}] + options: [{attributes: {when: 'always', spacing: {}}}] }, { code: ';', - options: [{attributes: {spaces: 'always', spacing: {objectLiterals: 'never'}}}] + options: [{attributes: {when: 'always', spacing: {objectLiterals: 'never'}}}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always', allowMultiline: true}] + options: [{when: 'always', allowMultiline: true}] }, { code: [ ';' ].join('\n'), - options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}] + options: [{when: 'always', spacing: {objectLiterals: 'never'}}] }, { code: ';' }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}] + options: [{attributes: {when: 'always', allowMultiline: false}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always', allowMultiline: true}}] + options: [{attributes: {when: 'always', allowMultiline: true}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'never', allowMultiline: true}}] + options: [{attributes: {when: 'never', allowMultiline: true}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';' }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}] + options: [{attributes: {when: 'always', allowMultiline: false}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: ';', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: [ ';' ].join('\n'), - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }, { code: ';', - options: [{attributes: {spaces: 'never'}}] + options: [{attributes: {when: 'never'}}] }, { code: '', - options: [{attributes: {spaces: 'never', spacing: {objectLiterals: 'always'}}}] + options: [{attributes: {when: 'never', spacing: {objectLiterals: 'always'}}}] }, { code: '
{bar}
', - options: [{attributes: {spaces: 'always'}}] + options: [{attributes: {when: 'always'}}] }], invalid: [{ @@ -284,7 +284,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never'}], + options: [{when: 'never'}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -297,7 +297,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false}], + options: [{when: 'never', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -306,7 +306,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never', spacing: {objectLiterals: 'never'}}], + options: [{when: 'never', spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -319,7 +319,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false, spacing: {objectLiterals: 'never'}}], + options: [{when: 'never', allowMultiline: false, spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -332,7 +332,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'never', allowMultiline: false, spacing: {objectLiterals: 'always'}}], + options: [{when: 'never', allowMultiline: false, spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -345,7 +345,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false, spacing: {objectLiterals: 'never'}}], + options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -358,7 +358,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false, spacing: {objectLiterals: 'always'}}], + options: [{when: 'always', allowMultiline: false, spacing: {objectLiterals: 'always'}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -367,7 +367,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'never', spacing: {objectLiterals: 'always'}}], + options: [{when: 'never', spacing: {objectLiterals: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -376,7 +376,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always'}], + options: [{when: 'always'}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -389,7 +389,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{spaces: 'always', allowMultiline: false}], + options: [{when: 'always', allowMultiline: false}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -398,7 +398,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {objectLiterals: 'never'}}], + options: [{when: 'always', spacing: {objectLiterals: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -407,7 +407,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{spaces: 'always', spacing: {objectLiterals: 'always'}}], + options: [{when: 'always', spacing: {objectLiterals: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -416,7 +416,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -425,7 +425,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -434,7 +434,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -443,7 +443,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -452,28 +452,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -484,7 +484,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -497,7 +497,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -506,7 +506,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', spacing: {}}}], + options: [{attributes: {when: 'always', spacing: {}}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -515,21 +515,21 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', spacing: {}}}], + options: [{attributes: {when: 'always', spacing: {}}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', spacing: {}}}], + options: [{attributes: {when: 'always', spacing: {}}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', spacing: {objectLiterals: 'never'}}}], + options: [{attributes: {when: 'always', spacing: {objectLiterals: 'never'}}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -538,7 +538,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -547,7 +547,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -556,7 +556,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -565,7 +565,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -574,28 +574,28 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }] }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }] @@ -606,7 +606,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -619,7 +619,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -628,7 +628,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -641,7 +641,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -654,7 +654,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -667,7 +667,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -680,7 +680,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required before \'}\'' }, { @@ -689,7 +689,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -698,7 +698,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -707,7 +707,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', output: ';', - options: [{attributes: {spaces: 'never'}}], + options: [{attributes: {when: 'never'}}], errors: [{ message: 'There should be no space before \'}\'' }, { @@ -722,7 +722,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'never', allowMultiline: false}}], + options: [{attributes: {when: 'never', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -741,7 +741,7 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), output: ';', - options: [{attributes: {spaces: 'always', allowMultiline: false}}], + options: [{attributes: {when: 'always', allowMultiline: false}}], errors: [{ message: 'There should be no newline after \'{\'' }, { @@ -754,7 +754,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '', output: '', - options: [{attributes: {spaces: 'never', spacing: {objectLiterals: 'always'}}}], + options: [{attributes: {when: 'never', spacing: {objectLiterals: 'always'}}}], errors: [{ message: 'There should be no space after \'{\'' }, { @@ -775,7 +775,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { @@ -792,7 +792,7 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '
', output: '
', - options: [{attributes: {spaces: 'always'}}], + options: [{attributes: {when: 'always'}}], errors: [{ message: 'A space is required after \'{\'' }, { From 65135b8403ed322bb04ad0316988b67a3ed47acb Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 13:03:08 +0200 Subject: [PATCH 5/9] Fix the behavior of the boolean "attributes" --- lib/rules/jsx-curly-spacing.js | 18 +++- tests/lib/rules/jsx-curly-spacing.js | 130 +++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 3 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index bdf397450c..ce8c4b1acf 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -239,11 +239,23 @@ module.exports = { * @returns {void} */ function validateBraceSpacing(node) { - // Only validate attributes - if (node.parent.type === 'JSXElement') { + var config; + switch (node.parent.type) { + case 'JSXAttribute': + case 'JSXOpeningElement': + config = attributesConfig; + break; + + case 'JSXElement': + return; + + default: + return; + } + if (config === null) { return; } - var config = attributesConfig; + var first = context.getFirstToken(node); var last = sourceCode.getLastToken(node); var second = context.getTokenAfter(first, {includeComments: true}); diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index 904ef625d0..daf01b8a4c 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -42,6 +42,38 @@ ruleTester.run('jsx-curly-spacing', rule, { ].join('\n') }, { code: ';' + }, { + code: ';', + options: [{attributes: true}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: true}] + }, { + code: ';', + options: [{attributes: true}] + }, { + code: ';', + options: [{attributes: false}] + }, { + code: [ + ';' + ].join('\n'), + options: [{attributes: false}] + }, { + code: ';', + options: [{attributes: false}] + }, { + code: ';', + options: [{attributes: false}] + }, { + code: ';', + options: [{attributes: false}] }, { code: ';', options: [{when: 'never'}] @@ -281,6 +313,24 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'There should be no space before \'}\'' }] + }, { + code: ';', + output: ';', + options: [{attributes: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] }, { code: ';', output: ';', @@ -413,6 +463,86 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'A space is required before \'}\'' }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'never'}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: [{attributes: true, when: 'never', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'never', spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'never', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'always'}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: [{attributes: true, when: 'always', allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'always', spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: [{attributes: true, when: 'always', spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }, { code: ';', output: ';', From 408e1b1b3c5790adb6782f539380abd531736319 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 15:18:55 +0200 Subject: [PATCH 6/9] 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 \'}\'' + }] }] }); From 55151301144365615eb3a9fbde0df0a0d00461f0 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Tue, 9 May 2017 23:17:04 +0200 Subject: [PATCH 7/9] Add more mixed tests --- tests/lib/rules/jsx-curly-spacing.js | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index c0ac7b85e7..9ef1f35bfa 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -475,6 +475,43 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: '{bar}', options: [{attributes: {when: 'always'}}] + }, { + code: '{bar}', + options: [{attributes: {when: 'always'}}] + }, { + code: [ + '', + '{foo} {{ bar: baz }}', + '' + ].join('\n'), + options: [{ + when: 'never', + attributes: {when: 'always', spacing: {objectLiterals: 'never'}}, + children: true + }] + }, { + code: [ + '', + '{foo} { { bar: baz } }', + '' + ].join('\n'), + options: [{ + when: 'never', + spacing: {objectLiterals: 'always'}, + attributes: true, + children: {when: 'never'} + }] + }, { + code: [ + '', + '{foo} { { bar: baz } }', + '' + ].join('\n'), + options: [{ + spacing: {objectLiterals: 'always'}, + attributes: {when: 'never', spacing: {objectLiterals: 'always'}}, + children: {when: 'never'} + }] }], invalid: [{ From 6903f18f2286ed0f3f0ffdefe6ed25eaef6ae63c Mon Sep 17 00:00:00 2001 From: fatfisz Date: Tue, 9 May 2017 23:45:48 +0200 Subject: [PATCH 8/9] Add back the support for the previous config --- lib/rules/jsx-curly-spacing.js | 43 +- tests/lib/rules/jsx-curly-spacing.js | 580 +++++++++++++++++++++++++++ 2 files changed, 613 insertions(+), 10 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 589d7f3e7d..3e161e5714 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -61,19 +61,39 @@ module.exports = { } }, - allOf: [{ - $ref: '#/definitions/basicConfig' + oneOf: [{ + allOf: [{ + $ref: '#/definitions/basicConfig' + }, { + type: 'object', + properties: { + attributes: { + $ref: '#/definitions/basicConfigOrBoolean' + }, + children: { + $ref: '#/definitions/basicConfigOrBoolean' + } + } + }] }, { - type: 'object', - properties: { - attributes: { - $ref: '#/definitions/basicConfigOrBoolean' - }, - children: { - $ref: '#/definitions/basicConfigOrBoolean' + enum: SPACING_VALUES + }] + }, { + type: 'object', + properties: { + allowMultiline: { + type: 'boolean' + }, + spacing: { + type: 'object', + properties: { + objectLiterals: { + enum: SPACING_VALUES + } } } - }] + }, + additionalProperties: false }] }, @@ -104,6 +124,9 @@ module.exports = { var sourceCode = context.getSourceCode(); var originalConfig = context.options[0] || {}; + if (SPACING_VALUES.indexOf(originalConfig) !== -1) { + originalConfig = Object.assign({when: context.options[0]}, context.options[1]); + } var defaultConfig = normalizeConfig(originalConfig, { when: DEFAULT_WHEN, allowMultiline: DEFAULT_ALLOW_MULTILINE diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index 9ef1f35bfa..a5da5215be 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -27,6 +27,8 @@ var parserOptions = { var ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-curly-spacing', rule, { valid: [{ + code: ';' + }, { code: '{bar};' }, { code: '{ bar };' @@ -42,6 +44,12 @@ ruleTester.run('jsx-curly-spacing', rule, { code: '{{ bar: true, baz: true }};' }, { code: '{ { bar: true, baz: true } };' + }, { + code: [ + ';' + ].join('\n') }, { code: [ ';', + options: ['never'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never', {spacing: {objectLiterals: 'never'}}] + }, { + code: ';', + options: ['always'] + }, { + code: ';', + options: ['always', {allowMultiline: false}] + }, { + code: ';', + options: ['never'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never'] + }, { + code: ';', + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never'] + }, { + code: [ + '{/* comment */};' + ].join('\n'), + options: ['never'] + }, { + code: ';', + options: ['never'] + }, { + code: ';', + options: ['always', {spacing: {}}] + }, { + code: [ + ';' + ].join('\n'), + options: ['always', {spacing: {}}] + }, { + code: ';', + options: ['always', {spacing: {objectLiterals: 'never'}}] + }, { + code: [ + ';' + ].join('\n'), + options: ['always', {allowMultiline: true}] + }, { + code: [ + ';' + ].join('\n'), + options: ['always', {spacing: {objectLiterals: 'never'}}] + }, { + code: ';', + options: ['never'] + }, { + code: ';', + options: ['always'] + }, { + code: ';', + options: ['always', {allowMultiline: false}] + }, { + code: [ + ';' + ].join('\n'), + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never'] + }, { + code: ';', + options: ['never'] + }, { + code: ';', + options: ['never'] + }, { + code: ';', + options: ['always'] + }, { + code: ';', + options: ['always', {allowMultiline: false}] + }, { + code: ';', + options: ['never'] + }, { + code: ';', + options: ['always'] + }, { + code: [ + ';' + ].join('\n'), + options: ['always'] + }, { + code: ';', + options: ['never'] + }, { + code: '', + options: ['never', {spacing: {objectLiterals: 'always'}}] + }, { + code: '{bar}', + options: ['always'] }], invalid: [{ @@ -1538,5 +1697,426 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { message: 'A space is required before \'}\'' }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['never', {allowMultiline: false}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['never', {allowMultiline: false, spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['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: ['always', {allowMultiline: false, spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['always', {allowMultiline: false, spacing: {objectLiterals: 'always'}}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {allowMultiline: false}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['never', {allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['always', {allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {spacing: {}}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {spacing: {}}], + errors: [{ + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {spacing: {}}], + errors: [{ + message: 'A space is required after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {spacing: {objectLiterals: 'never'}}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['never', {allowMultiline: false}], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always', {allowMultiline: false}], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['never', {allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['always', {allowMultiline: false}], + errors: [{ + message: 'There should be no newline after \'{\'' + }, { + message: 'There should be no newline before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['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: ';', + output: ';', + options: ['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: ';', + output: ';', + options: ['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: ';', + output: ';', + options: ['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: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required before \'}\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: ';', + output: ';', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space after \'{\'' + }, { + message: 'There should be no space after \'{\'' + }] + }, { + code: ';', + output: ';', + options: ['never'], + errors: [{ + message: 'There should be no space before \'}\'' + }, { + message: 'There should be no space before \'}\'' + }] + }, { + code: [ + ';' + ].join('\n'), + output: ';', + options: ['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: [ + ';' + ].join('\n'), + output: ';', + options: ['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: '', + output: '', + options: ['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: '', + output: '', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] + }, { + code: '', + output: '', + options: ['always'], + errors: [{ + message: 'A space is required after \'{\'' + }, { + message: 'A space is required before \'}\'' + }] }] }); From d57c1153e3ee6c9a75665a16f52320a15d401871 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Mon, 22 May 2017 00:40:58 +0200 Subject: [PATCH 9/9] Update the docs --- README.md | 2 +- docs/rules/jsx-curly-spacing.md | 155 ++++++++++++++++++++++++++++---- 2 files changed, 139 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0e017e3fd0..bf7bcd08d2 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](# * [react/jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable) * [react/jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX (fixable) * [react/jsx-closing-tag-location](docs/rules/jsx-closing-tag-location.md): Validate closing tag location in JSX (fixable) -* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable) +* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes and expressions (fixable) * [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable) * [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md): Restrict file extensions that may contain JSX * [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX (fixable) diff --git a/docs/rules/jsx-curly-spacing.md b/docs/rules/jsx-curly-spacing.md index 471f276e8e..47c27c0c1b 100644 --- a/docs/rules/jsx-curly-spacing.md +++ b/docs/rules/jsx-curly-spacing.md @@ -1,4 +1,4 @@ -# Enforce or disallow spaces inside of curly braces in JSX attributes. (react/jsx-curly-spacing) +# Enforce or disallow spaces inside of curly braces in JSX attributes and expressions. (react/jsx-curly-spacing) While formatting preferences are very personal, a number of style guides require or disallow spaces between curly braces. @@ -6,31 +6,55 @@ While formatting preferences are very personal, a number of style guides require ## Rule Details -This rule aims to maintain consistency around the spacing inside of JSX attributes. +This rule aims to maintain consistency around the spacing inside of JSX attributes and expressions inside element children. It either requires or disallows spaces between those braces and the values inside of them. -### Options +## Rule Options There are two main options for the rule: -* `"always"` enforces a space inside of curly braces -* `"never"` disallows spaces inside of curly braces (default) +* `{"when": "always"}` enforces a space inside of curly braces +* `{"when": "never"}` disallows spaces inside of curly braces (default) -Depending on your coding conventions, you can choose either option by specifying it in your configuration: +There are also two properties that allow specifying how the rule should work with the attributes (`attributes`) and the expressions (`children`). The possible values are: -```json -"react/jsx-curly-spacing": [2, "always"] +* `true` enables checking for the spacing using the options (default for `attributes`), e.g. `{"attributes": false}` disables checking the attributes +* `false` disables checking for the spacing (default for `children`, for backward compatibility), e.g. `{"children": true}` enables checking the expressions +* an object containing options that override the global options, e.g. `{"when": "always", "children": {"when": "never"}}` enforces a space inside attributes, but disallows spaces inside expressions + +### never + +When `{"when": "never"}` is set, the following patterns are considered warnings: + +```jsx +; +; +; ``` -#### never +The following patterns are not warnings: + +```jsx +; +; +; +{firstname}; +{ firstname }; +{ + firstname +}; +``` -When `"never"` is set, the following patterns are considered warnings: +When `{"when": "never", "children": true}` is set, the following patterns are considered warnings: ```jsx ; ; ; +{ firstname }; ``` The following patterns are not warnings: @@ -41,11 +65,15 @@ The following patterns are not warnings: ; +{firstname}; +{ + firstname +}; ``` -#### always +### always -When `"always"` is used, the following patterns are considered warnings: +When `{"when": "always"}` is set, the following patterns are considered warnings: ```jsx ; @@ -61,14 +89,42 @@ The following patterns are not warnings: ; +{ firstname }; +{firstname}; +{ + firstname +}; +``` + +When `{"when": "always", "children": true}` is set, the following patterns are considered warnings: + +```jsx +; +; +; +{firstname}; +``` + +The following patterns are not warnings: + +```jsx +; +; +; +{ firstname }; +{ + firstname +}; ``` -#### Braces spanning multiple lines +### Braces spanning multiple lines By default, braces spanning multiple lines are allowed with either setting. If you want to disallow them you can specify an additional `allowMultiline` property with the value `false`: ```json -"react/jsx-curly-spacing": [2, "never", {"allowMultiline": false}] +"react/jsx-curly-spacing": [2, {"when": "never", "allowMultiline": false}] ``` When `"never"` is used and `allowMultiline` is `false`, the following patterns are considered warnings: @@ -87,6 +143,11 @@ The following patterns are not warnings: ```jsx ; ; +{firstname}; +{ firstname }; +{ + firstname +}; ``` When `"always"` is used and `allowMultiline` is `false`, the following patterns are considered warnings: @@ -105,14 +166,39 @@ The following patterns are not warnings: ```jsx ; ; +{firstname}; +{ firstname }; +{ + firstname +}; +``` + +When `{"when": "never", "attributes": {"allowMultiline": false}, "children": true}` is set, the following patterns are considered warnings: + +```jsx +; +; +{ firstname }; ``` -#### Granular spacing controls +The following patterns are not warnings: + +```jsx +; +{firstname}; +{ + firstname +}; +``` + +### Granular spacing controls You can specify an additional `spacing` property that is an object with the following possible values: ```json -"react/jsx-curly-spacing": [2, "always", {"spacing": { +"react/jsx-curly-spacing": [2, {"when": "always", "spacing": { "objectLiterals": "never" }}] ``` @@ -135,6 +221,41 @@ When `"never"` is used and `objectLiterals` is `"always"`, the following pattern Please note that spacing of the object literal curly braces themselves is controlled by the built-in [`object-curly-spacing`](http://eslint.org/docs/rules/object-curly-spacing) rule. +### Shorthand options + +To preserve backward compatibility, two additional options are supported: + +```json +"react/jsx-curly-spacing": [2, "always"] +``` + +which is a shorthand for + +```json +"react/jsx-curly-spacing": [2, {"when": "always"}] +``` + +and + +```json +"react/jsx-curly-spacing": [2, "never"] +``` + +which is a shorthand for + +```json +"react/jsx-curly-spacing": [2, {"when": "never"}] +``` + +When using the shorthand options, only attributes will be checked. To specify other options, use another argument: + +```json +"react/jsx-curly-spacing": [2, "never", { + "allowMultiline": false, + "spacing": {"objectLiterals": "always"} +}] +``` + ## When Not To Use It -You can turn this rule off if you are not concerned with the consistency around the spacing inside of JSX attributes. +You can turn this rule off if you are not concerned with the consistency around the spacing inside of JSX attributes or expressions.