|
| 1 | +/** |
| 2 | + * @fileoverview enforce consistent line breaks inside jsx curly |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const docsUrl = require('../util/docsUrl'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +function getNormalizedOption(context) { |
| 14 | + const rawOption = context.options[0] || 'consistent'; |
| 15 | + |
| 16 | + if (rawOption === 'consistent') { |
| 17 | + return { |
| 18 | + multiline: 'consistent', |
| 19 | + singleline: 'consistent' |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + if (rawOption === 'never') { |
| 24 | + return { |
| 25 | + multiline: 'forbid', |
| 26 | + singleline: 'forbid' |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + return { |
| 31 | + multiline: rawOption.multiline || 'consistent', |
| 32 | + singleline: rawOption.singleline || 'consistent' |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +module.exports = { |
| 37 | + meta: { |
| 38 | + type: 'layout', |
| 39 | + |
| 40 | + docs: { |
| 41 | + description: 'enforce consistent line breaks inside jsx curly', |
| 42 | + category: 'Stylistic Issues', |
| 43 | + recommended: false, |
| 44 | + url: docsUrl('jsx-curly-newline') |
| 45 | + }, |
| 46 | + |
| 47 | + fixable: 'whitespace', |
| 48 | + |
| 49 | + schema: [ |
| 50 | + { |
| 51 | + oneOf: [ |
| 52 | + { |
| 53 | + enum: ['consistent', 'never'] |
| 54 | + }, |
| 55 | + { |
| 56 | + type: 'object', |
| 57 | + properties: { |
| 58 | + singleline: {enum: ['consistent', 'require', 'forbid']}, |
| 59 | + multiline: {enum: ['consistent', 'require', 'forbid']} |
| 60 | + }, |
| 61 | + additionalProperties: false |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + |
| 67 | + |
| 68 | + messages: { |
| 69 | + expectedBefore: 'Expected newline before \'}\'.', |
| 70 | + expectedAfter: 'Expected newline after \'{\'.', |
| 71 | + unexpectedBefore: 'Unexpected newline before \'{\'.', |
| 72 | + unexpectedAfter: 'Unexpected newline after \'}\'.' |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + create(context) { |
| 77 | + const sourceCode = context.getSourceCode(); |
| 78 | + const option = getNormalizedOption(context); |
| 79 | + |
| 80 | + // ---------------------------------------------------------------------- |
| 81 | + // Helpers |
| 82 | + // ---------------------------------------------------------------------- |
| 83 | + |
| 84 | + /** |
| 85 | + * Determines whether two adjacent tokens are on the same line. |
| 86 | + * @param {Object} left - The left token object. |
| 87 | + * @param {Object} right - The right token object. |
| 88 | + * @returns {boolean} Whether or not the tokens are on the same line. |
| 89 | + */ |
| 90 | + function isTokenOnSameLine(left, right) { |
| 91 | + return left.loc.end.line === right.loc.start.line; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Determines whether there should be newlines inside curlys |
| 96 | + * @param {ASTNode} expression The expression contained in the curlys |
| 97 | + * @param {boolean} hasLeftNewline `true` if the left curly has a newline in the current code. |
| 98 | + * @returns {boolean} `true` if there should be newlines inside the function curlys |
| 99 | + */ |
| 100 | + function shouldHaveNewlines(expression, hasLeftNewline) { |
| 101 | + const isMultiline = expression.loc.start.line !== expression.loc.end.line; |
| 102 | + |
| 103 | + switch (isMultiline ? option.multiline : option.singleline) { |
| 104 | + case 'forbid': return false; |
| 105 | + case 'require': return true; |
| 106 | + case 'consistent': |
| 107 | + default: return hasLeftNewline; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Validates curlys |
| 113 | + * @param {Object} curlys An object with keys `leftParen` for the left paren token, and `rightParen` for the right paren token |
| 114 | + * @param {ASTNode} expression The expression inside the curly |
| 115 | + * @returns {void} |
| 116 | + */ |
| 117 | + function validateCurlys(curlys, expression) { |
| 118 | + const leftCurly = curlys.leftCurly; |
| 119 | + const rightCurly = curlys.rightCurly; |
| 120 | + const tokenAfterLeftCurly = sourceCode.getTokenAfter(leftCurly); |
| 121 | + const tokenBeforeRightCurly = sourceCode.getTokenBefore(rightCurly); |
| 122 | + const hasLeftNewline = !isTokenOnSameLine(leftCurly, tokenAfterLeftCurly); |
| 123 | + const hasRightNewline = !isTokenOnSameLine(tokenBeforeRightCurly, rightCurly); |
| 124 | + const needsNewlines = shouldHaveNewlines(expression, hasLeftNewline); |
| 125 | + |
| 126 | + if (hasLeftNewline && !needsNewlines) { |
| 127 | + context.report({ |
| 128 | + node: leftCurly, |
| 129 | + messageId: 'unexpectedAfter', |
| 130 | + fix(fixer) { |
| 131 | + return sourceCode |
| 132 | + .getText() |
| 133 | + .slice(leftCurly.range[1], tokenAfterLeftCurly.range[0]) |
| 134 | + .trim() ? |
| 135 | + null : // If there is a comment between the { and the first element, don't do a fix. |
| 136 | + fixer.removeRange([leftCurly.range[1], tokenAfterLeftCurly.range[0]]); |
| 137 | + } |
| 138 | + }); |
| 139 | + } else if (!hasLeftNewline && needsNewlines) { |
| 140 | + context.report({ |
| 141 | + node: leftCurly, |
| 142 | + messageId: 'expectedAfter', |
| 143 | + fix: fixer => fixer.insertTextAfter(leftCurly, '\n') |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + if (hasRightNewline && !needsNewlines) { |
| 148 | + context.report({ |
| 149 | + node: rightCurly, |
| 150 | + messageId: 'unexpectedBefore', |
| 151 | + fix(fixer) { |
| 152 | + return sourceCode |
| 153 | + .getText() |
| 154 | + .slice(tokenBeforeRightCurly.range[1], rightCurly.range[0]) |
| 155 | + .trim() ? |
| 156 | + null : // If there is a comment between the last element and the }, don't do a fix. |
| 157 | + fixer.removeRange([ |
| 158 | + tokenBeforeRightCurly.range[1], |
| 159 | + rightCurly.range[0] |
| 160 | + ]); |
| 161 | + } |
| 162 | + }); |
| 163 | + } else if (!hasRightNewline && needsNewlines) { |
| 164 | + context.report({ |
| 165 | + node: rightCurly, |
| 166 | + messageId: 'expectedBefore', |
| 167 | + fix: fixer => fixer.insertTextBefore(rightCurly, '\n') |
| 168 | + }); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + // ---------------------------------------------------------------------- |
| 174 | + // Public |
| 175 | + // ---------------------------------------------------------------------- |
| 176 | + |
| 177 | + return { |
| 178 | + JSXExpressionContainer(node) { |
| 179 | + const curlyTokens = { |
| 180 | + leftCurly: sourceCode.getFirstToken(node), |
| 181 | + rightCurly: sourceCode.getLastToken(node) |
| 182 | + }; |
| 183 | + validateCurlys(curlyTokens, node.expression); |
| 184 | + } |
| 185 | + }; |
| 186 | + } |
| 187 | +}; |
0 commit comments