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