|
| 1 | +/** |
| 2 | + * @fileoverview Validate props indentation in JSX |
| 3 | + * @author Yannick Croissant |
| 4 | +
|
| 5 | + * This rule has been ported and modified from eslint and nodeca. |
| 6 | + * @author Vitaly Puzrin |
| 7 | + * @author Gyandeep Singh |
| 8 | + * @copyright 2015 Vitaly Puzrin. All rights reserved. |
| 9 | + * @copyright 2015 Gyandeep Singh. All rights reserved. |
| 10 | + Copyright (C) 2014 by Vitaly Puzrin |
| 11 | +
|
| 12 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + of this software and associated documentation files (the 'Software'), to deal |
| 14 | + in the Software without restriction, including without limitation the rights |
| 15 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | + copies of the Software, and to permit persons to whom the Software is |
| 17 | + furnished to do so, subject to the following conditions: |
| 18 | +
|
| 19 | + The above copyright notice and this permission notice shall be included in |
| 20 | + all copies or substantial portions of the Software. |
| 21 | +
|
| 22 | + THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | + THE SOFTWARE. |
| 29 | + */ |
| 30 | +'use strict'; |
| 31 | + |
| 32 | +// ------------------------------------------------------------------------------ |
| 33 | +// Rule Definition |
| 34 | +// ------------------------------------------------------------------------------ |
| 35 | +module.exports = function(context) { |
| 36 | + |
| 37 | + var MESSAGE = 'Expected indentation of {{needed}} {{type}} {{characters}} but found {{gotten}}.'; |
| 38 | + |
| 39 | + var extraColumnStart = 0; |
| 40 | + var indentType = 'space'; |
| 41 | + var indentSize = 4; |
| 42 | + |
| 43 | + if (context.options.length) { |
| 44 | + if (context.options[0] === 'tab') { |
| 45 | + indentSize = 1; |
| 46 | + indentType = 'tab'; |
| 47 | + } else if (typeof context.options[0] === 'number') { |
| 48 | + indentSize = context.options[0]; |
| 49 | + indentType = 'space'; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Reports a given indent violation and properly pluralizes the message |
| 55 | + * @param {ASTNode} node Node violating the indent rule |
| 56 | + * @param {Number} needed Expected indentation character count |
| 57 | + * @param {Number} gotten Indentation character count in the actual node/code |
| 58 | + * @param {Object=} loc Error line and column location |
| 59 | + */ |
| 60 | + function report(node, needed, gotten, loc) { |
| 61 | + var msgContext = { |
| 62 | + needed: needed, |
| 63 | + type: indentType, |
| 64 | + characters: needed === 1 ? 'character' : 'characters', |
| 65 | + gotten: gotten |
| 66 | + }; |
| 67 | + |
| 68 | + if (loc) { |
| 69 | + context.report(node, loc, MESSAGE, msgContext); |
| 70 | + } else { |
| 71 | + context.report(node, MESSAGE, msgContext); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get node indent |
| 77 | + * @param {ASTNode} node Node to examine |
| 78 | + * @param {Boolean} byLastLine get indent of node's last line |
| 79 | + * @param {Boolean} excludeCommas skip comma on start of line |
| 80 | + * @return {Number} Indent |
| 81 | + */ |
| 82 | + function getNodeIndent(node, byLastLine, excludeCommas) { |
| 83 | + byLastLine = byLastLine || false; |
| 84 | + excludeCommas = excludeCommas || false; |
| 85 | + |
| 86 | + var src = context.getSource(node, node.loc.start.column + extraColumnStart); |
| 87 | + var lines = src.split('\n'); |
| 88 | + if (byLastLine) { |
| 89 | + src = lines[lines.length - 1]; |
| 90 | + } else { |
| 91 | + src = lines[0]; |
| 92 | + } |
| 93 | + |
| 94 | + var skip = excludeCommas ? ',' : ''; |
| 95 | + |
| 96 | + var regExp; |
| 97 | + if (indentType === 'space') { |
| 98 | + regExp = new RegExp('^[ ' + skip + ']+'); |
| 99 | + } else { |
| 100 | + regExp = new RegExp('^[\t' + skip + ']+'); |
| 101 | + } |
| 102 | + |
| 103 | + var indent = regExp.exec(src); |
| 104 | + return indent ? indent[0].length : 0; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Checks node is the first in its own start line. By default it looks by start line. |
| 109 | + * @param {ASTNode} node The node to check |
| 110 | + * @param {Boolean} [byEndLocation] Lookup based on start position or end |
| 111 | + * @return {Boolean} true if its the first in the its start line |
| 112 | + */ |
| 113 | + function isNodeFirstInLine(node, byEndLocation) { |
| 114 | + var firstToken = byEndLocation === true ? context.getLastToken(node, 1) : context.getTokenBefore(node); |
| 115 | + var startLine = byEndLocation === true ? node.loc.end.line : node.loc.start.line; |
| 116 | + var endLine = firstToken ? firstToken.loc.end.line : -1; |
| 117 | + |
| 118 | + return startLine !== endLine; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Check indent for nodes list |
| 123 | + * @param {ASTNode[]} nodes list of node objects |
| 124 | + * @param {Number} indent needed indent |
| 125 | + * @param {Boolean} excludeCommas skip comma on start of line |
| 126 | + */ |
| 127 | + function checkNodesIndent(nodes, indent, excludeCommas) { |
| 128 | + nodes.forEach(function(node) { |
| 129 | + var nodeIndent = getNodeIndent(node, false, excludeCommas); |
| 130 | + if ( |
| 131 | + node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression' && |
| 132 | + nodeIndent !== indent && isNodeFirstInLine(node) |
| 133 | + ) { |
| 134 | + report(node, indent, nodeIndent); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + return { |
| 140 | + JSXOpeningElement: function(node) { |
| 141 | + var elementIndent = getNodeIndent(node); |
| 142 | + checkNodesIndent(node.attributes, elementIndent + indentSize); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | +}; |
| 147 | + |
| 148 | +module.exports.schema = [{ |
| 149 | + oneOf: [{ |
| 150 | + enum: ['tab'] |
| 151 | + }, { |
| 152 | + type: 'integer' |
| 153 | + }] |
| 154 | +}]; |
0 commit comments