Blame view
node_modules/eslint/lib/rules/comma-dangle.js
6.87 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 |
/** * @fileoverview Rule to forbid or enforce dangling commas. * @author Ian Christian Myers * @copyright 2015 Toru Nagashima * @copyright 2015 Mathias Schreck * @copyright 2013 Ian Christian Myers * See LICENSE file in root directory for full license. */ "use strict"; //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Gets the last element of a given array. * * @param {*[]} xs - An array to get. * @returns {*} The last element, or undefined. */ function getLast(xs) { if (xs.length === 0) { return null; } return xs[xs.length - 1]; } /** * Checks whether or not a trailing comma is allowed in a given node. * `ArrayPattern` which has `RestElement` disallows it. * * @param {ASTNode} node - A node to check. * @param {ASTNode} lastItem - The node of the last element in the given node. * @returns {boolean} `true` if a trailing comma is allowed. */ function isTrailingCommaAllowed(node, lastItem) { switch (node.type) { case "ArrayPattern": // TODO(t-nagashima): Remove SpreadElement after https://github.com/eslint/espree/issues/194 was fixed. return ( lastItem.type !== "RestElement" && lastItem.type !== "SpreadElement" ); // TODO(t-nagashima): Remove this case after https://github.com/eslint/espree/issues/195 was fixed. case "ArrayExpression": return ( node.parent.type !== "ForOfStatement" || node.parent.left !== node || lastItem.type !== "SpreadElement" ); default: return true; } } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = function(context) { var mode = context.options[0]; var UNEXPECTED_MESSAGE = "Unexpected trailing comma."; var MISSING_MESSAGE = "Missing trailing comma."; /** * Checks whether or not a given node is multiline. * This rule handles a given node as multiline when the closing parenthesis * and the last element are not on the same line. * * @param {ASTNode} node - A ndoe to check. * @returns {boolean} `true` if the node is multiline. */ function isMultiline(node) { var lastItem = getLast(node.properties || node.elements || node.specifiers); if (!lastItem) { return false; } var sourceCode = context.getSourceCode(), penultimateToken = sourceCode.getLastToken(lastItem), lastToken = sourceCode.getLastToken(node); if (lastToken.value === ",") { penultimateToken = lastToken; lastToken = sourceCode.getTokenAfter(lastToken); } return lastToken.loc.end.line !== penultimateToken.loc.end.line; } /** * Reports a trailing comma if it exists. * * @param {ASTNode} node - A node to check. Its type is one of * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, * ImportDeclaration, and ExportNamedDeclaration. * @returns {void} */ function forbidTrailingComma(node) { var lastItem = getLast(node.properties || node.elements || node.specifiers); if (!lastItem || (node.type === "ImportDeclaration" && lastItem.type !== "ImportSpecifier")) { return; } var sourceCode = context.getSourceCode(), trailingToken; // last item can be surrounded by parentheses for object and array literals if (node.type === "ObjectExpression" || node.type === "ArrayExpression") { trailingToken = sourceCode.getTokenBefore(sourceCode.getLastToken(node)); } else { trailingToken = sourceCode.getTokenAfter(lastItem); } if (trailingToken.value === ",") { context.report( lastItem, trailingToken.loc.start, UNEXPECTED_MESSAGE); } } /** * Reports the last element of a given node if it does not have a trailing * comma. * * If a given node is `ArrayPattern` which has `RestElement`, the trailing * comma is disallowed, so report if it exists. * * @param {ASTNode} node - A node to check. Its type is one of * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, * ImportDeclaration, and ExportNamedDeclaration. * @returns {void} */ function forceTrailingComma(node) { var lastItem = getLast(node.properties || node.elements || node.specifiers); if (!lastItem || (node.type === "ImportDeclaration" && lastItem.type !== "ImportSpecifier")) { return; } if (!isTrailingCommaAllowed(node, lastItem)) { forbidTrailingComma(node); return; } var sourceCode = context.getSourceCode(), trailingToken; // last item can be surrounded by parentheses for object and array literals if (node.type === "ObjectExpression" || node.type === "ArrayExpression") { trailingToken = sourceCode.getTokenBefore(sourceCode.getLastToken(node)); } else { trailingToken = sourceCode.getTokenAfter(lastItem); } if (trailingToken.value !== ",") { context.report( lastItem, lastItem.loc.end, MISSING_MESSAGE); } } /** * If a given node is multiline, reports the last element of a given node * when it does not have a trailing comma. * Otherwise, reports a trailing comma if it exists. * * @param {ASTNode} node - A node to check. Its type is one of * ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern, * ImportDeclaration, and ExportNamedDeclaration. * @returns {void} */ function forceTrailingCommaIfMultiline(node) { if (isMultiline(node)) { forceTrailingComma(node); } else { forbidTrailingComma(node); } } // Chooses a checking function. var checkForTrailingComma; if (mode === "always") { checkForTrailingComma = forceTrailingComma; } else if (mode === "always-multiline") { checkForTrailingComma = forceTrailingCommaIfMultiline; } else { checkForTrailingComma = forbidTrailingComma; } return { "ObjectExpression": checkForTrailingComma, "ObjectPattern": checkForTrailingComma, "ArrayExpression": checkForTrailingComma, "ArrayPattern": checkForTrailingComma, "ImportDeclaration": checkForTrailingComma, "ExportNamedDeclaration": checkForTrailingComma }; }; module.exports.schema = [ { "enum": ["always", "always-multiline", "never"] } ]; |