|
| 1 | +/** |
| 2 | + * @fileoverview Validate closing bracket location in JSX |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +module.exports = function(context) { |
| 11 | + |
| 12 | + var MESSAGE = 'The closing bracket must be {{location}}'; |
| 13 | + var MESSAGE_LOCATION = { |
| 14 | + 'after-props': 'placed after the last prop', |
| 15 | + 'after-tag': 'placed after the opening tag', |
| 16 | + 'props-aligned': 'aligned with the last prop', |
| 17 | + 'tag-aligned': 'aligned with the opening tag' |
| 18 | + }; |
| 19 | + |
| 20 | + /** |
| 21 | + * Get expected location for the closing bracket |
| 22 | + * @param {Object} tokens Locations of the opening bracket, closing bracket and last prop |
| 23 | + * @return {String} Expected location for the closing bracket |
| 24 | + */ |
| 25 | + function getExpectedLocation(tokens) { |
| 26 | + var location; |
| 27 | + // Is always after the opening tag if there is no props |
| 28 | + if (typeof tokens.lastProp === 'undefined') { |
| 29 | + location = 'after-tag'; |
| 30 | + // Is always after the last prop if this one is on the same line as the opening bracket |
| 31 | + } else if (tokens.opening.line === tokens.lastProp.line) { |
| 32 | + location = 'after-props'; |
| 33 | + // Else use configuration, or default value |
| 34 | + } else { |
| 35 | + location = context.options[0] && context.options[0].location || 'tag-aligned'; |
| 36 | + } |
| 37 | + return location; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Check if the closing bracket is correctly located |
| 42 | + * @param {Object} tokens Locations of the opening bracket, closing bracket and last prop |
| 43 | + * @param {String} expectedLocation Expected location for the closing bracket |
| 44 | + * @return {Boolean} True if the closing bracket is correctly located, false if not |
| 45 | + */ |
| 46 | + function hasCorrectLocation(tokens, expectedLocation) { |
| 47 | + switch (expectedLocation) { |
| 48 | + case 'after-tag': |
| 49 | + return tokens.tag.line === tokens.closing.line; |
| 50 | + case 'after-props': |
| 51 | + return tokens.lastProp.line === tokens.closing.line; |
| 52 | + case 'props-aligned': |
| 53 | + return tokens.lastProp.column === tokens.closing.column; |
| 54 | + case 'tag-aligned': |
| 55 | + return tokens.opening.column === tokens.closing.column; |
| 56 | + default: |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the locations of the opening bracket, closing bracket and last prop |
| 63 | + * @param {ASTNode} node The node to check |
| 64 | + * @return {Object} Locations of the opening bracket, closing bracket and last prop |
| 65 | + */ |
| 66 | + function getTokensLocations(node) { |
| 67 | + var opening = context.getFirstToken(node).loc.start; |
| 68 | + var closing = context.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start; |
| 69 | + var tag = context.getFirstToken(node.name).loc.start; |
| 70 | + var lastProp; |
| 71 | + if (node.attributes.length) { |
| 72 | + lastProp = context.getFirstToken(node.attributes[node.attributes.length - 1]).loc.start; |
| 73 | + } |
| 74 | + return { |
| 75 | + tag: tag, |
| 76 | + opening: opening, |
| 77 | + closing: closing, |
| 78 | + lastProp: lastProp |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + JSXOpeningElement: function(node) { |
| 84 | + var tokens = getTokensLocations(node); |
| 85 | + var expectedLocation = getExpectedLocation(tokens); |
| 86 | + if (hasCorrectLocation(tokens, expectedLocation)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + context.report(node, MESSAGE, { |
| 90 | + location: MESSAGE_LOCATION[expectedLocation] |
| 91 | + }); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | +}; |
| 96 | + |
| 97 | +module.exports.schema = [{ |
| 98 | + type: 'object', |
| 99 | + properties: { |
| 100 | + location: { |
| 101 | + enum: ['after-props', 'props-aligned', 'tag-aligned'] |
| 102 | + } |
| 103 | + } |
| 104 | +}]; |
0 commit comments