Blame view
node_modules/eslint/lib/rules/key-spacing.js
10.6 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
/** * @fileoverview Rule to specify spacing of object literal keys and values * @author Brandon Mills * @copyright 2014 Brandon Mills. All rights reserved. */ "use strict"; //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Checks whether a string contains a line terminator as defined in * http://www.ecma-international.org/ecma-262/5.1/#sec-7.3 * @param {string} str String to test. * @returns {boolean} True if str contains a line terminator. */ function containsLineTerminator(str) { return /[ \r\u2028\u2029]/.test(str); } /** * Gets the last element of an array. * @param {Array} arr An array. * @returns {any} Last element of arr. */ function last(arr) { return arr[arr.length - 1]; } /** * Checks whether a property is a member of the property group it follows. * @param {ASTNode} lastMember The last Property known to be in the group. * @param {ASTNode} candidate The next Property that might be in the group. * @returns {boolean} True if the candidate property is part of the group. */ function continuesPropertyGroup(lastMember, candidate) { var groupEndLine = lastMember.loc.start.line, candidateStartLine = candidate.loc.start.line, comments, i; if (candidateStartLine - groupEndLine <= 1) { return true; } // Check that the first comment is adjacent to the end of the group, the // last comment is adjacent to the candidate property, and that successive // comments are adjacent to each other. comments = candidate.leadingComments; if ( comments && comments[0].loc.start.line - groupEndLine <= 1 && candidateStartLine - last(comments).loc.end.line <= 1 ) { for (i = 1; i < comments.length; i++) { if (comments[i].loc.start.line - comments[i - 1].loc.end.line > 1) { return false; } } return true; } return false; } /** * Checks whether a node is contained on a single line. * @param {ASTNode} node AST Node being evaluated. * @returns {boolean} True if the node is a single line. */ function isSingleLine(node) { return (node.loc.end.line === node.loc.start.line); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ var messages = { key: "{{error}} space after {{computed}}key \"{{key}}\".", value: "{{error}} space before value for {{computed}}key \"{{key}}\"." }; module.exports = function(context) { /** * OPTIONS * "key-spacing": [2, { * beforeColon: false, * afterColon: true, * align: "colon" // Optional, or "value" * } */ var options = context.options[0] || {}, align = options.align, mode = options.mode || "strict", beforeColon = +!!options.beforeColon, // Defaults to false afterColon = +!(options.afterColon === false); // Defaults to true /** * Starting from the given a node (a property.key node here) looks forward * until it finds the last token before a colon punctuator and returns it. * @param {ASTNode} node The node to start looking from. * @returns {ASTNode} The last token before a colon punctuator. */ function getLastTokenBeforeColon(node) { var prevNode; while (node && (node.type !== "Punctuator" || node.value !== ":")) { prevNode = node; node = context.getTokenAfter(node); } return prevNode; } /** * Gets an object literal property's key as the identifier name or string value. * @param {ASTNode} property Property node whose key to retrieve. * @returns {string} The property's key. */ function getKey(property) { var key = property.key; if (property.computed) { return context.getSource().slice(key.range[0], key.range[1]); } return property.key.name || property.key.value; } /** * Reports an appropriately-formatted error if spacing is incorrect on one * side of the colon. * @param {ASTNode} property Key-value pair in an object literal. * @param {string} side Side being verified - either "key" or "value". * @param {string} whitespace Actual whitespace string. * @param {int} expected Expected whitespace length. * @returns {void} */ function report(property, side, whitespace, expected) { var diff = whitespace.length - expected, key = property.key, firstTokenAfterColon = context.getTokenAfter(key, 1), location = side === "key" ? key.loc.start : firstTokenAfterColon.loc.start; if ((diff && mode === "strict" || diff < 0 && mode === "minimum") && !(expected && containsLineTerminator(whitespace)) ) { context.report(property[side], location, messages[side], { error: diff > 0 ? "Extra" : "Missing", computed: property.computed ? "computed " : "", key: getKey(property) }); } } /** * Gets the number of characters in a key, including quotes around string * keys and braces around computed property keys. * @param {ASTNode} property Property of on object literal. * @returns {int} Width of the key. */ function getKeyWidth(property) { var startToken, endToken; // Ignore shorthand methods and properties, as they have no colon if (property.method || property.shorthand) { return 0; } startToken = context.getFirstToken(property); endToken = getLastTokenBeforeColon(property.key); return endToken.range[1] - startToken.range[0]; } /** * Gets the whitespace around the colon in an object literal property. * @param {ASTNode} property Property node from an object literal. * @returns {Object} Whitespace before and after the property's colon. */ function getPropertyWhitespace(property) { var whitespace = /(\s*):(\s*)/.exec(context.getSource().slice( property.key.range[1], property.value.range[0] )); if (whitespace) { return { beforeColon: whitespace[1], afterColon: whitespace[2] }; } } /** * Creates groups of properties. * @param {ASTNode} node ObjectExpression node being evaluated. * @returns {Array.<ASTNode[]>} Groups of property AST node lists. */ function createGroups(node) { if (node.properties.length === 1) { return [node.properties]; } return node.properties.reduce(function(groups, property) { var currentGroup = last(groups), prev = last(currentGroup); if (!prev || continuesPropertyGroup(prev, property)) { currentGroup.push(property); } else { groups.push([property]); } return groups; }, [[]]); } /** * Verifies correct vertical alignment of a group of properties. * @param {ASTNode[]} properties List of Property AST nodes. * @returns {void} */ function verifyGroupAlignment(properties) { var length = properties.length, widths = properties.map(getKeyWidth), // Width of keys, including quotes targetWidth = Math.max.apply(null, widths), i, property, whitespace, width; // Conditionally include one space before or after colon targetWidth += (align === "colon" ? beforeColon : afterColon); for (i = 0; i < length; i++) { property = properties[i]; whitespace = getPropertyWhitespace(property); if (!whitespace) { continue; // Object literal getters/setters lack a colon } width = widths[i]; if (align === "value") { report(property, "key", whitespace.beforeColon, beforeColon); report(property, "value", whitespace.afterColon, targetWidth - width); } else { // align = "colon" report(property, "key", whitespace.beforeColon, targetWidth - width); report(property, "value", whitespace.afterColon, afterColon); } } } /** * Verifies vertical alignment, taking into account groups of properties. * @param {ASTNode} node ObjectExpression node being evaluated. * @returns {void} */ function verifyAlignment(node) { createGroups(node).forEach(function(group) { verifyGroupAlignment(group); }); } /** * Verifies spacing of property conforms to specified options. * @param {ASTNode} node Property node being evaluated. * @returns {void} */ function verifySpacing(node) { var whitespace = getPropertyWhitespace(node); if (whitespace) { // Object literal getters/setters lack colons report(node, "key", whitespace.beforeColon, beforeColon); report(node, "value", whitespace.afterColon, afterColon); } } /** * Verifies spacing of each property in a list. * @param {ASTNode[]} properties List of Property AST nodes. * @returns {void} */ function verifyListSpacing(properties) { var length = properties.length; for (var i = 0; i < length; i++) { verifySpacing(properties[i]); } } //-------------------------------------------------------------------------- // Public API //-------------------------------------------------------------------------- if (align) { // Verify vertical alignment return { "ObjectExpression": function(node) { if (isSingleLine(node)) { verifyListSpacing(node.properties); } else { verifyAlignment(node); } } }; } else { // Strictly obey beforeColon and afterColon in each property return { "Property": function(node) { verifySpacing(node); } }; } }; module.exports.schema = [ { "type": "object", "properties": { "align": { "enum": ["colon", "value"] }, "mode": { "enum": ["strict", "minimum"] }, "beforeColon": { "type": "boolean" }, "afterColon": { "type": "boolean" } }, "additionalProperties": false } ]; |