|
| 1 | +/** |
| 2 | + * @fileoverview Enforce or disallow spaces inside of curly braces in JSX attributes. |
| 3 | + * @author Jamund Ferguson, Brandyn Bennett, Michael Ficarra, Vignesh Anand, Jamund Ferguson, Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +module.exports = function(context) { |
| 12 | + var spaced = context.options[0] === 'always'; |
| 13 | + |
| 14 | + // -------------------------------------------------------------------------- |
| 15 | + // Helpers |
| 16 | + // -------------------------------------------------------------------------- |
| 17 | + |
| 18 | + /** |
| 19 | + * Determines whether two adjacent tokens are have whitespace between them. |
| 20 | + * @param {Object} left - The left token object. |
| 21 | + * @param {Object} right - The right token object. |
| 22 | + * @returns {boolean} Whether or not there is space between the tokens. |
| 23 | + */ |
| 24 | + function isSpaced(left, right) { |
| 25 | + return left.range[1] < right.range[0]; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Reports that there shouldn't be a space after the first token |
| 30 | + * @param {ASTNode} node - The node to report in the event of an error. |
| 31 | + * @param {Token} token - The token to use for the report. |
| 32 | + * @returns {void} |
| 33 | + */ |
| 34 | + function reportNoBeginningSpace(node, token) { |
| 35 | + context.report(node, token.loc.start, |
| 36 | + 'There should be no space after \'' + token.value + '\''); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Reports that there shouldn't be a space before the last token |
| 41 | + * @param {ASTNode} node - The node to report in the event of an error. |
| 42 | + * @param {Token} token - The token to use for the report. |
| 43 | + * @returns {void} |
| 44 | + */ |
| 45 | + function reportNoEndingSpace(node, token) { |
| 46 | + context.report(node, token.loc.start, |
| 47 | + 'There should be no space before \'' + token.value + '\''); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Reports that there should be a space after the first token |
| 52 | + * @param {ASTNode} node - The node to report in the event of an error. |
| 53 | + * @param {Token} token - The token to use for the report. |
| 54 | + * @returns {void} |
| 55 | + */ |
| 56 | + function reportRequiredBeginningSpace(node, token) { |
| 57 | + context.report(node, token.loc.start, |
| 58 | + 'A space is required after \'' + token.value + '\''); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Reports that there should be a space before the last token |
| 63 | + * @param {ASTNode} node - The node to report in the event of an error. |
| 64 | + * @param {Token} token - The token to use for the report. |
| 65 | + * @returns {void} |
| 66 | + */ |
| 67 | + function reportRequiredEndingSpace(node, token) { |
| 68 | + context.report(node, token.loc.start, |
| 69 | + 'A space is required before \'' + token.value + '\''); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Determines if spacing in curly braces is valid. |
| 74 | + * @param {ASTNode} node The AST node to check. |
| 75 | + * @param {Token} first The first token to check (should be the opening brace) |
| 76 | + * @param {Token} second The second token to check (should be first after the opening brace) |
| 77 | + * @param {Token} penultimate The penultimate token to check (should be last before closing brace) |
| 78 | + * @param {Token} last The last token to check (should be closing brace) |
| 79 | + * @returns {void} |
| 80 | + */ |
| 81 | + function validateBraceSpacing(node, first, second, penultimate, last) { |
| 82 | + if (spaced && !isSpaced(first, second)) { |
| 83 | + reportRequiredBeginningSpace(node, first); |
| 84 | + } |
| 85 | + if (!spaced && isSpaced(first, second)) { |
| 86 | + reportNoBeginningSpace(node, first); |
| 87 | + } |
| 88 | + if (spaced && !isSpaced(penultimate, last)) { |
| 89 | + reportRequiredEndingSpace(node, last); |
| 90 | + } |
| 91 | + if (!spaced && isSpaced(penultimate, last)) { |
| 92 | + reportNoEndingSpace(node, last); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // -------------------------------------------------------------------------- |
| 97 | + // Public |
| 98 | + // -------------------------------------------------------------------------- |
| 99 | + |
| 100 | + return { |
| 101 | + JSXExpressionContainer: function(node) { |
| 102 | + var first = context.getFirstToken(node); |
| 103 | + var second = context.getFirstToken(node, 1); |
| 104 | + var penultimate = context.getLastToken(node, 1); |
| 105 | + var last = context.getLastToken(node); |
| 106 | + |
| 107 | + validateBraceSpacing(node, first, second, penultimate, last); |
| 108 | + } |
| 109 | + }; |
| 110 | +}; |
| 111 | + |
| 112 | +module.exports.schema = [{ |
| 113 | + enum: ['always', 'never'] |
| 114 | +}]; |
0 commit comments