From 1534bdbea75bc8910cfec32ffff28585ed0d4ab3 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 7 May 2017 22:58:25 +0200 Subject: [PATCH] Fix the newline with object literals bug --- lib/rules/jsx-curly-spacing.js | 54 ++++++------------- tests/lib/rules/jsx-curly-spacing.js | 81 +++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 40 deletions(-) diff --git a/lib/rules/jsx-curly-spacing.js b/lib/rules/jsx-curly-spacing.js index 1ecfe5a19c..90563798ef 100644 --- a/lib/rules/jsx-curly-spacing.js +++ b/lib/rules/jsx-curly-spacing.js @@ -54,15 +54,15 @@ module.exports = { create: function(context) { + var DEFAULT_SPACING = SPACING.never; var DEFAULT_ALLOW_MULTILINE = true; var sourceCode = context.getSourceCode(); - var spaced = context.options[0] === SPACING.always; + var baseSpacing = context.options[0] || DEFAULT_SPACING; var config = context.options[1] || {}; var multiline = has(config, 'allowMultiline') ? config.allowMultiline : DEFAULT_ALLOW_MULTILINE; - var spacing = config.spacing || {}; - var defaultSpacing = spaced ? SPACING.always : SPACING.never; - var objectLiteralSpacing = spacing.objectLiterals || (spaced ? SPACING.always : SPACING.never); + var spacingConfig = config.spacing || {}; + var objectLiteralSpacing = spacingConfig.objectLiterals || baseSpacing; // -------------------------------------------------------------------------- // Helpers @@ -84,14 +84,14 @@ module.exports = { * @param {Token} token - The token to use for the report. * @returns {void} */ - function reportNoBeginningNewline(node, token) { + function reportNoBeginningNewline(node, token, spacing) { context.report({ node: node, loc: token.loc.start, message: `There should be no newline after '${token.value}'`, fix: function(fixer) { var nextToken = sourceCode.getTokenAfter(token); - return fixer.replaceTextRange([token.range[1], nextToken.range[0]], spaced ? ' ' : ''); + return fixer.replaceTextRange([token.range[1], nextToken.range[0]], spacing === SPACING.always ? ' ' : ''); } }); } @@ -102,14 +102,14 @@ module.exports = { * @param {Token} token - The token to use for the report. * @returns {void} */ - function reportNoEndingNewline(node, token) { + function reportNoEndingNewline(node, token, spacing) { context.report({ node: node, loc: token.loc.start, message: `There should be no newline before '${token.value}'`, fix: function(fixer) { var previousToken = sourceCode.getTokenBefore(token); - return fixer.replaceTextRange([previousToken.range[1], token.range[0]], spaced ? ' ' : ''); + return fixer.replaceTextRange([previousToken.range[1], token.range[0]], spacing === SPACING.always ? ' ' : ''); } }); } @@ -217,53 +217,29 @@ module.exports = { } var isObjectLiteral = first.value === second.value; - if (isObjectLiteral) { - if (objectLiteralSpacing === SPACING.never) { - if (sourceCode.isSpaceBetweenTokens(first, second)) { - reportNoBeginningSpace(node, first); - } else if (!multiline && isMultiline(first, second)) { - reportNoBeginningNewline(node, first); - } - if (sourceCode.isSpaceBetweenTokens(penultimate, last)) { - reportNoEndingSpace(node, last); - } else if (!multiline && isMultiline(penultimate, last)) { - reportNoEndingNewline(node, last); - } - } else if (objectLiteralSpacing === SPACING.always) { - if (!sourceCode.isSpaceBetweenTokens(first, second)) { - reportRequiredBeginningSpace(node, first); - } else if (!multiline && isMultiline(first, second)) { - reportNoBeginningNewline(node, first); - } - if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) { - reportRequiredEndingSpace(node, last); - } else if (!multiline && isMultiline(penultimate, last)) { - reportNoEndingNewline(node, last); - } - } - } else if (defaultSpacing === SPACING.always) { + var spacing = isObjectLiteral ? objectLiteralSpacing : baseSpacing; + if (spacing === SPACING.always) { if (!sourceCode.isSpaceBetweenTokens(first, second)) { reportRequiredBeginningSpace(node, first); } else if (!multiline && isMultiline(first, second)) { - reportNoBeginningNewline(node, first); + reportNoBeginningNewline(node, first, spacing); } - if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) { reportRequiredEndingSpace(node, last); } else if (!multiline && isMultiline(penultimate, last)) { - reportNoEndingNewline(node, last); + reportNoEndingNewline(node, last, spacing); } - } else if (defaultSpacing === SPACING.never) { + } else if (spacing === SPACING.never) { if (isMultiline(first, second)) { if (!multiline) { - reportNoBeginningNewline(node, first); + reportNoBeginningNewline(node, first, spacing); } } else if (sourceCode.isSpaceBetweenTokens(first, second)) { reportNoBeginningSpace(node, first); } if (isMultiline(penultimate, last)) { if (!multiline) { - reportNoEndingNewline(node, last); + reportNoEndingNewline(node, last, spacing); } } else if (sourceCode.isSpaceBetweenTokens(penultimate, last)) { reportNoEndingSpace(node, last); diff --git a/tests/lib/rules/jsx-curly-spacing.js b/tests/lib/rules/jsx-curly-spacing.js index 9f59c7b182..cebe4eee6f 100644 --- a/tests/lib/rules/jsx-curly-spacing.js +++ b/tests/lib/rules/jsx-curly-spacing.js @@ -28,9 +28,22 @@ var ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-curly-spacing', rule, { valid: [{ code: ';' + }, { + code: [ + ';' + ].join('\n') }, { code: ';', options: ['never'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never', {spacing: {objectLiterals: 'never'}}] }, { code: ';', options: ['always'] @@ -40,13 +53,20 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { code: ';', options: ['never'] + }, { + code: [ + ';' + ].join('\n'), + options: ['never'] }, { code: ';', options: ['always'] }, { code: [ ';' ].join('\n'), options: ['always'] @@ -92,6 +112,13 @@ ruleTester.run('jsx-curly-spacing', rule, { '} />;' ].join('\n'), options: ['always', {allowMultiline: true}] + }, { + code: [ + ';' + ].join('\n'), + options: ['always', {spacing: {objectLiterals: 'never'}}] }, { code: ';' }, { @@ -182,6 +209,58 @@ ruleTester.run('jsx-curly-spacing', rule, { }, { 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: ';',