summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/rules/comma-spacing.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2017-12-22 16:53:42 +0100
committerMichaël Zasso <targos@protonmail.com>2018-01-11 09:48:05 +0100
commit3dc30632755713179f345f4af024bd904c6162d0 (patch)
treef28c4f6dd6dfc5992edf301449d1a371d229755b /tools/node_modules/eslint/lib/rules/comma-spacing.js
parenta2c7085dd4a8e60d1a47572aca8bb6fcb7a32f88 (diff)
downloadandroid-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.gz
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.bz2
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.zip
tools: move eslint from tools to tools/node_modules
This is required because we need to add the babel-eslint dependency and it has to be able to resolve "eslint". babel-eslint is required to support future ES features such as async iterators and import.meta. Refs: https://github.com/nodejs/node/pull/17755 PR-URL: https://github.com/nodejs/node/pull/17820 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/comma-spacing.js')
-rw-r--r--tools/node_modules/eslint/lib/rules/comma-spacing.js183
1 files changed, 183 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/lib/rules/comma-spacing.js b/tools/node_modules/eslint/lib/rules/comma-spacing.js
new file mode 100644
index 0000000000..25a0e7d82c
--- /dev/null
+++ b/tools/node_modules/eslint/lib/rules/comma-spacing.js
@@ -0,0 +1,183 @@
+/**
+ * @fileoverview Comma spacing - validates spacing before and after comma
+ * @author Vignesh Anand aka vegetableman.
+ */
+"use strict";
+
+const astUtils = require("../ast-utils");
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "enforce consistent spacing before and after commas",
+ category: "Stylistic Issues",
+ recommended: false
+ },
+
+ fixable: "whitespace",
+
+ schema: [
+ {
+ type: "object",
+ properties: {
+ before: {
+ type: "boolean"
+ },
+ after: {
+ type: "boolean"
+ }
+ },
+ additionalProperties: false
+ }
+ ]
+ },
+
+ create(context) {
+
+ const sourceCode = context.getSourceCode();
+ const tokensAndComments = sourceCode.tokensAndComments;
+
+ const options = {
+ before: context.options[0] ? !!context.options[0].before : false,
+ after: context.options[0] ? !!context.options[0].after : true
+ };
+
+ //--------------------------------------------------------------------------
+ // Helpers
+ //--------------------------------------------------------------------------
+
+ // list of comma tokens to ignore for the check of leading whitespace
+ const commaTokensToIgnore = [];
+
+ /**
+ * Reports a spacing error with an appropriate message.
+ * @param {ASTNode} node The binary expression node to report.
+ * @param {string} dir Is the error "before" or "after" the comma?
+ * @param {ASTNode} otherNode The node at the left or right of `node`
+ * @returns {void}
+ * @private
+ */
+ function report(node, dir, otherNode) {
+ context.report({
+ node,
+ fix(fixer) {
+ if (options[dir]) {
+ if (dir === "before") {
+ return fixer.insertTextBefore(node, " ");
+ }
+ return fixer.insertTextAfter(node, " ");
+
+ }
+ let start, end;
+ const newText = "";
+
+ if (dir === "before") {
+ start = otherNode.range[1];
+ end = node.range[0];
+ } else {
+ start = node.range[1];
+ end = otherNode.range[0];
+ }
+
+ return fixer.replaceTextRange([start, end], newText);
+
+ },
+ message: options[dir]
+ ? "A space is required {{dir}} ','."
+ : "There should be no space {{dir}} ','.",
+ data: {
+ dir
+ }
+ });
+ }
+
+ /**
+ * Validates the spacing around a comma token.
+ * @param {Object} tokens - The tokens to be validated.
+ * @param {Token} tokens.comma The token representing the comma.
+ * @param {Token} [tokens.left] The last token before the comma.
+ * @param {Token} [tokens.right] The first token after the comma.
+ * @param {Token|ASTNode} reportItem The item to use when reporting an error.
+ * @returns {void}
+ * @private
+ */
+ function validateCommaItemSpacing(tokens, reportItem) {
+ if (tokens.left && astUtils.isTokenOnSameLine(tokens.left, tokens.comma) &&
+ (options.before !== sourceCode.isSpaceBetweenTokens(tokens.left, tokens.comma))
+ ) {
+ report(reportItem, "before", tokens.left);
+ }
+
+ if (tokens.right && !options.after && tokens.right.type === "Line") {
+ return;
+ }
+
+ if (tokens.right && astUtils.isTokenOnSameLine(tokens.comma, tokens.right) &&
+ (options.after !== sourceCode.isSpaceBetweenTokens(tokens.comma, tokens.right))
+ ) {
+ report(reportItem, "after", tokens.right);
+ }
+ }
+
+ /**
+ * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
+ * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
+ * @returns {void}
+ */
+ function addNullElementsToIgnoreList(node) {
+ let previousToken = sourceCode.getFirstToken(node);
+
+ node.elements.forEach(element => {
+ let token;
+
+ if (element === null) {
+ token = sourceCode.getTokenAfter(previousToken);
+
+ if (astUtils.isCommaToken(token)) {
+ commaTokensToIgnore.push(token);
+ }
+ } else {
+ token = sourceCode.getTokenAfter(element);
+ }
+
+ previousToken = token;
+ });
+ }
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ return {
+ "Program:exit"() {
+ tokensAndComments.forEach((token, i) => {
+
+ if (!astUtils.isCommaToken(token)) {
+ return;
+ }
+
+ if (token && token.type === "JSXText") {
+ return;
+ }
+
+ const previousToken = tokensAndComments[i - 1];
+ const nextToken = tokensAndComments[i + 1];
+
+ validateCommaItemSpacing({
+ comma: token,
+ left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.indexOf(token) > -1 ? null : previousToken,
+ right: astUtils.isCommaToken(nextToken) ? null : nextToken
+ }, token);
+ });
+ },
+ ArrayExpression: addNullElementsToIgnoreList,
+ ArrayPattern: addNullElementsToIgnoreList
+
+ };
+
+ }
+};